How to send packets at periodic intervals

Hi,

I am using Dev studio open at application. I am beginner to this tool. In this i tried the sample code given for TCP client. By using this code i can able to communicate with my server in the opened socket. Also i tried to send packets at periodic intervals by using timer function. For this i developed code below

adl_tmrSubscribe ( TRUE,150,ADL_TMR_TYPE_100MS,packetsend);

the packetsend function as described follows

wip_channel_t socket;
socket = wip_TCPClientCreate ( TCP_PEER_STRADDR, TCP_PEER_PORT, cbevh, NULL );
if ( !socket )
{
TRACE ( ( ERROR_TRACE_LEVEL, “[SAMPLE] Can’t connect” ) );
return;
}

but that function is only send up to 15 packets with creating 15 different port no of client with same ip after that no more packet reached to server.
In trace window we got “[SAMPLE] Can’t connect”

can anyone tell me why the socket can’t create.
Another question:
Which i have used to send packets at periodic interval to server is correct way ?
If is there some other way means kindly help me.

In your code you are creating a new TCP client every 15 seconds. The internet plugin can only support 15 concurrent TCP clients, which is why you are failing to connect. You have two options to resolve this:

  1. Open the TCP client, send the data, close the TCP client, repeat every 15 seconds, or
  2. Open the TCP client, send data every 15 seconds, close the client.

I’d recommend option 2. So, when you start your application and have a GSM network connection and have started your GPRS bearer, create the TCP client and store the handle. Then, in your 15 seconds timer, call wip_write with the TCP client handle and the data you want to send. See the picture below from the WIP Development Guide.

[attachment=0]TCP Client Create.png[/attachment]

Hi,
i developed code as follows to send packets at periodic intervals

adl_tmrSubscribe ( TRUE,150,ADL_TMR_TYPE_100MS,packetsend);

the packetsend function as defined

static void packetsend ( wip_event_t *ev, void *ctx )
{  
          int nwrite;
            TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Can send more data" ) );
            nwrite = wip_write ( ev->channel, snd_buffer + snd_offset, sizeof ( snd_buffer ) - snd_offset );
            if ( nwrite < 0 )
            {
                TRACE ( ( ERROR_TRACE_LEVEL, "[SAMPLE] write error %i", nwrite ) );
                return;
            }
            snd_offset += nwrite;
            if ( snd_offset == sizeof ( snd_buffer ) )
            {
                TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Everything has been sent, won't send more." ) );
            }
            else
            {
                TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Wrote %i bytes. %i bytes left to send in snd_buffer",
                                nwrite, sizeof ( snd_buffer ) - snd_offset ) );
            }
}

but for this function it not continuously sending data to server
and also returns write error -999

can you explain me how to use wip_write function to send packets at periodic interval?

Thanks

I think you are not understanding the way OpenAt works at all, and should probably start with a simpler example.

Firstly, you have tried to use a wip callback function as your timer callback function. These should be completely separate. Do it like this:
[attachment=0]TCP Client.png[/attachment]

When you create the TCP Client, you provide it with an event handler.

static void tcpEventHandler (wip_event_t *ev, void *ctx)

When you receive the WIP_CEV_OPEN event in your TCP client handler, store the channel

wip_channel_t channel = ev->channel

then start your timer. When your timer callback is called you can write to the channel using wip_write and the channel you stored

Did you not get a compiler error - or, at least, a warning - for that :question:

hi,
No i didn’t get any compiler error or warnings for that.

Thanks

Hi,
I wrote code as below

void AppliEntryPoint ( void )
{
    wip_channel_t socket;
    TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE]: connecting to client %s:%i", TCP_PEER_STRADDR, TCP_PEER_PORT ) );
    socket = wip_TCPClientCreate ( TCP_PEER_STRADDR, TCP_PEER_PORT, cbevh, NULL );
    if ( !socket )
    {
        TRACE ( ( ERROR_TRACE_LEVEL, "[SAMPLE] Can't connect" ) );
        return;
    }
}

the cbevh function is defined as

static void cbevh ( wip_event_t *ev, void *ctx )
{
    if ( ev->kind == WIP_CEV_OPEN  )
    {
       wip_channel_t channel =ev->channel;
       adl_tmrSubscribe ( TRUE,150,ADL_TMR_TYPE_100MS,packetsend );
       
            TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Connection established successfully" ) );
            
        }
}

the packetsend is defined as

static void packetsend ( wip_event_t *ev, void *ctx )
{
           int nwrite;
            TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Can send more data" ) );
            nwrite = wip_write ( ev->channel, snd_buffer + snd_offset, sizeof ( snd_buffer ) - snd_offset );

            if ( nwrite < 0 )
            {
                TRACE ( ( ERROR_TRACE_LEVEL, "[SAMPLE] write error %i", nwrite ) );
                return;
            }
            snd_offset += nwrite;
            if ( snd_offset == sizeof ( snd_buffer ) )
            {
                TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Everything has been sent, won't send more." ) );
            }
            else
            {
                TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Wrote %i bytes. %i bytes left to send in snd_buffer",
                                nwrite, sizeof ( snd_buffer ) - snd_offset ) );
            }
}

for this time also received error ad “[SAMPLE] write Error -999”
can anyone explain me why this error occurs?
From the error value i guess socket is not opened is it wright?
i have doubt in passing arguments to function packetsend?

is there anyother way available to send packets at periodic intervals?
if avail means send me that.

thanks

You are not following the steps provided!

  1. WAIT FOR GSM REGISTRATION!
  2. CREATE A GPRS BEARER CONNECTION!
  3. DON’T USE A WIP CALLBACK FOR A TIMER CALLBACK!

Unless you fix these errors, you will not get anywhere.

I think you should go back to the hello world sample application and make sure you understand that fully before moving on to something as complex as a TCP client application.

Hi,

I take the TCP CLIENT sample code only given in the samples.
In this code only after GSM registration the function

called from there onwards only i done the changes

Thanks

Ok, then you will need to fix point 3, try the following:

wip_channel_t tcp_channel;

static void timerCallback (u8 Id, void * Context)
{
    int nwrite;
    
    nwrite = wip_write ( tcp_channel, snd_buffer + snd_offset, sizeof ( snd_buffer ) - snd_offset );
    
    if ( nwrite < 0 )
    {
        TRACE ( ( ERROR_TRACE_LEVEL, "[SAMPLE] write error %i", nwrite ) );
        return;
    }
}
/***************************************************************************/
/*  Function   : cbevh                                                     */
/*-------------------------------------------------------------------------*/
/*  Object     : Handling events happenning on the TCP client socket.      */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  ev                | X |   |   |  WIP event                             */
/*--------------------+---+---+---+----------------------------------------*/
/*  ctx               | X |   |   |  user data (unused)                    */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void cbevh ( wip_event_t *ev, void *ctx )
{
    switch ( ev->kind )
    {
        case WIP_CEV_OPEN:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Connection established successfully" ) );
            tcp_channel = ev->channel;
            
            adl_tmrSubscribe(TRUE, 150, ADL_TMR_TYPE_100MS, timerCallback);
            break;
        }
    }
}

Hi,
i have tried the code which you given in the last reply,
but for that i got the following errors
.\src\entry_point.c:255: error: invalid storage class for function ‘timerCallback’
…\src\entry_point.c:265: error: expected declaration or statement at end of input

how can resolve those errors?

thanks

Sorry, that code compiles fine for me. Check at the indicated lines if you have made any errors.

15:01:07 Build Finished (took 1s.764ms)

Thanks tomridl

Its working fine

Thanks :smiley:

Hi,
If the connection loss happens means i.e socket closed, server down how to reconnect with server.

If suppose an of the following received means how do reinitialize the connection with server

WIP_CEV_ERROR
WIP_CEV_PEER_CLOSE
and also is there anyother response to be handle for connection loss or socket close?
Could you guide me

Thanks

If the server closes the connection and you get WIP_CEV_PEER_CLOSE, or if some other error occurs, shutdown the client connection using

wip_closeAndClear(&ev->channel);

and reopen it using

wip_TCPClientCreate

If there is some other GPRS or GSM related error, you may also have to close down your GPRS connection and restart that too. Alternatively, the other solution is to just reset your entire modem and start from scratch.

the above code i used to send packets at periodic intervals.
In this code WIP_CEV_OPEN only processed with timer then how do i handle following error responses
WIP_CEV_ERROR
WIP_CEV_PEER_CLOSE

whether can check those state in the switch case inside the cbevh function?

could you kindly guide me?
Thanks

Yes, look at the sample application. The code provided was an example showing the relevant section, not a complete solution. You should handle all possible cases of the switch variable.

so from your reply can i write the code as follows

/***************************************************************************/
/*  Function   : cbevh                                                     */
/*-------------------------------------------------------------------------*/
/*  Object     : Handling events happenning on the TCP client socket.      */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  ev                | X |   |   |  WIP event                             */
/*--------------------+---+---+---+----------------------------------------*/
/*  ctx               | X |   |   |  user data (unused)                    */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void cbevh ( wip_event_t *ev, void *ctx )
{
    switch ( ev->kind )
    {
        case WIP_CEV_OPEN:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Connection established successfully" ) );
            tcp_channel = ev->channel;
           
            adl_tmrSubscribe(TRUE, 150, ADL_TMR_TYPE_100MS, timerCallback);
            break;
        }
case WIP_CEV_ERROR:
        {
            TRACE ( ( ERROR_TRACE_LEVEL, "[SAMPLE] Error %i on socket. Closing.",  ev->content.error.errnum ) );
            wip_close ( ev->channel );
            break;
        }

        case WIP_CEV_PEER_CLOSE:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE] Connection closed by peer" ) );
            wip_close ( ev->channel );
            break;
        }

        default:
        {
            break;
        }
    }
}

then i called wip_TCPClientCreate like follows

void AppliEntryPoint ( void )
{
    wip_channel_t socket;
    TRACE ( ( NORMAL_TRACE_LEVEL, "[SAMPLE]: connecting to client %s:%i", TCP_PEER_STRADDR, TCP_PEER_PORT ) );
    socket = wip_TCPClientCreate ( TCP_PEER_STRADDR, TCP_PEER_PORT, cbevh, NULL );
    if ( !socket )
    {
        TRACE ( ( ERROR_TRACE_LEVEL, "[SAMPLE] Can't connect" ) );
        return;
    }

}

or else
call the function

or need to call from the

if need to use the above functions how do i call those functions?

thanks