Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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!");
    Serial.begin(9600);
}

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

python

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

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

share|improve this question
    
what happens when you write "est." using the Arduino IDE Serial Monitor? –  geometrikal Aug 7 '12 at 11:53
    
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. –  Chris Stratton Aug 7 '12 at 13:19
    
@geometrikal after I type "est." it appears est on LCD for a second then the first ch>by himself . –  yahya tawil Aug 7 '12 at 15:20
    
@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 . –  yahya tawil Aug 7 '12 at 15:24
    
Try put a short delay (100ms) between readbytesuntil and flush. Do different strings give the same corrupted character? –  geometrikal Aug 7 '12 at 21:57
show 3 more comments

3 Answers

up vote 2 down vote accepted

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';
    Serial.flush();
    Serial.println();
    lcd.print(rd);
}
share|improve this answer
    
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 :) –  yahya tawil Aug 8 '12 at 1:50
    
@yahyatawil cheers, i am glad that i could help :) –  geometrikal Aug 8 '12 at 2:51
add comment

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

Usage:

port = configure_port("/dev/ttyACM0")
port.open()

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.

share|improve this answer
    
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 ? –  yahya tawil Aug 7 '12 at 16:15
    
Not sure. Have you tried calling Serial.println("Some test text"); to see if it is maybe a problem with your buffering? –  Chris Laplante Aug 7 '12 at 16:20
    
there is no problem with println it works . –  yahya tawil Aug 7 '12 at 16:31
    
yah!! I found through serial monitor that the text is recived correctly but next loop it gives © as a value of rd . –  yahya tawil Aug 7 '12 at 16:34
    
Try getting rid of the readBytesUntil and flush calls, and just use println. Or did you already do that? –  Chris Laplante Aug 7 '12 at 16:35
show 2 more comments

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 .

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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