Raspberry PI connect to RS485

Hi everyone, due to the coronavirus, I have to work at home for my project, which is adapted from my co-worker.

One part of the project is interfacing as: Raspberry Pi 3 <-> RS485 Converter <-> speed controllers -> fans

We use the RS485 Converter as:

On my Pi, I'm using Nodered (Nodejs v12.16.1). The starting flow is:

Start Node-RED
Once Node-RED has started, point a browser at
On Pi Node-RED works better with the Firefox or Chrome browser
Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot
To find more nodes and example flows - go to http://flows.nodered.org
Starting as a systemd service.
Started Node-RED graphical event wiring tool.
9 Apr 15:54:39 - [info]
Welcome to Node-RED
9 Apr 15:54:39 - [info] Node-RED version: v1.0.4
9 Apr 15:54:39 - [info] Node.js  version: v12.16.1
9 Apr 15:54:39 - [info] Linux 4.19.66-v7+ arm LE
9 Apr 15:54:40 - [info] Loading palette nodes
9 Apr 15:54:44 - [info] Dashboard version 2.19.4 started at /ui
9 Apr 15:54:44 - [info] Settings file  : /home/pi/.node-red/settings.js
9 Apr 15:54:44 - [info] Context store  : 'default' [module=memory]
9 Apr 15:54:44 - [info] User directory : /home/pi/.node-red
9 Apr 15:54:44 - [warn] Projects disabled : editorTheme.projects.enabled=false
9 Apr 15:54:44 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi.json
9 Apr 15:54:44 - [info] Server now running at
9 Apr 15:54:44 - [warn]
Your flow credentials file is encrypted using a system-generated key.
If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.
You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
9 Apr 15:54:44 - [info] Starting flows
9 Apr 15:54:47 - [info] Started flows
9 Apr 15:54:47 - [info] serial port /dev/ttyUSB1 opened at 115200 baud 8N1

Now I'm not sure how I should be able to talk to the speed controller via RS485. I got confused between the:
node-red-contrib-serial-modbus, and the
I do not know which one to use. But the last one seems most suitable for my project, so I tried to install it but failed. It said
`Failed to install: node-red-contrib-serialport-rs485

Install failed

Check the log for more information`

This is the log:

I'm working via realVNC installed in the PI, so I'm not really able to test each method. With the three libraries above, can anyone suggest which one should I use? and if possible, how could I test them in my case? My goal is to turn on/off the fans as well as able to adjust their speed.

Thank you in advance for any help.
I'll provide more info if anyone needs it.

Did you try a forun search using RS485?

Also why do you VNC into the pi? If the Pi is at home with you on the same network, you can just you the browser on your computer and point it at the IP of the Pi (unless I missed something and the Pi is at work)

Yes, the Pi is at workplace, and i have to work at home. But that’s not the big deal, via VNC it’s basically the same.

I have tried installing RS485 but failed. You can read back to see the error and the whole log that said it cannot be installed. I dont know how to fix that for now.

Did you look at the other threads on the forum - is search in RS485?

to let you knw I do not have that devide and have never used it jst trying to point you where others have used/tried to use the device.

Of course, I have searched before opening a new topic. I found three libraries above that I may use. But I don't know which one is the best for my project (in terms of effective usage and memory).
It kinds of hard for me because I can not see whether the fan is turned on/off (as I'm working from home), so I do not know how to test it using each different library. And I have also never using Modbus before.
Thank you for your response anyway.

Bond, first thing we need to know is what is the protocol of speed controllers Modbus (RTU/ASCII) or some simple ASCII/HEX control charters?
Can you share a manual? or lin to ?

I use a RS485 adapter and use use the standard node-red serial in/out nodes.
If your using ASCII control characters it's very easy.
If your using Hex then best use a node buffer.

Hi iiLaw, thank you for your response.
I'm using a RTU on the speed controllers Modbus. My co-worker did not provide me the manual so i think i have to figure it out.
I'm using the adapter too, i did not think the standard node-red serial in/out would work. But as you say so i can try that too.So what command do you send to be able to talk to the controllers?


In my case I'm not using RTU but devices that use RS485
That said I can capture RTU msg I use a timeout of 10ms and deliver binary buffers

I have used modcr to play with creating RTU CRC
But you have to install via npm and then refernce it in a function node to use


I think the following log tell me that i have installed the modbuscrc:

pi@raspberrypi:~/.node-red $ npm i modbuscrc -S
npm WARN node-red-project@0.0.1 No repository field.
npm WARN node-red-project@0.0.1 No license field.

+ modbuscrc@1.0.4
added 1 package from 1 contributor and audited 884 packages in 8.025s

3 packages are looking for funding
  run `npm fund` for details

found 13 low severity vulnerabilities
  run `npm audit fix` to fix them, or `npm audit` for details

I'm not too sure how to talk to the RS485 using your link. I'll spend some time to figure that link out.
Thank you.

A few years ago I posted a flow that works with a Danfoss variable speed drive. I used node-red-contrib-modbus and did take some time but worked well. It is necessary to have detailed information of the modbus registers in the speed controller and the RS485 protocol settings.
The flow is posted here


@Bond I've just tested with A RTU slave simulator.
Code could be more concise but it works

[{"id":"e8d88969.351038","type":"debug","z":"a1f9c381.b10a2","name":"read req","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":540,"y":110,"wires":[]},{"id":"2b000f7e.589df","type":"serial in","z":"a1f9c381.b10a2","name":"","serial":"4ff7cd16.12af44","x":150,"y":110,"wires":[["e8d88969.351038"]]},{"id":"b31493c5.0cbb8","type":"serial out","z":"a1f9c381.b10a2","name":"","serial":"4ff7cd16.12af44","x":530,"y":210,"wires":[]},{"id":"42ec7de5.0f48d4","type":"inject","z":"a1f9c381.b10a2","name":"write slave 0x22","topic":"","payload":"020600020022","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":210,"wires":[["27db8126.cd7b9e"]]},{"id":"27db8126.cd7b9e","type":"function","z":"a1f9c381.b10a2","name":"hex & crc","func":"// master one word read (FC03) to slave 02:  020300020001\n// 0203 0002              0001\n//      | reg add 40003   | read 1 word\n//\n// master one word write (FC06) to slave 02: 020600020020\n// 0206 0002                0020\n//      | reg add 40003   | value 0x20 =  33 dec\n\nvar modcrc = global.get('modcr');\nvar modCmd = msg.payload;\nvar crc16 = modcrc(modCmd.toString(16));\nvar modMsg = modCmd+crc16;\nmsg.payload= Buffer.from(modMsg, 'hex');\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":210,"wires":[["b31493c5.0cbb8","e95ccd04.21b8f"]]},{"id":"e95ccd04.21b8f","type":"debug","z":"a1f9c381.b10a2","name":"read/write","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":520,"y":258,"wires":[]},{"id":"305f2589.b4db3a","type":"comment","z":"a1f9c381.b10a2","name":"maser/slave","info":"","x":110,"y":60,"wires":[]},{"id":"dfb81a04.f9f6c8","type":"inject","z":"a1f9c381.b10a2","name":"read slave","topic":"","payload":"020300020001","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":170,"wires":[["27db8126.cd7b9e"]]},{"id":"9f38db04.b58f08","type":"inject","z":"a1f9c381.b10a2","name":"write slave 0x11","topic":"","payload":"020600020011","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":250,"wires":[["27db8126.cd7b9e"]]},{"id":"4ff7cd16.12af44","type":"serial-port","z":"","serialport":"/dev/serial0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"10","bin":"bin","out":"time","addchar":"","responsetimeout":"5"}]

I've used lots of modbus devices over RS-485, and mtoko and iiLaw are correct you will need to know all of the details about the modbus registers as well as the RS485 settings for the fan speed controllers. Without those settings all the libraries in the world won't help because you won't know how to set up the connection. If you're just missing a couple of the details you can try all the options until you find one that works, but otherwise you're just guessing in the dark. Try and track down the model number of the speed controller and then see if you can't find a programming manual online which defines baudrate, stopbit, parity, bytesize, etc, and which register you want to see the value for.

are you sure that the usb / rs485 converter you are using supports the ModbusRTU protocol?

I found in the old document say that the controller we're using is: Powertran PI9130A 004G3. But I'm not able to test or simulate them yet.
@rromele Yes, I'm very sure that the USB converter supports the Modbus RTU.

Just so we are clear
USB to RS485 is an Electrical conversion
From Universal Serial Bus to RS485

Modbus RTU is a protocol than can even run it over an medium. :slight_smile:

After many attempts using different methods. I still can not read the status of the controller.
The following data shows what the setting in the Modbus RTU should be:
Data Sheet

Thank you for all the help so far but I cannot really talk to the controller using NodeRed at the moment. Can anyone tell me where to start with?

This is the python code that used to work for the controller: Code
I'm trying to adapt this code to nodered.
Any ideal?

@Bond This can be frustrating, just some comments
Does the python code still work? If it does (or even partially) this would give you some clues. I notice in the python code there is a setting for connection to an MQTT broker, if this code works you could setup a broker in Node Red and communicate through MQTT using the working python code.
The python code uses the minimalmodbus python library and only sets the baud rate so the remaining serial settings should be default based on the minimalmodbus docs which is 8N1 so in the modbus config you should try serial expert and set the config to 8 data bits none parity and 1 stop bit.
I used a modbus scanner to initially get the communications working and then scanned the likely registers to read some parameters. This enabled me to fault find towards a solution. The scanner is here

1 Like

@mtoko thank you for your comments
I have checked out the Modbus scanner. Unfortunately, it cannot be installed on Raspberry PI (or just because i don't know how to).
The python code should be working. However the project requirement stated that we do not need to use MQTT, and need to adapt everything from that python code to a browser-based editor, which is Nodered.

I have a lot of experience with using the FTDI rs485 cables to talk to yaskawa ac drives. I like the node-red-contrib-modbus package myself. It's very simple and has a lot of tweakables.

In your case, it looks like you should have everything you need in your python file to make this work. The biggest thing that messes people up with serial connections is the wiring. Does this controller have separate rs232 and rs485 ports? Most don't. On yaskawa drives, one must jumper the S+ to R+ and S- to R- for rs485, since it uses a differential signal. Also, don't forget the commond wire, which should bond both the Pi and the controller to the same ground potential.