Connect the relay board to PC
To control the channels on the board, you need to connect the board to your PC using an USB to RS485 converter. It provides easy connectivity between the PC and the standard communication port. The Figure 1 shows how to connect it to the board. Then, on the board you need to turn on the A5 switcher. Turning on A5 means that the board wait for Modbos RTU type of commands (and not AT). Finally, attach it to your computer.
Figure 1 - Modbus relay board wiring diagram
Specifications of the board
There are many relay board exists such as 2-channel, 4-channel, 8-channel and 16-channel ones. Most of them need the same voltage and support the same operating modes, but the other parameters may be different from this one. Due to this, the following specification applies to this 4-channel relay board only.
Setting up the relay board
You can set the slave ID of the device using the A0-A4 switches found on
the board. This allows 32 different IDs to be set. The numbers below the
switches are added together and the result will be the identifier of the device.
So, if you would like to set the slave ID to 1, you need to turn A1 on and off the
others. To set ID to 2, turn A2 on and off the others. To set the ID to 3, turn A1
and A2 on and turn off the others and so on.
Figure 2 - Relay board switches
The serial port settings of the relay board can not be configured. These values are predetermined by the manufacturer. In Table 2 below, you can see what setting values should be used to communicate with the device (called slave). These values of another manufacturer's relay board may be different.
Modbus is a serial communication protocol that uses command and response frames to implement the communication between one master and many slaves. Only one master is connected to the bus, and one or several (247 maximum) slaves are also connected to the same serial bus. The Modbus communication is always initiated by the master. There are several versions of Modbus protocol that you can find more information about under Connections. The master can write the register values of the slave device and get them by sending a command message. The slave device sends back a response one that contains the requested information.
Connect more relay boards to master
To connect more slaves to master, you need to wire them like the Figure 3 shows. You can connect up to 32 slaves in parallel to the bus due to the relay board limitations. Ensure that each device has a unique slave ID.
Figure 3 - Connect more relay boards to master
Relay board modbus command frame
The Modbus RTU command frame build up from 8 bytes totally (Table 3). It is constructed from 1 byte address field which value can be set between 1 and 32. After that there is 1 byte function field. This module support 2 type of function codes: function code 6 for control the relay channels and function code 3 for read its state. It is followed by 4 bytes data field. By sending control command, this field contains the channel number, the command code, and the delay time. For reading the channels status, the field tells which relay channel state you would like to get. Finally, 2 bytes for checksum field that calculated from the previous 6 bytes using CRC-16.
Modify channel state
You can see the summary in Table 4 how to build up a control command message. The slave ID is the ID of the relay board you wish to control. Function code 6 defines the channel control on the specified board. The channel number (between 0x0001 and 0x0004) tells which channel state you would like to modify. The command identify the operation, for example 0x01 equals to open the selected channel etc. The delay time is always 0, unless you use the delay command. The checksum is CRC-16 calculated from the previous 6 bytes. After you have sent this command message, the relay board will execute the operation and will sent back a response message about the channel actual state. The values in the table below interpreted in hexadecimal numeral system.
Read channel state
The Table 5 shows how to build up a read status command message. The slave ID is the device ID again. The function code 3 determines the channel status reading on the selected board. In this case, the starting register address identify the channels. Each channel state stored in one register. The register length tells how many registers you would like to get from the starting address. If you would like to get one channel state, this number is always 1 (0x0001). Finally, there are 2 bytes checksum field.
Latch channel 1
To latch channel 1, the following modbus message must be sent to the device in hexadecimal format: 01 06 00 01 04 00 DA CA, like the Table 7 shows. First byte (0x01) is the slave ID that identifies the device. The next byte (0x06) is the function code that specifies the channel modification. The following two bytes (0x0001) determine the channel on which the operation will be performed. The next one (0x04) is the command code that specifies the channel latch. In this case, the delay time is not interpreted, so it is 0 (0x00). Finally, the last 2 bytes (0xDACA) are the checksum. When the device receives the message, it latches the channel 1.
Read channel 1 state
To read channel 1 state, the following modbus message must be sent to the device in hexadecimal format: 01 03 00 01 00 01 D5 CA as you can see in Table 8. First byte (0x01) is the slave ID again that identifies the device. The next byte (0x03) is the function code that specifies the channel state reading. This is followed by two bytes (0x0001) that specify the starting address. The address 0x0001 stores the channel 1 state. The next two bytes (0x0001) are the length that specify how many registers you would like to read from the starting address. The last 2 bytes (0x95CB) are the checksum calculated from the previous 6 bytes using CRC-16.
After you have sent the command message above, you will receive a response message that contains the specified channel state. If the selected channen is open, a response message is equals to the content of the Table 9.
Buy the modbus relay board