Skip to content

ESP32 Support #98

Open
Open
@tinkering4fun

Description

@tinkering4fun
Contributor

Hi Yaacov,
thanks for this great Modbus Slave library (the best I found so far for building a Modbus RTU Slave).

While using the code on ESP32 I found a minor regression from issue #97:
The signature of the callback functions has changed, while the examples do not yet adopt to it.
Easy to fix, just append the missing , void *context in the signature of the example's callback functions.
(Btw. When compiling for AVR there is no error on compile time, but I didn't try to run the code on AVR.)

Besides this, I would suggest another small fix in ModbusSlave.cpp:
The class constructor should initialize all cbVector[] entries with NULL.
To be honest, I'm currently unsure about the best single (?) place in the constructor(s) where to do it.
Without the init the slave crashes when a Modbus request is received, with a FC where no appropriate callback vector has been set for.
Because the if(callback) in the code snippet below does not evaluate to false for this case, maybe this is also just a ESP32 issue.

860: uint8_t Modbus::executeCallback() {
...
865: ModbusCallback callback = _slaves[i].cbVector[callbackIndex];
...
868: if (callback)
...
875: if (callback)

What do you think about official ESP32 support for your code?
The benefit with ESP32 as Modbus RTU Slave is imho the 2nd hardware Serial port available (and of course some projects may also benefit from WiFi etc.).
See my working ESP32 Slave example code below (derived from simple.ino).
Requires the trivial fix in ModbusSlave.cpp desribed above.

Kind regards
Werner

simpleESP32.ino.txt

edit:
As an absolute beginner at GitHub, I just try to follow this documentation https://docs.github.com/en/get-started/quickstart/contributing-to-projects ...
Did a fork, and pushed my changes in branch ESP32, hope it helps.

Activity

yaacov

yaacov commented on Feb 5, 2023

@yaacov
Owner

Thank you for the issue

tinkering4fun

tinkering4fun commented on Feb 18, 2023

@tinkering4fun
ContributorAuthor

Update: I try to re-sort my fork into different branches ...

  • issue98: is the fix where this issue is about.

The rest goes way beyond this ...

  • kernel: A class which already provides some which I would expect from any of my workable slaves.
    The idea is to have a consistent set of kernel functionality and code the dedicated application functions in a derived class.

  • kernel-examples: Examples using the above concept.

  • allnew: Merging all of the above

  • EP32: Will finally only contain the addition in library.properties file. ESP32 architecture is currently not listed, but the library seems to work.

Erfan12699

Erfan12699 commented on Jun 20, 2023

@Erfan12699

hi Werner
thanks for good issue.
please tag simpleESP32.ino.
text is not correct to open.

tinkering4fun

tinkering4fun commented on Jun 20, 2023

@tinkering4fun
ContributorAuthor

Hi Erfan, sorry for not exactly understanding your message ...
Do you have issues when trying to open attached simpleESP32.ino.txt ?
Of course it needs to be renamed after download. I took the .txt naming by intention to get only a quick preview in text editor when clicking on it (avoid immediate start of Arduino IDE). As mentioned, I'm still a Github newbie ...

btw: In the meantime I re-organized my fork https://github.com/tinkering4fun/ArduinoModbusSlave-ESP32-fork
See branch 'specific-examples' and example 'plainSlave'

ArduinoModbusSlave-ESP32-fork/examples/specific/ESP32/plainSlave

Erfan12699

Erfan12699 commented on Jun 20, 2023

@Erfan12699

thanks dear Werner

I want to put an ESP32 module as a slave by Yaacov's library (Modbusslave.h) and read the analog data by Modbus pull in my pc.

can you help me?

Erfan12699

Erfan12699 commented on Jun 20, 2023

@Erfan12699

dear Werner.
Does this library (Modbusslave.h) work on STM32 (bule pill)?

tinkering4fun

tinkering4fun commented on Jun 21, 2023

@tinkering4fun
ContributorAuthor

Hi Erfan,
i guess my mentioned example ArduinoModbusSlave-ESP32-fork/examples/specific/ESP32/plainSlave
does exactly what you asked for ...?

See line 445
slave->writeRegisterToBuffer(i, analogRead(adc_pins[address + i]));

I assume you are already familiar with Modbus and have some Modbus client software at your PC-
If so, just use FC04 to read the input registers 0 and 1, which will return the values from pins ADC_0 and ADC_1.

There is also a straight-forward and simple example in Yaacov's lib
ArduinoModbusSlave/examples/simple

It is made for Arduino board, but the code should also run on ESP32 after changing the ADC pin definitions.
Just 150 lines of code, so it should be a good starter.

I never used STM32, if you own one, just give it a try and see ...

btw:
If you never used Modbus before, it may be too complex to learn for just just pulling ADC values into an PC.
Sorry, but I will not guide you to a final Modbus end to end solution for your specific problem.

Hope this helped to some extend.

Regards
Werner

Erfan12699

Erfan12699 commented on Jun 22, 2023

@Erfan12699

Thanks dear Werner.

yes, i used Yaacov's lib and read analog on Modbus pull.

i could use from Yaakov's library on ESP32 with your guide. very good issue.

thank you and dear Dr zemir

Regards
Erfan

added a commit that references this issue on Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @yaacov@tinkering4fun@Erfan12699

        Issue actions

          ESP32 Support · Issue #98 · yaacov/ArduinoModbusSlave