How to diagnose RTCS connection problems?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How to diagnose RTCS connection problems?

跳至解决方案
1,112 次查看
michael_wahler
Contributor III

Hi again,

I have been able to compile and run a simple RTCS-based program (MQX 4.1, C++). As a simple example, I am trying to connect to a web server.

The problem is that the connection times out. I am unsure about the following things:

- After initialization, mac_address is 1 and ethernet_address is {0, 0, 94, 168, 1, 202}. Is this correct?

- Does the rest of the code look OK (in terms of RTCS initialization :smileywink:

Kind regards

Michael

#ifndef NETWORK_H_

#define NETWORK_H_

#include <mqx.h>

#include <bsp.h>

// network-specific headers below

extern "C" {

#include <rtcs.h>

#include <ipcfg.h>

#include <enet.h>

}

/*

** Define IP address and IP network mask

*/

#ifndef ENET_IPADDR

#define ENET_IPADDR    IPADDR(192, 168, 1, 202)

#endif

#ifndef ENET_IPMASK

#define ENET_IPMASK    IPADDR(255, 255, 255, 0)

#endif

#ifndef ENET_IPGATEWAY

#define ENET_IPGATEWAY IPADDR(192, 168, 1, 1)

#endif

#define TCPSERVER_RECV_BUF_SIZE     10

#define TCPSERVER_SEND_BUF_SIZE    50

#define TCPSERVER_TASK_INDX         2

#define TCPS_SUBTHREAD_INDX 3

struct _ip_digital

{

  uint8_t high;

  uint8_t middlehigh;

  uint8_t middlelow;

  uint8_t low;

};

void ip_to_digital(_ip_address ip, _ip_digital *p)

{

  p->high       = (ip >> 24) & 0xFF;

  p->middlehigh = (ip >> 16) & 0xFF;

  p->middlelow  = (ip >> 8)  & 0xFF;

  p->low        = (ip)       & 0xFF;

}

class Network

{

private:

  _enet_address ethernet_address;  // TODO ?? = BSP_DEFAULT_ENET_OUI;

  IPCFG_IP_ADDRESS_DATA ip_data;

  uint32_t mac_address;

public:

  Network ()

{

  _RTCS_socket_part_init = 8;

  uint32_t result = RTCS_create();

  if (result == RTCS_OK)

  {

  mac_address = ENET_get_mac_address (

  BSP_DEFAULT_ENET_DEVICE,

  ENET_IPADDR,

  ethernet_address);

  ip_data.ip      = ENET_IPADDR;

  ip_data.mask    = ENET_IPMASK;

  ip_data.gateway = ENET_IPGATEWAY;

  result = ipcfg_init_device (BSP_DEFAULT_ENET_DEVICE, ethernet_address);

  if (result == RTCS_OK)

  {

  result = ipcfg_bind_staticip (BSP_DEFAULT_ENET_DEVICE, &ip_data);

  if (result == RTCS_OK)

  {

// _task_create (0, TCPSERVER_TASK_INDX, 0);

  }

  else

  {

  _io_printf ("[Network] Error binding to IP address.\n");

  }

  }

  else

  {

  _io_printf ("[Network] Error initalizing device.\n");

  }

  }

  print_status_info ();

  _io_printf ("[Network] constructor finished.\n");

}

  void print_status_info ()

  {

  _io_printf("[Network] Ethernet address: '");

  for (size_t i = 0; i < 6; ++i)

  {

  _io_printf ("%d",ethernet_address [i]);

  if (i < 5)

  _io_printf ("-");

  else

  _io_printf("\n");

  }

  _ip_digital x;

  ip_to_digital(ENET_IPADDR, &x);

  _io_printf("[Network] IP address: %d.%d.%d.%d\n", x.high, x.middlehigh, x.middlelow, x.low);

  _io_printf("[Network] MAC address: %d\n", mac_address);

  }

  /**

  * just a test routine to send a packet to a server

  */

  void send_something ()

  {

  _ip_address ip = IPADDR(192,168,1,100);

  uint16_t port = 8000;

  _io_printf ("[Network::send_something] to 192.168.1.100:8000\n");

  uint32_t     error;

  char        buffer[50];

  sockaddr    server_socket_addr;

  uint32_t     socket_handle;

  _ip_digital digit;

  uint32_t     opt_val;

  uint32_t     opt_len;

  opt_val =  15 * 1000;

  opt_len = sizeof(uint32_t);

  // set remote host(TCPServer) socket address

  server_socket_addr.sin_family      = AF_INET;

  server_socket_addr.sin_port        = port;

  server_socket_addr.sin_addr.s_addr = ip;

  // loop to establish connection

  do {

  // Create a stream socket for TCPClient.

  socket_handle = socket(AF_INET, SOCK_STREAM, 0);

  if (socket_handle == RTCS_SOCKET_ERROR) {

  _io_printf("\n[Network::send_something]: Failed to create socket.\n");

  _task_block();

  }

  _io_printf ("[Network::send_something] created socket.\n");

  // set 15s for connection Timeout

  error = setsockopt(socket_handle, SOL_TCP, OPT_CONNECT_TIMEOUT, &opt_val, opt_len);

  if (error != RTCS_OK) {

  _io_printf("\n[Network::send_something]: Failed to setsockopt for OPT_CONNECT_TIMEOUT, CODE = 0x%x.\n", error);

  _task_block();

  }

  _io_printf ("[Network::send_something] set socket timeout.\n");

  // try to connect TCPServer

  error = connect(socket_handle, &server_socket_addr, sizeof(server_socket_addr));

  if (error != RTCS_OK) {

  if (error == RTCSERR_TCP_TIMED_OUT) {

  _io_printf("\n[Network::send_something]: Connection timeout.\n");

  } else if (error == RTCSERR_TCP_CONN_RESET) {

  _io_printf("\n[Network::send_something]: Connection reset by peer.\n"); // When connect to PC but TCPServer shuntdown, will alert this error

  }

  shutdown(socket_handle, FLAG_ABORT_CONNECTION);

  _io_printf("\n[Network::send_something]: Retry to connect TCPServer 5 seconds later...\n");

  _time_delay(5 * 1000);

  }

  else

  {

  _io_printf ("[Network::send_something] connection established.\n");

  }

  } while (error != RTCS_OK);

  _io_printf ("[Network::send_something] ready to send.\n");

  }

};

#endif /* NETWORK_H_ */

标签 (1)
1 解答
683 次查看
michael_wahler
Contributor III

Problem solved. Jumpers on the CPU module and the SER module were not set correctly.

TWR-K60F120M Tower Module User's Manual Section 2.11 helped.

Thanks again for your reply!

在原帖中查看解决方案

0 项奖励
回复
3 回复数
683 次查看
matthewkendall
Contributor V

Your call to ENET_get_mac_address does not look right. It normally looks like this:

error = ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE, serno, mac_address);

The first parameter is the device number. The third is a pointer to storage for the result (usually just a variable of type _enet_address since this is a just typedef name for an array of six unsigned char). The second parameter (of type uint_32) allows you to pass in a serial number or similar, which will get incorporated into the MAC address. This provides a way to make every board's MAC address unique if you already have a unique serial number.

The actual implementation of ENET_get_mac_address is in your board support package. You may find it does not do what you want, particularly in the way it incorporates the unique number, in which case feel free to change it.

683 次查看
michael_wahler
Contributor III

Thanks for your reply! Here is an updated account of the situation:

  • My board (TWRK60F120M) has a label with the MAC address on it, so it seems to be fixed (00:04:9F:01:8E:9B).The output also looks OK (see below)
  • I have changed BSP_DEFAULT_ENET_OUI in the BSP such that it matches the MAC address.
  • There was a bug in ENET_get_mac_address(). This function returns is supposed to return ENET_OK or an error code, but it would return error code 1 if _bsp_get_mac_address was successfully executed.
  • I'm also trying RTCS_ping to the Ethernet interface on the PC that is connected to the board, no success.

This is the program's output. Any other hints on how to find the source of the problem?

[Network] Successfully initialized device.

[Network] Successfully set static IP address. Now starting TCP Server.

[Network] Ethernet address: '0-4-9F-1-8E-9B

[Network] IP address: 192.168.1.202

[Network] Pinging 192.168.1.100...Request timed out

0 项奖励
回复
684 次查看
michael_wahler
Contributor III

Problem solved. Jumpers on the CPU module and the SER module were not set correctly.

TWR-K60F120M Tower Module User's Manual Section 2.11 helped.

Thanks again for your reply!

0 项奖励
回复