3
\$\begingroup\$

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?

asked Feb 21, 2011 at 5:37
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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.

answered Feb 21, 2011 at 6:43
\$\endgroup\$
5
  • \$\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\$ Commented Feb 21, 2011 at 13:12
  • \$\begingroup\$ @vicatcu the docs I read said 128 bytes, but it could also be hardware-dependent. \$\endgroup\$ Commented Feb 21, 2011 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\$ Commented Feb 21, 2011 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\$ Commented Feb 22, 2011 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\$ Commented Feb 22, 2011 at 5:43
-1
\$\begingroup\$

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

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

See: SoftwareSerial: overflow().

Ricardo
6,22420 gold badges57 silver badges90 bronze badges
answered Feb 25, 2016 at 11:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.