From 2733ceae72c913a4f7ff1344b053e82f79c95002 Mon Sep 17 00:00:00 2001 From: aleks Date: Fri, 13 May 2022 15:15:17 +0200 Subject: [PATCH] fix modbus read dead slave cause wdt timeout --- freemodbus/port/port.h | 1 + freemodbus/tcp_master/port/port_tcp_master.c | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/freemodbus/port/port.h b/freemodbus/port/port.h index b5d8858..bff33be 100644 --- a/freemodbus/port/port.h +++ b/freemodbus/port/port.h @@ -90,6 +90,7 @@ #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 f2ba624..790dcc7 100644 --- a/freemodbus/tcp_master/port/port_tcp_master.c +++ b/freemodbus/tcp_master/port/port_tcp_master.c @@ -286,18 +286,22 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR int xLength = 0; UCHAR* pucBuf = pucDstBuf; USHORT usBytesLeft = usLength; + struct timeval xTime; 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; + setsockopt(pxInfo->xSockId, SOL_SOCKET, SO_RCVTIMEO, &xTime, sizeof(xTime)); + // Receive data from connected client while (usBytesLeft > 0) { xMBTCPPortMasterCheckShutdown(); - // none blocking read from socket with timeout - xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, MSG_DONTWAIT); + xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, 0); if (xLength < 0) { if (errno == EAGAIN) { // Read timeout occurred, continue reading - continue; } else if (errno == ENOTCONN) { // Socket connection closed ESP_LOGE(TAG, "Socket(#%d)(%s) connection closed.", @@ -316,7 +320,6 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR if (xMBTCPPortMasterGetRespTimeLeft(pxInfo) == 0) { return ERR_TIMEOUT; } - vTaskDelay(1); } return usLength; } @@ -847,12 +850,13 @@ static void vMBTCPPortMasterTask(void *pvParameters) pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr); } else if ((xRet == ERR_TIMEOUT) || (xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo) == 0)) { // Timeout occurred when receiving frame, process respond timeout + xMBTCPPortMasterFsmSetError(EV_ERROR_RESPOND_TIMEOUT, EV_MASTER_ERROR_PROCESS); ESP_LOGD(TAG, MB_SLAVE_FMT(", frame read timeout."), pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr); } else if (xRet == ERR_BUF) { // After retries a response with incorrect TID received, process failure. xMBTCPPortMasterFsmSetError(EV_ERROR_RECEIVE_DATA, EV_MASTER_ERROR_PROCESS); - ESP_LOGD(TAG, MB_SLAVE_FMT(", frame error."), + ESP_LOGW(TAG, MB_SLAVE_FMT(", frame error."), pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr); } else { ESP_LOGE(TAG, MB_SLAVE_FMT(", critical error=%d."),