2020-05-10 04:40 AM
Dear friends,
I am trying to establish a robust TCP interaction between two different boards with the same MCU (stm32f407vgt6) and ethernet phy (dp83848cvv) and I use lwIP Netconn APIs as TCP/IP stack and freeRTOS as a real time operating system.
In both MCUs I must run a TCP server task and a TCP Client task. When both boards are connected together directly there is no problem. The problem occurs when both boards are connected through a network switch. After working for a while, the boards TCP tasks (Server & Client) stop working and the boards do not ping. (Ping request is timed out.)
The code for Server and Client tasks are as bellow:
void Task_TCP_Server(void const * argument)
{
/* init code for LWIP */
MX_LWIP_Init();
/* USER CODE BEGIN 5 */
struct netconn *conn, *newconn;
err_t err, accept_err;
struct netbuf *buf;
void *data;
u16_t len;
err_t recv_err;
uint8_t *tcp_rx_buf;
uint8_t temp_buff[100] = {0};
data_type respond_len = {0};
data_type start_addr = {0};
data_type num_reg = {0};
uint8_t byte_cnt = 0;
LWIP_UNUSED_ARG(argument);
// osDelay(50);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, TCP_SERVER_PORT_NUMBER);
if (err == ERR_OK)
{
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1)
{
/* Grab new connection. */
accept_err = netconn_accept(conn, &newconn);
/* Process the new connection. */
if (accept_err == ERR_OK)
{
recv_err = netconn_recv(newconn, &buf);
while ( recv_err == ERR_OK)
{
do
{
netbuf_data(buf, &data, &len);
tcp_rx_buf = (uint8_t *)data;
if(tcp_rx_buf[2] == 0x00 && tcp_rx_buf[3] == 0x00)
{
/* Transaction ID */
temp_buff[0] = tcp_rx_buf[0];
temp_buff[1] = tcp_rx_buf[1];
/* Protocol ID */
temp_buff[2] = tcp_rx_buf[2];
temp_buff[3] = tcp_rx_buf[3];
/* Unit Identifier */
temp_buff[6] = tcp_rx_buf[6];
start_addr.byte[1] = tcp_rx_buf[8];
start_addr.byte[0] = tcp_rx_buf[9];
if(tcp_rx_buf[7] == PRESET_MULTIPLE_REG_FCODE)
{
num_reg.byte[1] = tcp_rx_buf[10];
num_reg.byte[0] = tcp_rx_buf[11];
byte_cnt = tcp_rx_buf[12];
if(tcp_rx_buf[14] == ON)
Relay_Write(RELAY_CHANNEL_1, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_1, MH_RELAY_OFF);
if(tcp_rx_buf[16] == ON)
Relay_Write(RELAY_CHANNEL_2, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_2, MH_RELAY_OFF);
if(tcp_rx_buf[18] == ON)
Relay_Write(RELAY_CHANNEL_3, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_3, MH_RELAY_OFF);
if(tcp_rx_buf[20] == ON)
Relay_Write(RELAY_CHANNEL_4, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_4, MH_RELAY_OFF);
if(tcp_rx_buf[22] == ON)
Relay_Write(RELAY_CHANNEL_5, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_5, MH_RELAY_OFF);
if(tcp_rx_buf[24] == ON)
Relay_Write(RELAY_CHANNEL_6, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_6, MH_RELAY_OFF);
if(tcp_rx_buf[26] == ON)
Relay_Write(RELAY_CHANNEL_7, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_7, MH_RELAY_OFF);
if(tcp_rx_buf[28] == ON)
Relay_Write(RELAY_CHANNEL_8, MH_RELAY_ON);
else
Relay_Write(RELAY_CHANNEL_8, MH_RELAY_OFF);
netconn_write(newconn, data, len, NETCONN_COPY);
}
// else if(tcp_rx_buf[7] == PRESET_SINGLE_REG_FCODE)
// {
//
// Write_Group_SingleReg_TCP(start_addr.value);
// netconn_write(newconn, data, len, NETCONN_COPY);
//
// }
else
{
netconn_write(newconn, data, len, NETCONN_COPY);
}
}
else
{
netconn_write(newconn, data, len, NETCONN_COPY);
}
}
while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
recv_err = netconn_recv(newconn, &buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(newconn);
printf(" can not bind TCP netconn");
}
}
else
{
printf("can not create TCP netconn");
}
/* USER CODE END 5 */
}
void Task_TCP_Client(void const * argument)
{
/* USER CODE BEGIN 5 */
uint8_t pData[10] = {0};
osEvent evt;
err_t err;
ip4_addr_t serverIPAddress;
struct netbuf *recvData;
char *dataBuff;
osDelay(300);
IP4_ADDR(&serverIPAddress, SERVER_IP_0, SERVER_IP_1, SERVER_IP_2, SERVER_IP_3);
while(1)
{
osSignalWait(0x01, osWaitForever);
struct netconn* tcpClientConn = netconn_new(NETCONN_TCP);
if(tcpClientConn != NULL)
{
if(err == ERR_OK)
{
err = netconn_connect(tcpClientConn, &serverIPAddress, 503);
if(err == ERR_OK)
{
err = netconn_write(tcpClientConn, &sendData[1], sendData[0], NETCONN_COPY);
if(err == ERR_OK)
{
err = netconn_recv(tcpClientConn, &recvData);
if(err == ERR_OK)
{
netbuf_data(recvData, (void *)&dataBuff, &netbuf_len(recvData));
netbuf_delete(recvData);
err = netconn_close(tcpClientConn);
if(err == ERR_OK)
{
}
err = netconn_delete(tcpClientConn);
if(err == ERR_OK)
{
}
}
else
{
netconn_close(tcpClientConn);
err = netconn_delete(tcpClientConn);
if(err == ERR_OK)
{
}
}
}
else
{
err = netconn_close(tcpClientConn);
if(err == ERR_OK)
{
}
err = netconn_delete(tcpClientConn);
if(err == ERR_OK)
{
}
}
}
else
{
err = netconn_delete(tcpClientConn);
if(err == ERR_OK)
{
}
}
}
else
{
netconn_delete(tcpClientConn);
}
}
else
{
}
}
/* USER CODE END 5 */
}
Any helps would be appreciated in advance,
kind regards,
Mujtaba
Solved! Go to Solution.
2020-05-14 06:04 AM
Hello Kurta,
the problem was in the MAC address. I forgot to change the MAC address of two boards.
2020-05-10 04:50 AM
Hi,
After working for a while -> how long does it work for you? We also experiencing with LwIP and for us it was stable for 2 days without any problems, I haven't tested it longer yet.
2020-05-14 06:04 AM
Hello Kurta,
the problem was in the MAC address. I forgot to change the MAC address of two boards.