Example #1
0
def main():

    try: 

        # Set Global Watchdog timeout in Seconds
        MOD.watchdogEnable(300)

        res = JANUS_SER.init("115200",'8N1')
        if (res == -1):
            return
  
        #Set Network specific settings
        res = NETWORK.initGsmNetwork(myApp.NETWORK,myApp.BAND)
        if (res == -1):
            return

        #Init GPRS
        GPRS.init('1',myApp.APN)

        #Inform Application that a Data Connection is not available
        JANUS_SER.set_DCD(0)

        # Loop forever, without this loop the script would run once and exit script mode.  On reboot or power-on the script would run once more
        while (1):

            MOD.watchdogReset()

            #Wait until module is registered to GSM Network
            res = NETWORK.isGsmRegistered(180)  #Wait 180 seconds for module to obtain GSM registration
            if (res == -1):
                return
            
            #Wait until module is attached to GPRS    
            res = NETWORK.isGprsAttached(180)  #Wait 180 seconds for module to obtain GPRS Attachment
            if (res == -1):
                return

            #############################################################################################################################
            ## Opening Socket Connection to Server
            #############################################################################################################################

            res = GPRS.openSocket(myApp.IP,myApp.PORT,1,myApp.USERNAME,myApp.PASSWORD,myApp.PROTOCOL,0)

            #Inform Application that a Data Connection is not available
            DCD = MDM.getDCD()
            if (DCD == 1):
                JANUS_SER.set_DCD(1)
                #Forward CONNECT Message to Serial Port
                JANUS_SER.sendUART('\r\nCONNECT\r\n') 

            ##Loop while Socket connected
            while(1):

                MOD.watchdogReset()

                #Forward serial port data to socket connection
                DCD = MDM.getDCD()
                if (DCD == 1):
                    #Get data from serial port
                    res = JANUS_SER.readUART()
                    if (len(res)!=0):
                        #Forward data to open socket
                        res = MDM.send(res,1)

                #Forward  socket data to serial port
                DCD = MDM.getDCD()
                if (DCD == 1):
                    #Get data from open socket connection
                    res = MDM.receive(1)
                    if (len(res)!=0):
                        #Forward socket data to serial port
                        JANUS_SER.sendUART(res) 

                #When socket is closed exit loop via this path
                #Will guarantee that '\r\nNO CARRIER\r\n' is sent every time
                DCD = MDM.getDCD()
                if (DCD == 0):
                    #Inform Application that a Data Connection is not available
                    JANUS_SER.set_DCD(0)
                    ATC.delaySec(1)
                    #Get any remaining data from closed socket connection
                    res = MDM.receive(1)
                    if (len(res)!=0):
                        #Forward socket data to serial port
                        JANUS_SER.sendUART(res)
                    break
    except:
        print "Script encountered an exception"
        print "Exception Type: " + str(sys.exc_type)
        print "MODULE -> TerminusS2E"
        
    return