Serial availableForWrite function?

I am porting some arduino code over to the photon and ran into an issue where the serial method availableForWrite is not available. I am wondering if there are any plans to add serial availableForWrite functionality to the firmware and if not, does anyone have ideas on how to workaround this using the currently available firmware?

https://www.arduino.cc/en/Serial/AvailableForWrite

Thanks!

What would be a use case for this function?
Since sending TX is not blocking anyhow. But a pull request for a fn that returns the difference between head and tail of the TX buffer might be pulled.

1 Like

The use case is LED control via serial tx, so the timing is important. I suppose if I knew the write buffer length I could code around it?

The RX & TX buffers are 64 byte ring buffers.

You could find these infos in the open source repo
https://github.com/spark/firmware/blob/latest/hal/inc/usart_hal.h

Issue filed here - https://github.com/spark/firmware/issues/798

1 Like

Hi,

Can I follow up on this…Just tried to use ‘availableForWrite()’ as I need to send a block of 600x (0x55) characters.

This function always simply responds with 0

void MBus::Wakeup()
{
  int i = 60;   //send 600 wakeup bytes

  while (i-- > 0)
  {
    Serial1.print(Serial1.availableForWrite());
    if (sermbus.availableForWrite() > 1)
      sermbus.print(0x55);
    else
      delay(100);
  }
}

currently shortened to 60 for debugging…

Code runs and simply prints a set of 0’s

Am I to guess that this ACTUALLY returns the number of bytes IN the buffer and not ‘remaining space’ ???.

NB sermbus is #defined as Serial1 in the header file.

This also prints zero’s

b = Serial1.availableForWrite();
Serial1.print(b);

Thanks

Graham