Plain simple modbus master / slave -demo-code

Hello everybody,

I have looked up multiple modbus-libraries on Github and read the example-codes.

Well until now I did not find an example that is plain simple to use.
I want to describe what I define as "plain simple to use":

  • Has an example-code where the master-code directly fits to a given slave-code
    which means
    use serial1 for the data-exchange
    flash master-code into arduino mega 1
    flash slave-code into arduino mega 2

connect both Arduino Megas over serial1-io-pins
serial monitor shows datatransfer in the sense of
at least a single bit in the data is toggling and the serial printing shows the changing value

or
having two buttons and two leds connected to the master
having a button and a led connected to the slave1
having a button and a led connected to the slave2

whenever the button1 of the master is pressed the LED of the slave1 lights up
whenever the button2 of the master is pressed the LED of the slave2 lights up

whenever the button of the slave1 is pressed the LED 1 of the master lights up
whenever the button of the slave2 is pressed the LED 2 of the master lights up

I am not addicted to use modbus RTU
if there exists a different library that does In one sentence:

send/receive bytewise data up to 128 bytes with a serial interface as the hardware
and using a protocol that enables to have multiple slaves I will use it.

My quick search for Arduino Modbus libraries on GitHub produced at least half a dozen different ones. Are you saying that in all the example codes for all those libraries, you didn't find a single one that meets your needs? Not even one that could be modified to meet your needs?

Did you try simple rs485 library by Nick Gammon?

I am saying that the libs that I did find did not have

If "modifying" the sketch includes: learn all the details about this particular modbus-lib what each and every single parameter of each and every line of code that configures the bus.

That is my experience with many libraries. Emphasizing
learn all the details about the particular paremeters what each and every single parameter of each and every line of code that configures the bus to make it work.

I am asking for an either a very well documented demo-code which means the demo-code explains each and every single parameter in the function calls

or

A set of two / three demo-codes for master / slave that fit to each other 100.00%
do the hardware wiring, flash => runs

No example-code at all

No comments that would explain the parameters

The simple-master-comment says

well this is what all my computers have installed and I am familiar with
(ironic - no of course not!)

master-example shows all functions
This program use 8 modbus functions and shows how to use it in loop.

slave-code
This example shows how to use ModbusSlaveRTU object in pair with ModbusMemory object.

what is the ModBusMemory-Object ?? sounds like something extra = extra work an time to find out. master and slave-code seem not to match each other

This sketch creates a Modbus RTU Client and demonstrates
how to use various Modbus Client APIs.
This sketch creates a Modbus RTU Server and demonstrates
how to use various Modbus Server APIs.

Now what is a client and server in case of modbus?? I thought it is master/slave?
is it the same?
Do the demo-codes fit to eacher? Seems not to be so

This is all that the slaves demo-code contains

void loop() {
  mb.task();
  yield();
}

??? So again the situation of analysing the library itself
It is called ESP8266-modbusRTU - not very promising I am looking for non-ESP-modbus-code

the name says it ModbusRTUSlave
no code for a modbus-master included
Additionally the repo says

Warning

ModbusRTUSlave is currently undergoing a redesign. Things may not work, or may not be corretly documented.

Did you find another one? If yes please post the link.

Does anybody use his own microcontrollers
which means both modbus-master and modbus-slave

Check your Google. Wikipeda has an explanation.

This pair is apparently written by the same author:
https://github.com/CMB27/ModbusRTUMaster
https://github.com/CMB27/ModbusRTUSlave

For any library, if you find the documentation inadequate, the best advise is to start digging into its source code. Start with the interface which is typically specified in a .h file. The source code is, after all, the definitive explanation.

Hi @gfvalvo

thank you very much for the link
indeed even the code-comment says

ModbusRTUSlaveExample

  This example demonstrates how to setup and use the ModbusRTUSlave library (https://github.com/CMB27/ModbusRTUSlave).
  It is intended to be used with a second board 
  running ModbusRTUMasterExample from 
  the ModbusRTUMaster library (https://github.com/CMB27/ModbusRTUMaster).

Hi @StefanL38
I have just published a NonBlockingModbusMaster library with a detailed tutorial
How to use NonBlockingModbusMaster Library

No slave support :frowning: so may not be a complete solution for you, but does have UNO based software serial examples.

The NonBlockingModbusMaster library has the following features:-

– With suitably sized serial TX and RX buffers, it is completely non-blocking.
– No callback methods are necessary for single command non-blocking use.
– Uses RTU modbus and works with any Arduino Stream object
– Supports multiple device IDs.
– Has a simple command chaining facility to group commands together in a single executable non-blocking block.
– Has a retry() method for use when the command times out (or other some other error occurs)
– Has oneTimeDelay() method to insert an extra pre-command delay. e.g. when switching between device ID's
– Supports optional results processing functions that are called when the command completes.
– Runs on UNO with software serial while maintaining responsive Serial and other loop() processing.

I am stuck with the same problem. Among all these libraries, is there any specific library that supports STM32 chips (F070RBT6 one that I am using) using STM core in the Arduino IDE.