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

garysdickinson

  • Expert Member
  • Posts: 443
  • Old PLC Coder
    • View Profile
User written Modbus RTU example code
« on: June 12, 2019, 12:22:29 AM »
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 08, 2019, 05:25:58 AM by Forum Admin »

garysdickinson

  • Expert Member
  • Posts: 443
  • Old PLC Coder
    • View Profile
Slave PLC code for test
« Reply #1 on: June 12, 2019, 12:28:35 AM »
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, 05:27:59 PM by garysdickinson »

garysdickinson

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

support

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

garysdickinson

  • Expert Member
  • Posts: 443
  • Old PLC Coder
    • View Profile
Re:User written Modbus RTU example code
« Reply #4 on: June 19, 2019, 05:45:06 PM »
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