I'm trying to read SMS using GSM Shield 2 and Python, but I have a problem where I can't read the SMS content using pySerial while it works fine with the Serial Mac app.
I do the same thing on both Python and Serial (app). Wait for the module to start, use AT+CMGF=1 for SMS mode, then wait for the SMS to arrive and use AT+CMGR= to read the SMS.
Serial app log:
Start Reset End Reset Enter your AT commands (with CR & NL)... AT+CMGF=1%13%%13%%10% OK%13%%10% %13%%10% +CMTI: "SM",24%13%%10% AT+CMGR=24%13%%13%%10% +CMGR: "REC UNREAD","+XXXXXXXXX","","2019/08/01 12:14:19+08"%13%%10% XXXXXXX%13%%10% %13%%10% OK%13%%10%
import serial ser = serial.Serial( port='/dev/tty.usbmodemFD111', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, ) str_modem_ready="Enter your AT commands (with CR & NL)..." com_set_sms_mode="AT+CMGF=1".encode('utf-8') resp_sms_received='+CMTI: "SM"'.encode('utf-8') com_read_sms='AT+CMGR=' modem_state=0 # WAIT FOR MODEM TO START UP while(modem_state==0): x = ser.read_until() if str_modem_ready in x.decode('utf-8'): modem_state=1 print("Modem ready") ser.write(com_set_sms_mode) print("SMS mode set, listening...") ''' LISTEN FOR OUTPUT CONTAINING '+CMTI: "SM"' (SMS received) THEN EXTRACT THE ID WHERE THE SMS IS STORED. SEND AT+CMGR=<id> TO READ SMS BACK TO LOOP LISTENING FOR RESPONSE CONTAINING 'AT+CMGR=' ''' while True: x = ser.readline() print(x) if resp_sms_received in x: sms_slot = re.search(r"\d+(?=%13)", x.decode('utf-8')).group() #read sms in slot com_read_sms_encoded = com_read_sms+sms_slot com_read_sms_encoded = com_read_sms_encoded.encode('utf-8') ser.write(com_read_sms_encoded) print("Reading SMS in slot "+sms_slot) break # LISTEN TO PORT BYTE BY BYTE while True: x=ser.read() print(x)
Modem ready SMS mode set, listening... b'AT+CMGF=1%13%%10%\r\n' b'+CMTI: "SM",9%13%%10%\r\n' Reading SMS in slot 9 b'A' b'T' b'+' b'C' b'M' b'G' b'R' b'=' b'9'
As you can see it all goes well until it sends the AT+CMGR=9 command to read the SMS at id=9, but then nothing. The +CMGR:... response is missing as opposed to what I get from Serial app.