Serial.read noob

Your source for all things Atmel.

Moderator: phalanx

Post Reply
rdg-123
Posts: 20
Joined: Thu Nov 12, 2009 11:56 pm

Serial.read noob

Post by rdg-123 » Fri Mar 26, 2010 11:27 pm

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...

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: Serial.read noob

Post by waltr » Sun Mar 28, 2010 5:46 pm

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.

Post Reply