Py学习  »  Python

如何比较python中ser.readline()的输出?

Nate • 5 年前 • 1390 次点击  

我正在编写一个在Raspberry Pi 2模型B和Arduino UNO之间进行接口的代码,我无法通过将ser.readline()的输出与变量进行比较得到肯定的响应。下面是我的raspberry-pi代码的代码片段,其中“u”只是我发送给arduino让它解释的命令,我已经能够确认它能够成功地解释它。

while 1:
   time.sleep(1)
   ser.write(u.encode('utf-8'))
   print('ACK sent')
   res = ser.readline()
   if res = 'ON':
      print('Pass')
   else:
      print('Try again')

在Arduino上,我发送给ser.write(命令)的响应是

Serial.write("ON\n")

不管我用什么来比较这两个值,pi总是打印“再试一次”,即使当我打印res值时,它清楚地打印“on”。

我错过了什么?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41032
 
1390 次点击  
文章 [ 2 ]  |  最新文章 5 年前