Reputation: 833
I have this program that defines a function and then calls it, but no matter what I do, the program doesn't execute the function's call. Am I missing something? I already checked with the other questions but I couldn't find anything similar to what I am facing.
baudrate = 115200
port = '/dev/ttyUSB2'
def serial_data(ser):
print ser.name # it doesn't print here at all!
sys.stdout.flush()
while True:
yield ser.readline()
ser.close()
for line in serial_data('/dev/ttyUSB2', 115200):
print "data : "
print line
data = []
ser = serial.Serial(port, baudrate)
serial_data(ser)
The output for this program is
#nothing, it just hangs.
If I remove the infinite loop, the program terminates immediately.
Upvotes: 0
Views: 56
Reputation: 46
From: https://pythonhosted.org/pyserial/shortintro.html#readline (I believe this is the lib you're using)
Be carefully when using readline(). Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received.
Also nothing after ser.close()
(including) in the serial_data
function will be executed because of the yielding from the infinite loop.
Edit Try this:
baudrate = 115200
port = '/dev/ttyUSB2'
ser = serial.Serial(port, baudrate, timeout=5)
print(ser.name)
print(ser.readline())
ser.close()
Upvotes: 1