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

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

      1. <tfoot id='CHxAw'></tfoot>
      2. <legend id='CHxAw'><style id='CHxAw'><dir id='CHxAw'><q id='CHxAw'></q></dir></style></legend>

        Python Serial:如何使用 read 或 readline 函数一次读取多个字符

        Python Serial: How to use the read or readline function to read more than 1 character at a time(Python Serial:如何使用 read 或 readline 函数一次读取多个字符)

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

                  <small id='8jpsd'></small><noframes id='8jpsd'>

                  <legend id='8jpsd'><style id='8jpsd'><dir id='8jpsd'><q id='8jpsd'></q></dir></style></legend>
                  本文介绍了Python Serial:如何使用 read 或 readline 函数一次读取多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法使用我的程序读取多个字符,我似乎无法弄清楚我的程序出了什么问题.

                  I'm having trouble to read more than one character using my program, I can't seem to figure out what went wrong with my program.

                  import serial
                  
                  ser = serial.Serial(
                      port='COM5',
                      baudrate=9600,
                      parity=serial.PARITY_NONE,
                      stopbits=serial.STOPBITS_ONE,
                      bytesize=serial.EIGHTBITS,
                          timeout=0)
                  
                  print("connected to: " + ser.portstr)
                  count=1
                  
                  while True:
                      for line in ser.read():
                  
                          print(str(count) + str(': ') + chr(line) )
                          count = count+1
                  
                  ser.close()
                  

                  这是我得到的结果

                  connected to: COM5
                  1: 1
                  2: 2
                  3: 4
                  4: 3
                  5: 1
                  

                  其实我早就料到了

                  connected to: COM5
                  1:12431
                  2:12431
                  

                  类似于上面提到的东西,它可以同时读取多个字符,而不是一个一个.

                  something like the above mentioned which is able read multiple characters at the same time not one by one.

                  推荐答案

                  我发现了几个问题.

                  第一:

                  ser.read() 一次只会返回 1 个字节.

                  ser.read() is only going to return 1 byte at a time.

                  如果您指定计数

                  ser.read(5)
                  

                  它将读取 5 个字节(如果在 5 个字节到达之前发生超时,则更少.)

                  it will read 5 bytes (less if timeout occurrs before 5 bytes arrive.)

                  如果您知道您的输入始终以 EOL 字符正确终止,更好的方法是使用

                  If you know that your input is always properly terminated with EOL characters, better way is to use

                  ser.readline()
                  

                  这将继续读取字符,直到收到 EOL.

                  That will continue to read characters until an EOL is received.

                  第二:

                  即使你让 ser.read() 或 ser.readline() 返回多个字节,由于您正在迭代返回值,因此您将仍然一次处理一个字节.

                  Even if you get ser.read() or ser.readline() to return multiple bytes, since you are iterating over the return value, you will still be handling it one byte at a time.

                  摆脱

                  for line in ser.read():
                  

                  然后说:

                  line = ser.readline()
                  

                  这篇关于Python Serial:如何使用 read 或 readline 函数一次读取多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)
                  <i id='T3krI'><tr id='T3krI'><dt id='T3krI'><q id='T3krI'><span id='T3krI'><b id='T3krI'><form id='T3krI'><ins id='T3krI'></ins><ul id='T3krI'></ul><sub id='T3krI'></sub></form><legend id='T3krI'></legend><bdo id='T3krI'><pre id='T3krI'><center id='T3krI'></center></pre></bdo></b><th id='T3krI'></th></span></q></dt></tr></i><div id='T3krI'><tfoot id='T3krI'></tfoot><dl id='T3krI'><fieldset id='T3krI'></fieldset></dl></div>
                • <legend id='T3krI'><style id='T3krI'><dir id='T3krI'><q id='T3krI'></q></dir></style></legend>

                      <tbody id='T3krI'></tbody>

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

                      <tfoot id='T3krI'></tfoot>
                        <bdo id='T3krI'></bdo><ul id='T3krI'></ul>