0

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()
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Silly question, but is there really a Hayes-compatible modem on the other end? – Charles Duffy Apr 20 '21 at 17:27
  • you can also use `at_cmd = b'AT'` to skip the `.encode()` in the following line. – Arusekk Apr 20 '21 at 17:28
  • 1
    Also, I don't see you sending a newline after the `AT` (as @hobbs describes in more detail in the comment just below mine). – Charles Duffy Apr 20 '21 at 17:29
  • (that said, I'm surprised that `read(2)` returns data at all instead of blocking indefinitely; if there's content in the buffer before you start, you might want to be sure that gets consumed; whereas if line configuration is wrong and that's creating noise in place of the bytes that are supposed to be transferred, that's a whole 'nother problem that needs to be separately addressed) – Charles Duffy Apr 20 '21 at 17:32
  • @Arusekk, tks for your suggestion. – Italo Santos Apr 21 '21 at 20:43
  • @CharlesDuffy, could please help me again? Like a I said, I sent the "AT" command and I know, using another program, that return of Samsung device (SM-J410G) is "OK". But, how can I get this information in my code? – Italo Santos Apr 21 '21 at 20:49
  • @ItaloSantos, `AT` _alone_ doesn't send an `OK`. As hobbs said, there needs to be a carriage return after the AT, because otherwise the modem doesn't know that it isn't part of a bigger command (for example, `ATDT` is a separate command, even though it starts with `AT`, so the modem can't know when it _only_ sees `AT` if it just received a attention command, or if it's the start of an ATDT command). The answer by hlovdal is on-point. – Charles Duffy Apr 21 '21 at 21:11

1 Answers1

1

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.

hlovdal
  • 26,565
  • 10
  • 94
  • 165