Understanding Serial.begin() behaviour

Hej guys,

I'm working on a project where my Arduino (I'm using a Nano) will be connected to a computer via USB. On my computer I then run the Software that interprets the input from the Arduino. In my setup() function, I establish a Serial connection to the computer by calling Serial.begin(38400); Unfortunatley, I'm not sure of the way the begin() function works. Does it block until it has received some kind of signal from the PC? And if that be the case: Does it wait till my program has connected to it? Or is this not the case at all?

I need to figure this out, because after establishing the Serial connection I will do some more setup-stuff and it is crucial that the Arduino does not send any stuff out into the Ether while I'm still busy starting up my program on the PC. Starting it beforehand is only a temporary solution, but cannot be final.

While working with the Serial Monitor of the Arduino IDE I noticed that the messages I send seem to be only send after I opened the Monitor. But I'm suspicious and don't really trust it, so it would be really great if you guys could help me out with this, it would be much appreciated.

While we're already at it, I've got another question: What exactly is the use of Serial.flush()? I need to be sure my loop() doesn't start before all information was send, and currently just put a flush() at the end of loop(). But is this necessary? And if not: what IS the use of flush()?

Thanks in advance for your help

K

begin() does not block, but does not cause any transmission either.
you need not call it in setup(), it can be called or re-called at any time.

flush() is designed to let you determine if and when all pending transmissions have completed.
one example usage is for rs485 trsnceiver chips, which typically must be kept enabled for transmission until all characters have been transmitted. examples follow: the first one will cause problems, the second will not

enable_485_for_transmit();
serial1.write(" this is a test message");
disable_485_tramsnt();

enable_485_for_transmit();
serial1.write(" this is a test message");
serial1.flush();
disable_485_transmit();

Serial.begin() only sets up the parameters (baudrate, number of databits, number of stop bits and parity). It does NOT establish a connection; there is no such thing in serial communications except for the physical cable ;)

Although one can call Serial.begin multiple times, there is no need for it. If you do so, you need to first call Serial.end(). This will, among other things, flush the buffer.

The simplest way to prevent the Arduino from doing anything is defining a simple protocol. You can make the Arduino wait for a message from the computer application before it starts sending data.

void setup()
{
  Serial.begin(115200);
  ...
  ...

  pinMode(13, OUTPUT);

  // wait for data to arrive from the PC
  while(Serial.available() == 0)
  {
    // flash the led on pin 13
    digitalWrite(13, !digitalRead(13));
    delay(100);
  }
  digitalWrite(13, HIGH);
}

void loop()
{
}

The above Arduino code will flash pin13 on the Arduino while it is waiting for the first data from the PC. The data can be anything or you can define e.g. the character 'S' to start (and e.g. 'E' (for end) to stop if needed).

Thanks guys, that clears things up a lot :) Then it is as I first expected when I started programming, because I planned for a small protocol that signals the start ... but because of the behaviour of the Serial Monitor I then thought it was not necessary. (as I said, it prints the stuff I write into the Serial in my setup() function, even if I open it a lot later. Same was happening when using Software on the PC, I did get those first setup messages. Any idea why this is happening? Does teh buffer of Serial only flush after it was connected to something?). But I guess I will implement a protocol then :)

It's actually the opening of the serial port pn the PC that causes the resets the Arduino.

So in your application you should only open the port once.

There is a hardware fix (cutting a track if I'm not mistaken) or you can use a FTDI or MAX232 cable.