SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By rdg-123
#97115
I've been looking into serial communication between multiple arduinos and have learned many things about the necessary hardware and wiring needed. Having given up on using a twi system ( viewtopic.php?f=8&t=20585 ) I plan on running a separate cable (CAT5?) from arduino to arduino. My current problem is in trying to learn the serial.read() code.

I've got the switch case ( I think) part down. I want to run a string with a header (0x00) , 4 pairs of ASCII data (00-99), and an end (ETX, 0x04?) with checksum (2 bytes, right?). 12 parts, yes?

So I tell serial.read() to read until the byte == my header. Then how do I parse the remaining bytes in order? Or assign them to a variable so I can make my code deal with the data? All of the examples use real-time streaming from Tx to display and say nothing of interpretting, say, the middle data block in some way.

I've seen reference to /n, <<, &&, |...are these things to increment byte to byte? Is byte the right term for each parcel I'm trying to parse out of my string?

Ending up feeling stoopit, since I can't seem to get a handle...
By waltr
#97231
I've seen reference to /n, <<, &&, |...are these things to increment byte to byte?
In the C programming language these are:

/n = new line which may be an ASCII LF (line feed) character or 0x10

<< = shift the bits left
&& = logical AND used in compasisons (not to be confused with a single & which is a bit-wise AND).
| = bit-wise OR

All of these can be found in any good book on C. The standard is the K&R book.
Here is a link an on-line reference:
http://publications.gbdirect.co.uk/c_book/

Parsing data is always an interesting programming exercise. If the format of your data is the same every time then it is easier. Data counters and switch-case statements can be used to keep track of what byte is being grabbed next to be converted from ASCII to binary, shifted and the next byte added in.

Another common method I use for serial data is a ring buffer for the incoming data then parse the packet as its being removed from the ring buffer. The ring buffer is filled by an ISR that handles the UART receiver.