user1322388
user1322388

Reputation: 45

Capturing serial data in background process

I am currently trying to capture serial data within a python script. I intend to begin capturing a log of all the data captured on a serial port while the rest of the script continues to interact with the system I am testing.

If I use pyserial I believe it will end up blocking the rest of the tests I want to carry out until I finish logging.

My options I have considered are:

I am sure I could find a way to get either of these to work, but if anyone knows of a more direct way of doing it then I would love to know.

Thank you in advance.

Upvotes: 2

Views: 6235

Answers (2)

Dudi b
Dudi b

Reputation: 260

you can always check if there is available data with ser.inWaiting() see link

from serial import Serial
ser = Serial(port=0, baudrate=19200) # set the parameters to what you want
while 1:
    if ser.inWaiting():
        temp = ser.read()
    #all the other code in the loop

if there is no data available to read the loop skips the serial reading

or if the data is to time sensitive you can do this

Upvotes: 4

Vinayak Kolagi
Vinayak Kolagi

Reputation: 1881

Why create another process for reading data from pySerial ? For non-blocking the read you can configure the timeout in serial class. e.g.

ser = serial.Serial()
ser.baudrate = 19200
ser.port = 0
ser.timeout = 2 #By default, this is set to None
ser.open()

Also look at the wrapper class for reference.

http://pyserial.sourceforge.net/examples.html#wrapper-class

You can run a thread to keep reading the data from serial and update it to the buffer.

Creating another process invloves the overhead of IPC and not recommended for this task.

Upvotes: 6

Related Questions