Closing the Socket

7 thoughts on “Closing the Socket

  1. I am encountering some behavior that is causing issues regarding closing the socket on the board after data has been sent out.  Here is a section of code and configuration settings that I am doing at the moment.

     

    Sometimes the board closes the socket other times it just sends all the following AT commands out through the socket.  Is there a way to for sure close the socket, a tcp socket configuration option? 

     

     def send_packet(packet):

        # empty the receive buffer

        clear_buffer()

        # status flag

        status = 0

        # counter

        count = 0

     

    print ‘Configuring TCP Sockets’

        count = 0

        MDM.send(‘AT#SCFG=1,1,300,0,600,50r’,2)

        status = status_check()

        # upon a failed configuration of the first socket, will

        # attempt to configure the socket 10 times

        while ((status == 0) and (count < 10)):

            MDM.send(‘AT#SCFG=1,1,300,0,600,50r’,2)

            status = status_check()

            count = count + 1

        # ensure that upon exit of the loop, check to see if the

        # status is still zero

        if (status == 0):

            print ‘Error setting first TCP socket’

            return 0 

     

    # GPRS connection information

        print ‘Setting the GPRS connection information’

        MDM.send(‘AT+CGDCONT=1,"IP","’+GPRS_APN+‘"r’,2)

        status = status_check()

        count = 0

        while ((status == 0) and (count < 10)):

            MDM.send(‘AT+CGDCONT=1,"IP","’+GPRS_APN+‘"r’,2)

            status = status_check()

            count = count + 1

        if (status == 0):

            print ‘Error with GPRS setup’

            return 0

        print ‘GPRS connection information set’ 

     

        # Set the board to have no hardware or software

        # control flow.

        print ‘Setting the Control flow for the board’

        MDM.send(‘AT&K0r’,2)

        status = status_check()

        count = 0

        

        print ‘Creating socket’

        MDM.send(‘AT#SD=1,0,’+TCP_PARSE_SERVER_PORT+‘,"’+TCP_PARSE_SERVER_ADDR+‘",0,0,0r’,2)

        status = status_check()

        # if status is 0, then could not be established, dont try again since it may consume

        # too much time

        if (status == 0):

            print ‘Could not connect on the transmit socket’

            return 0

        print ‘Socket created’

     

        #print ‘Sending packet’

        # Essentially wait for either an OK that the data was sent or ERROR can only do once

        # due to the terminator being at the end of the packet (could change this)

        #MDM.send(‘AT#SSEND=1r’,2)

        MDM.send(packet,2)

        #MDM.sendbyte(0x1A,2)

        #MDM.send(‘r+++’,2)

        #status = data_check()

        #count = 0

        #while ((status == 0) && (count < 10)):

        #    MDM.send(packet,2)

        #    status = check_status()

        #    count = count + 1

        # Cant do anything if it does or is not able to send the TCP packet

        #if (status == 0):

        #    print ‘Failed sending the packet’

        #else:

        print ‘Packet sent’

     

        #MOD.sleep(50)

        

        print ‘Closing the socket’

        MDM.send(‘AT#SH=1r’,2)

        #status = status_check()

        #count = 0;

        #while ((status == 0) and (count < 10)):

        #    MDM.send(‘AT#SH=1r’,2)

        #    status = status_check()

        #    count = count + 1

        #if (status == 0):

        #    print ‘Failed closing the socket’

        #    return 0

        print ‘Socket successfully created and closed.  Data may or may not have been sent’

        return 1

     

    def status_check():

        # instead of doing a dedicated amount of time waiting will wait

        # for a status update, either an error or an ok must be received

        status = MDM.receive(2)

        print status

        while ((status.find(‘ERROR’) < 0and (status.find(‘OK’) < 0) and (status.find(‘CONNECT’) < 0) and (status.find(‘NO CARRIER’) < 0)):

            status = MDM.receive(2)

        if (status.find(‘ERROR’) >= 0):

            return 0

        if ((status.find(‘OK’) >= 0) or (status.find(‘CONNECT’) >= 0) or (status.find(‘NO CARRIER’) < 0)):

            return 1 

    1. You need to exit first data mode with +++ sequence guarded before and after it with silence time of 2-3 seconds.

       

      MOD.sleep(30)

      MDM.send(‘+++’,2)

      MOD.sleep(30)

       

       

      1. Here is the end of uploaded file using FTP:

         

        <coordinates>023.29722,42.72051</coordinates>
        </Point>
        </Placemark>
        </Document>
        </kml>
        +++AT#FTPTYPE=0
        AT#FTPFSIZE=BT_28_1_T-04_Jun_12-18_57_09.kml

         

        Data mode is closed using:

         

        MOD.sleep(30)                        
        MDM2.send(‘+++’,2)
        MOD.sleep(30)

         

        And after that the module is waiting additional 10 sec for "NO CARRIER".

         

        Only after that the next 2 commands are isssued:

         

        AT#FTPTYPE=0
        AT#FTPFSIZE=BT_28_1_T-04_Jun_12-18_57_09.kml

         

        Why they are in file?

        This happens just only once.

         

        1. Strange indeed. Do you see NO CARRIER coming after those 10 seconds?

          Try a larger silence time, maybe 5 seconds or even more.

           

          "Just only once" means the first time a file is uploaded, then the closure works well?

           

          1. The problem is that the device was in field tests error monitoring was impossible. I have to make some debud data logging.

             

            I will try with increased silence time, but it is undesirable. Cosmin, do you remember my first bird tracking device BT-70? This is  smaller brother  – 28 grams BT-28. The battery capacity is only 160mAh and solar panel gives max 5mA charging current at bright shining Sun. So, I have to reduce activity time of GSM module as much as possible.

             

            Yesterday there was second case of bad closure with other device, but same behaviour.    

          2. Increasing guard time to 5 sec with MOD.sleep(50) before and after escape sequence, and sending res = MDM2.send(‘+++’,10) till res == 1 solve the problem.