我在Raspberry Pi 2和Arduino Mega之间有一个串行连接
连接开始时,Arduino会发送一条"Power On"
的消息
我想比较这条消息来检查Arduino上是否一切正常,但我无法将消息与字符串进行比较。它总是返回false。
如果我运行我的代码,终端上的输出是:
Power On
no
def open_conn_arduino(self):
self.connection = serial.Serial('/dev/ttyACM0', 9600)
if not self.connection.isOpen():
self.connection.open()
# Needs time to setup serial
time.sleep(2)
message = self.read()
print message
if message == "Power On":
print "yes"
else:
print "no"
def send(self, message):
self.connection.write(str(message))
self.read()
def read(self):
while True:
response = self.connection.readline()
if response.__contains__("\n"):
return str(response.split("\n")[0])
def close_conn_arduino(self):
self.connection.close()