Pages: [1]   Go Down
Author Topic: Can't use pyserial to read serial data from Arduino  (Read 3618 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

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

My Arduino is running this code:

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

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

And Python is running this code:

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My Arduino is running this code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My Arduino is running this code:
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:

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

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

And Python to:

Code:
import serial

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

Still no luck. 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Wellington, New Zealand
Offline Offline
Sr. Member
****
Karma: 1
Posts: 404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My Arduino is running this code:
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:
Code:
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:
Code:
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.
Logged


Wellington, New Zealand
Offline Offline
Sr. Member
****
Karma: 1
Posts: 404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My Arduino is running this code:
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:

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

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

And Python to:

Code:
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:
Code:

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


Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz). 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

WOW! this worked! thank you!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz). 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.
Logged

Pages: [1]   Go Up
Jump to: