Socket Communication MQX 4.1

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Socket Communication MQX 4.1

1,013 Views
juanmanuelpanie
Contributor II

Hello,

I'm working on a TWRK70 kit, with MQX 4.1. I trying to communicate via socket using mqx. But when I build the project, appears an error at project level, on one of the header used, not at my code.

I will paste here the code and some images.

Code:

main.h:

#ifndef __main_h_

#define __main_h_

#include <mqx.h>

#include <bsp.h>

#include <rtcs/rtcs_sock.h>

#include <rtcs/socket.h>

#include <rtcs/rtcs.h>

#include <rtcs/rtcscfg.h>

#include <enet.h>

#define MAIN_TASK 1

#define SERVER_TASK 2

#define CLIENT_TASK 3

extern void main_task(uint32_t);

extern void server_task(uint32_t);

extern void client_task(uint32_t);

/* PPP device must be set manually and

** must be different from the default IO channel (BSP_DEFAULT_IO_CHANNEL)

*/

#define PPP_DEVICE      "ittyb:"

/*

** Define PPP_DEVICE_DUN only when using PPP to communicate

** to Win9x Dial-Up Networking over a null-modem

** This is ignored if PPP_DEVICE is not #define'd

*/

#define PPP_DEVICE_DUN  1

/*

** Define the local and remote IP addresses for the PPP link

** These are ignored if PPP_DEVICE is not #define'd

*/

#define PPP_LOCADDR     IPADDR(192,168,0,216)

#define PPP_PEERADDR    IPADDR(192,168,0,217)

/*

** Define a default gateway

*/

#define GATE_ADDR       IPADDR(192,168,0,1)

#endif /* __main_h_ */

main.c:

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

*

*   This file contains MQX only stationery code.

*

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

#include "main.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

TASK_TEMPLATE_STRUCT MQX_template_list[] =

{

/*  Task number, Entry point, Stack, Pri, String, Auto? */

   {MAIN_TASK,   main_task,   1500,  9,   "main", MQX_AUTO_START_TASK,0,0},

   {SERVER_TASK,   server_task,   1500,  9,   "server", MQX_TIME_SLICE_TASK ,0,3},

   {CLIENT_TASK,   client_task,   1500,  9,   "client", MQX_TIME_SLICE_TASK ,0,3},

   {0,           0,           0,     0,   0,      0,                 }

};

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

*

* Task Name    : Main_task

* Comments     :

*    This task prints " Hello World "

*

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

void main_task(uint32_t initial_data)

{

  printf("Arrancamos...  \n");

  _task_create(0,SERVER_TASK,0);

  RTCS_create();

  _task_block();

}

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

*

* Task Name    : server_task

* Comments     :

*   

*

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

void server_task(uint32_t initial_data)

{

  sockaddr_in laddr,raddr;

  uint32_t sock, newsock;

  int32_t length;

  uint32_t index;

  uint32_t error;

  uint16_t rlen;

  laddr.sin_family = AF_INET;

  laddr.sin_port = 2000;

  laddr.sin_addr.s_addr = INADDR_ANY;

  /* Create a stream socket: */

  sock = socket(PF_INET, SOCK_STREAM, 0);

  if (sock == RTCS_SOCKET_ERROR) {

  printf("\nFailed to create the stream socket.");

  _task_block();

  }

  /* Bind the stream socket to a TCP port: */

  error = bind(sock, &laddr, sizeof(laddr));

  if (error != RTCS_OK) {

  printf("\nFailed to bind the stream socket - 0x%lx", error);

  _task_block();

  }

  /* Set up the stream socket to listen on the TCP port: */

  error = listen(sock, 0);

  if (error != RTCS_OK) {

  printf("\nlisten() failed - 0x%lx", error);

  _task_block();

  }

  printf("\n\nQuote Server activo en puerto 2000.\n");

  _task_create(0,CLIENT_TASK,0);

  rlen = sizeof(raddr);

  newsock = accept(sock,&raddr,&rlen);

  if (newsock == RTCS_SOCKET_ERROR) {

  printf("\naccept() fallo, error 0x%lx",

  RTCS_geterror(newsock));

  continue;

  }

  char *buf = malloc(50*sizeof(char));

  recv(newsock,buf,sizeof(buf),0);

  _time_delay(1000);

  printf("dato recibido: %s\n",buf);

  shutdown(newsock,FLAG_CLOSE_TX);

  _task_block();

}

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

*

* Task Name    : client_task

* Comments     :

*   

*

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

void client_task(uint32_t initial_data)

{

  sockaddr_in raddr;

  uint32_t sock;

  int32_t length;

  uint32_t index;

  uint32_t error;

  uint16_t rlen;

  sock = socket(PF_INET,SOCK_STREAM,0);

  if (sock == RTCS_SOCKET_ERROR) {

  printf("\nFallo al crear stream socket en cliente.");

  _task_block();

  }

  raddr.sin_family = AF_INET;

  raddr.sin_port = 2000;

  raddr.sin_addr.s_addr = INADDR_ANY;

  rlen = sizeof(raddr);

  error = connect(sock,&raddr,&rlen);

  if(error != RTCS_OK){

  printf("\nError al conectar.");

  _task_block();

  }

  char *buf = malloc(50*sizeof(char));

  buf = "Hola socket andando\n";

  send(sock,buf,sizeof(buf),0);

  _time_delay(1000);

  printf("\nMensaje enviado \n");

  _task_block();

  /* Bind the stream socket to a TCP port: */

  error = bind(sock, &laddr, sizeof(laddr));

  if (error != RTCS_OK) {

  printf("\nFailed to bind the stream socket - 0x%lx", error);

  _task_block();

  }

  /* Set up the stream socket to listen on the TCP port: */

  error = listen(sock, 0);

  if (error != RTCS_OK) {

  printf("\nlisten() failed - 0x%lx", error);

  _task_block();

  }

  printf("\n\nQuote Server activo en puerto 2000.\n");

  _task_create(0,CLIENT_TASK,0);

  rlen = sizeof(raddr);

  newsock = accept(sock,&raddr,&rlen);

  if (newsock == RTCS_SOCKET_ERROR) {

  printf("\naccept() fallo, error 0x%lx",

  RTCS_geterror(newsock));

  continue;

  }

  char *buf = malloc(50*sizeof(char));

  recv(newsock,buf,sizeof(buf),0);

  _time_delay(1000);

  printf("dato recibido: %s\n",buf);

  shutdown(newsock,FLAG_CLOSE_TX);

  _task_block();

}

/* EOF */

After I build this results appears:

imagen1.jpg

imagen2.jpg

imagen3.jpg

Colud someone help me? Why it is not working?

Thanks. Regards,

Juan Manuel

Tags (4)
0 Kudos
3 Replies

590 Views
juanandujar
Contributor I

Hello,

I have taken a look to your code, you should review the way to link the libraries. In my code I have it in this way:

#include <mqx.h>

#include <bsp.h>

#include <lwevent.h>

#include <message.h>

#include <rtcs.h>

Furthermore, I think you should review the input argument of the bind method. It should create at least 1 "child socket". Therefore, it will be

bind(sock_name,1);

Best regards.

0 Kudos

590 Views
juanmanuelpanie
Contributor II

Hi Juan,

About libraries, I have CodeWarrior 10.1 and if I not put like I have (#include <rtcs/rtcs.h>), it no recognized it. Even more, I probe with out rtcs/ (#include <rtcs.h>) and an error appears sayin that the library does not exist.

Regards

0 Kudos

590 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Juan,

CW10.1 does not support MQX4.1 by default, you may need to make adjustments in the project settings such as source files paths, libraries paths and Windows environment variables. I strongly recommend to use CW10.6.

Do not forget to build MQX libraries (BSP and PSP) and also RTCS library. Make sure your project is including RTCS library in the linker settings of the project. You can take a look to Lab 1 in the post below.

MQX Basics Training - 6 Tutorials (Labs) for MQX Begginers

Best regards,

Carlos

0 Kudos