1

How do you find how many bytes are currently waiting in the transmit buffer?

I'm trying to diagnose a weird serial disconnection problem with an Arduino Uno and a rosserial node. It works fine for 10-20 minutes, and then mysteriously losing connection. At first, I thought the Arduino was running out of memory, but I'm able to use the MemoryFree library to log available memory, and it never drops below 9051 bytes.

I now want to make sure the problem isn't because the serial buffer is overflowing, from the Arduino trying to send too much data.

asked Jan 16, 2018 at 15:17
2
  • 1
    tx buffer doesn't overflow. the write function of HardwareSerial waits for space in the tx buffer Commented Jan 16, 2018 at 17:38
  • You mean the: arduino.cc/reference/en/language/functions/communication/serial/… As @Juraj wrote, the tx buffer does not overflow, if you send too much data the Arduino is slowed down a lot (100 times or more) because it is waiting for space in the tx buffer. Commented Jan 16, 2018 at 18:29

2 Answers 2

3

If you really want to know (not that it would do you any good) you can use the Serial.availableForWrite() function (which returns how much space is left in the buffer) and subtract the returned value from the size of the buffer.

int used = 64 - Serial.availableForWrite(); // Assuming a 64 byte buffer

I haven't tested it, but you may be able to use SERIAL_TX_BUFFER_SIZE instead of the 64 to make it properly portable.

int used = SERIAL_TX_BUFFER_SIZE - Serial.availableForWrite(); // Possibly portable
answered Jan 16, 2018 at 18:39
0

I now want to make sure the problem isn't because the serial buffer is overflowing, from the Arduino trying to send too much data.

If you are using the software serial Arduino library there is a way to check for overflow.

answered Jan 16, 2018 at 16:32
1
  • that is for rx buffer Commented Jan 16, 2018 at 17:36

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.