mirror of
https://github.com/espressif/esp-modbus.git
synced 2025-07-31 02:47:16 +02:00
fix tcp master read timeout
This commit is contained in:
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user