python - pyserial readline() : SerialException

标签 python serial-port pyserial

我正在编写用于向 avr 发送订单的代码。我发送了几条信息,但在每次写入之间,我必须等待一个答案(我必须等待机器人到达坐标系上的一个点)。正如我在文档中读到的那样,readline() 至少应该读取到超时,但是一旦我发送第一个坐标,readline() 就会自动返回:

SerialException: device reports readiness to read but returned no data (device disconnected?)

当我在 for 循环中的每个 write() 之间放置一个 sleep() 时,一切正常。我尝试使用 inWaiting() 但它仍然不起作用。这是我如何使用它的示例:

for i in chemin_python:

     self.serieInstance.ecrire("goto\n" + str(float(i.x)) + '\n' + str(float(-i.y)) + '\n')

     while self.serieInstance.inWaiting():
         pass

     lu = self.serieInstance.readline()
     lu = lu.split("\r\n")[0]
     reponse = self.serieInstance.file_attente.get(lu)
     if reponse != "FIN_GOTO":
          log.logger.debug("Erreur asservissement (goto) : " + reponse)

最佳答案

这里是如何在 python 中使用串行的片段

s.write(command); 
st = ''
initTime = time.time()
while True:
  st +=  s.readline()
  if timeout and (time.time() - initTime > t) : return TIMEOUT
if st != ERROR: return OK
else:           return ERROR

关于python - pyserial readline() : SerialException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9649482/

相关文章:

python - 将数组写入 csv python(一列)

python - 如何使用 PIL 根据人脸检测裁剪选定区域?

c# - SerialPort RS-485 和通信限制

python - 在后台进程中捕获串行数据

Python 网络应用程序 - 同步串行访问

python - python 包串行已重命名为 pyserial 吗?

python - 如何在 Django 应用程序的 View 中引用静态文件

python - time.time() 无法在 Cython 中运行 while 循环预定时间

c# - 如何自动检测串行COM端口c#的连接

python - 无法停止收到此错误 : ValueError: invalid literal for int() with base 10: ''