AnsweredAssumed Answered

ethernet socket issue with multiple instances

Question asked by khoanguyen on Nov 11, 2012
Latest reply on Nov 14, 2012 by Martin Latal

Hi,

 

I'm currently using MQX 3.8.1 with Code Warrior 10.2 and the Kinetis k60 tower. I'm currently having trouble sending strings over multiple instances and was wondering if this was a timing issue.

 

My setup:

I have two tasks: connect_task and print_task.

the connect_task initializes RTCS and creates and connects a socket to an external server. My tower is acting as a client.

This task is successful because I sent a string confirming connection and verified that the server was able to receive it, so I know that the connection was successful and I was able to send a string over TCP/IP to a server.

Once finished it creates 5 instances of the print task and passes in 5 different strings. This is almost exactly like the mutex example provided.

 

the print_task is where I'm having trouble. Since I have already opened the socket, I set the same parameters and attempted to send that data the same way I did in the connect_task. I was wondering if this was a timing issue and the send was blocking.

 

Below is my code.

 

Thanks,

 

Khoa Nguyen

 

/****************************************************************************

* RYSK TECH

* RMD

* Created by: Khoa Nguyen

* Date: October 1, 2012

* Description: Demo for the CDR

****************************************************************************/

#include "main.h"

#include "mutex.h"

 

 

#if !BSPCFG_ENABLE_IO_SUBSYSTEM

#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.

#endif

 

 

#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED

#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.

#endif

 

#if ! MQX_HAS_TIME_SLICE

#error This application requires MQX_HAS_TIME_SLICE defined non-zero in user_config.h. Please recompile kernel with this option.

#endif

 

#if ! BSPCFG_ENABLE_GPIODEV

#error This application requires BSPCFG_ENABLE_GPIODEV defined non-zero in user_config.h. Please recompile BSP with this option.

#endif

 

TASK_TEMPLATE_STRUCT MQX_template_list[] =

{

        /* Task Index,           Function,       Stack,  Priority,  Name,       Attributes,       Param, Time Slice */

            { IDLE_TASK,        idle_task,      2000,   7,     "idle",        MQX_AUTO_START_TASK,     0,     0 },

            { CONNECT_TASK,        connect_task,      1000,   6,     "connect",           0,                 0,     0 },

            { PRINT_TASK,       print_task,     1000,   9,     "print",        MQX_TIME_SLICE_TASK,     0,     3 },

            { 0 }

};

 

 

/*TASK*-----------------------------------------------------

*

* Task Name    : Main_task

* Comments     :

*

*END*-----------------------------------------------------*/

 

void idle_task(uint_32 initial_data)

{

    /*Initialize Mutex struct and hard code 4 strings*/

    MUTEX_ATTR_STRUCT mutexattr;

    char*             string1 = "1 Sensor ONE task\n";

    char*             string2 = "2 Sensor TWO task\n";

    char*             string3 = "3 Sensor THREE task\n";

    char*             string4 = "4 Sensor FOUR task\n";

   

   

    /* Initialize mutex attributes */

    if (_mutatr_init(&mutexattr) != MQX_OK) {

          printf("Initialize mutex attributes failed.\n");

          _task_block();

       } 

       /* Initialize the mutex */

    if (_mutex_init(&print_mutex, &mutexattr) != MQX_OK) {

          printf("Initialize print mutex failed.\n");

          _task_block();

       }

   

    printf("\n Welcome to the RYSK Tech RMD device \n");

  

    _task_create(0, CONNECT_TASK, 0);

       _time_delay(5000);

       /* Create the print tasks */

   _task_create(0, PRINT_TASK, (uint_32)string1);

   _task_create(0, PRINT_TASK, (uint_32)string2);

   _task_create(0, PRINT_TASK, (uint_32)string3);

   _task_create(0, PRINT_TASK, (uint_32)string4);

   _task_block();

  

}

 

void connect_task(uint_32 initial_data)

{

    _ip_address     ipaddr;

    uint_32         sock;

    uint_32            result;

    sockaddr_in        addr;

    char*            test = "This is a test for stream";

   

    while(1)

    {

        if (_mutex_lock(&print_mutex) != MQX_OK)

            {

                printf("Mutex lock failed.\n");

                _task_block();

            }   

    rtcs_init();

   

    addr.sin_family     = AF_INET;

    addr.sin_port        = 10000;

    addr.sin_addr.s_addr = IPADDR(192,168,3,102);

   

    sock = socket(AF_INET, SOCK_STREAM, 0);

   

    result = connect(sock, &addr, sizeof(addr));

    //_time_delay(1000);

    if(result != RTCS_OK)

        {

            printf("ERROR the socket could not CONNECT");

        }

    if(result == RTCS_OK)

    {

        printf("\nREADY TO SEND DATA!!!!!!!!!!!!!!!!!");

        printf("\nConnected to %lx, port %d",addr.sin_addr.s_addr, addr.sin_port);

    }

   

    send(sock, (void *)test, strlen(test), 0);

 

   

    _mutex_unlock(&print_mutex);

    _task_block();

    }

}

 

void print_task(uint_32 initial_data)

{

    while(1)

    {

        _ip_address     ipaddr;

        uint_32         sock;

        uint_32            result;

        sockaddr_in        addr;

        char* cBuffer;

        char tag[50];

        char             Buffer[50] ={0};

       

       

        sprintf(tag, (char *)initial_data);

       

        sprintf(Buffer, (char *)initial_data);

        cBuffer = Buffer;

       

        if (_mutex_lock(&print_mutex) != MQX_OK) {

                 printf("Mutex lock failed.\n");

                 _task_block();

              }

       

 

        addr.sin_family     = AF_INET;

        addr.sin_port        = 10000;

        addr.sin_addr.s_addr = IPADDR(192,168,3,102);

           

 

       

        send(sock, (void *)Buffer, strlen(Buffer), 0);

        _io_puts((char *)initial_data);

 

         _mutex_unlock(&print_mutex);

    }

}

 

/* EOF */

 


Outcomes