Some strings
Zap-o
Posts: 452
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.
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Life may be "too short", but it's the longest thing we ever do.
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.
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
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