From 3fa18b1b7ed1d9d9983b24af42efd31a394abd0b Mon Sep 17 00:00:00 2001 From: aleks Date: Thu, 23 Jun 2022 13:33:58 +0200 Subject: [PATCH] fix tcp master read timeout --- freemodbus/port/port.h | 1 - freemodbus/tcp_master/port/port_tcp_master.c | 14 ++++++++------ freemodbus/tcp_slave/port/port_tcp_slave.c | 9 +++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/freemodbus/port/port.h b/freemodbus/port/port.h index bff33be..b5d8858 100644 --- a/freemodbus/port/port.h +++ b/freemodbus/port/port.h @@ -90,7 +90,6 @@ #define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS #define MB_TCP_SEND_TIMEOUT (pdMS_TO_TICKS(MB_TCP_SEND_TIMEOUT_MS)) #define MB_TCP_PORT_MAX_CONN (CONFIG_FMB_TCP_PORT_MAX_CONN) -#define MB_TCP_PORT_RESPOND_TIMEOUT (CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND * 1000) #define MB_TCP_FRAME_LOG_BUFSIZE (256) diff --git a/freemodbus/tcp_master/port/port_tcp_master.c b/freemodbus/tcp_master/port/port_tcp_master.c index 790dcc7..9aeb063 100644 --- a/freemodbus/tcp_master/port/port_tcp_master.c +++ b/freemodbus/tcp_master/port/port_tcp_master.c @@ -281,7 +281,7 @@ static int vMBTCPPortMasterRxCheck(int xSd, fd_set* pxFdSet, int xTimeMs) return xRes; } -static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHORT usLength) +static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHORT usLength, uint16_t xTimeMs) { int xLength = 0; UCHAR* pucBuf = pucDstBuf; @@ -291,8 +291,8 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR MB_PORT_CHECK((pxInfo && pxInfo->xSockId > -1), -1, "Try to read incorrect socket = #%d.", pxInfo->xSockId); // Set receive timeout for socket <= slave respond time - xTime.tv_sec = 0; - xTime.tv_usec = MB_TCP_PORT_RESPOND_TIMEOUT; + xTime.tv_sec = xTimeMs / 1000; + xTime.tv_usec = (xTimeMs % 1000) * 1000; setsockopt(pxInfo->xSockId, SOL_SOCKET, SO_RCVTIMEO, &xTime, sizeof(xTime)); // Receive data from connected client @@ -301,7 +301,7 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, 0); if (xLength < 0) { if (errno == EAGAIN) { - // Read timeout occurred, continue reading + // Read timeout occurred, check the timeout and return } else if (errno == ENOTCONN) { // Socket connection closed ESP_LOGE(TAG, "Socket(#%d)(%s) connection closed.", @@ -334,7 +334,8 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo) if (pxInfo) { MB_PORT_CHECK((pxInfo->xSockId > 0), -1, "Try to read incorrect socket = #%d.", pxInfo->xSockId); // Read packet header - xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[0], MB_TCP_UID); + xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[0], + MB_TCP_UID, xMBTCPPortMasterGetRespTimeLeft(pxInfo)); if (xRet < 0) { pxInfo->xRcvErr = xRet; return xRet; @@ -347,7 +348,8 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo) // If we have received the MBAP header we can analyze it and calculate // the number of bytes left to complete the current request. xLength = (int)MB_TCP_GET_FIELD(pxInfo->pucRcvBuf, MB_TCP_LEN); - xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[MB_TCP_UID], xLength); + xRet = xMBTCPPortMasterGetBuf(pxInfo, &pxInfo->pucRcvBuf[MB_TCP_UID], + xLength, xMBTCPPortMasterGetRespTimeLeft(pxInfo)); if (xRet < 0) { pxInfo->xRcvErr = xRet; return xRet; diff --git a/freemodbus/tcp_slave/port/port_tcp_slave.c b/freemodbus/tcp_slave/port/port_tcp_slave.c index c5eee09..bf7e997 100644 --- a/freemodbus/tcp_slave/port/port_tcp_slave.c +++ b/freemodbus/tcp_slave/port/port_tcp_slave.c @@ -61,7 +61,7 @@ /* ----------------------- Defines -----------------------------------------*/ #define MB_TCP_DISCONNECT_TIMEOUT ( CONFIG_FMB_TCP_CONNECTION_TOUT_SEC * 1000000 ) // disconnect timeout in uS -#define MB_TCP_RESP_TIMEOUT_MS ( MB_MASTER_TIMEOUT_MS_RESPOND - 2 ) // slave response time limit +#define MB_TCP_RESP_TIMEOUT_MS ( MB_MASTER_TIMEOUT_MS_RESPOND - 1 ) // slave response time limit #define MB_TCP_NET_LISTEN_BACKLOG ( SOMAXCONN ) /* ----------------------- Prototypes ---------------------------------------*/ @@ -150,12 +150,13 @@ xMBTCPPortInit( USHORT usTCPPort ) xConfig.pcBindAddr = NULL; // Create task for packet processing - BaseType_t xErr = xTaskCreate(vMBTCPPortServerTask, - "tcp_server_task", + BaseType_t xErr = xTaskCreatePinnedToCore(vMBTCPPortServerTask, + "tcp_slave_task", MB_TCP_STACK_SIZE, NULL, MB_TCP_TASK_PRIO, - &xConfig.xMbTcpTaskHandle); + &xConfig.xMbTcpTaskHandle, + MB_PORT_TASK_AFFINITY); vTaskSuspend(xConfig.xMbTcpTaskHandle); if (xErr != pdTRUE) {