Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Writing another script to capture logs using pyserial, call this script using subprocess.Popen()
  • Using built in unix tools such as tail or cat and calling these with subprocess.Popen()

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.

like image 280
user1322388 Avatar asked Feb 03 '26 12:02

user1322388


2 Answers

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.

like image 95
Vinayak Kolagi Avatar answered Feb 05 '26 03:02

Vinayak Kolagi


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

like image 37
Dudi b Avatar answered Feb 05 '26 02:02

Dudi b