Author Topic: User written Modbus RTU example code  (Read 16670 times)

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
User written Modbus RTU example code
« on: June 11, 2019, 05:22:29 PM »
I am presenting the code that I have been using for the last 5 years as an alternative to the TBASIC ReadModbus, ReadMB2, WriteModbus and WriteMB2.

This code is supports all possible Modbus function codes.

This code is non-blocking. The PLC does not lock up waiting for a response from the slave device.

This code is an example of a user written communication protocol

This code allows you to see both the Modbus response packet and every byte received from the Modbus slave.  This allows you tremendous debug capability that is not available with the TBASIC Modbus support.

I will post the PLC code and the documentation in pieces to keep them under 200KB

Regards

Gary Dickinson
« Last Edit: August 07, 2019, 10:25:58 PM by Forum Admin »

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Slave PLC code for test
« Reply #1 on: June 11, 2019, 05:28:35 PM »
Variations of this Modbus RTU code is currently running 24/7 on over 40 PLCs.  Some of these installations date back 5 years.  This is why I am convinced that the code is reliable.

I extracted the Modbus RTU code out of running production code. I simplified this code for use as a demo.

I needed to test the demo code against a real Modbus RTU slave device. I decided to use a spare TRI PLC as the slave.  This choice solved my my need to test the code and provided an opportunity to explain how the low-level TRI handles Modbus RTU requests.

This posting is the PLC program that I am running on the slave PLC.  This code initializes the serial port on the slave and writes data patterns into DM[] and some of the PLC RELAYS.

The data patterns are used to help me verify that my code running on the master PLC is reading/writing the correct registers in the slave.

The slave PLC responds to the Modbus RTU requests using the TRI written firmware. My software on the slave PLC is NOT handling the Modbus requests.

Have fun,

Gary D*ckinson
« Last Edit: June 19, 2019, 10:27:59 AM by garysdickinson »

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Documentation
« Reply #2 on: June 11, 2019, 05:32:47 PM »
This is the PDF version of the documentation.
« Last Edit: June 12, 2019, 11:28:05 AM by garysdickinson »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:User written Modbus RTU example code
« Reply #3 on: June 19, 2019, 08:14:39 AM »
Thanks for the great contribution to the community!
« Last Edit: August 05, 2019, 05:54:47 PM by Forum Admin »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:User written Modbus RTU example code
« Reply #4 on: June 19, 2019, 10:45:06 AM »
Thanks,

I try to periodically post PLC software that illustrates an approach to PLC programming that solves a complicated problem without a lot of programming.

As always, I invite people to use this or any other examples that I have posted.

Best regards,

Gary D*ckinson