MODBUS on the Pi

First of all, I would like to point out that MODBUS is a 30+ yr old protocol that completely sucks by todays standards. However, since virtually all the industry in the world still uses it, here we go…

The purpose of this article is to show how you can use a Raspberry Pi to communicate over the MODBUS protocol so that it can be used like an industrial controller. I am using it to build a simple home security and automation system.

This article is getting very long, so I have broken it up into pages.

Part 1 Compiling MODBUS on the Pi a very basic example to get you started.

Part 2 Adding functionality to make it more useful, emulating some larger data structures from existing MODBUS devices.

Part 3 Interfacing with IO’s.

Part 4 Creating a MODBUS Server Application to communicate with the Pi. (Not started yet…)

11 thoughts on “MODBUS on the Pi

  1. Thank you very much for sharing, I appreciate it very much and I encourage you to continue with point 4 mod bus server application, hope is on the web.
    Greetings and thanks

  2. i would like to learn, how could i develop a device to listen modbus.
    can you please suggest me something.
    and you also have said “MODBUS is a 30+ yr old protocol that completely sucks by todays standards”, i am also willing to know what are latest industrial communication protocols, where i can get the latest protocols update news.
    i am newbie in this line, and also wanna learn these things..
    please reply
    thank you

    • I will be posting code for a dedicated MODBUS client in c++ soon. With regards to why MODBUS sucks, it is a very bandwidth inefficient protocol with very limited packet sizes and does not support push notifications. When I used to work with them they were extremely prone to data corruption and loss compared to other protocols, and had far greater bandwidth costs as well (4 times greater!). There are no new protocols that I know of other than ones I have helped create (but that are not publicly available, sorry).

  3. This tutorial has been very helpful for me, it is exactly what I am doing in a project and if you post how to read the GPIO port and put that data into the mb_mapping would be absolutely amazing! Thank you very much!

  4. Very interesting. I also look forward to seeing the next installment.

    Also, would you be so kind as to post the code for part 2 ? I’m somewhat uncertain about where to place the calls to the two new functions into the part 1 code.

    Thanks !

  5. Hi, I am working on a microcontroller project which will have an option to listen in Modbus RTU mode. I am currently implementing this and will use information from this post to help me send test data to the UART. Any input on this is welcome. I am happy to share my findings if there is any interest.

  6. Hello, i’d like to thank u for the first tutorial, ithelped me a lot in implementing the modbus server.
    I will be grateful if you post the third tutoriel concerning the GPIO .
    I’am working right now on how to get the state of a led and sending it to modbus client.Thanks in advance.

    • Hi, I will start working on the 3rd part of the post this week. I did not finish it because I did not think anyone was interested, but lately I have gotten quite a few requests to finish it up. I am glad that you found it helpful!

  7. I would love to see the code you wrote for part 3. I’ve been able to do everything up to that point, and I have the ability to change GPIO, I’m just wondering how to incorporate libmodbus so that it toggle GPIO.

Comments are closed.