Assume that in setup() I call Serial.end().
Assume that in my code I have a debug output thus:
// debug
Serial.print("Handshake message starts as >");
Serial.print(handshakeData);
Serial.println('<');
// debug
What happens to that code when the program is run? Is it ignored? Is it executed normally with out actually being transmitted? What does happen?
Thanks
This is in the Serial.write() function (which is called by all the print functions) of a Uno (variant)
size_t HardwareSerial::write(uint8_t c)
{
// If the buffer and the data register is empty, just write the byte
// to the data register and be done. This shortcut helps
// significantly improve the effective datarate at high (>
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) {
*_udr = c;
sbi(*_ucsra, TXC0);
return 1;
}
tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
while (i == _tx_buffer_tail) {
if (bit_is_clear(SREG, SREG_I)) {
// Interrupts are disabled, so we'll have to poll the data
// register empty flag ourselves. If it is set, pretend an
// interrupt has happened and call the handler to free up
// space for us.
if(bit_is_set(*_ucsra, UDRE0))
_tx_udr_empty_irq();
} else {
// nop, the interrupt handler will free up space for us
}
}
_tx_buffer[_tx_buffer_head] = c;
_tx_buffer_head = i;
sbi(*_ucsrb, UDRIE0);
_written = true;
return 1;
}
There is no check if serial is enabled. So all you print() statements will execute. And they will fill the data register and the buffer which is not a problem. UNTIL the buffer is full! Once the Serial buffer is full the write will wait until there is room in the buffer aka blocking the program. And because the buffer is not emptied the program will hold there...
It might have been a nice idea to embed a check to see if Serial was indeed setup.
Thanks septillion. I thought that might be the case.