isfand yar
Raspberry Pi and Rs485 Modbus
4 min readApr 14, 2021

--

MODBUS RS485 Raspberry Pi

INTRODUCTION TO Rs485/Modbus RTU:

RS-485 is an asynchronous serial communication protocol which doesn’t not require clock. It uses a technique called differential signal to transfer binary data from one device to another.

Differential signal method works by creating a differential voltage by using a positive and negative 5V. It provides a Half-Duplex communication when using two wires and Full-Duplex requires 4 fours wires.

Data Packet of Rs485:

Modbus RTU encodes data as binary and uses big-endian encoding for 16-bit values. This means that the most significant byte of a 16-bit word is sent first.

Lets suppose that we want to read register 1 of our modbus slave
our data packet will look like this

slave id 01function code 03address of first register to read (2 bytes) 00 01number of registers to read 00 01checksum(2bytes) D5 CA01 03 00 01 00 01 D5 CAThe reply from slave will look like thisSlave id 01Function code 03number of bytes of data 02the value of register (2bytes) 00 01checksum (2bytes) 25 CA01 03 02 00 01 25 CA

Raspberry Pi Rs485 Modbus:

Now we will see how to do rs485 Modbus protocol in raspberry pi without use of any library of Modbus
What we will use instead is rs485 section of serial python library.

Components required:
1: Raspberry pi raspbian installed

2: Usb to rs485

3: Max485 ttl to rs485

4:pymodbus slave software: https://sourceforge.net/projects/pymodslave/

Max485 module

Wiring:

Max485 to raspberry pi:

Make sure DE and RE of max485 are shorted together

MAX485 →Raspberry PiDI →GPIO14DE RE →GPIO4R0 →GPIO15(RX)VCC →5VGND →GNDUsb rs485 with max485:Usb485 →Max485A →AB →B

Programming Raspberry Pi as Master using Python

Enabling the UART (Serial Port) pins in Raspberry Pi: only bold

Before using UART pins in Raspberry Pi, it needs to be enabled. Follow the steps below to enable the UART (Serial) Pins in Raspberry Pi board.

1. Open a terminal and type sudo raspi-config

2. Select Interfacing options

3. And then select serial

4. Then click on ‘No’ (This is used to disable Linux UART console)

5. After that exit the raspi-config

6. Reboot the Pi

Now Serial port is ready to be used.

Open Terminal and install these libraries:

sudo apt-get update 
sudo apt-get install python-pip
sudo apt-get install python-pil
sudo pip install RPi.GPIO
sudo apt-get install python-serial
sudo pip install serial
sudo pip install pyserial

Copy the following code to python file on raspberry pi.

import serial 
import serial.rs485
import time
import RPi.GPIO as GPIO


TXDEN_1=7 # transmit enable pin

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(TXDEN_1, GPIO.OUT, initial=GPIO.HIGH)


ser=serial.rs485.RS485(port=’/dev/ttySC0',baudrate=9600,timeout=5,parity=serial.PARITY_EVEN)
ser.rs485_mode = serial.rs485.RS485Settings(rts_level_for_tx=False,
rts_level_for_rx=False,
delay_before_tx=0.0,
delay_before_rx=-0.0)

SendFrame =b’\x01\x03\x00\x02\x00\x01\x25\xCA’ # data is in hex format in python since rs485 rtu uses hex encoded data


while True:
GPIO.output(TXDEN_1, GPIO.HIGH) #write enabled for sending data frame to read the register
ser.write(SendFrame) #sending data frame
GPIO.output(TXDEN_1, GPIO.LOW) #read enabled to get reply from pymodbus slave software
coming_data = ser.inWaiting() #checking buffer with data available
print “comming_data:”,coming_data # if no data is available comming data will be equal to 0
x=ser.read(ser.inWaiting()) #reading the actual data from pymodbus slave
print repr(x)# printing in hex format
print “ok”
time.sleep(2)

Connect usb to rs485 to your pc or laptop

Open pymodbus slave software on your pc or laptop

go to option modbus rtu and select following settings:

click on holding register tab and make sure hex is selected:

Click on connect and you will see registers

Write any value on address 1

Make connection of max485 with raspberry pi as shown in table above
also make connection of usb and max485 as shown in table above

After all the connections are made run the python file

your output should look something like this:

--

--