The flow based visual programming tool Node-RED becomes more and more popular for Raspberry Pi developers. This instructable will show you how to use our isolated RS422 / RS485 Serial HAT under Node-Red for simple RS485 communication and for MODBUS applications too.
Materials:
Software:
recommended software)
The easiest way is to use the raspi-config tool to switch the UART to the GPIO14/15 pins. take a fresh Raspbian image
Now you can access the UART via /dev/serial0
Our RS422/RS485 HAT comes with 3 DIP switch banks. You have to set these DIP switches for RS485 as shown in the picture above.
*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
Start Node-RED:
Node-RED is part of Raspbian Stretch and Buster (with desktop and recommended software). You can use the node-red command to run Node-RED in a terminal or on the desktop via the 'Programming' menu.
Open the editor:
Once Node-RED is running you can access the editor in a browser. If you are using the browser on the Pi desktop, you can open the address: http://localhost:1880.
In this example flow the Raspberry Pi will send the text 'Hello World' via the RS485 after pressing the inject button. The flow will receive incoming strings (terminated by \d) and show the string in the debugging window on the right side.
The communication will be realised by using the serial in and out nodes, which are pre installed. It is very important to set the properties of the Serial Port to /dev/serial0 as in the picture above.
You can test the flow with a connected PC (via an USB to RS485 adaptor) and a simple terminal program.
In the following steps I want to show you how to implement a simple Modbus RTU communication under Node-RED.
First we have to install additional Modbus nodes node-red-contrib-modbus via the palette manager or on the bash by entering:
npm install node-red-contrib-modbus
Now you can import the flow.
For the test I've connected an Arduino with RS485 Shield as Modbus slave (you can check this instructable for more information).
Modbus Read will poll Unit 1 all 2s and read 8 registers of the slave. You can see the result in the status of Modbus Response. Via the 2 injectors you can set the register 6 of the slave to 0 or 255.