10

I'm new to Arduino, and I am trying a couple of tutorials. What does this line do in a program?

while (! Serial); 

1 Answers1

12

On boards with an FT232 chip or other USB->Serial bridge chip (Uno, Mega, etc) it does absolutely nothing.

On boards with a direct USB connection, such as the Leonardo or the Yùn it waits for an active serial connection to be established by the PC (i.e., for the serial port to be opened by a piece of software).

When you open the serial port of a board like the Uno or Mega the whole board usually resets*, so opening the serial port allows you to see the first bits of the serial data. On the Leonardo etc it doesn't reset when you open the serial, so any serial output during the setup() function would be missed. Adding that line makes the board pause until you open the serial port, so you get to see that initial bit of data.

*) Unless you specifically write some software that doesn't assert DTR when you open the port

Majenko
  • 105,095
  • 5
  • 79
  • 137
  • Strictly "When you open the serial port of a board like the Uno or Mega the whole board resets," is not correct. The Arduino's IDE does that by default. However, simply opening the serial port from the USB host PC does not reset the ATmega. I've written code that opens a serial connection over USB from the host, and it doesn't reset the ATmega. – gbulmer Oct 11 '14 at 17:55
  • 1
    Actually, it is both OS and driver dependant, not just down to the software that does the port opening. – Majenko Oct 11 '14 at 18:34
  • 1
    The arduino resets when it gets the DTR signal. It is transmitted by default whenever I open a connection from anywhere, not just arduino's IDE. – BrettFolkins Oct 11 '14 at 18:37
  • In some OSses it's possible to open the serial without asserting DTR. In others it's possible to configure a port to not assert DTR when opened, but only after it's been opened once. By default, as mentioned, the IDE always explicitly asserts DTR. – Majenko Oct 11 '14 at 18:39
  • I believe it is possible to open an FTDI SUB/Serial without asserting DTR on Windows, OS X, and Linux, because I've done it. It is optional. So please update the answer, either say that when the IDE opens the serial port the board is reset, or that opening the serial monitor resets it, or that opening the serial port usually resets the board. I don't mind which is chosen. However the current answer is not accurate. – gbulmer Oct 11 '14 at 19:08
  • @BrettM - which OS? – gbulmer Oct 11 '14 at 19:08
  • On Linux you have to open the port first in order to set the port to not assert DTR. That first opening will always reset the board regardless. – Majenko Oct 11 '14 at 19:09
  • Note that if that line is in your sketch, you are using a 32u4 or M0 or similar chip with built-in USB support, and you are trying to run without being connected to a host via USB, your sketch will hang forever waiting for Serial to be ready. This is a common problem: "Sketch runs when connected to PC, but not when unconnected". – Dan Halbert Jul 08 '18 at 20:35