-2

我正在编写一个程序,它将命令代码发送到串行设备并从设备获取响应,然后发送另一个命令。我可以完美地发送命令,甚至得到响应。但是,我的 if 语句似乎不承认响应。

例子:

response = sendCommand(beep)
if response == '0D6010100':
  print"Command beeped!"

就像我提到的,我得到一个成功的响应代码 0D6010100,但是 if 语句不承认它。有任何想法吗?

我正在使用 Python 2.7 并使用 PySerial

UDPATE:

print type(response) 给了我: print repr(response) 给了我: '0D6010100\r\n' 我假设 repr(response) 末尾的 \r\n 在这个问题上有一些帮助?

4

2 回答 2

2

正如您所怀疑的,\r\n最后是问题所在。做

response = sendCommand(beep).strip()

从字符串的末尾去除空格。

于 2012-12-19T04:59:18.023 回答
2

您需要先修剪响应:

if response.rstrip() == '0D6020100':
   print 'beepity boop beep woohoo!'
于 2012-12-19T04:59:58.643 回答