A non-blocking Serial/Print class member function.

The want being to only feed the USART when it's ready, no buffer but the port, and return fail if the port flags indicate that it's in use. Even a version of Print.available() would be an improvement.

When I read the ATMEL docs I see the registers and which bits and then I see that Serial and Print use and may change modes that me not knowing JAVA stopped at the Serial source. I'm too old for that mountain and what I do too.

I just want to be able to print the way I read which is without blocking code execution. If you know what modes the USART runs by, any changes that make a difference to the empty/ready bits then please even the info is welcome.

PS -- you see, I have text in PROGMEM that would not need buffering and blocking to print.

A simple "availableOut" would be simple, no "JAVA"(?) required. Take a look at the source of the existing "available"

I believe that latest IDE (core) may have what you need... https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h#L117 https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp#L179

const int WriteThreshold = 27;

// Are there at least WriteThreshold bytes left in the buffer?
if ( Serial.availableForWrite() >= WriteThreshold )
{
  // Yes.  This will not block.
  Serial.println( F( "123456789.123456789.12345" ) );
}

[quote author=Coding Badly link=msg=2110354 date=1424809384] I believe that latest IDE (core) may have what you need... https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h#L117 https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp#L179

const int WriteThreshold = 27;

// Are there at least WriteThreshold bytes left in the buffer?
if ( Serial.availableForWrite() >= WriteThreshold )
{
  // Yes.  This will not block.
  Serial.println( F( "123456789.123456789.12345" ) );
}

[/quote]

That's buffer space available.

I want to know when the port is empty and ready

Serial.println() blocks.

Serial.println only blocks when there's no space in the buffer. I don't see what your objection to this method is.

My understanding is that it doesn't return until the buffer is cleared. perhaps except for 1 char.

print will return as soon as all the characters it has been given have been written to the output buffer. As long as there's room for them, that'll be pretty quick.

GoForSmoke: That's buffer space available.

I want to know when the port is empty and ready

Well, call Serial.availableForWrite() immediately after Serial.begin(), and store the number. That's how much space is available for writing when nothing has been transmitted yet.

Then when you want to know if the buffer is empty and ready, compare the number you get from Serial.availableForWrite() to the stored number. If it's less, you'll know some data is still in the buffer. When it's equal, you'll know the buffer is empty.

If you're hoping for the Arduino code to know whether there is a host-side program listening to the serial port, that's just ... not possible.

westfw: If you're hoping for the Arduino code to know whether there is a host-side program listening to the serial port, that's just ... not possible.

Isn't it possible to tell when a virtual serial session over USB is being used when using native USB h/w support? i.e. on Leonardo or Teensy?

--- bill

Yup...

https://www.pjrc.com/teensy/td_serial.html

Serial.dtr() Serial.rts()

But the other side has to participate (assert DTR and/or RTS).