简体   繁体   English

PySerail Readline进入无限循环

[英]PySerail Readline enters infinite loop

I am working on a BP sensor serial and am using serial readline() to read the output provided by the port, when I initiate the port as serial.Serial('/dev/ttyUSB2',115200,timeout=1) and print the date read using readline() in a loop as follows 当我将端口初始化为serial.Serial('/dev/ttyUSB2',115200,timeout=1)并打印时,我正在处理BP传感器串行,并使用serial readline()读取端口提供的输出。在循环中使用readline()读取日期,如下所示

for I in range(1,10)
  print('integration')
  print(ser.readline())

the readline() seems to into infinite loop as the program prints integration once and then goes infinite however when I try the same with timeout=0 the program prints integration 9 times with blank space after each line. readline()似乎陷入无限循环,因为该程序一次打印积分,然后变为无限,但是当我尝试使用timeout=0进行相同操作时,该程序在每一行后打印9次积分,并带有空白。 Need to know what I am doing wrong as the same code seems to work fine with other sensors like laser distance sensor etc. Already referred to Reference1 Reference2 and a few more. 需要知道我在做什么错,因为相同的代码似乎可以与其他传感器(如laser distance sensor等)一起正常工作。已经参考了Reference1 Reference2等。

ser.readline() expects to find a \\n character so it keeps reading data until it finds it. ser.readline()希望找到一个\\n字符,因此它将一直读取数据,直到找到为止。

You need to make sure your sensor is sending this default EOL character. 您需要确保您的传感器正在发送此默认EOL字符。 If that is not the case, you can specify a different character. 如果不是这种情况,则可以指定其他字符。

There used to be a simple way to do that but apparently, it's not working anymore. 过去曾经有一种简单的方法可以做到这一点,但是显然,它不再起作用了。 The workaround is to use TextIOWrapper , see here . 解决方法是使用TextIOWrapper ,请参见此处 Pay attention to the first answer to that question to make sure you change the default buffer size. 请注意该问题的第一个答案,以确保您更改默认的缓冲区大小。

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

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