1

This is a code snippet written in python to receive sms via a usb modem. When I run the program all I get is a status message "OK"., but nothing else.How do I fix the issue to print the messages I am receiving?

import serial

class HuaweiModem(object):

    def __init__(self):
        self.open()

    def open(self):
        self.ser = serial.Serial('/dev/ttyUSB_utps_modem', 115200, timeout=1)
        self.SendCommand('ATZ\r')
        self.SendCommand('AT+CMGF=1\r')

    def SendCommand(self,command, getline=True):
        self.ser.write(command)
        data = ''
        if getline:
            data=self.ReadLine()
        return data 



    def ReadLine(self):
        data = self.ser.readline()
        print data
        return data 

    def GetAllSMS(self):
        self.ser.flushInput()
        self.ser.flushOutput()



        command = 'AT+CMGL="all"\r'
        print self.SendCommand(command,getline=False)
        self.ser.timeout = 2
        data = self.ser.readline()
        print data

        while data !='':
            data = self.ser.readline()
        if data.find('+cmgl')>0:
            print data


h = HuaweiModem()
h.GetAllSMS()   
BenMorel
  • 34,448
  • 50
  • 182
  • 322
DesirePRG
  • 6,122
  • 15
  • 69
  • 114

2 Answers2

0

In GetAllSMS there are two things I notice:

1) You are using self.ser.readline and not self.Readline so GetAllSMS will not try to print anything (except the first response line) before the OK final response is received, and at that point data.find('+cmgl')>0 will never match.

Is that just the problem?

2) Will print self.SendCommand(command,getline=False) call the function just as it were written as self.SendCommand(command,getline=False)? (Just checking since I do not write python myself)


In any case, you should rework your AT parsing a bit.

def SendCommand(self,command, getline=True):

The getline parameter here is not a very good abstraction. Leave out reading responses from the SendCommand function. You should rather implement proper parsing of the responses given back by the modem and handle that outside. In the general case something like

self.SendCommand('AT+CSOMECMD\r')
data = self.ser.readline()
while ! IsFinalResult(data):
    data = self.ser.readline()
    print data        # or do whatever you want with each line

For commands without any explicit processing of the responses, you can implement a SendCommandAndWaitForFinalResponse function that does the above. See this answer for more information about a IsFinalResult function.

Community
  • 1
  • 1
hlovdal
  • 26,565
  • 10
  • 94
  • 165
0

where you are having problems is here in your GetAllSMS function. Now replace my GeTALLSMS function with yours and see what happens

     def GetAllSMS(self):
        self.ser.flushInput()
        self.ser.flushOutput()



        command = 'AT+CMGL="all"\r' #to get all messages both read and unread
        print self.SendCommand(command,getline=False)
        while 1:
            self.ser.timeout = 2
            data = self.ser.readline()
            print data

or this

    def GetAllSMS(self):
        self.ser.flushInput()
        self.ser.flushOutput()



        command = 'AT+CMGL="all"\r' #to get all messages both read and unread
        print self.SendCommand(command,getline=False)
        self.ser.timeout = 2
        data = self.ser.readall() #you can also u read(10000000)
        print data

thats all...

Transformer
  • 3,642
  • 1
  • 22
  • 33