Hi All
I use MQX's RTCS UDP socket to communicate to Linux based application. My Linux application sends message every 100 ms. It is received by another Linux application and the traffic is also detected by Wireshark.
Here is the code to set up UDP:
============================
============================
Solved! Go to Solution.
hi all
i have tried the code submited by Charlie.the code runs successfully.
IAR 6.70 MQX4.0 WIN7 OS
my pc can recv the datagrams sent by mqx.
and mqx can recv the datagrams sent by my pc also.
Hi All
I use MQX's RTCS UDP socket to communicate to Linux based application. My Linux application sends message every 10 ms. It is received by another Linux application and the traffic is also detected by Wireshark.
By running following code and running one linux app at same time I can see this message
UDP send "A4701D0 UDP port 6200 to A470136 UDP port 6200 " OK
but it never receives and any message.
Can anyone give me hint how to find the problem?
Thanks
Charlie
Here is the code to test UDP:
============================
int32_t main()
{
if (b_UDP_Debug_Print) {
printf("RTCS_Setup============\r\n");
fflush(stderr);
}
bool b_UDP_Debug_Print = TRUE;
_ip_address nENET_GATEWAY = IPADDR(10, 71, 0, 1);
_ip_address nENET_NETWORK = IPADDR(10, 71, 0, 0);
_ip_address nENET_NETMASK = IPADDR(255,255,254,0);
_ip_address nENET_IPADDR_LOCAL = IPADDR(10, 71, 1, 208);
_ip_address nENET_IPADDR_REMOTE = IPADDR(10, 71, 1, 54);
int nPortNo = 6200;
int32_t error;
_enet_handle enethandle;
_rtcs_if_handle ifhandle;
_enet_address enet_local_address;
uint32_t sock;
sockaddr_in localSin;
sockaddr_in remoteSin;
/* We are running on a memory constrained device, lower RTCS's defaults */
/* runtime RTCS configuration */
_RTCSPCB_init = 4;
_RTCSPCB_grow = 2;
_RTCSPCB_max = 6;
_RTCS_msgpool_init = 4;
_RTCS_msgpool_grow = 2;
_RTCS_msgpool_max = 8;
_RTCS_socket_part_init = 4; /*https://community.freescale.com/thread/63630*/
_RTCS_socket_part_grow = 2;
_RTCS_socket_part_max = 6;
_RTCSTASK_stacksize = 2200;
if (b_UDP_Debug_Print) {
printf("Step %i: Changing RTCS Running Parameters\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
char taskname[] = "RTCS";
error = RTCS_create();
if (error != RTCS_OK)
{
printf("Step %i: Failed to initialize %s, error = %X\r\n", ++m_nUDP_Steps, taskname, error);
return error;
} else {
if (b_UDP_Debug_Print) {
printf("Step %i: RTCS_create\tOK\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
}
ENET_get_mac_address(ENET_DEVICE, nENET_IPADDR_LOCAL, enet_local_address);
error |= ENET_initialize(ENET_DEVICE, enet_local_address, 0, &enethandle);
if (error != RTCS_OK)
{
printf("Step %i: ENET_initialize failed, error = %X\r\n", ++m_nUDP_Steps, error);
return error;
} else {
if (b_UDP_Debug_Print) {
printf("Step %i: ENET_initialize\tOK\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
}
error |= RTCS_if_add(enethandle, RTCS_IF_ENET, &ifhandle);
if (error != RTCS_OK)
{
printf("Step %i: RTCS_if_add failed, error = %X\r\n", ++m_nUDP_Steps, error);
return error;
} else {
if (b_UDP_Debug_Print) {
printf("Step %i: RTCS_if_add\tOK\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
}
error |= RTCS_if_bind(ifhandle, nENET_IPADDR_LOCAL, nENET_NETMASK); /* nENET_IPADDR_LOCAL RTCS_if_bind_DHCP()*/
if (error != RTCS_OK)
{
printf("Step %i: RTCS_if_bind failed, error = %X\r\n", ++m_nUDP_Steps, error);
return error;
} else {
if (b_UDP_Debug_Print) {
printf("Step %i: RTCS_if_bind\tOK\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
}
printf("======RTCS setup\tOK\r\n\r\n");
fflush(stderr);
if (b_UDP_Debug_Print) {
printf("Creating socket============\r\n");
fflush(stderr);
}
/*A socket is an abstraction that identifies an endpoint.*/
/* Create a datagram socket: */
sock = socket(AF_INET, SOCK_DGRAM, 0); /*PF_INET==AF_INET*/
if (sock == RTCS_SOCKET_ERROR)
{
printf("Step %i: call socket failed\r\n", ++m_nUDP_Steps);
return 1;
} else {
if (b_UDP_Debug_Print) {
printf("Step %i: call socket\tOK\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
}
/* Set up no wait option: */
uint32_t opt_value = 1;
uint32_t opt_length = sizeof(uint32_t);
setsockopt(sock, SOL_UDP, OPT_RECEIVE_NOWAIT, &opt_value, opt_length);
setsockopt(sock, SOL_UDP, OPT_SEND_NOWAIT, &opt_value, opt_length);
std::memset((char *) &localSin, 0, sizeof(localSin));
localSin.sin_family = AF_INET;
localSin.sin_port = nPortNo;
localSin.sin_addr.s_addr = nENET_IPADDR_LOCAL;
/*Usually, TCP/IP servers bind to INADDR_ANY, so that one instance of the server can service all IP addresses.*/
error = bind(sock, &localSin, sizeof(sockaddr_in));
if (error != RTCS_OK)
{
printf("Step %i: call bind failed\r\n", ++m_nUDP_Steps);
return error;
} else {
if (b_UDP_Debug_Print) {
printf("Step %i: call bind\tOK\r\n", ++m_nUDP_Steps);
fflush(stderr);
}
}
uint16_t remoteSinBytes = sizeof(remoteSin);
memset((char *) &remoteSin, 0, remoteSinBytes);
remoteSin.sin_family = AF_INET;
remoteSin.sin_port = nPortNo;
remoteSin.sin_addr.s_addr = nENET_IPADDR_REMOTE;
error = connect(sock, &remoteSin, remoteSinBytes);
/*The connect() function might be used multiple times. Whenever connect() is called, the current endpoint is replaced by the new one.*/
if (error != RTCS_OK)
{
printf("Step %i: Error--connect() failed with error code %lx.\r\n", ++m_nUDP_Steps, error);
return error;
} else {
if (b_UDP_Debug_Print) printf("Step %i: call connect\tOK. Connected to %lX, port %d.\r\n", ++m_nUDP_Steps
, remoteSin.sin_addr.s_addr, remoteSin.sin_port);
fflush(stderr);
}
printf("======Socket creation\tOK\r\n\r\n");
fflush(stderr);
char output_buffer[258], input_buffer[1024];
int obufsize = sprintf(output_buffer, "%lX UDP port %i to %lX UDP port %i ",
nENET_IPADDR_LOCAL, nPortNo, nENET_IPADDR_REMOTE, nPortNo);
while (1) {
int result = -1;
result = send(sock, output_buffer, obufsize, 0);
/*Sends data on the stream socket, or on a datagram socket, for which a remote endpoint has been specified.*/
if (result != obufsize) {
int err = RTCS_geterror(sock);
printf("UDP send ERROR: %i\r\n", err);
} else {
printf("UDP send \"%s\" OK\r\n", output_buffer);
}
result = recv(sock, input_buffer, 512, 0);
if (result >0) {
printf("UDP received \"%s\"\r\n", input_buffer);
}
fflush(stderr);
_time_delay(10);
}
return 0;
}
============================
Hello Charlie,
Are you using a Freescale board??
What MQX version are you using?
Have a great day,
Sol
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Sol
I am using our own board with Cortex-M4 MK60DN512VMD10. IDE is IAREW 6.3/MQX 3.8 and IAREW 7.2/MQX 4.1.
Can you run my code on your system to verify my code?
Thanks
Charlie
Hi Sol
I attached a screen shot below:
Top is MQX firmware output, bottom is my two linux applications.
Regards
Charlie
hi all
i have tried the code submited by Charlie.the code runs successfully.
IAR 6.70 MQX4.0 WIN7 OS
my pc can recv the datagrams sent by mqx.
and mqx can recv the datagrams sent by my pc also.