I am hoping this is a simple stupid noob mistake that can be fixed with the addition of a single line of code somewhere.
I'm using pySerial to read in serial data from a USB port and print it out to standard output. I'm running Mac OSX 10.6. I open terminal and type "python", then the following:
>>> import serial;
>>> ser = serial.Serial('/dev/tty.usbserial-XXX', 9600, timeout=1);
>>> while True:
>>> if ser.inWaiting() > 0:
>>> ser.readline();
>>> [done, I hit enter...]
This works beautifully. It starts outputting my serial data nicely, exactly as I'd expect it to. Great, I think, let me put this into its own script with command line arguments and then I can call it any time I want to:
import sys;
import serial;
serialPort = sys.argv[1]
baudRate = sys.argv[2]
ser = serial.Serial(serialPort, baudRate, timeout=1)
while True:
if ser.inWaiting() > 0:
ser.readline()
On the command line, I type "python myScript.py [my serial port] 9600" and sit back and wait for a beautiful flow of serial data - but nothing comes out. It just kinda hangs until I kill the process.
I thought, well maybe for some reason it's not working - so I put some debugging prints into the code. I update my while loop to look like this:
while True:
print("looping...")
print(ser.inWaiting());
if ser.inWaiting() > 0:
ser.readline()
I run it again, and I get a repeating output stream of "Looping..." and "0". I think, well maybe there's something wrong with my command line arguments - so I hard-coded the port and the baud rate into the script, same thing.
So, why would this be the case? Is my use of while True: somehow blocking the script from accepting serial data? Is there a better way to do this?
I'm a complete Python noob. I am writing this script in order to create a faster way to communicate between Adobe AIR and an Arduino board. I'm hoping there's a magic bullet I can drop in here to make it work - is there?