stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.44k stars 1.74k forks source link

Incomplete commit revert? #711

Closed martinwag closed 2 months ago

martinwag commented 1 year ago

libmodbus version

Git b25629bfb508bdce7d519884c0fa9810b7d98d44 (Head as of 2023-08-10)

Description

commit db1cbc593501590eef16bbf9e0746290ac9116b9 introduced some changes with commit description "windows only". However, this commit also changed linux behavior in three places. Commit 9b679b7c3b267f7a5d87e26b8f94138edd76f1d9 then reverts this, but misses one place.

@@ -409,7 +438,19 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)

         if (rc == -1) {
             _error_print(ctx, "read");
+#ifdef _WIN32
+            wsa_err = WSAGetLastError();
             if ((ctx->error_recovery & MODBUS_ERROR_RECOVERY_LINK) &&
+                (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_TCP) &&
+                (wsa_err == WSAENOTCONN || wsa_err == WSAENETRESET || wsa_err == WSAENOTSOCK ||
+                wsa_err == WSAESHUTDOWN || wsa_err == WSAECONNABORTED || wsa_err == WSAETIMEDOUT ||
+                wsa_err == WSAECONNRESET)) {
+                modbus_close(ctx);
+                modbus_connect(ctx);
+            }
+#else
-->  +            if ((ctx->error_recovery & MODBUS_ERROR_RECOVERY_LINK) &&
-->  +                (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_TCP) &&
                 (errno == ECONNRESET || errno == ECONNREFUSED ||
                  errno == EBADF)) {
                 int saved_errno = errno;

I don't think this was intentionally left like this, but I also can't verify it. Please have a look if this is correct.

stephane commented 2 months ago

You're right and I think there is an issue on Windows with serial. I'm sure we should revert the check on TCP for not Windows in third place and to have a behavior similar to previous version for Windows serial, I reverted the TCP check for Windows too.