Hamoudy
Hamoudy

Reputation: 579

Random character at the beginning of serial.readline() in python and arduino

I get random characters when I perform a serial.readline(), sometimes it is other umbers and sometimes it is whole messages. The output should be "1,2" or "2,2"

Here are a couple of screenshots of the output of serial.readline(). I have tried to put a delay at before serial.readline() but it did not make a difference. There is usually a strange character at the beginning:

enter image description here

enter image description here

I have also received strange messages: enter image description here

There is a problem later on in the program that causes the program to hand because sometimes I just receive a blank line.

Is there a way to get consistent output from serial?

Here is the arduino code:

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  Serial.begin(9600);  
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
   Serial.println("1,2"); 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    Serial.println("2,2");
  }


}

And here is the python code:

ser = serial.Serial('/dev/ttyUSB0', 9600)

line=ser.readline()
coord= line.strip()

print coord

EDIT: I tried putting ser.flushInput() after the ser.open() and I get the same output.

Upvotes: 0

Views: 2687

Answers (4)

Laaaars
Laaaars

Reputation: 55

I've been having the same issue when interfacing between pyserial and Arduino. This may be an old post, but in case someone else is having the same trouble, I remedied my problem by inserting:

ser.flushInput()

...right before my ser.readline() call.

Upvotes: 1

Chris Heydrick
Chris Heydrick

Reputation: 41

What happens if you break it down to something simpler?

On the Arduino:

void setup()
{
  Serial.begin(9600);
}

int i = 0;
void loop()
{
  delay(1000);
  Serial.print("Hello World ");
  Serial.println(i);
  i++;
}

And in the Python REPL...

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
while(1):
    ser.readline()

The first behavior you should observe is that when you initiate the serial connection, the Arduino reboots. This takes a few seconds.

The second behavior you should observe is that (if you're typing this in the REPL slowly) when the while(1) loop begins, you get all of the serial data that had accumulated since you initiated the serial connection. It takes me a few seconds to type all that out, so when I hit Enter after ser.readline() I see:

'Hello World 1\r\n'
'Hello World 2\r\n'
'Hello World 3\r\n'

I only mention this to make sure you're familiar with two things that burned me a bit the last time I messed with serial communication to an Arduino: it needs time to reboot after connecting, and the serial data buffers. readline() will only give you one line in the buffer - not everything that's in the buffer.

Is it possible you're trying to sent/receive data before it's done rebooting? What about button bounce - could a dozen high/low state detections be messing something up?

Upvotes: 0

Paulo Neves
Paulo Neves

Reputation: 1196

How to Read serial data from an Arduino in Linux

Nothing fancy here. I’m getting the current port configuration, setting the input/output speed to 9600 baud, setting the data expectations to be 8 bits per word, with no parity bit and no stop bit, setting canonical mode, and then committing those changes back to the serial port.

If I am not mistaken you have to change the mentioned settings when connecting through serial port.

You do not mention it, but I guess you are using the pySerial library. Regardless you can use it with the correct settings to connect through serial. The constructor of the API allows all the parameters which are noted below: Pyserial Library

I have not tested this approach as I have had a similar problem in C not Python.

Upvotes: 0

Naib
Naib

Reputation: 1029

Have you flushed the serial buffer

ser.flushInput()

Upvotes: 1

Related Questions