From 7652e26a524c475bd45aed58f2b6330548ea16d0 Mon Sep 17 00:00:00 2001 From: "Ben, Cui" <55608454@qq.com> Date: Wed, 20 May 2026 19:02:18 +0800 Subject: [PATCH] fixed rtu message recevie in Windows Platform when running in windows platform, I can not receive message from 485 serial channel --- src/modbus.c | 72 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index c1c9a148..13370b9a 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -383,17 +383,21 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) return -1; } - /* Add a file descriptor to the set */ - FD_ZERO(&rset); - if (ctx->s < 0 || ctx->s >= FD_SETSIZE) { - if (ctx->debug) { - fprintf(stderr, "ERROR Invalid socket descriptor %d\n", ctx->s); +#ifdef _WIN32 + if (ctx->backend->backend_type != _MODBUS_BACKEND_TYPE_RTU) +#endif + { + /* Add a file descriptor to the set */ + FD_ZERO(&rset); + if (ctx->s < 0 || ctx->s >= FD_SETSIZE) { + if (ctx->debug) { + fprintf(stderr, "ERROR Invalid socket descriptor %d\n", ctx->s); + } + errno = EINVAL; + return -1; } - errno = EINVAL; - return -1; + FD_SET(ctx->s, &rset); } - FD_SET(ctx->s, &rset); - /* We need to analyse the message step by step. At the first step, we want * to reach the function code because all packets contain this * information. */ @@ -419,32 +423,39 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) } while (length_to_read != 0) { - rc = ctx->backend->select(ctx, &rset, p_tv, length_to_read); - if (rc == -1) { - _error_print(ctx, "select"); - if (ctx->error_recovery & MODBUS_ERROR_RECOVERY_LINK) { #ifdef _WIN32 - wsa_err = WSAGetLastError(); + if (ctx->backend->backend_type != _MODBUS_BACKEND_TYPE_RTU) { + Sleep(1); + rc = 1; // 模拟就绪 + } else { +#endif + rc = ctx->backend->select(ctx, &rset, p_tv, length_to_read); + if (rc == -1) { + _error_print(ctx, "select"); + if (ctx->error_recovery & MODBUS_ERROR_RECOVERY_LINK) { +#ifdef _WIN32 + wsa_err = WSAGetLastError(); - // no equivalent to ETIMEDOUT when select fails on Windows - if (wsa_err == WSAENETDOWN || wsa_err == WSAENOTSOCK) { - modbus_close(ctx); - modbus_connect(ctx); - } + // no equivalent to ETIMEDOUT when select fails on Windows + if (wsa_err == WSAENETDOWN || wsa_err == WSAENOTSOCK) { + modbus_close(ctx); + modbus_connect(ctx); + } #else - int saved_errno = errno; - - if (errno == ETIMEDOUT) { - _sleep_response_timeout(ctx); - modbus_flush(ctx); - } else if (errno == EBADF) { - modbus_close(ctx); - modbus_connect(ctx); - } - errno = saved_errno; + int saved_errno = errno; + + if (errno == ETIMEDOUT) { + _sleep_response_timeout(ctx); + modbus_flush(ctx); + } else if (errno == EBADF) { + modbus_close(ctx); + modbus_connect(ctx); + } + errno = saved_errno; #endif + } + return -1; } - return -1; } rc = ctx->backend->recv(ctx, msg + msg_length, length_to_read); @@ -532,7 +543,6 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) return ctx->backend->check_integrity(ctx, msg, msg_length); } - /* Receive the request from a modbus master */ int modbus_receive(modbus_t *ctx, uint8_t *req) {