I wounder what is the format of the serial data stream that usually is sent from computer to arduino through serial connection . my project use serial connection with arduino to send data from python(pyserial) to arduino , and when I send a string and I try to print it on LCD it appears but the first character changes to a chinese or japanese character .

so can any body explane the steam format to understand what is the wrong or what happens .

this is the arduino code and python code I used : arduino :

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
    lcd.begin(16, 2);
    lcd.print("hello, world!");

void loop() {
    lcd.setCursor(5, 1);
    char rd[5] ;
    Serial.readBytesUntil('.', rd, 5);


import serial
s = serial.Serial('/dev/ttyACM0',buadrate = 9600)

update I found through serial monitor that the text is recived correctly but next loop it gives © as a value of rd .

  • \$\begingroup\$ what happens when you write "est." using the Arduino IDE Serial Monitor? \$\endgroup\$ Aug 7, 2012 at 11:53
  • \$\begingroup\$ Beyond line settings such as no parity, 8 data bits, 1 stop bit, and the 9600 baud you have set, the format would be up to you to define in your software. A very common choice is to end messages with a newline. Right now, likely culprits are either buffer corruption, analog noise, or LCD wiring. You might try making a test sketch that receives up to a newline and echoes it back on the serial, to get the LCD out of the way. You could even make a python program that tests the echoing of random strings and logs any failures. \$\endgroup\$ Aug 7, 2012 at 13:19
  • \$\begingroup\$ @geometrikal after I type "est." it appears est on LCD for a second then the first ch>by himself . \$\endgroup\$ Aug 7, 2012 at 15:20
  • \$\begingroup\$ @ChrisStratton thank you but I think the problem is not from analog noise all characters are right except first one , I tried to re-send the string more than 20 time an I had the same result . so I don't think it is a noise problem . \$\endgroup\$ Aug 7, 2012 at 15:24
  • \$\begingroup\$ Try put a short delay (100ms) between readbytesuntil and flush. Do different strings give the same corrupted character? \$\endgroup\$ Aug 7, 2012 at 21:57

3 Answers 3


Strings need to be terminated in a '\0' character (typically 0x00) to indicate where the end of the string is.

e.g if you initialise a string like this:

char message[] = "hello";

It will store 'h' 'e' 'l' 'l' 'o' '\0' in the memory.

Then in a routine like 'print', the code loops through the string printing each character until it reaches a '\0' character. If that character is not present it will keep going.

I think what is happening in your code is that the 'rd' byte array does not have a terminating '\0' character. To allow for different length strings try the following:

void loop() {
    lcd.setCursor(5, 1);
    //6 bytes long to include '\0'
    char rd[6];
    //Read up to 5 bytes
    byte bytesReceived = Serial.readBytesUntil('.', rd, 5);
    //Set the next byte to '\0' to terminate the string
    rd[bytesReceived] = '\0';
  • \$\begingroup\$ really thank you it is solved know ;) I was thinking about termination issue as you mentioned up , but I didn't thought that it was the real problem becuase I know that strings are usally ended with \0 as you said , but It appears it was the problem :) \$\endgroup\$ Aug 8, 2012 at 1:50
  • \$\begingroup\$ @yahyatawil cheers, i am glad that i could help :) \$\endgroup\$ Aug 8, 2012 at 2:51

This is the Python function I use for configuring Arduino serial ports. Give it a try:

def configure_port(port_id):
    ser = serial.Serial()
    ser.port = port_id
    ser.baudrate = 9600
    ser.rtscts = True
    ser.dsrdtr = True
    return ser


port = configure_port("/dev/ttyACM0")

I think setting rtscts and dsrdtr to true is what worked for me. The above code at least works for Pro Micros; you didn't specify which Arduino you are using, so this is my best guess.

  • \$\begingroup\$ I tried what you put but it didn't work , I had the same result (I had the string but the first character is changed after a second). dose the connection stream adds any bit to data after finishing which changes the ascii number and causes the problem ? \$\endgroup\$ Aug 7, 2012 at 16:15
  • \$\begingroup\$ Not sure. Have you tried calling Serial.println("Some test text"); to see if it is maybe a problem with your buffering? \$\endgroup\$ Aug 7, 2012 at 16:20
  • \$\begingroup\$ there is no problem with println it works . \$\endgroup\$ Aug 7, 2012 at 16:31
  • \$\begingroup\$ yah!! I found through serial monitor that the text is recived correctly but next loop it gives © as a value of rd . \$\endgroup\$ Aug 7, 2012 at 16:34
  • \$\begingroup\$ Try getting rid of the readBytesUntil and flush calls, and just use println. Or did you already do that? \$\endgroup\$ Aug 7, 2012 at 16:35

thanks to geometrikal, the problem was : arduino recives a not ended string with \0 , and when the next loop comes it goes to the position (5,1) which is the first character of the printed string on the screen and arduino prints over it the default value of the variable rd if nothing is recived .


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.