• sales

    +86-0755-88291180

Modbus-RTU-Relay-C User Guide

Overview

Electrical Safety Precautions

  • This product should be operated and used by professional electricians or technical personnel. During use, please ensure electrical safety and take protective measures such as anti-leakage and insulation.
  • Before installing, maintaining, or replacing relay equipment, make sure to turn off the power and unplug the device.
  • Do not attempt to dismantle relay equipment to avoid damaging the device or risking electric shock.
  • Please properly install and place the relay equipment. Avoid using it in damp, overly hot, or flammable environments to prevent safety accidents due to improper installation or use.

Specifications

Power SupplyDC 7~36V
Communication InterfaceRS485
Communication Baudrate4800,9600,19200,38400,57600,115200,128000,256000
Default Communication Format9600,N,8,1
Relay Channels8 Channels
Relay TypeMagnetic latching relay
Contact Form1NO 1NC
Contact Load≤10A 250V AC 或 ≤10A 28V DC
Modbus ProtocolStandard Modbus RTU protocol
RS485 Address Setting1~255
IndicatorSTA: MCU indicator, keep flashing when MCU normally working
TXD: Transmit Data indicator, lights up when sending data
RRXD: Receive Data indicator, lights up when receiving data

Precautions

  • The module comes out of the factory in the default state, but due to impacts during transportation or installation, its operational state may change. Therefore, it is necessary to reset the relay state when powering on as needed.
  • The magnetic latching relay will only supply power when the state is changed, and the relay is usually in a power-off state and only relies on magnetic attachment, so it should be avoided to vibrate the module strongly, and the relay state may be changed when it is impacted.
  • The module status information can be saved when powered off and restored when powered on. However, if the relay is powered off when the state is changed, the state may be uncertain, and the state may change when it is shocked, so it is recommended to reset the relay status as needed after each power-on.
  • During use, please keep the product away from strong magnetic fields to avoid changes in the operating voltage and reset voltage.

Hardware Connection

  • Connect the USB TO 485 to the target boards via cables, A-A and B-B connected as shown below:


Example Demonstration

The demo shows how the following two software operate.
SSCOM serial port debugging assistant is more convenient to operate, free of installation, and more convenient for complete display and analysis of instructions, but the disadvantage is that the data is not intuitive.
Modbus Poll software is directly operated on the register, and the data display is more convenient to observe, but the disadvantage is that the instruction is not displayed completely, so you need to be familiar with the Modbus register operation.
You can test using any method. It is recommended to use the SSCOM serial port debugging assistant software for the first test.

SSCOM Serial Port Debugging Assistant

  • Download SSCOM Serial port debugging assistant and open it on the computer, open the corresponding port number, and set the baud rate as 9600. Click Multi-Char to open the Send Multi-Char window, and click the function to send the corresponding command.



  • If you need to send other commands, choose SendHEX. For checksum validation, select ModbusCRC16. After entering the first six bytes of the command, clicking SEND will automatically add the CRC check code.


  • For detailed control commands, please see the development protocol.

Modbus Poll Software

  • The serial port software is not convenient to observe the data, you can choose Modbus Poll software to read the data. Download and install the Modbus Poll software.
  • Open the software, select Setup->Read/Write Definition, select the actual device address for Slave ID, select 01 Read Coils function code for Function, and change Quantity to 8 channels. Click OK to confirm.


  • Select Connection->Connect..., choose the corresponding serial port, set the baud rate to 9600, and select 8 Data bits and None Parity. Click OK to connect.


  • After the connection is normal, you can check the current relay status. Select the corresponding channel, then double-click the status value to pop up the send page. Choose On or Off, then Click Send to control the relay opening and closing.


Demo Test

Note: RS485 can not be directly connected to the serial port of the Raspberry Pi, otherwise it may burn the device, you need to add 485 level conversion. For Raspberry Pi, it is recommended to work with the RS485 CAN HAT module. For NUCLEO-F103RB and Arduino, it is recommended to work with the RS485 CAN Shield module.

Raspberry Pi

Open the Raspberry Pi terminal and enter the following command to enter the configuration interface

sudo raspi-config
Select Interfacing Options -> Serial, disable shell access, and enable the hardware serial port



Then restart Raspberry Pi:

sudo reboot

Open the /boot/config.txt file, find the following configuration statement to enable the serial port, if not, you can add it to the end of the file.

enable_uart=1

For Raspberry Pi 3B users, the serial port is used for Bluetooth and needs to be commented out:

#dtoverlay=pi3-miniuart-bt

Then restart Raspberry Pi:

sudo reboot

Insert the RS485 CAN HAT into the Raspberry Pi, and connect the Modbus RTU Relay module to RS485 CAN HAT through A and B.
If you are using other 485 devices, make sure to connect A-A, B-B.< br/> Run the following commands to run the demo:

sudo apt-get install unzip
wget https://files.waveshare.com/wiki/Modbus%20RTU%20Relay/Modbus_RTU_Relay_Code.zip
unzip Modbus_RTU_Relay_Code.zip
cd Modbus_RTU_Relay_Code/Python3
sudo python3 main.py

STM32

The STM32 demo is based on the NUCLEO-F103RB and RS485 CAN Shield module.
Find the STM32 demo file directory and open the STM32 project. Please note that the keil5 software is installed before use, and download the demo to the development board.
Normal operation of the relay module will sequentially open and then close the channels. The serial port will output the commands to be sent.

Arduino

The Arduino demo is based on the UNO PLUS and RS485 CAN Shield module.
Use the Arduino IDE to open the demo, select the corresponding development board, and download the demo.
Normal operation of the relay module will sequentially open and then close the channels. The serial port will output the commands to be sent.

Development Protocol

Function Code Introduction

Function CodeDescriptionNote
01Read coil statusRead relay status
03Read holding registerRead the address and version
05Write single coilWrite single relay
06Write single registerSet the baud rate and address
0FWrite multiple coilsWrite all relays

Register Address Introduction

Address (HEX)Address storage contentRegister valuePermissionModbus Function Code
0x0000
……
0x0007
Channel 1~8 relay address0xFF00: relay on
0x0000: relay off
0x5500: relay toggle
Read/Write0x01, 0x05, 0x0F
0x00FFControl all relays0xFF00: all relays on
0x0000: all relays off
0x5500: all relays toggle
Write0x05
0x0100
……
0x0107
Channel 1~8 relay toggle0xFF00: relay toggle
0x0000: relay unchanged
Write0x05, 0x0F
0x01FFAll relays toggle0xFF00: all relays toggle
0x0000: all relays unchanged
Write0x05
0x0200
……
0x0207
Channel 1~8 relay flash onInterval time: data*100ms
Value: 0x0007, Interval time: 7*100MS = 700MS
Write0x05
0x0400
……
0x0407
Channel 1~8 relay flash offInterval time: data*100ms
Value: 0x0007, Interval time: 7*100MS = 700MS
Write0x05
4x2000UART ParameterThe high eight bits indicate the parity mode: 0x00~0x02
The low eight bits indicate the baud rate mode: 0x00~0x07
Read/Write0x03, 0x06
4x4000Device AddressDirectly store Modbus address
Device address: 0x0001-0x00FF
Read/Write0x03, 0x06
4x8000Software VersionConverting to decimal and then shifting the decimal point two places to the left will represent the software version
0x0064 = 100 = V1.00
Read0x03

Modbus RTU Command Introduction

Control Single Relay

Send code: 01 05 00 00 FF 00 8C 3A

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0505 CommandRelay control
00 00AddressThe register address of the relay to be controlled, 0x0000-0x0007
FF 00Command0xFF00: relay on;
0x0000: relay off;
0x5500: relay toggle
8C 3ACRC16The CRC16 checksum of the first 6 bytes of data

Return code: 01 05 00 00 FF 00 8C 3A

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0505 CommandRelay control
00 00AddressThe register address of the relay to be controlled, 0x0000-0x0007
FF 00Command0xFF00: relay on;
0x0000: relay off;
0x5500: relay toggle
8C 3ACRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]:

Relay 0 on: 01 05 00 00 FF 00 8C 3A
Relay 0 off: 01 05 00 00 00 00 CD CA
Relay 1 on: 01 05 00 01 FF 00 DD FA
Relay 1 off: 01 05 00 01 00 00 9C 0A
Relay 2 on: 01 05 00 02 FF 00 2D FA
Relay 2 off: 01 05 00 02 00 00 6C 0A
Relay 3 on: 01 05 00 03 FF 00 7C 3A
Relay 3 off: 01 05 00 03 00 00 3D CA
Relay 0 toggle: 01 05 00 00 55 00 F2 9A
Relay 1 toggle: 01 05 00 01 55 00 A3 5A
Relay 2 toggle: 01 05 00 02 55 00 53 5A
Relay 3 toggle: 01 05 00 03 55 00 02 9A

Control All Relays

Send code: 01 05 00 FF FF 00 BC 0A

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0505 CommandRelay control
00 FFAddressFixed 0x00FF
FF 00Command0xFF00: relay on;
0x0000: relay off;
0x5500: relay toggle
BC 0ACRC16The CRC16 checksum of the first 6 bytes of data

Return code: 01 05 00 FF FF 00 BC 0A

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0505 CommandRelay control
00 FFAddressFixed 0x00FF
FF 00Command0xFF00: relay on;
0x0000: relay off;
0x5500: relay toggle
BC 0ACRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]:

All relays on: 01 05 00 FF FF 00 BC 0A
All relays off: 01 05 00 FF 00 00 FD FA
All relays toggle: 01 05 00 FF 55 00 C2 AA

Read Relay Status

Send code: 01 01 00 00 00 08 3D CC

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0101 CommandQuery relay status
00 00Relay Start AddressThe register address of the relay, 0x0000 - 0x0007
00 08Relay NumberThe number of relays to be read, which must not exceed the maximum number of relays
3D CCCRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 01 01 01 00 51 88

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0101 CommandQuery relay status
01Byte NumberThe number of all bytes of the returned status information
00Query statusReceived relay status
Bit0: the first relay status;
Bit1: the second relay status;
And so on, with the idle high bit being zero
51 88CRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]

Send: 01 01 00 00 00 08  3D CC
Receive: 01 01 01 00 51 88    //all relays off
Send: 01 01 00 00 00 08  3D CC
Receive: 01 01 01 01 90 48    //Relay 0 is on, others are off
Send: 01 01 00 00 00 08  3D CC
Receive: 01 01 01 41 91 B8 //Relay 0 and 6 are on, others are off

Write Relay Status

Send code: 01 0F 00 00 00 08 01 FF BE D5

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0F0F CommandWrite relay status
00 00Relay Start AddressThe register address of the relay to be controlled, 0x0000 - 0x0007
00 08Relay NumberThe number of relays to be operated, which must not exceed the maximum number of relays
01Byte NumberThe byte number of the status
FFRelay StatusBit0: the first relay status;
Bit1: the second relay status;
And so on, with the idle high bit being zero
BE D5CRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 01 0F 00 00 00 08 54 0D

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0F0F CommandControl all registers
00 00Relay Start AddressThe register address of the relay to be controlled, 0x0000 - 0x0007
00 08Relay NumberThe number of relays to be operated
54 0DCRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]

All relays on: 01 0F 00 00 00 08 01 FF BE D5
All relays off: 01 0F 00 00 00 08 01 00 FE 95
0-1 on; 2-7 off: 01 0F 00 00 00 08 01 03 BE 94

Relay Flash ON/OFF Command

Send code: 01 05 02 00 00 07 8D B0

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0505 CommandSingle control command
02Flash on flash off02: flash on, 04: flash off
00Relay AddressThe address of the relay to be controlled, 0x00~0x07
00 07Interval TimeThe interval time: data*100ms
Value: 0x0007, Interval time: 7*100MS = 700MS
The maximum setting for the flash-on flash-off time is 0x7FFF
8D B0CRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 01 05 02 00 00 07 8D B0

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0505 CommandSingle control command
02Flash on flash off02: flash on, 04: flash off
00Relay AddressThe address of the relay to be controlled, 0x00~0x07
00 07Interval TimeThe interval time: data*100ms
Value: 0x0007, Interval time: 7*100MS = 700MS
The maximum setting for the flash-on flash-off time is 0x7FFF
8D B0CRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]

Relay 0 flash on: 01 05 02 00 00 07 8D B0  //700MS = 7*100MS = 700MS
Relay 1 flash on: 01 05 02 01 00 08 9C 74  //800MS
Relay 0 flash off: 01 05 04 00 00 05 0C F9  //500MS
Relay 1 flash off: 01 05 04 01 00 06 1D 38  //600MS

Set Baudrate Command

Send code: 01 06 20 00 00 05 42 09

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0606 commandSet the baud rate and device address
20 00Baud rate register0x2000: set the baud rate
00Parity Methodx00: no parity, 0x01: even parity; 0x02: odd parity
05Baud Rate ValueCorresponding baud rate
0x00: 4800
0x01: 9600
0x02: 19200
0x03: 38400
0x04: 57600
0x05: 115200
0x06: 128000
0x07: 256000
42 09CRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 01 06 20 00 00 05 42 09

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0606 commandSet the baud rate and device address
20 00Baud rate register0x2000: set the baud rate
00Parity Methodx00: no parity, 0x01: odd parity; 0x02: even parity
05Baud RateCorresponding baud rate
0x00: 4800
0x01: 9600
0x02: 19200
0x03: 38400
0x04: 57600
0x05: 115200
0x06: 128000
0x07: 256000
42 09CRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]

Set the baud rate as 4800: 01 06 20 00 00 00 82 0A
Set the baud rate as 9600: 01 06 20 00 00 01 43 CA
Set the baud rate as 115200: 01 06 20 00 00 05 42 09

Set Device Address Command

Send code: 01 06 40 00 00 01 5D CA

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0606 commandSet the baud rate and device address
40 00Device address register0x4000: set the device address
00 01Device AddressSet the device address, 0x0001-0x00FF
5D CACRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 01 06 40 00 00 01 5D CA

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0606 commandSet the baud rate and device address
40 00Device address register0x4000: set the device address
00 01Device AddressSet the device address, 0x0001-0x00FF
5D CACRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 1 device]

Set the device address as 0x01: 00 06 40 00 00 01 5C 1B
Set the device address as 0x02: 00 06 40 00 00 02 1C 1A
Set the device address as 0x03: 00 06 40 00 00 03 DD DA

Read Device Address Command

Send code: 00 03 40 00 00 01 90 1B

FieldDescriptionNote
00Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0303 CommandRead the device address
40 00Device address register0x4000: read the device address
00 01Byte NumberFixed 0x0001
90 1BCRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 00 03 02 00 01 44 44

FieldDescriptionNote
00Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0303 CommandRead the software version and device address
02Byte NumberThe number of bytes returned
00 01Device AddressSet the device address, 0x0001-0x00FF
44 44CRC16The CRC16 checksum of the first 6 bytes of data

For example: [Address 2 device]

Send: 00 03 40 00 00 01 90 1B
Receive: 00 03 02 00 02 04 45     //Address: 0x02

Read Software Version Command

Send code: 01 03 80 00 00 01 AD CA

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0303 CommandRead the software version and device address
80 00Software version register0x4000: read the device address, 0x8000: read software version
00 01Byte NumberFixed 0x0001
AD CACRC16The CRC16 checksum of the first 6 bytes of data

Receive code: 01 03 02 01 2C B8 09

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
0303 CommandRead the software version and device address
02Byte NumberThe number of bytes returned
01 2CSoftware VersionConverting to decimal and then shifting the decimal point two places to the left will represent the software version
0x012C = 300 = V3.00
B8 09CRC16The CRC16 checksum of the first 6 bytes of data

For example:

Send: 01 03 80 00 00 01 AD CA
Receive: 01 03 02 01 2C B8 09       //0x012C = 300 =V3.00

Exception Function Code

When the received command is incorrect or the device is abnormal, an exception response will be returned in the following format:

Receive: 01 85 03 02 91

FieldDescriptionNote
01Device Address0x00 indicates the broadcast address, 0x01-0xFF indicates the device address
85Exception Function CodeException function code = Request function code + 0x80
03Byte NumberException Code
02 91CRC16The CRC16 checksum of the first 6 bytes of data

An exception code is a single-byte value that indicates the type of error. Several commonly used exception codes defined by the Modbus protocol:

Exception CodeNameDescription
0x01Illegal FunctionThe requested function code is not supported
0x02Illegal Data AddressThe requested data address is incorrect
0x03Illegal Data ValueThe requested data value or operation cannot be executed
0x04Server FailureServer equipment failure
0x05ResponseThe request has been received and is being processed
0x06Device BusyThe device is currently busy and cannot perform the requested operation


FAQ

Support

Monday-Friday (9:30-6:30) Saturday (9:30-5:30)

Email: services01@spotpear.com