Python Pyserial同时从多个串口读取数据

2023-07-24Python开发问题
19

本文介绍了Python Pyserial同时从多个串口读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 Python 2.7 和 PySerial 同时读取多个串行端口.
特征应该是:

I'm trying to read out multiple serial ports at the same time with Python 2.7 and PySerial.
Features should be:

  1. 在主程序中,我获取所有打开的串行端口,打开它们并将串行对象附加到串行对象
  2. 我想在一个子进程中读取每个串口数据进行并行化

最大的问题是:如何将串口对象传递给子进程?
或者:
是否存在另一种(也许更好的)解决方案?(也许 这个:如何将扭曲的串行端口应用于我的问题?)

The big problem is: how do I pass the serial port object to the subprocess?
OR:
Does another (and maybe better) solution exist to this? (Maybe this: How do I apply twisted serial ports to my problem?)

我想我并不完全清楚我想要实现的目标.
我想同时读取 2 个或更多串口.由于超时和读出时间,不可能在一个进程中同时读出它们.
下面的做法

I think I wasn't totally clear what i want to achieve.
I want to read out 2 or more serial ports at the same time. Because of timeout and readout times it isn't possible to read them out at the same time in one process.
The following approach

ser1 = serial.Serial(port="COM1",baudrate=9600)
ser2 = serial.Serial(port="COM2",baudrate=9600)

ser1.write('command for reading out device 1')
output1 = ser1.readline()

ser2.write('command for reading out device 2')
# now you have to wait at least 100ms for device 2 to respond
output2 = ser2.readline()

不能满足我的需求.

另一种方法是并行化子进程中的串行读数.

Another approch is to parallelize the serial readings in subprocesses.

import serial   # serial communication
from subprocess import Popen, PIPE

ports = ["COM1", "COM2"]
for port in ports:
    ser = serial.Serial()
    ser.port=port
    ser.baudrate=9600
    # set parity and ...

    serialobjects.append(ser)

# call subprocess
# pass the serial object to subprocess
# read out serial port


# HOW TO PASS SERIAL OBJECT HERE to stdin
p1 = Popen(['python', './ReadCOM.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM1 permanently
p2 = Popen(['python', './ReadCOM.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM2 permanently

for i in range(10):
    print "received from COM1: %s" % p1.stdout.readline() # print output from ReadCOM.py for COM1
    print "received from COM2: %s" % p2.stdout.readline() # print output from ReadCOM.py for COM2

ReadCOM.py(取自相关帖子 并编辑)

import sys

while True:  # The program never ends... will be killed when master is over.
    # sys.stdin.readline()

    ser.write('serial command here
') # send command to serial port
    output = ser.readline() # read output

    sys.stdout.write(output) # write output to stdout
    sys.stdout.flush()

提前致谢!

推荐答案

先修改ReadCOM.py来接收参数

import sys
import serial

ser = serial.Serial(port=sys.argv[1],baudrate=int(sys.argv[2]))
while True:  # The program never ends... will be killed when master is over.
    # sys.stdin.readline()

    ser.write('serial command here
') # send command to serial port
    output = ser.readline() # read output

    sys.stdout.write(output) # write output to stdout
    sys.stdout.flush()

并在 main.py 中传递它之后:

and after pass it in main.py:

from subprocess import Popen, PIPE

# call subprocess
# pass the serial object to subprocess
# read out serial port


# HOW TO PASS SERIAL OBJECT HERE to stdin
p1 = Popen(['python', './ReadCOM.py', "COM1", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM1 permanently
p2 = Popen(['python', './ReadCOM.py', "COM2", "9600"], stdin=PIPE, stdout=PIPE, stderr=PIPE) # read COM2 permanently

for i in range(10):
    print "received from COM1: %s" % p1.stdout.readline() # print output from ReadCOM.py for COM1
    print "received from COM2: %s" % p2.stdout.readline() # print output from ReadCOM.py for COM2

这篇关于Python Pyserial同时从多个串口读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11