3

I 99% of finishing my script but have run into a problem

im trying to display the text messages recived from a GSM modem in python.

So far I can read that a text has come in but cant display the message.

I know the self.ser.readlines() comes back as a list

import serial
import time
class TextMessage:
    def connectPhone(self):
        self.ser = serial.Serial('COM14', 460800, timeout=5) #for mine this was ttyUSB0 but could be ttyUSB1 etc. good idea to runs ls usb and find out that way
        time.sleep(1)

    def read(self):
        self.ser.write('ATZ\r')
        time.sleep(1)
        self.ser.write('AT+CMGF=1\r')# put in textmode
        time.sleep(1)
        self.ser.write('''AT+CMGL="ALL"''' + '''\r''') #fetch all sms's
        read = self.ser.readlines()
        for msg in read:
         if "+CMGL" in msg: #+CMGL looks for all SMS messages
          print msg 

    def disconnectPhone(self):
        self.ser.close()

sms = TextMessage()
sms.connectPhone()
sms.read()
sms.disconnectPhone()
raw_input("Press anykey to exit")

here is output

AT+CMGL="ALL"
+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"

+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"

+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"

My desired output would be

+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"

Test back

+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"

Im a message

+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"

TEST TEST TEST

Can anyone assist?

Output with if removed

ATZ

OK

AT+CMGF=1

Ok


AT+GCML="ALL"

+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38" 
Test back
+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38" 
Im a message 
+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"
TEST TEST TEST


OK
shaggs
  • 600
  • 7
  • 27
  • 1
    Are you sure that the message body has `+CMGL` in its line,because you restricted the data to print to those lines that contain this word (remove the line `if "+CMGL" in msg` and show us the new output please). – EbraHim May 02 '16 at 15:02

1 Answers1

0

Hope this helps

#!/usr/bin/env python2.7

import serial
import time
class TextMessage:
    def connectPhone(self):
        self.ser = serial.Serial('/dev/ttyUSB2', 9600, timeout=5) #for mine this was ttyUSB0 but could be ttyUSB1 etc. good idea to runs ls usb and find out that way
        time.sleep(1)

    def read(self):
        self.ser.write('ATZ\r')
        time.sleep(1)
        self.ser.write('AT+CMGF=1\r')# put in textmode
        time.sleep(1)
        self.ser.write('''AT+CMGL="ALL"''' + '''\r''') #fetch all sms's
        read = self.ser.readlines()
        i=0
        for msg in read:
         if "+CMGL" in msg: #+CMGL looks for all SMS messages
          bvb = 'AT+CMGR={}'.format(str(i)) + '\r'
          self.ser.write(bvb) #fetch all sms's
          ead = self.ser.readlines()
          for x in ead:
            print (x)
            print (i)
          i=i+1

    def disconnectPhone(self):
        self.ser.close()

sms = TextMessage()
sms.connectPhone()
sms.read()
sms.disconnectPhone()
raw_input("Press anykey to exit")