Shop OBEX P1 Docs P2 Docs Learn Events
Some strings — Parallax Forums

Some strings

Zap-oZap-o Posts: 452
edited 2010-01-28 17:41 in Propeller 1
I am using the LMx9838 Blue tooth serial port module. I can communicate to it and all, but I was wondering how you fellas use strings. I have a byte array that stores the data that is transmitted via the propeller using the FullDuplexSerial object.

One problem that I am curious about is how to compare a byte array to another. Is there a way?

For example the byte string that I would get

02 43 16 06 00 5F 00 04 30 30 30 30 03
or
02 43 11 02 00 56 1F 01 03

I wanted some fresh eye opening ideas.

Comments

  • BradCBradC Posts: 2,601
    edited 2010-01-28 13:26
    Generally strings are zero terminated and you can use the spin strcomp() to compare them. If they (as your example shows) contain 0's then you will need to write your own string handling stuff. I generally use a byte array for the string and a single byte to indicate the length (A bit like the old Pascal string type).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Life may be "too short", but it's the longest thing we ever do.
  • kwinnkwinn Posts: 8,697
    edited 2010-01-28 13:33
    Simple enough to do. Compare the individual bytes using a common index (Compare StringA to StringB). Normally this is done for equal length strings but it can also be done for strings of different lengths provided you stop at the end of the shorter string.
    If I were doing a lot of string compares I would write a general purpose comparison routine in PASM to return the status of the comparison as well as the comparative string lengths as:

    A=B, A>B, A<B - Comparison of string values up to length of shorter string.
    A=B, A>B, A<B - Comparison of string of string lengths.

    And possibly include the lengths of the strings as part of the returned information for zero terminated strings.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-01-28 17:15
    First, please let's change the wording. Looking at your bytes let's me think that it's not a string that you receive. A string in programming lanuages usually contains readable characters and is what you'd want to show the user or log in a readable file. What you have is a byte stream. What I currently do not understand is, whether this bytestream is comeing from the bluetooth module itself or is it the stream the bluetooth module received or maybe a mixture?

    Usually you find a description of the bytestream in the datasheet of any device that sends and receives data. This is called the protocoll. There are answer, response protocolls ( so the device will only send something when it's asked to) there are paket based protocols (where each package has a defined size or the size is contained in the first bytes). There are protocolls where data is send continuous. Then you need a protocol definition which tells you how to detect start or end or both of a 'message'.

    I think there are more protocols out there where the data changes a lot (for example for any kind of sensors). There a stream compare over the whole stream makes no sense. Usually you'd convert the stream back into variables. For example the definition of a protocol says: first you receive $00 which marks begin of a data-set, then you receive $a1 for a temp/huminity package, then you receive a word with most significant bit (MSB) first and least significant next (LSB) which is the temperature and then you receive a word with MSB/LSB which is the huminity and then a $ff for end of packet.
    It simply does not make sense to compare the whole stream, as the temp and huminity bytes vary.

    So, it's important to read the datasheet. What does the stream look like. How can I detect start and/or end of a message. Which bytes in the stream means what and how can I convert it back to a useable way ( for example store the bytes for temperature in a variable temp and the bytes for huminity in hum). To find out what data has been send you only need to compare the second byte in my example.

    For example GPS devices send text format streams - so, really strings. Here a good solution is to have a general parser. I remember that we had a thread about a regular expression object lately. Maybe someone linked it and can add a reference here.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-01-28 17:41
    Here's a spin version of the C memcmp() function.· It compares byte arrarys of the same length.

    Dave

    PUB memcmp(p1, p2, n)
    · REPEAT WHILE (n-- > 0)
    ··· IF (BYTE[noparse][[/noparse]p1] <> BYTE[noparse][[/noparse]p2])
    ····· RETURN BYTE[noparse][[/noparse]p1] - BYTE[noparse][[/noparse]p2]
    ··· p1++
    ··· p2++
    · RETURN 0
Sign In or Register to comment.