Go Down

Topic: Effect of Serial.end() (Read 1 time) previous topic - next topic

vagulus

Assume that in setup() I call Serial.end().
Assume that in my code I have a debug output thus:
Code: [Select]
  // 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
"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977))

septillion

This is in the Serial.write() function (which is called by all the print functions) of a Uno (variant)
Code: [Select]

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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

vagulus

Thanks septillion.  I thought that might be the case.
"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977))

Go Up