簡體   English   中英

Python 3 比較字節數組字符串的條件語句

[英]Python 3 conditional statement comparing byte array strings

Python 3 強制我將字符串文字作為字節數組發送。 如下代碼所示:

cmdSer=serial.Serial('/dev/ttyUSB3', 115200)
cmdSer.write('AT+CGREG?\r'.encode())

while True:
  response = cmdSer.readline()
  print(response)
if '+CGREG:'.encode() in response:
  print("inside if\r")

當我打印響應時,我得到:

b'AT+CGREG?\r\r\n'
b'+CGREG: 0,1\r\n'
b'\r\n'
b'OK\r\n'

即使串行響應包含“+CGREG:”,我的代碼也從未找到它。 我試圖解碼和比較。 在這段代碼中,我正在編碼和比較。 似乎沒有任何效果。 有誰知道如何解決這個問題,我已經在谷歌上搜索了幾個小時。

嘗試將b'<string>'用於字節字符串。 對我來說,encode()/decode() 更直接也更容易混淆:

cmdSer=serial.Serial('/dev/ttyUSB3', 115200)
cmdSer.write(b'AT+CGREG?\r')

while True:
  response = cmdSer.readline()
  print(response)
if b'+CGREG:' in response:
  print("inside if\r")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM