I have a Raspberry Pi 4B, a SIM7600G-H 4G HAT, and an AT&T SIM card. There are 4 messages in the SIM card, which can be displayed in minicom through AT+GMGL="ALL"
I am using the following code to retrieve the messages in Python. Sometimes I can get all the messages, but sometimes get empty output.
Initially, I got empty output. Thanks to MiscBits, I can see the messages in Python. But in an unstable way. So I decided to start a new post to see if someone can explain the weird situation.
The following are the tests I did on the following code:
The first time I got the messages: I changed timeout = 5, once the script starts running, immediately send a message to the SIM card. Then all messages were printed out.
Then I switch back to timeout=1, run the code (as posted bellow), no messages sent to the SIM card at this time, I still get the messages printed out.
Then I wrapped it as a function into another script, return message. This ran gave me nothing.
When I tried to run the original script (as posted bellow), which was tested before wrapping to a function, I got empty result.
Then I unplugged the Pi for one night.
Next day:
I ran the following script, I got all messages printed out.
Then ran the script with the wrapped function, no message came out.
And at the same time, the following script won't read any messages again. Even restart the Pi. Maybe next morning it will, I guess.
When running the Python script, I can see the AT commands in minicom. Every time all messages will be listed when the AT+CMGL="ALL" is executed. But empty [] in Python output.
import serial
import time
ser = serial.Serial(port='/dev/ttyS0', baudrate=115200, timeout=1)
ser.write(b'AT+CMGF=1\r\n')
time.sleep(1)
ser.write(b'AT+CMGL="ALL"\r\n')
time.sleep(1)
response = ser.read(1024).decode()
messages = []
lines = response.split('\r\n')
index = 0
while index < len(lines):
if lines[index].startswith('+CMGL:'):
parts = lines[index].split(',')
index_number = int(parts[0].split(':')[1].strip())
sender = parts[2].strip().strip('"')
date_time = parts[4].strip().strip('"')
message = lines[index + 1].strip()
messages.append({'index': index_number, 'sender': sender, 'date_time': date_time, 'message': message})
index += 2
else:
index += 1
print(messages)
I am using the following code to retrieve the messages in Python. Sometimes I can get all the messages, but sometimes get empty output.
Initially, I got empty output. Thanks to MiscBits, I can see the messages in Python. But in an unstable way. So I decided to start a new post to see if someone can explain the weird situation.
The following are the tests I did on the following code:
The first time I got the messages: I changed timeout = 5, once the script starts running, immediately send a message to the SIM card. Then all messages were printed out.
Then I switch back to timeout=1, run the code (as posted bellow), no messages sent to the SIM card at this time, I still get the messages printed out.
Then I wrapped it as a function into another script, return message. This ran gave me nothing.
When I tried to run the original script (as posted bellow), which was tested before wrapping to a function, I got empty result.
Then I unplugged the Pi for one night.
Next day:
I ran the following script, I got all messages printed out.
Then ran the script with the wrapped function, no message came out.
And at the same time, the following script won't read any messages again. Even restart the Pi. Maybe next morning it will, I guess.
When running the Python script, I can see the AT commands in minicom. Every time all messages will be listed when the AT+CMGL="ALL" is executed. But empty [] in Python output.
import serial
import time
ser = serial.Serial(port='/dev/ttyS0', baudrate=115200, timeout=1)
ser.write(b'AT+CMGF=1\r\n')
time.sleep(1)
ser.write(b'AT+CMGL="ALL"\r\n')
time.sleep(1)
response = ser.read(1024).decode()
messages = []
lines = response.split('\r\n')
index = 0
while index < len(lines):
if lines[index].startswith('+CMGL:'):
parts = lines[index].split(',')
index_number = int(parts[0].split(':')[1].strip())
sender = parts[2].strip().strip('"')
date_time = parts[4].strip().strip('"')
message = lines[index + 1].strip()
messages.append({'index': index_number, 'sender': sender, 'date_time': date_time, 'message': message})
index += 2
else:
index += 1
print(messages)
Statistics: Posted by lhx0901 — Wed Apr 24, 2024 9:57 pm