简体   繁体   English

如何在python中使用多处理读取串行数据?

[英]How to read serial data with multiprocessing in python?

I have a device that outputs data at irregular intervals. 我有一台以不规则间隔输出数据的设备。 I want to write data onto a csv in 2 second intervals. 我想每隔2秒将数据写入CSV。 So I figured multiprocessing with a queue might work. 因此,我认为使用队列进行多重处理可能有效。

Here I'm trying to just pass data from one process to another but I get Serial Exception. 在这里,我试图将数据从一个进程传递到另一个进程,但出现串行异常。 Also, I'm unable to run it on IDLE. 另外,我无法在IDLE上运行它。 So I'm stuck with using the terminal. 所以我坚持使用终端。 As a result, the error message closes as soon as it opens. 结果,错误消息一打开就关闭。

Here's the code: 这是代码:

import multiprocessing
import time
import datetime
import serial

try:
    fio2_ser = serial.Serial("COM3",
                baudrate=2400,
                bytesize=serial.EIGHTBITS,
                parity =serial.PARITY_ODD)

except serial.SerialException:
        print("FiO2 Analyser Device not detected")   


def Read_Data(q):
    global fio2_ser

    while True:

        try:                    
            fio2_data = fio2_ser.readline().decode('utf-8')
            fio2_data = str(fio2_data).replace("\r\n","")
            fio2_data = fio2_data.replace("\x000","")

        except:
                fio2_data = "FiO2 Data Unavailable"

        q.put(fio2_data)

def Disp_Data(q):

    while q.empty() is False:

        fio2_data = q.get()
        print(fio2_data)

        time.sleep(2)


if __name__ == "__main__":

    q = multiprocessing.Queue()
    p1 = multiprocessing.Process(target=Read_Data, args=(q,))
    p2 = multiprocessing.Process(target=Disp_Data, args=(q,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

When I run a separate module to collect data, it runs fine and collects data. 当我运行一个单独的模块来收集数据时,它可以正常运行并收集数据。

import serial

try:
    fio2_ser = serial.Serial("COM3",
                baudrate=2400,
                bytesize=serial.EIGHTBITS,
                parity =serial.PARITY_ODD)

except serial.SerialException:
        print("FiO2 Analyser Device not detected")   

def Reader():
    global fio2_ser
    try:                    
        fio2_data = fio2_ser.readline().decode('utf-8')
        fio2_data = str(fio2_data).replace("\r\n","")
        fio2_data = fio2_data.replace("\x000","")
        return fio2_data
    except:
            return "FiO2 Data Unavailable"

if __name__ =='__main__':
    value = Reader()
    print(value) 

The Disp_Data() function will stop running when q.empty() is True. 当q.empty()为True时,Disp_Data()函数将停止运行。 In my case the loop exits immediately. 就我而言,循环立即退出。

Might be useful to display the error message thrown by SerialException to see the cause: 显示由SerialException抛出的错误消息以查看原因可能很有用:

except serial.SerialException as msg:
        print( "Error opening serial port %s" % msg)

Also, it would be better to gracefully shut down the child-processes. 同样,最好适当地关闭子进程。 In my case, they kept running after killing the main process, so the Read_Data() process keeps the port open. 就我而言,它们在终止主进程后仍保持运行,因此Read_Data()进程使端口保持打开状态。

The multiprocessing module doesn't like pickling pyserial. 多处理模块不喜欢腌制pyserial。

This following code snip works on my Windows10 box 以下代码片段可在我的Windows10机器上使用

  • Used threading instead. 改用线程。
  • Added some print statements here ad there to understand what is happening. 在此处的广告中添加了一些打印声明,以了解发生了什么。
  • Used multiprocessing.Event() to improve shutdown. 使用multiprocessing.Event()改善关机。
  • Print exception error messages to see what causes serial exceptions. 打印异常错误消息,以查看导致串行异常的原因。
  • One second timeout on serial port to allow read loop to continue. 串行端口超时一秒钟,以允许读取循环继续。
    • Maybe not necessary for release code. 发行代码可能不需要。


import threading, multiprocessing
import time
import serial
import sys


def OpenSerialPort(port=""):
    print ("Open port %s" % port)

    fio2_ser = None

    try:
        fio2_ser = serial.Serial(port,
                    baudrate=2400,
                    bytesize=serial.EIGHTBITS,
                    parity =serial.PARITY_ODD)

    except serial.SerialException as msg:
        print( "Error opening serial port %s" % msg)

    except:
        exctype, errorMsg = sys.exc_info()[:2]
        print ("%s  %s" % (errorMsg, exctype))

    return fio2_ser


def Read_Data(queue, serialPort, stopped):
    print ("Start reading data.")

    serialPort.timeout = 1.0
    while not stopped.is_set(): 
        fio2_data = ''       
        try:                    
            #print "Reading port..."
            fio2_data = serialPort.readline()

        except:
            exctype, errorMsg = sys.exc_info()[:2]
            print ("Error reading port - %s" % errorMsg)
            stopped.set()
            break

        if len(fio2_data) > 0:
            fio2_data = fio2_data.decode('utf-8')
            fio2_data = str(fio2_data).replace("\r\n","")
            fio2_data = fio2_data.replace("\x000","")
            queue.put(fio2_data)
        else:
            queue.put("Read_Data() no Data")

    serialPort.close()
    print ("Read_Data finished.")

def Disp_Data(queue, stopped):
    print ("Disp_Data started")
    while not stopped.is_set():
        #print "Check message queue."
        if queue.empty() == False:        
            fio2_data = queue.get()
            print(fio2_data)

    print ("Disp_Data finished")

if __name__ == "__main__":


    #serialPort = OpenSerialPort('/dev/ttyUSB0')
    serialPort = OpenSerialPort('COM3')
    if serialPort == None: sys.exit(1)

    queue = multiprocessing.Queue()
    stopped = threading.Event()
    p1 = threading.Thread(target=Read_Data, args=(queue, serialPort, stopped,))
    p2 = threading.Thread(target=Disp_Data, args=(queue, stopped,))

    p1.start()
    p2.start()

    loopcnt = 20
    while (loopcnt > 0) and (not stopped.is_set()):
        loopcnt -= 1
        print ("main() %d" % loopcnt)
        try:
            time.sleep(1)

        except KeyboardInterrupt: #Capture Ctrl-C
            print ("Captured Ctrl-C")
            loopcnt=0
            stopped.set()

    stopped.set()
    loopcnt=0        

    print ("Stopped")
    p1.join()
    p2.join()

    serialPort.close()
    print ("Done")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM