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