I'm connect arduino to my PC through serial monitor. Very common situation:
Serial.begin(9600);
delay(100);
Serial.print("Hello, World!");
But I always get an ¤ello, World!
. How can I get the first symbol straight? I've tried on both unix & windows, mega & uno. I've also tried println
, but with no happiness
3 Answers 3
See the following article:
http://www.arduino.cc/en/Main/ArduinoBoardNano
See the section at the bottom called:
Automatic (Software) Reset
Basically, one has to delay for 1 second (at least) after initializing the serial port before you can read data. It seems that the Arduino makes an assumption that after the serial port is initialized, what may happen next is a request to load a program. As such, it looks for a period of time (I guess up to a second) for a string of magic characters that indicate a program load. If it doesn't find those, then it starts to behave as you would expect ... but only after it times out looking for a program load.
-
1That delay only applies to the host. The sketch does not start until the bootloader surrenders control.Chris Stratton– Chris Stratton11/16/2015 19:26:05Commented Nov 16, 2015 at 19:26
I'd have to be pretty quick to connect my serial monitor to see immediate terminal output from my program, if that output uses the same USB I used to load the program. You can test this by resetting the Arduino so it will repeat the output and see if you get a clear message this time. So I leave a couple of seconds' delay in the code to let the slow human catch up. Most of my devices announce their name and code-version on the serial I/O and flash a quick "I'm here" on the LED (if I chose to attach one) when they first wake up:
const char IdString[] = "Hello.cpp: $Revision: 1.3 $";
void setup(void)
{
Serial.begin(57600); // open serial port
// Identify to the terminal
delay(2000);
Serial.println(IdString); // hello (terminal)
// Init LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
nblink(4); // hello (LED)
...
This is more a wild guess than a real answer... Assuming your terminal is using a character set more or less like ISO-8859-1 or Windows-1252, then you are receiving 0xA4 when you expect 0x48. My guess is that the very first start bit got lost somehow.
Here are the serial port waveforms corresponding to the expected and received bytes. ‘S’ and ‘T’ are the start bit and stop bit respectively. The data bits are sent least-significant first. The idle state is HIGH:
• ‘H’ = 0x48 = 0b01001000
────┐ ┌───┐ ┌───┐ ┌──────
│ S 0 0 0 │ 1 │ 0 0 │ 1 │ 0 │ T
└───────────────┘ └───────┘ └───┘
• ‘¤’ = 0xA4 = 0b10100100
────┐ ┌───┐ ┌───┐ ┌──────────
│ S 0 0 │ 1 │ 0 0 │ 1 │ 0 │ 1 T
└───────────┘ └───────┘ └───┘
You can see that, if you remove the start bit from the first waveform, you get exactly the second one. I do not know what may be the cause of this though.
Serial.print(<something>);
? [2] What happens if you change the data rates (in code and Serial Monitor)? [3] If you change the first letter (H), does the displayed symbol change too? [4] If it does change, could you edit your question to include a list of half a dozen examples? The character you've shown is not part of neither the standard nor the extended ASCII tables. This suggests an extra byte is being sent or the Serial Monitor is adding the preceeding byte and decoding it along with the 'H'.ser.read()
before actual communication.ser
was the name of the Serial instance.