python – pyserial readline():SerialException

我正在写一个用于向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)

最佳答案 这里有一个snipet如何在python中使用serial

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
点赞