Skip to content

Fix libmodbus build for mingw #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: rtu_usb
Choose a base branch
from

Conversation

jimklimov
Copy link
Member

@jimklimov jimklimov commented Jan 18, 2025

Breakage was introduced by upstream for modbus-tcp (some time after v3.1.7 that built well), and by ssize_t printing from modbus-rtu-usb.

Fixes verified by:

  • semi-native builds on Windows (MSYS2 packaging of mingw, target x86_64-w64-mingw32),
  • cross-builds on Linux for x86_64-w64-mingw32 and i686-w64-mingw32 targets (with gcc), and
  • native builds on x86_64 Linux and ARM Linux (RPi) by gcc and clang.

No warnings/errors emitted by either scenario.

UPDATE: While trying to check this on other NUT CI farm workers (OpenBSD, FreeBSD, MacOS, OmniOS, OpenIndiana...) a few recurrent issues popped up and also were fixed in this branch.

UPDATE: Piece by piece, parts of this PR are extracted into smaller PRs with a more focused scope of changes, proposed for this NUT fork of libmodbus as well as for upstream (where easily applicable). It is being rebased as those less-questionable change sets get merged into the fork, so I expect it to become a null change soon and be just closed.

While the remaining code of this PR mostly concerns tests (and specifically flakiness of the test case with small timeouts), and console-flushing needed when running verbosely on Windows, there is also a questionable change about flushing the modbus connection when retrying. Not sure if it needs be.

@jimklimov jimklimov added the bug Something isn't working label Jan 18, 2025
@jimklimov jimklimov force-pushed the fix-mingw-build branch 3 times, most recently from 6ef9d67 to f64103c Compare January 18, 2025 18:39
@jimklimov
Copy link
Member Author

Ooooh that was an adventure to get builds and make check to pass on all platforms of the NUT CI farm!

src/modbus.c Outdated
}
// else: We have at most tried some default FD's but not
// the (lacking) one for the backend, so fall through for
// its recv method anyway (e.g. query libusb directly).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last commit regards networkupstools/nut#2609 (comment) and follows up from PR #1:

I also feel a bit stupid, as I overlooked that the method involved in _modbus_receive_msg() is not a standard select(), but a specific ctx->backend->select() so the one here _modbus_rtu_usb_select() does not care about rset and talks to the libusb context. The fixes in that PR to avoid FD_SET(-1) are valid (to avoid illegal memory access), but are not the full answer here as I hoped it could be.

…() to auto-flush so logs are comprehensible

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
… console logs on WIN32

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
…> 5ms" test case

It fails or passes randomly on different platforms
Win32/mingw, FreeBSD, MacOS X
maybe something remains in buffer? We'll see if extra sleep helps.

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
…isters" so the buffer does not contaminate the result on some systems

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
… "33ms > 20ms"

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
…d OpenBSD

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
… their own category

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
… > Yms)" to increase the timeouts and fit all laggy systems better

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
… on WIN32, and we follow MODBUS_ERROR_RECOVERY_LINK, do modbus_flush(ctx)

At least this allows unit-tests to pass...

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant