예제 #1
0
파일: SMS_HE910.py 프로젝트: JanusRC/Python
def sendSMS(theSmsMsg,theDestination,theTerminator,retry,timeOut):
#This function sends an SMS Message

  # Input Parameter Definitions
  #   theSmsMsg: The text SMS Message
  #   theTerminator: string or character at the end of AT Command
  #   retry:  How many times the command will attempt to retry if not successfully send 
  #   timeOut: number of [1/10 seconds] command could take to respond

    try:

        rtnList = [-1,-1]    #[return status,return data]
        # return status:
        #   -2:    Timeout
        #   -1:    Exception occurred
        #    0:    No errors occurred, no return data
        #    1:    No errors occurred, return data        

      #Note that the 145 being sent in with the destination address is the "type" of destination address, with 145 including a "+" for international
        while (retry != -1):

            rtnList[1] = MDM.send('AT+CMGS="' + str(theDestination) + '",145', 0)
            rtnList[1] = MDM.sendbyte(0x0d, 0)
            rtnList = ATC.mdmResponse('\r\n>', timeOut)

            rtnList[1] = MDM.send(theSmsMsg, 0)
            rtnList[1] = MDM.sendbyte(0x1a, 0)

            #Wait for AT command response
            rtnList = ATC.mdmResponse(theTerminator, timeOut)
              
            #Did AT command respond without error?
            pos1 = rtnList[1].rfind(theTerminator,0,len(rtnList[1]))
            if (pos1 != -1):
              retry = -1
              rtnList = ATC.parseResponse(rtnList[1])
            else:
              retry = retry - 1
     
        rtnList[0] = 0  #no error, no data  

  #If the function fails to find the proper response to the SMS send ('OK<cr><lf>') then we receive a timeout string: 'timeOut'  
    except:
        print sys.exc_info()
        rtnList[0] = -1


    return rtnList
예제 #2
0
파일: SMS_HE910.py 프로젝트: JanusRC/Python
def CheckNewSMS():
# This function checks for a new SMS. If one is found it parses the information.
  
    try:

        rtnList = [-1,-1]    #[return status,return data]
        # return status:
        #   -2:    Timeout
        #   -1:    Exception occurred
        #    0:    No errors occurred, no SMS
        #    1:    No errors occurred, return received SMS

        #Now try to list all newly received SMS.
        #Using this method because the sendatcommand method ends up parsing out info we need due to the multiple \r\n response.
        rtnList[1] = MDM.send('AT+CMGL="REC UNREAD"', 0)
        rtnList[1] = MDM.sendbyte(0x0d, 0)
        #Grab the response of the command
        rtnList = ATC.mdmResponse('OK', 10)

        res  = rtnList[1]        
        
        #Try to list all currently stored SMS's. This is just a check to see if we have old/already read messages.
        rtnList[1] = MDM.send('AT+CMGL="ALL"', 0)
        rtnList[1] = MDM.sendbyte(0x0d, 0)
        #Grab the response of the command
        rtnList = ATC.mdmResponse('OK', 10)

        res2 = rtnList[1]        
        
        #Check for messages, search from 0 to the length of the AT Command response
        pos0 = res.find('+CMGL: 1',0,len(res))        
        GeneralCheck = res2.find('+CMGL:',0,len(res2))
        
        if (pos0 != -1):
            #New message found, let's parse the useful information
            #first, let's split the received information so we can echo a response to the originating phone number
            #Below is the normal response we are parsing
            #+CMGL: <index>,<stat>,<oa/da>,<alpha>,<scts><CR><LF><data><CR><LF>
            #+CMGL: 1,"REC UNREAD","+xxxxxxxxxxx","test","12/06/06,15:59:50-20"
            #Data

            #Separate by \r\n to separate the SMS data
            parts1 = res.split('\r\n')

             
            #Now split by comma to get individual data from the main chunk of information
            parts2 = parts1[1].split(",")

            SMSInfo.index = parts2[0]
            SMSInfo.stat = parts2[1]
            SMSInfo.OriginatingPN = parts2[2]
            SMSInfo.alpha = parts2[3]
            SMSInfo.date = parts2[4]
            SMSInfo.time = parts2[5]
            SMSInfo.SMS = parts1[2]

            #Delete ALL SMS to ensure a clear buffer for the next read
            rtnList = ATC.sendAtCmd('AT+CMGD=1,4' ,ATC.properties.CMD_TERMINATOR,5)            

            rtnList[1] = SMSInfo.SMS
            rtnList[0] = 1 #indicate data received

            return rtnList            
        

        #Enter this AT command to ensure the buffer is empty and ready to receive new SMS messages
        if (GeneralCheck != -1):
            rtnList = ATC.sendAtCmd('AT+CMGD=1,4' ,ATC.properties.CMD_TERMINATOR,5)


        rtnList[0] = 0 #No errors, but no data received
        
    except:
        print sys.exc_info()
        rtnList[0] = -1

    return rtnList