I have a stupid python problem.
I’m trying to read a line from a file everytime i get a ‘READY’ message from a serial connection so i wrote this :
import serial
from time import sleep
port = "/dev/tty.usbserial-A400fYTT"
speed = 57600
polarfile = 'polarfile.pg'
f = open(polarfile, 'r')
ser = serial.Serial(port, speed, timeout=0)
while True:
data = ser.read(9999)
if len(data) > 0:
if(data == 'READY'):
f.readline()
else:
sleep(0.5)
sleep(1)
ser.close()
But it doesn’t work, however if i replace the if(data == ‘READY’ block by print data. I get the READY message.
Also i can read my file with f.readline()…
Thanks to give advice to a py newbie
—
edit :
Important info, the serial doesn’t receive only “READY” message, but a bunch of other, but i want just to react when the “READY” messsage is received.
I just replace
data = ser.read(9999)
by
data = ser.readline(9999)which gives me the message line by line instead of second by second of input data and then replaceif( data == 'READY' ):byif (data.startswith('READY')):and now it works 🙂