Here is the function I am using to poll for new messages received by a GSM modem connected to my laptop via a serial port. I have created a serial object 'phone' which I am using to read and write data.
'phone' reads the data into a list x.
The response I get when I get a new message is of the format:
+CMTI: "SM",0
Here 0 is an index, indicating the index position at which the message is stored on the SIM. The following code is used to poll the list x which contains the data the I am reading and writing from the serial port. I am using regular expressions to parse x which has been converted to a string myRegexableString to see whether I have got the notification or not.
def poll(x):
myRegexableString = "".join(x)
print "Printing myRegexableString"
pprint(myRegexableString)
regex = re.compile(r'\+CMTI: "SM",(\d+)')
lst = []
for l in myRegexableString:
for m in [regex.search(l)]:
if myRegexableString:
lst.append(m)
print "You have received a new message!"
I want to use the above code for a AT command that checks for network registration. I will need to poll for new messages while I am executing other commands too, or any command for that matter but for the sake of this question I am just going to take the example of network registration. Here is how it would normally run without using the polling function.
AT+CREG? #command
+CREG: 0,1 #response
OK #status
Now here's the code for that.
def register():
print "Checking for network registration"
phone.write(b'AT+CREG?\r')
sleep()
x=phone.read(50)
sleep()
print x
Now suppose I include polling here the code would change to
def register():
print "Checking for network registeration"
phone.write(b'AT+CREG?\r')
sleep()
x=phone.read(50)
poll(x)
sleep()
print x
The output I am getting from the changed code is
Checking for network registeration
Printing myRegexableString
'AT+CREG?\r\r\n+CREG: 0,1\r\n\r\nOK\r\n'
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
You have received a new message!
AT+CREG?
+CREG: 0,1
OK
Why is it printing "You have received a new message!" so many times?
Especially when I have not received a new message!
Please help.