简体   繁体   中英

I have a empty response of AT command sending by pyserial. How can I get "OK" response?

The response of print(msg) is b' ' and I'm expecting the "OK" response.

import serial

ser = serial.Serial(port='COM57')
if not ser.isOpen():
    ser.open()
print('COM57 is open', ser.isOpen())
at_cmd = 'AT'
ser.write(at_cmd.encode())
msg = ser.read(2)
print(msg)
print(type(msg))
ser.close()

There are several things you need to change here. As already mentioned in the comments sending just "AT" will do nothing. You need to fill your holes in AT command knowledge and distinguish between an AT command and an AT command line . The best place to start is reading all of chapter 5 in the standard V.250 which is the fundamental, basic standard for AT command handling. Do not panic if there is something you do not quite get, but make sure you really get the syntax part (prefix + body + termination).

Note that despite the suggestions in the comments, an AT command line command line should be terminated by \r only and not \r\n ("The termination character may be selected by a user option (parameter S3), the default being CR (IA5 0/13).", and S3 should absolutely not be changed from its default value 13, so in practice you never have to deal with that register).

And with regards to reading and parsing the response you need to put in a proper algorithm. You need to read one by one character and combine those characters into response lines before you even think about trying to interpret the meaning of those (aka "framing" in data protocols). Following that, you need to detect whether you have received a final result code or not (and possible handle intermediate result codes/information text).

There are some more details in these two answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM