Serial Data

Dear All,

I hope this is in the correct forum, sorry if not. I am posting today to ask whether the Serial.begin(9600); has to be called at setup or if I can call it at any point, as the software I am writing is designed to be standalone yet you can select a computer controlled option from the menu I have created for the LCD Screen. When this computer controlled option is selected it would then be plugged in to the PC ready for control, but I do not know how long in this would be?

Failing this could I start it at the beginning and just use the serial.available() to wait for a connection once in that part of the program.

Thanks

Callum

You can call it at any time before you use the port.

could I start it at the beginning and just use the serial.available() to wait for a connection once in that part of the program.

That's how you would normally do it. But isn't that the same as your first option?

the Serial.begin(9600); has to be called at setup


Rob

You can use Serial.begin() where you like. You should not call any Serial members when not initialized. Serial.available() tells you if a character is in the receive queue. It is not a check if the Serial is up/initialized.

Thanks for clarification Rob and Marek080.

Rob: Sorry I didn't explain myself very well, in one case i.e the first paragraph would be to call it at the required time and the second case to call it in setup part of the program.

Marek080: Thanks, for some reason I thought serial.available was a report that the serial device was connected and working.

Thanks once again.

Callum

I don't know the logic in this but I think you are still going to have problems. When you plug the Arduino into the USB port, it resets the Arduino. Evern if it is already plugged in, when you open the serial monitor it also resets the Arduino.

I think there is a trace you can cut or something to make it not do this but I don't think it is trivial.

If you are going to build a breadboard version, I think this is a non-issue but it does make one wonder why it would reset the Arduino but no a standalone Chip on a breadboard?

I think this is a non-issue but it does make one wonder why it would reset the Arduino but no a standalone Chip on a breadboard?

Because the arduino contains an auto reset circuit, that is a capacitor linking the DTR pin and the reset. If you miss that out on a stand alone then this will not happen. However it is harder to upload sketches because you have to hit the board's reset button at exactly the right time.