<bdo id='rhuxY'></bdo><ul id='rhuxY'></ul>

    <i id='rhuxY'><tr id='rhuxY'><dt id='rhuxY'><q id='rhuxY'><span id='rhuxY'><b id='rhuxY'><form id='rhuxY'><ins id='rhuxY'></ins><ul id='rhuxY'></ul><sub id='rhuxY'></sub></form><legend id='rhuxY'></legend><bdo id='rhuxY'><pre id='rhuxY'><center id='rhuxY'></center></pre></bdo></b><th id='rhuxY'></th></span></q></dt></tr></i><div id='rhuxY'><tfoot id='rhuxY'></tfoot><dl id='rhuxY'><fieldset id='rhuxY'></fieldset></dl></div>

        <tfoot id='rhuxY'></tfoot><legend id='rhuxY'><style id='rhuxY'><dir id='rhuxY'><q id='rhuxY'></q></dir></style></legend>

        <small id='rhuxY'></small><noframes id='rhuxY'>

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

        时间:2023-07-24
        • <bdo id='wMk2m'></bdo><ul id='wMk2m'></ul>
          • <legend id='wMk2m'><style id='wMk2m'><dir id='wMk2m'><q id='wMk2m'></q></dir></style></legend>

              1. <small id='wMk2m'></small><noframes id='wMk2m'>

                  <tfoot id='wMk2m'></tfoot>
                    <tbody id='wMk2m'></tbody>

                1. <i id='wMk2m'><tr id='wMk2m'><dt id='wMk2m'><q id='wMk2m'><span id='wMk2m'><b id='wMk2m'><form id='wMk2m'><ins id='wMk2m'></ins><ul id='wMk2m'></ul><sub id='wMk2m'></sub></form><legend id='wMk2m'></legend><bdo id='wMk2m'><pre id='wMk2m'><center id='wMk2m'></center></pre></bdo></b><th id='wMk2m'></th></span></q></dt></tr></i><div id='wMk2m'><tfoot id='wMk2m'></tfoot><dl id='wMk2m'><fieldset id='wMk2m'></fieldset></dl></div>
                2. 本文介绍了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同时从多个串口读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 python 进行串行数据记录 下一篇:读取串行端口 - 在一定时间内忽略写入串行端口的部分数据

                  相关文章

                  • <bdo id='nY9tY'></bdo><ul id='nY9tY'></ul>

                  <tfoot id='nY9tY'></tfoot>

                    <legend id='nY9tY'><style id='nY9tY'><dir id='nY9tY'><q id='nY9tY'></q></dir></style></legend>
                      <i id='nY9tY'><tr id='nY9tY'><dt id='nY9tY'><q id='nY9tY'><span id='nY9tY'><b id='nY9tY'><form id='nY9tY'><ins id='nY9tY'></ins><ul id='nY9tY'></ul><sub id='nY9tY'></sub></form><legend id='nY9tY'></legend><bdo id='nY9tY'><pre id='nY9tY'><center id='nY9tY'></center></pre></bdo></b><th id='nY9tY'></th></span></q></dt></tr></i><div id='nY9tY'><tfoot id='nY9tY'></tfoot><dl id='nY9tY'><fieldset id='nY9tY'></fieldset></dl></div>

                      <small id='nY9tY'></small><noframes id='nY9tY'>