Can I detect whether bytes are getting dropped by the Arduino Serial driver because of buffer overflow (i.e. if I'm not keeping up with my input stream)? If so, how?


If you're using SoftwareSerial, I think you have no chance of detecting serial overflow, because the docs say you can't use Serial.available. But even if you were using the hardware serial library, I don't think you can detect if bytes are getting dropped. The closest thing you could do is use Serial.available to see when you're about to overrun the buffer.

But this is really what hardware and XON/XOFF flow control are for. When the serial driver supports this functionality, you can prevent buffer overflows because the host will tell the client when no more data can be accepted over the serial line, and vice versa.

I don't think Arduino supports any kind of flow control, especially since begin() only takes a serial port speed as a parameter.

  • \$\begingroup\$ Good thinking, using Serial.available() function is a good indirect approach... Do you know how deep the hardware Serial library rx buffer is? It might suffice for me to track whether the queue is growing over time since I have a continuous flow of bytes coming in... flow control wouldn't help in my case either because I essentially have a streaming source. \$\endgroup\$ – vicatcu Feb 21 '11 at 13:12
  • \$\begingroup\$ @vicatcu the docs I read said 128 bytes, but it could also be hardware-dependent. \$\endgroup\$ – Dave Feb 21 '11 at 14:38
  • \$\begingroup\$ @vicatcu so perhaps in your case you'll need to simulate threading with a round-robin state machine approach. One such "thread" could monitor the buffer, and you'd have a "low buffer" threshold as well as a "high buffer" threshold. This could adjust the streaming rate, which could throttle up or down depending upon where you are in the buffer. This assumes that you have a communication protocol with the client that allows for such speed adjustments. \$\endgroup\$ – Dave Feb 21 '11 at 14:41
  • \$\begingroup\$ @Dave I can confirm available maxes out at 127, experimentally. I have no means of throttling the stream. On the bright side, it looks like I don't have a buffer overflow problem after all... I'm not showing an increasing queue size operationally. Back to the drawing board... \$\endgroup\$ – vicatcu Feb 22 '11 at 1:59
  • \$\begingroup\$ @vicatcu thanks, and feel free to keep posting comments as you progress through your project, and maybe I can give more feedback. \$\endgroup\$ – Dave Feb 22 '11 at 5:43

With SoftwareSerial you use overflow() to check for overflow...

if ( myport.overflow() ) {
  // The input buffer overflowed

See: SoftwareSerial: overflow().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.