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.

  • 1
    tx buffer doesn't overflow. the write function of HardwareSerial waits for space in the tx buffer – Juraj Jan 16 '18 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. – Jot Jan 16 '18 at 18:29
2

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

  • that is for rx buffer – Juraj Jan 16 '18 at 17:36

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.