I'm using an Arduino Leonardo to interface with two pressure sensors and Python on my laptop, a MackBook Pro.
I'm using arduino IDE V. 1.5.4, Python V. 2.7.6, and OSX 10.8.5.
One pressure sensor is digital, using the Wire lib, and the other is analog.
My sketch compiles, runs, and works until I try to get the data in Python using pyserial.
If I try to grab a bunch of data using the readlines method in the scrip it will only return blank data or hang, depending on the serial object timeout settings. My python code sometimes works from the repl when I initialize the serial object and wait before reading from it. If I don't wait the serial object will only partially fill a list and then any future reads will return an empty string. This continues even after closing the serial object and reopening it in Python, or closing the serial object in Python and then opening the Serial Monitor in the Arduino IDE. Once the serial port stops sending, I have to reset the Leonardo in order to read new data.
I could be running into some kind of buffering problem, but if that were the case I would expect future calls to return data. Another option is that the Wire protocol is getting stuck and halting the program.
Here's my sketch
#include <Wire.h>
int ADDRESS = 0x28;//I2C address of sensor
int REQUEST = 4;//# of bytes to request from sensor
int BAUD = 19200;//serial rate
int PRESSURE_PIN = 1;
void setup()
{
pinMode(A1, INPUT);
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(BAUD); // start serial for output
}
void loop()
{
Wire.requestFrom(ADDRESS, REQUEST); // request 6 bytes from slave device #2
Serial.print(analogRead(PRESSURE_PIN));
Serial.print(',');
while(Wire.available()) // slave may send less than requested
{
Serial.print(((unsigned int)Wire.read() << 8) + Wire.read());
Serial.print(',');
Serial.println(((unsigned int)Wire.read()) << 3) + (Wire.read() >> 5);
}
delay(10);
}
and here is how I read it from python
import serial
serial_port = '/dev/tty.usbmodem1a1221'
ser = serial.Serial(serial_port, 19200, timeout=0)
vals = [val.split(',')[0:2] for val in ser.readlines(1001*14)]