0

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.

Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41

1 Answers1

1

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()
dvlahovski
  • 46
  • 4
  • but the `print ser.name` doesn't even print even tho it is right after `ser = serial.Serial(port, baudrate)` – Ahmed Al-haddad Mar 11 '16 at 13:00
  • Most likely, the output buffering is affecting this. Try to flush stdout after that print (http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print) – dvlahovski Mar 11 '16 at 13:01
  • where should I put it? After the `print ser.name` line? – Ahmed Al-haddad Mar 11 '16 at 13:06
  • Just a note too, if I remove the infinite loop, the program terminates immediately..! – Ahmed Al-haddad Mar 11 '16 at 13:08
  • Yup, after the `print ser.name` line. I would also suggest you to try to read just one line without using any loops and print the name and response – dvlahovski Mar 11 '16 at 13:12
  • I tried as you said sir and the program instantly terminates, without printing or anything. As if it doesn't even see the function! – Ahmed Al-haddad Mar 11 '16 at 13:25
  • Could you edit your answer and add to it the latest code that you tried running – dvlahovski Mar 11 '16 at 13:29
  • Done. I tried not putting a function but `yield` only works if it's inside a function. If this doesn't work out at all, is there another way of writing this that will produce the same output? – Ahmed Al-haddad Mar 11 '16 at 13:35
  • See my answer - I edited it. Also, it isn't necessary to use yield. – dvlahovski Mar 11 '16 at 13:41
  • This works, but it cannot fulfill what I need. Thus I would be thankful if you could check [my earlier question](http://stackoverflow.com/questions/35936644/python-read-from-the-serial-port-data-line-by-line-into-a-list-when-available). The problem with this is that I need to anticipate whenever data is sent through the serial port. I am using `yield` from here [link] (http://raspberrypi.stackexchange.com/questions/15600/reading-from-serial-port-loop) – Ahmed Al-haddad Mar 11 '16 at 13:56