forked from espressif/esp-modbus
fix modbus read dead slave cause wdt timeout
This commit is contained in:
@@ -90,6 +90,7 @@
|
|||||||
#define MB_TCP_SEND_TIMEOUT_MS (500) // send event timeout in mS
|
#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_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_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)
|
#define MB_TCP_FRAME_LOG_BUFSIZE (256)
|
||||||
|
|
||||||
|
@@ -286,18 +286,22 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR
|
|||||||
int xLength = 0;
|
int xLength = 0;
|
||||||
UCHAR* pucBuf = pucDstBuf;
|
UCHAR* pucBuf = pucDstBuf;
|
||||||
USHORT usBytesLeft = usLength;
|
USHORT usBytesLeft = usLength;
|
||||||
|
struct timeval xTime;
|
||||||
|
|
||||||
MB_PORT_CHECK((pxInfo && pxInfo->xSockId > -1), -1, "Try to read incorrect socket = #%d.", pxInfo->xSockId);
|
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
|
// Receive data from connected client
|
||||||
while (usBytesLeft > 0) {
|
while (usBytesLeft > 0) {
|
||||||
xMBTCPPortMasterCheckShutdown();
|
xMBTCPPortMasterCheckShutdown();
|
||||||
// none blocking read from socket with timeout
|
xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, 0);
|
||||||
xLength = recv(pxInfo->xSockId, pucBuf, usBytesLeft, MSG_DONTWAIT);
|
|
||||||
if (xLength < 0) {
|
if (xLength < 0) {
|
||||||
if (errno == EAGAIN) {
|
if (errno == EAGAIN) {
|
||||||
// Read timeout occurred, continue reading
|
// Read timeout occurred, continue reading
|
||||||
continue;
|
|
||||||
} else if (errno == ENOTCONN) {
|
} else if (errno == ENOTCONN) {
|
||||||
// Socket connection closed
|
// Socket connection closed
|
||||||
ESP_LOGE(TAG, "Socket(#%d)(%s) 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) {
|
if (xMBTCPPortMasterGetRespTimeLeft(pxInfo) == 0) {
|
||||||
return ERR_TIMEOUT;
|
return ERR_TIMEOUT;
|
||||||
}
|
}
|
||||||
vTaskDelay(1);
|
|
||||||
}
|
}
|
||||||
return usLength;
|
return usLength;
|
||||||
}
|
}
|
||||||
@@ -847,12 +850,13 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
|||||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||||
} else if ((xRet == ERR_TIMEOUT) || (xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo) == 0)) {
|
} else if ((xRet == ERR_TIMEOUT) || (xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo) == 0)) {
|
||||||
// Timeout occurred when receiving frame, process respond timeout
|
// 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."),
|
ESP_LOGD(TAG, MB_SLAVE_FMT(", frame read timeout."),
|
||||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||||
} else if (xRet == ERR_BUF) {
|
} else if (xRet == ERR_BUF) {
|
||||||
// After retries a response with incorrect TID received, process failure.
|
// After retries a response with incorrect TID received, process failure.
|
||||||
xMBTCPPortMasterFsmSetError(EV_ERROR_RECEIVE_DATA, EV_MASTER_ERROR_PROCESS);
|
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);
|
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", critical error=%d."),
|
ESP_LOGE(TAG, MB_SLAVE_FMT(", critical error=%d."),
|
||||||
|
Reference in New Issue
Block a user