How to Use Modbus With Raspberry Pi

41,896

42

4

Modbus is a serial communication standard and has become a de facto standard communication protocol and is now a commonly available means of connecting industrial electronic devices. In Modbus RTU and Modbus ASCII RS485 is used as the physical layer. Its possible to use a Raspberry Pi as Master or Slave in Modbus applications, but a RS485 interface is needed. Our RS422 / RS485 Serial HAT is a fully galvanic isolated serial communication HAT designed for use with the Raspberry Pi and the perfect choice for such kind of applications.

The objective of this document is to show how to configure the HAT and the Raspberry Pi to be used for Modbus serial communication.

Check out our instructable about MODBUS & Arduino too!

Step 1: Bill of Material

Step 2: Wiring

The wiring is very simple. You have to connect only the A and B terminals of the HAT with the A and B line of the Modbus system. Y and Z terminals are not used for this kind of application. For long distances it is recommend to use twisted pairs for A and B.

Step 3: DIP Switch Setting

Our RS422/RS485 HAT comes with 3 DIP switch banks. You have to set these DIP switches for Modbus as shown in the picture above.
Switch 1: 1-OFF 2-ON 3-ON 4-OFF

Switch 2: 1-OFF 2-OFF 3-ON 4-ON

Switch 3: 1-OFF or ON* 2-OFF 3-OFF 4-OFF

*Depending of the position of the RS422/RS485 HAT in the Modbus line you have to switch the terminating resistor ON or OFF. Please switch the resistor to ON position only if the HAT is on one end of the bus line. In all other cases switch the terminating resistor OFF:

Step 4: Free Up Serial Line and Enable UART

First we have to config the Raspberry Pi:

sudo raspi-config

Goto -> 5 Interfacing Options -> P6 Serial -> choose YES

Additionally remove any references to console=serial0,115200and ttyAMA0 from /boot/cmdline.txt.

Our file looks like this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Update the Raspbian Jessie operating system now:

sudo apt-get update
sudo apt-get upgrade

On the Raspberry Pi 3 /dev/ttyAMA0 is used by the Wifi and Blue-
tooth module. There is a “mini UART” available on /dev/ttyS0 by default. It is better though, to use the hardware UART and switch the Wifi/Bluetooth module to mini UART. To do that, add this line at the end of /boot/cmdline.txt

dtoverlay=pi3-miniuart-bt
enable_uart=1

force_turbo=1

Reboot your Raspberry Pi

Step 5: Software

You will find an easy-to-use and tested Modbus RTU and Modbus
ASCII implementation for Python on:

http://minimalmodbus.readthedocs.io/en/master/inst...

http://minimalmodbus.readthedocs.io/en/master/usag...

Share

    Recommendations

    • 1 Hour Challenge

      1 Hour Challenge
    • Classroom Science Contest

      Classroom Science Contest
    • Pets Challenge

      Pets Challenge

    4 Discussions

    0
    None
    avandermeer

    2 months ago

    There is a major issue with the hat: the print on the pcb for the terminal block connector A and B should be switched; took me while to figure out, but found the issue when I looked into your schematics at https://www.hwhardsoft.de/app/download/11143497697/RPI_RS485+Schematic+V01-01-00.pdf?t=1551555753 . The print for pin 1 on the terminal should be B and for pin 2 should be A.

    Switching the cables (e.g. A to B and B to A) solved the issue for me.

    For reference, I have board version 01-01-00

    2 replies
    0
    None
    harty123avandermeer

    Reply 2 months ago

    Yes, you are right. There is a small issue in the printing. We will fix it in the next batch. The workaround for this is ver simple - interchanging of A and B wire will solve this issue.

    0
    None
    avandermeerharty123

    Reply 2 months ago

    Yes; it took me an hour to figure out, so maybe you can put a warning in the current manual or something to save time of other users :)

    Anyway, thanks for building the product, I find it very usefull!

    1
    None
    BjornR

    6 months ago

    This is nothing more than what's written in the designers' documentation.
    Perhaps you could add some example code or a video showing the thing working.