Skip to content
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

Modbus RTU library #36

Closed
smartynov opened this issue Jan 9, 2020 · 6 comments · Fixed by #53
Closed

Modbus RTU library #36

smartynov opened this issue Jan 9, 2020 · 6 comments · Fixed by #53

Comments

@smartynov
Copy link

I would like to implement a control over several Modbus RTU modules in Controllino.

This repository gives some examples, but they are somewhat outdated and even dangerous. For example, the following code found in ModbusRtu.h

        while ( port->available() )
        {
            au8Buffer[ u8BufferSize ] = port->read();
            u8BufferSize ++;

            if (u8BufferSize >= MAX_BUFFER) bBuffOverflow = true;
        }

...doesn't stop on buffer overflow and may just spoil memory beyond 64-byte au8Buffer occasionally. I believe such mistakes should never occur in a code for an industrial-grade PLC.

I found a few other libraries, however they will not work directly because of the non-standard pins.

Can you recommend any Modbus RTU library that works well with Controllino?

If there is none, probably it is a good idea to write one and include for all Controllino users. I'm ready to contribute to the code.

@CONTROLLINO-Support
Copy link
Member

CONTROLLINO-Support commented Aug 10, 2020

Hello, thank you for the note about the potentialy dangerous code. But we have just taken some existing Modbus RTU library and prepared an example how to use it. The main reason is just to demonstrate that the CONTROLLINO is ready for the Modbus RTU from the hardware point of view. For sure it is not a software solution to be directly used for some industrial application.
Do you have any progress here, please? Have you found a better library you can recommend to other CONTROLLINO users? Thanks!

@paolocavagnolo
Copy link

paolocavagnolo commented Nov 14, 2020

Hi,
first time working with MODBUS RTU, so please be patient.

I finally succeed in using a Controllino MEGA as SLAVE, i'm checking it with QModMaster with a USB-RS485 interface.

After trying to implement the Controllino_RS485_pins on different libraries I ended up with this one.

The changes I've made are all in the modbusSlave.cpp, modbusSlave.cpp.zip:

  1. add #include <Controllino.h>
  2. change Serial in Serial3
  3. set the receiver mode
    digitalWrite(CONTROLLINO_RS485_nRE, LOW); digitalWrite(CONTROLLINO_RS485_DE, LOW);
    in two point: in checkSerial and of course in serialRx
  4. set the transmission mode
    digitalWrite(CONTROLLINO_RS485_nRE, HIGH); digitalWrite(CONTROLLINO_RS485_DE, HIGH);
    in run,
  5. and also a Serial3.flush after the transmission

The working arduino code is the following: https://gist.github.com/paolocavagnolo/e3c50ca65e479a7f72b06fcc18a16e36

Tested in both read coil and write coil.

**** FEW DAYS LATER ***

After some tests I didn't like that library very much, so I returned back to "ArduinoMODBUS" and just edited the last line of RS485.cpp by substituting with:

RS485Class RS485(Serial3, CONTROLLINO_RS485_TX, CONTROLLINO_RS485_DE, CONTROLLINO_RS485_nRE);
and adding #include <Controllino.h> at the beginnig.

As explained here

Now I'm totally satisfied! (always testing it with QModMaster)

@CONTROLLINO-Support
Copy link
Member

Hi Paolo,

well done! 👍

Thank you very much for sharing this! 👏

Lukas

@MosiMiso
Copy link

MosiMiso commented Dec 4, 2020

Hi Controllino Team,
I am going to use a Controllino MAXI as a Modbus Master to read in a sensor.
Because you wrote the library is not intended to use for industrial solutions, i am wondering if you can provide a library for Modbus RTU Master?
The soution Paolo mentened is for Modbus Slave as far as i saw.

Would be great, if you can provide a working Modbus library for master and slave for Controllino.
Michael

@GregDavid123
Copy link

GregDavid123 commented Oct 19, 2021

Hi @CONTROLLINO-Support @MosiMiso @paolocavagnolo ,

I will also like to use the controllino MEGA as a Master to communicate with a frequency inverter.
please I will therefore like to know if you already found a suitable libary that can be used. Especially as it will have to modify the Modbus Serial to SERIAL_8E1

@GregDavid123
Copy link

GregDavid123 commented Oct 20, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants