Newbie questions - Serial

Hello.

I am new to Arduino world, and I have just recently purchased an Arduino Bano v3.0 board with pins for protoboard experiments. I am, however, familiar with programming in C/C++, so the arduino IDE and the language quickly became familiar to me. I am looking forward to becoming an arduino guru. I can see it is possible to automate a lot of things with this nifty little gadget! Okay, here's the deal. I am thinking about implementing a binary serial protocol for communication between an arduino board and the PC. I'll be using this to command & control the device(s). It will be a packet-based design sort of like TCP or UDP. I do have a few newbie questions to ask before I get my hands dirty.

  • I've read in the reference docs that the arduino only has 128 bytes of buffer for storing the incoming traffic from the PC (via a USB cable). I can live with that since most packets aren't going to be nearly as big. I am, however, concerned about the transmission. Suppose the PC transmits data to the arduino board faster than the arduino can process it. What happens when the RX buffer is full? Does arduino simply drop any incoming traffic until there is room in the buffer or does the buffer get overwritten?

  • Assuming the serial is a streaming protocol, I'll likely have to address some more issues. Suppose the PC sends an array of binary data (lets say 80 bytes) to the arduino board. The arduino is programmed to read all the bytes in a packet at once (actually byte by byte until all 80 bytes have been read) - this means the full binary array that was sent by the PC. Is it possible that the arduino will attempt to read the buffer before it has been fully filled with data i.e. only 40 bytes have been transmitted when the arduino executed the Serial.read() loop?

  • The arduino language reference shows that the Serial library has the Serial.read() and Serial.write() calls. The write() call has multiple prototypes, one of which lets you specify a source buffer and a length of data to be read and transmitted. Unfortunately the read() call only reads one byte at a time. Is there a way to read an arbitrary length of data from the RX buffer with one call i.e. Serial.read(my_buffer, 10)?

  • Lastly I'd ask a few questions about the serial protocol. Since my protocol will be packet-oriented, I will likely need to assure data integrity. All packets will always be at least two bytes in length - the first byte being full packet length, and the second the packet type/id. After that there is data. Packets will be of variable length. Do I have to include a byte for checksum and verify the transmitted data manually, or does the serial protocol already have a mechanism to ensure the transmitted data is correct?

Thanks in advance.

Tritium,

  1. As far as getting bytes out of the buffer with Serial.read(), the Arduino will have no problem keeping up with the data, as long as there is not a lot of time-consuming processing happening on the Arduino side. The bottleneck is usually the data transmission. At 9600 bps, only 960 characters can be transmitted each second. That's about one millisecond per character, and you can do a lot in an Arduino in one millisecond.

I don't remember if overflow bytes are discarded or overwrite existing bytes, but the source code is there if you want to read it. At any rate, if your protocol is robust, it will handle either case correctly.

  1. It's not only possible that you will start reading bytes from the buffer before they have all been transmitted, it is likely. In fact, this is a common programming error. See 1., above about transmission speed vs. processing speed. Serial.available(), when used properly, can avoid this problem. Once again, the code you write to implement your protocol needs to handle this properly. Your code needs to assemble a packet, byte by byte, before processing the packet.

  2. There is no multi-byte read method in the current Serial class. This function is left as an exercise for the reader. :wink:

  3. There is no data integrity checking in the Serial class. If the data gets garbled in transit, you will have garbled data in the buffer, and Serial.read() will return garbage characters. Once again, your protocol will have to check the data integrity if you think that's necessary.

This thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1269302644/2 contains a simple routine (lineAvailable() ), which reads serial chars into a buffer.

Regards,

-Mike

Thank you for answering these. This clears up a lot of things, and I can start coding straight away. One thing though. Could you please point out where I can find the source code for the Serial class and the RX buffer (sorry, I'm a newbie)?

Arduino/hardware/arduino/cores/arduino
or
Arduino/libraries

This is good reading for EVERYBODY :wink:

Ah, I see it now!
The file I want to check is Arduino/hardware/arduino/cores/arduino/HardwareSerial.cpp and it looks like I can even manually increase the buffer size by editing that file.

#define RX_BUFFER_SIZE 128

Thanks for pointing this out! It's modding time. ;D

See ya.

~Trit