0

I'm trying to communicate with my arduino uno but it seems to have troubles to understand characters. For example, printing things with Serial display weird things, changes characters, removes some.

For exemple printing "test !" every two seconds results in that :

enter image description here

Every characters have been removed except "!"...

Is my arduino broken ? Thanks in advance

3
  • Use Serial.print() instead of Serial.write().
    – Majenko
    Mar 9, 2015 at 16:12
  • I started with Serial.print but when I noticed that it wasn't acted normally I used serial.write. But it is also acting weirdly with Serial.print()
    – Michaël Villeneuve
    Mar 9, 2015 at 16:17
  • Actually with Serial.print() it seems to be only displaying numbers... Serial.print("Test!"); output '8225'
    – Michaël Villeneuve
    Mar 9, 2015 at 16:20

1 Answer 1

1

You are doing 2 things wrong in your program:

First is using serial.write(), which writes bytes rather than ASCII.

Second is you are declaring a string literal as a char instead, you must declare a string using double quotation marks (which is ") so that your program should read:

Serial.print("Test!");

When you declare 'Test !' you are doing it wrong, and it will only take the last char in the string, which is '!' - using single quote marks is for a single char declaration, like '!' or 'T', and you can make an array of chars into a string if you wanted, like this:

char array[] = {'T', 'E', 'S', 'T', '!' };
Serial.write(array, 5);

but why would you when the earlier one works better?

6
  • The single vs. double quotation marks is a good catch. Serial.write("...") should still work though according to the documentation, as an ASCII string is just a sequence of bytes like any other. Mar 9, 2015 at 16:43
  • @PeterR.Bloomfield indeed, however they are not formatted as ASCII human-readable bytes for proper use in the terminal viewer - the issue the OP had was junk bytes being displayed, or just '!' due to the compiler and it's string literal vs const char declaration behaviour
    – KyranF
    Mar 9, 2015 at 16:46
  • Serial.print("...") is literally just a wrapper for Serial.write("..."). They send exactly the same thing. Mar 9, 2015 at 17:00
  • @PeterR.Bloomfield well it depends what you are sending, and to what - if you are doing serial to an external serial device you may not want ASCII conversion of your bytes, so Serial.write should be used. For nice terminal output, and lots of extra functionality of specifying the format of data in ASCII (such as the HEX formatter) using Serial.print() is better. The OP in his case should be using print, just for convenience sake. I've pointed out why it's not technically working, and indeed if he understands how both work then he CAN use both for reading terminal data.
    – KyranF
    Mar 9, 2015 at 17:23
  • It inevitably comes down to both ways using .write() but .print() has cool stuff, and for noobs I suggest they use it until they get better at programming.
    – KyranF
    Mar 9, 2015 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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