<legend id='rVScw'><style id='rVScw'><dir id='rVScw'><q id='rVScw'></q></dir></style></legend>
    <bdo id='rVScw'></bdo><ul id='rVScw'></ul>

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

    <tfoot id='rVScw'></tfoot>

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

      Python - 可用时从串行端口数据逐行读取到列表中

      Python - read from the serial port data line by line into a list when available(Python - 可用时从串行端口数据逐行读取到列表中)
      • <bdo id='P9dUD'></bdo><ul id='P9dUD'></ul>
          <legend id='P9dUD'><style id='P9dUD'><dir id='P9dUD'><q id='P9dUD'></q></dir></style></legend>

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

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

                <i id='P9dUD'><tr id='P9dUD'><dt id='P9dUD'><q id='P9dUD'><span id='P9dUD'><b id='P9dUD'><form id='P9dUD'><ins id='P9dUD'></ins><ul id='P9dUD'></ul><sub id='P9dUD'></sub></form><legend id='P9dUD'></legend><bdo id='P9dUD'><pre id='P9dUD'><center id='P9dUD'></center></pre></bdo></b><th id='P9dUD'></th></span></q></dt></tr></i><div id='P9dUD'><tfoot id='P9dUD'></tfoot><dl id='P9dUD'><fieldset id='P9dUD'></fieldset></dl></div>
                本文介绍了Python - 可用时从串行端口数据逐行读取到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的目标是编写一个代码,该代码将无限期地监听和读取串行端口,每隔几秒就会产生一次输出

                串口输出:

                aaaa::abcd:0:0:0//printf("%d
                ",data[0]);2387//printf("%d
                ",data[1]);14-9244-44108

                我希望将数据附加到这样的列表中,python 假定输出

                [abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]

                我在许多其他代码中尝试了此代码,但没有任何效果,我一直没有输出编辑-下面的代码给了我这个输出

                '''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #or[['abcd::abcd:0:0:c9
                ', '2406
                ', '14
                ', '-7
                ']] # 以此类推,每次迭代输出不同'''#[['aaaa::c30c:0:0:c9
                ', '2462
                ', '11
                ', '-9
                ', '242
                ', '-45
                ','106
                ']] 显然它只工作了一次.ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)打印序列号而真:数据 = []data.append(ser.readlines())打印数据# 进一步处理# 将数据发送到其他地方等打印数据ser.close()

                解决方案

                readline 会一直读取数据,直到读取一个终止符(新行).请尝试:阅读.

                更新:

                使用picocom -b 115200/dev/ttyUSB0 或者putty(serial model) 来检测端口和波特率是否正确.我在你的两个问题中有两个不同的端口.如果打开错误端口,read() 将一直等待直到读取一个字节.像这样:

                导入序列号# Windows 7的ser = serial.Serial()ser.port = 'COM1'ser.open()ser.read() # COM1 没有数据,读取一直等到读取一个字节.

                如果你在控制台输入这段代码,控制台不会有这样的输出:

                <块引用>

                >>>导入序列号>>>ser = serial.Serial()>>>ser.port = 'COM1'>>>ser.open()>>>ser.read()_

                我们需要为读取添加超时来修复它.
                你可以试试这个:

                导入序列号进口时间z1 波特率 = 115200z1port = '/dev/ttyUSB0' # 运行前设置正确的端口z1serial = serial.Serial(端口=z1port,波特率=z1baudrate)z1serial.timeout = 2 # 设置读取超时# print z1serial # 调试串口.print z1serial.is_open # 打开时为真如果 z1serial.is_open:而真:大小 = z1serial.inWaiting()如果尺寸:数据 = z1serial.read(大小)打印数据别的:打印'没有数据'时间.sleep(1)别的:print 'z1serial 未打开'# z1serial.close() # 如果 z1serial 打开,则关闭 z1serial.

                I am aiming to write a code that will be indefinitely listening and reading from to a serial port that will have this output produced every few seconds

                serial port output:

                aaaa::abcd:0:0:0
                //printf("%d
                ",data[0]);
                2387
                //printf("%d
                ",data[1]);
                14
                -9
                244
                -44
                108
                

                I want the data to be appended in a list like this, python supposed output

                [abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]
                

                I tried this code amongst many others but nothing worked, I keep on getting no output EDIT- the code below gives me this output

                '''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #or
                [['abcd::abcd:0:0:c9
                ', '2406
                ', '14
                ', '-7
                ']] # and so on, different output for each iteration''' 
                #[['aaaa::c30c:0:0:c9
                ', '2462
                ', '11
                ', '-9
                ', '242
                ', '-45
                ', '106
                ']] apparently it worked only once. 
                
                
                ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)
                print ser.name
                while True:
                    data = []
                    data.append(ser.readlines())
                    print data 
                    # further processing 
                    # send the data somewhere else etc
                print data
                ser.close()
                

                解决方案

                readline will keep reading data until read a terminator(new line). please try: read.

                UPDATED:

                use picocom -b 115200 /dev/ttyUSB0 or putty(serial model) to detect the port and baud-rate is right. I got two different ports in your two questions.if open error port, read() will keep waiting until read a byte. like this:

                import serial
                # windows 7
                ser = serial.Serial()
                ser.port = 'COM1'
                ser.open()
                ser.read() # COM1 has no data, read keep waiting until read one byte.
                

                if you type this code in console, console will no output like this:

                >>> import serial
                >>> ser = serial.Serial()
                >>> ser.port = 'COM1'
                >>> ser.open()
                >>> ser.read()
                _
                

                we need add timeout for read to fix it.
                you can try this:

                import serial
                import time
                
                z1baudrate = 115200
                z1port = '/dev/ttyUSB0'  # set the correct port before run it
                
                z1serial = serial.Serial(port=z1port, baudrate=z1baudrate)
                z1serial.timeout = 2  # set read timeout
                # print z1serial  # debug serial.
                print z1serial.is_open  # True for opened
                if z1serial.is_open:
                    while True:
                        size = z1serial.inWaiting()
                        if size:
                            data = z1serial.read(size)
                            print data
                        else:
                            print 'no data'
                        time.sleep(1)
                else:
                    print 'z1serial not open'
                # z1serial.close()  # close z1serial if z1serial is open.
                

                这篇关于Python - 可用时从串行端口数据逐行读取到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                  <tfoot id='hjPdT'></tfoot>

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

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

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