Skip to content

Conversation

fedepell
Copy link

Adds support for serial for ESP-IDF and introduces a document on how to use it in that environment.

Assumes to be merged after #741 (I will rebase accordingly in case).

Copy link

cla-bot bot commented May 20, 2024

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

1 similar comment
Copy link

cla-bot bot commented Jun 29, 2024

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

stephane added a commit that referenced this pull request Jul 16, 2024
stephane added a commit that referenced this pull request Jul 16, 2024
@diplfranzhoepfinger
Copy link
Contributor

sorry, i overlooked this !

@fedepell
Copy link
Author

sorry, i overlooked this !

No problem :) Sorry for my delay now, was on a short vacation ;)

I hope we can get this (ESP support in general, in some variant) in as it may be very handy to have it mainstreamed. Also I've done a lot of testing with my code (both serial and TCP) and works like a charm (while the official internal library has a lot of limitations, ie. trivially missing possibility to have multiple connections which is just addressed in the beta).

@robin-twice
Copy link

@fedepell Thanks for your efforts with the ESP-IDF platform support!

I've tried to use your implementation as well, but stumbled upon some issues and wanted to asked whether you can confirm that: Using the ESP-IDF UART VFS with Modbus RTU, the uart_read() function does return early on some special characters (0x0A, 0x0D) depending on the line endings, which then creates either CRC errors or incomplete messages thereafter:

2024-10-07 07:23:09 Waiting for a confirmation...
2024-10-07 07:23:09 <47><03><02><00><0D><0A><B1>
2024-10-07 07:23:09 ERROR CRC received 0xB10A != CRC calculated 0x4EF0
2024-10-07 07:23:10 [47][03][9C][98][00][01][25][13]
2024-10-07 07:23:10 Waiting for a confirmation...
2024-10-07 07:23:10 <8C><47><03><02><00>
2024-10-07 07:23:10 Request for slave 140 ignored (not 71)
2024-10-07 07:23:10 The responding slave 140 isn't the requested slave 71

I've found a ticket on the ESP-IDF project where this issue with VFS UART read() behaviour is being addressed:
espressif/esp-idf#14155

Would be interested in your opinion about that and whether you've seen similar issues.

@stephane
Copy link
Owner

Could you rebase your draft PR against master branch, please?

@fedepell fedepell force-pushed the esp32_serial_support branch from 96a9754 to 8dd26d1 Compare October 23, 2024 02:39
@cla-bot cla-bot bot added the cla-signed label Oct 23, 2024
@fedepell
Copy link
Author

Could you rebase your draft PR against master branch, please?

Rebased on master and squashed commits to one!

@fedepell
Copy link
Author

@robin-twice : sorry for the late response, very busy period :(

I've noticed some seldom "bad crc" messages, but didn't investigate in big detail as I saw (I did many long term tests and kept statistics of errors by type and so on) the same (very low) amount, actually even slightly higher, using the ESP native modbus library (which may suffer from the same problem if it reads data same way?) which I compared to (so I assumed it was just my lab-grade cabling or the 485 level adapter I have flying).

I'll try to give it a deeper look ASAP, hopefully this weekend or next week!

@fedepell fedepell marked this pull request as ready for review October 23, 2024 03:11
@fedepell fedepell force-pushed the esp32_serial_support branch from 8dd26d1 to a4eb887 Compare October 30, 2024 06:43
@fedepell
Copy link
Author

I just pushed a few moments ago two little changes:

  • use uart_vfs_dev_use_driver instead of esp_vfs_dev_uart_use_driver as the latter is deprecated and resulted in a warning
  • Add in the docs also a note of having possibly (if console UART is disabled, something I faced today ;-) ) to explicitly initialize the VFS in your code if needed (I would not put this inside libmodbus as it is a system wide decision) with uart_vfs_dev_register (see:
    solution. Be aware that if you are not using the UART0 as console (ie. you are
    )

@fedepell
Copy link
Author

fedepell commented Nov 19, 2024

@fedepell Thanks for your efforts with the ESP-IDF platform support!

I've tried to use your implementation as well, but stumbled upon some issues and wanted to asked whether you can confirm that: Using the ESP-IDF UART VFS with Modbus RTU, the uart_read() function does return early on some special characters (0x0A, 0x0D) depending on the line endings, which then creates either CRC errors or incomplete messages thereafter:

2024-10-07 07:23:09 Waiting for a confirmation...
2024-10-07 07:23:09 <47><03><02><00><0D><0A><B1>
2024-10-07 07:23:09 ERROR CRC received 0xB10A != CRC calculated 0x4EF0
2024-10-07 07:23:10 [47][03][9C][98][00][01][25][13]
2024-10-07 07:23:10 Waiting for a confirmation...
2024-10-07 07:23:10 <8C><47><03><02><00>
2024-10-07 07:23:10 Request for slave 140 ignored (not 71)
2024-10-07 07:23:10 The responding slave 140 isn't the requested slave 71

I've found a ticket on the ESP-IDF project where this issue with VFS UART read() behaviour is being addressed: espressif/esp-idf#14155

Would be interested in your opinion about that and whether you've seen similar issues.

Sorry it took forever to me to test :-( ($DAYJOB priorities ;-) )

I partially confirm what you found: to reproduce I crafted requests of 10 registers (so a 0x0a gets there with no doubts) and could reproduce, the write was not going through.

I've then added (see last pushed commit) the ESP_LINE_ENDING_LF configuration (to both read and write) and that seems to be gone now. And now I don't get also that seldom errors I saw (interesting enough the same problem was in the ESP internal modbus protocol, so that mislead me :-( ).

But as I see here in libmodbus we are anyway select()-ing and read()-ing until a certain length we want (or timeout of course) so we are anyhow looping on it (as in "alternatives you have considered" section in the ticket you linked to), so I'm not sure the problem should apply?

If it should, it seems that the solution was finally merged in master (espressif/esp-idf@92b4c62#diff-8f70148cf331b8d8293e6c953cf8d7ce6a5951cb0ceb6c087ffd9c176aabc292R172) so should be out with next release, so maybe still not worth doing further workarounds? (what do you say @stephane ?)

Many thanks for your feedback and sorry again for the very late feedback :(

@robin-twice
Copy link

@fedepell No worries, thanks for coming back to it :)
ESP_LINE_ENDING_LF did help somewhat for me, but there were still some cases where it returned early (can't remember exactly whether on 0x0A or 0x0D).

Anyway, I also agree that with espressif/esp-idf#14155 fixed it should not be an issue anymore, I haven't tested it myself though. Maybe you could also add to the readme that it needs >= xx.xx release of ESP-IDF to properly work.

By default some line ending conversion may be done, so make sure to
disable this on the UART used with libmodbus.
@fedepell
Copy link
Author

fedepell commented Nov 19, 2024

@fedepell No worries, thanks for coming back to it :) ESP_LINE_ENDING_LF did help somewhat for me, but there were still some cases where it returned early (can't remember exactly whether on 0x0A or 0x0D).

Anyway, I also agree that with espressif/esp-idf#14155 fixed it should not be an issue anymore, I haven't tested it myself though. Maybe you could also add to the readme that it needs >= xx.xx release of ESP-IDF to properly work.

Added a few lines to address this in ESP specific README heads-up and a reference to the ticket, good to save possible headaches to some future user :)

@fedepell fedepell force-pushed the esp32_serial_support branch from 4c22fff to ef8e7f8 Compare November 19, 2024 10:40
Ziink4 pushed a commit to neurala/libmodbus that referenced this pull request Sep 4, 2025
* Move malloc before starting unit tests

* Fix warning issues

When compiling with gcc and option -Wconversion it fixes the
warning message

  warning: conversion from ‘X’ {aka ‘x’} to ‘Y’ {aka ‘y’} may change value

Signed-off-by: José Bollo <[email protected]>

* Add modbus_[get|set]_indication_timeout to doc build

* docs: fix simple typo, reponse -> response

There is a small typo in tests/bandwidth-client.c.

Should read `response` rather than `reponse`.

* Update README.md

Add Embox RTOS to the list of supported OSes

* Install the NEWS and AUTHORS files

Fix stephane#513

* Include the test LICENSE in tarball

Fix for stephane#542

* Add .clabot

* typo

* Replace .dir-locals.el (Emacs) by .editorconfig

* Fix many typos

Thanks to @peternewman

* cosmetic changes in man page standardizing itemization

* Add FUNDING.yml

* Add the baud rate of 256k for Windows (closes stephane#603)

* Remove duplicate ';' (closes stephane#602)

* Fix position of CC flags in documentation (closes stephane#599)

* Replace obsolete AC_PROG_CC_STDC by AC_PROG_CC

* Fix typo in comment (closes stephane#588)

Thank you @qqq89513

* Add SECURITY.md (closes stephane#613)

* modbus_reply: fix copy & paste error in sanity check (fixes stephane#614)

While handling MODBUS_FC_WRITE_AND_READ_REGISTERS, both address offsets
must be checked, i.e. the read and the write address must be within the
mapping range.

At the moment, only the read address was considered, it looks like a
simple copy and paste error, so let's fix it.

Signed-off-by: Michael Heimpold <[email protected]>

* Bump version to 3.1.7

* Minor changes to NEWS

* Fix typos (closes stephane#620)

* Add many contributors to .clabot

* Minor documentation correction

Sorry for the two PRs, I did one then realized I should check the other

* Minor documentation correction

* Fix network library detection on Haiku

* Fix typo on connection (closes stephane#586).

Thank you @peternewman.

* Add rm5248 to .clabot

* Fix comment about EMBUNKEXC (closes stephane#566)

This native libmodbus error code is defined but not used.

* Fix float endianness issue on big endian architecture.

It converts float values depending on what order they come in.

This patch was modified from rm5248 [1]

[1] synexxus@a511768

* Make sub-command more clear

* Swap CRC bytes in request data but not at CRC computing (stephane#397)

* address check in single register / coil responses added (stephane#463)

Address check in single register / coil responses added

According to Modbus standard the address of single register / coils request and response must match

Co-authored-by: Heinrich Gsponer <[email protected]>

* Add contributors to .clabot

* VCLinkerTool version 1.0.0 to 1.0, NO link error

Even the newest visual studio use /VERSION:major[.minor]  .  
It means link.exe  /VERSION:1.0  just  need two version number .
If Version = 1.0.0 , when visual studio  link the program , vs will produce link error:

```
1>"/OUT:C:\Users\Lenovo\Desktop\libmodbus-master\src\win32\modbus.dll" "/VERSION:1.0.0" /INCREMENTAL:NO /NOLOGO ws2_32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST "/MANIFESTUAC:level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG "/PDB:C:\Users\Lenovo\Desktop\libmodbus-master\src\win32\modbus.pdb" /MAP /SUBSYSTEM:CONSOLE /TLBID:1 "/IMPLIB:C:\Users\Lenovo\Desktop\libmodbus-master\src\win32\modbus.lib" /MACHINE:X86 /SAFESEH /DLL "C:\Users\Lenovo\Desktop\libmodbus-master\src\win32\/modbus.res"
1>"Debug\modbus-data.obj"
1>"Debug\modbus-rtu.obj"
1>"Debug\modbus-tcp.obj"
1>Debug\modbus.obj
1>LINK : fatal error LNK1117: option“VERSION:1.0.0” grammatical error
```
see: 
https://docs.microsoft.com/en-us/cpp/build/reference/version-version-information?view=msvc-160

* Add jcarrano to CLA

* Update .gitignore

* Add build check to CI

* Add autogen.sh call

* Remove check target (duplicate of distcheck)

* Replace Travis CI badge by GitHub CI

* Merge NEWS with v3.0.x branch

* Fixed MODBUS_ERROR_RECOVERY_LINK not working on Windows.

* Fixed a typo error in a comment.

* Do not change the behavior for non-windows OS.

* Reduce memory use of TCP PI backend (closes stephane#621)

- allocate exact memory required to store node and service strings
  instead of around 1kb of static memory.
- accept NULL value of service to use default Modbus port number (502)
- unit test updated

The new documentation will be updated in another commit.

* Rewrite documentation with Material for mkdocs

- remove doc build from autotools
- don't depend anymore on asciidoc
- don't provide man pages anymore
- new illustrations
- provide mkdocs instructions

* Bump version to 3.1.8

* Remove travis.yml

* Minor adjustments to MD files

* Remove year range in Copyright

* Move migration content to libmodbus.org

* Avoid negative value in FD_SET call

* Test socket against positive value instead of -1

* Add 0x34d to .clabot

* Add jordanjohnson56 to CLA

* Have autogen.sh fail if autoreconf fails

If the autoreconf program does not succeed, autogen.sh
should not succeed either.

The definition of "succeed" being "exit with exit code 0".

* Have autoreconf fail if LT_INIT is not defined

If the LT_INIT macro is not available in a *.m4 file to be
found during the autoreconf run, it is impossible to build
a useful configure script.

Therefore, we blacklist the LT_INIT macro so when it appears
unexpanded in the configure script, autoreconf will report
the failure instead of silently creating a broken configure.

This is generally a good idea for any macro not shipped with
Automake or Autoconf.

* Fix doc links

* test the protocol id for 0

previous test would fail if one byte was non zero.

* Fix double negative in tests

* New quirks handler (closes stephane#38 stephane#533)

Useful functions when you are confronted with equipment which does
not respect the protocol, which behaves strangely or when you wish
to move away from the standard.

Thank you @mhei for the great initial version.

* Fix bitwise OR in documentation

* Improve doc about slave ID in RTU

* Add .clang-format and format code (closes stephane#394)

Thank you @pboettch for initial proposal.

* Remove constraints on baud rate values

* Accept IP or device in arg of unit test progs

* Avoid compilation issue with VS2022 with strdup

* Display created files in configure.js

* Use strcpy_s under Windows

* Replace inet_addr by inet_pton calls

* Replace inet_ntoa by inet_ptop

- check conversion
- display IP in in IPv6

* Update configure.ac/config.h.win32 for new inet functions

* Instructions to build libmodbus in a VS project

Tested with Visual Studio 2022.

* Updated CLA bot file

* Fix connection check for Windows RTU (closes stephane#660, stephane#662)

* Add CIFuzz Github action

* Convert a few int to unsigned int (stephane#402)

* Fix wrong close on recent commit 5a6efec

* Adjust fuzzing to run on main branch only

* Bump version to 3.1.9

* fix modbus quirks function definition

* Split POSIX and Windows RTU connect functions

Easier to read

* Fix setting baud rate for Linux (partial revert of fa20798)

* Remove fuzzing action in waiting for a fix

* Bump version to 3.1.10

* docs: small re-wordings, fix various small typos and style issues

Signed-off-by: Michael Heimpold <[email protected]>

* Update CLA

* Fix doc of modbus_mapping_new_start_address (stephane#615)

* Free addrinfo struct on getaddrinfo() errors

* Minor to docs

* Add polar.sh to FUNDING

* Fix errno value on timeout with TCP connect (closes stephane#736, stephane#756)

Thank you kyllingstad for the issue and psychon for the fix.

* Update CLA

* Log error in read input registers if debug (closes stephane#755)

* Add checks for netinet/ip.h and gai_strerror

Extracted from stephane#745

* Don't use gai_strerror if not available

Identical check to stephane#745

* stephane#675 fix wrong generated version for double number

* adding modbus_send_raw_request_tid

* doc

* doc

* Don't use loop initial declaration (closes stephane#752)

C99 remains too modern :/

* Minor format

* Update docs about memory consumption (closes stephane#715)

Not true anymore since stephane#621

* Revert TCP checks for recovery (closes stephane#711)

Requires more tests on Windows with serial communications...
Related to db1cbc5 and 9b679b7.

* Link against socket and nsl libs when building on SunOS

On OpenIndiana (SunOS) functions such as bind, socket, listen
require -lnsl -lsocket to be given to linker.

* Format

* Fixes float encoding/decoding for both big and little endian (fixes stephane#665, stephane#694)

* Removes useless memcpy calls and no longer used swap32 and swap16 macros.
* Updated float encoding functions to consider endianess of 16-bit registers (fixes stephane#665)
* Deprecated modbus_get_float() and modbus_set_float() are implemented using cdab variants.

* Duplicate in .clabot

* Revert "Fixes float encoding/decoding for both big and little endian (fixes stephane#665, stephane#694)"

This reverts commit 13bd584.

* Remove useless cast in setsockopt call (closes stephane#721)

* Update CLA

* Fix insecure data handling

CID 416366: INTEGER_OVERFLOW found with Coverity Scan.

* Add Coverity scan status

* Remove duplicate warning flag

* Check length passed to modbus_reply (write_bit)

The modbus_reply function is designed to receive arguments
from modbus_receive. This patch avoid a wrong use of memcpy if
the user chooses to inject a bad length argument.

Thank you Nozomi Networks Labs Advisory for the report.

* Check length passed to modbus_reply (write_register)

Related to df79a02.

* Small cleanups of unit test server

* Rename warning flags variable

* Do not build with debug flag active by default

* Remove useless configure checks

* Update .clabot

* Fix request length check in modbus_reply in RTU

- rename internal *_prepare_response_tid to *_get_response_tid
- change signature, don't need req length anymore
- remove misleading modification of req_length
- check of req length before use in memcpy for mask write register

Related to df79a02

* Revert "Fix float endianness issue on big endian architecture." and fix test suite and setter functions.

This reverts commit 49af73d.

* Prepare NEWS file for next release

* Use port 502 instead of 1502 in documentation (closes stephane#771)

The default port number of Modbus is 502 but it requires privileges on
Linux.

* HAVE_NETINET_IN_H as guard around header (stephane#765)

Thank you @diplfranzhoepfinger

* RTU - Check CRC before filtering on slave ID

* Minor

* Bump version to 3.1.11

* modbus-tcp.c doesn't include config.h, possible workaround - defining HAVE_NETINET_IP_H directly in modbus-tcp.c for Linux builds to force including <netinet/ip.h> which provides IPTOS_LOWDELAY definition

---------

Signed-off-by: José Bollo <[email protected]>
Signed-off-by: Michael Heimpold <[email protected]>
Co-authored-by: Stéphane Raimbault <[email protected]>
Co-authored-by: José Bollo <[email protected]>
Co-authored-by: Tim Gates <[email protected]>
Co-authored-by: Anton Bondarev <[email protected]>
Co-authored-by: Richard Ash <[email protected]>
Co-authored-by: Stefan Nilsson <[email protected]>
Co-authored-by: Marc Haber <[email protected]>
Co-authored-by: Michael Heimpold <[email protected]>
Co-authored-by: Rick M <[email protected]>
Co-authored-by: begasus <[email protected]>
Co-authored-by: SZ Lin (林上智) <[email protected]>
Co-authored-by: Mochamad Arifin <[email protected]>
Co-authored-by: Pascal Bach <[email protected]>
Co-authored-by: Heinrich Gsponer <[email protected]>
Co-authored-by: along <[email protected]>
Co-authored-by: Stéphane Raimbault <[email protected]>
Co-authored-by: Mohamed Amine Mzoughi <[email protected]>
Co-authored-by: Hans Ulrich Niedermann <[email protected]>
Co-authored-by: Jordan Johnson <[email protected]>
Co-authored-by: Alexander Polleti <[email protected]>
Co-authored-by: David Korczynski <[email protected]>
Co-authored-by: 20162026 <[email protected]>
Co-authored-by: Yau Yimsing <[email protected]>
Co-authored-by: Cedric Boudinet <[email protected]>
Co-authored-by: Lauri Nurmi <[email protected]>
Co-authored-by: Andreas Nicolai <[email protected]>
Co-authored-by: Andreas Nicolai <[email protected]>
Co-authored-by: diplfranzhoepfinger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants