0

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

  • What happens in you increase the delay? – Gerben Apr 1 '15 at 15:25
  • @Gerben, is this an required option - to make a delay after build? Will try soon – asiniy Apr 1 '15 at 15:30
  • Questions, questions: [1] What happens if you do a second 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'. – CharlieHanson Jun 1 '15 at 2:23
  • I had the same problem when were I sending data from the ptython script. First byte was alway lost. As a workaround I add unnecessery ser.read() before actual communication. ser was the name of the Serial instance. – wizofwor Jun 1 '15 at 8:57
  • @chaaarlie2: ‘¤’ is U+00A4. It's a single byte in quite a few 8-bit character sets, including Latin-1, several other ISO-8859 variations, and Windows-1252. – Edgar Bonet Jun 1 '15 at 10:09
1

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.

1

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.

  • 1
    That delay only applies to the host. The sketch does not start until the bootloader surrenders control. – Chris Stratton Nov 16 '15 at 19:26
0

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)
   ...
  • 1
    The Uno resets itself when you open the terminal. – Gerben Apr 1 '15 at 16:20
  • Oh, yes, of course. I'm too used to bare chips without auto-reset implemented. – JRobert Apr 1 '15 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.