Hi. I am building a new Arduino-based interface for controlling a very big, very old radio telescope dome.
I hope to be sending 9600 baud serial data (velocity, position error) over an infrared light beam, like your TV remote control, but bigger. One end of the light path is on the antenna mount, and the other is on the dome wall 40 feet away. The serial data needs to be sent continuously, in order to keep the AC-coupled photodiodes happy. I am both sending and receiving serial data continuously.
I would like my code to be a non-blocking state machine, rather than using the silly Serial.flush() function, which blocks until the transmit buffer is empty.
Is there a non-blocking method of determining if the serial transmit buffer is empty?
Thanks. I did RTFM, but it didn't say anything about how big the buffer is, so it didn't seem helpful.
I found another thread on StackExchange that suggested that the transmit buffer has SERIAL_TX_BUFFER_SIZE locations. Of course, the example they provided doesn't actually work, since the available space in the buffer is not the buffer size, but the size minus one.
I set up a test with an oscilloscope to see what was actually happening. The Serial.availableForWrite() result becomes 63 when there are only two characters left to be serialized. So it's not quite what I am looking for, but it will work. Also, this behavior ISN'T DOCUMENTED! (until now). So your RTFM snark isn't warranted.
This code that I wrote (after some playing around) seems to work correctly:
/*
Serial output looping test for dome tracker LED driver
*/
int used;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
used = SERIAL_TX_BUFFER_SIZE - Serial.availableForWrite() - 1; // buffer fullness
if (used == 0) {
digitalWrite(3, HIGH); // 'scope trigger
delay(3); // wait for last 2 characters to send
Serial.println("A127B678C90");
}
else {
digitalWrite(3, LOW); // 'scope trigger
}
}
nixiebunny:
The Serial.availableForWrite() result becomes 63 when there are only two characters left to be serialized. So it's not quite what I am looking for, but it will work. Also, this behavior ISN'T DOCUMENTED! (until now). So your RTFM snark isn't warranted.
Well what are you looking for? Your Original Post was not very specific