Can't use pyserial to read serial data from Arduino

Hello,

I'm having some trouble interfacing Python with my Arduino.

My Arduino is running this code:

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

void loop() {
Serial.write("test");
delay(500);
}

And Python is running this code:

import serial

serialport = "/dev/tty.usbserial-A6008hrf"
ser = serial.Serial(serialport, 9600)
ser.readline()

This doesn't do anything, it just sits there not receiving anything. I'm able to use Arduino's serial monitor to view the output though, and I'm able to send serial data to the Arduino using pyserial and the same serial port. It's very odd...

My computer:
2011 Macbook Pro running 10.7.3 and Apple-supplied Python 2.7 and latest PySerial

Any suggestions?

My Arduino is running this code:

Serial.write("test");

Why? Are you sending binary data somewhere? No, you are not. You should be using Serial.print() to send ASCII data.

Opening the serial port, by pyserial, resets the Arduino. It does not look like your pyserial code does any kind of looping. The code to read the Arduino output appears to expect data once, immediately. Since there is no data available immediately, the pyserial code gives up and moves on.

PaulS:

My Arduino is running this code:

Serial.write("test");

Why? Are you sending binary data somewhere? No, you are not. You should be using Serial.print() to send ASCII data.

Opening the serial port, by pyserial, resets the Arduino. It does not look like your pyserial code does any kind of looping. The code to read the Arduino output appears to expect data once, immediately. Since there is no data available immediately, the pyserial code gives up and moves on.

Okay, I've changed Arduino to:

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

void loop() {
Serial.print("test");
delay(500);
}

And Python to:

import serial

serialport = "/dev/tty.usbserial-A6008hrf"
ser = serial.Serial(serialport, 9600)
while(1):
    ser.readline()

Still no luck.

I should also say that my Arduino's TX LED is blinking appropriately.

PaulS:

My Arduino is running this code:

Serial.write("test");

Why? Are you sending binary data somewhere? No, you are not. You should be using Serial.print() to send ASCII data.

Opening the serial port, by pyserial, resets the Arduino. It does not look like your pyserial code does any kind of looping. The code to read the Arduino output appears to expect data once, immediately. Since there is no data available immediately, the pyserial code gives up and moves on.

Here's the implementation of write(char *) from Print.h in the arduino core:

size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }

Here's the implementation of print(char *) from Print.cpp in the arduino core:

size_t Print::print(const char str[])
{
  return write(str);
}

Pauls, can we stop this senseless attack on the use of write to send strings to Serial? No bits are being harmed.

drewbharris:

PaulS:

My Arduino is running this code:

Serial.write("test");

Why? Are you sending binary data somewhere? No, you are not. You should be using Serial.print() to send ASCII data.

Opening the serial port, by pyserial, resets the Arduino. It does not look like your pyserial code does any kind of looping. The code to read the Arduino output appears to expect data once, immediately. Since there is no data available immediately, the pyserial code gives up and moves on.

Okay, I've changed Arduino to:

void setup() {

Serial.begin(9600);
}

void loop() {
Serial.print("test");
delay(500);
}




And Python to:



import serial

serialport = "/dev/tty.usbserial-A6008hrf"
ser = serial.Serial(serialport, 9600)
while(1):
    ser.readline()




Still no luck.

The python ser.readline() function is going to keep reading data until it gets a newline. It won't return until then. Your sketch never sends a newline. Try Serial.println("test") or Serial.write("test\n\r") instead.

Also the python code you posted doesn't do anything with the data read from the serial port, so even if it works you won't be able to tell. Did you mean to print it or write it to a file?

If you don't want to send a newline from your sketch, try this python code instead:

while 1:
        byte = ser.read(size=1)
        print byte,

Wow, thanks! Never thought about the new line thing. I'll try it when I get home today.

Also, I had tried my script with a "print set.readline()" to no avail before. I'll give this a shot though.

if you continue having issues you might consider using pytty. i helped write it and since then i've been using it for about 2 years in the HacDC occupancy sensor with no problems (due to pytty atleast :P). it's much more straight forward to use and is pure python so it's a bit less hassle if you don't need to do more than read and write to the device.

dhunt:
If you don't want to send a newline from your sketch, try this python code instead:

while 1:

byte = ser.read(size=1)
        print byte,

WOW! this worked! thank you!

haxwithaxe:
if you continue having issues you might consider using pytty. i helped write it and since then i've been using it for about 2 years in the HacDC occupancy sensor with no problems (due to pytty atleast :P). it's much more straight forward to use and is pure python so it's a bit less hassle if you don't need to do more than read and write to the device.

Thanks! I'll be trying this as well, I do like pure python.