Connect Slave Device via Modbus TCP

Modbus TCP enables communication over Ethernet. In the typical setup, two VisionFive 2 boards are used: one as the Modbus master and one as the Modbus slave.

Hardware Preparation

Prepare for the following hardware items:
Item Quantity Note
VisionFive 2 or VisionFive 2 Lite 2 One configured as Master, the other as Slave
Ethernet cable or network switch 1 For direct connection or LAN connectivity
Breadboard 1 For Slave board wiring
LED 2 Preferably two different colors
Button 2 For digital input testing
Dupont wires Several For breadboard connections

Hardware Circuit

As shown in the figure below, two pieces of VisionFive 2 or VisionFive 2 Lite are selected as the Modbus-TCP master/slave devices respectively.
Figure 1. Connect to 40-Pin GPIO Header

The button that controls the blue/red LED flashing is connected to %IX0.0 and %IX0.1 of the master device, the blue LED is connected to %QX0.0 of the master device, and the red led is connected to %QX0.0 of the slave device, which is %QX100.0 of the master device. There is no electrical connection between the master and slave devices.

For details about the pin mapping for the 40-pin GPIO header on the master/slave board, see I/O Capabilities and Pin Mapping References.

Run Blank Program on Slave Board

When running as a Modbus-TCP slave, the slave board must run the default Blank Program. The OpenPLC runtime automatically exposes the local GPIO through the Modbus TCP server (port 502).
Figure 2. Run Blank Program

Add Slave Device on Master Board

On the Master VisionFive 2 board:
  1. Open the OpenPLC web interface: http://<Master-IP>:8080

  2. Navigate to Slave Devices in the left menu.

  3. Click Add New Device.
    Figure 3. Slave Device Settings
  4. Fill in the slave device properties.

    Table 1. Slave Device Properties
    Property Description
    Device Name Descriptive name for the slave VisionFive 2 board
    Device Type Protocol selection. Select Generic Modbus-TCP Device.
    Slave ID ID pf the slave board
    IP Address IP address of the slave board
    IP Port Modbus TCP port (default: 502)
  5. Map the I/O points and registers between the master device and slave device. For example, if there are 8 digital input points, 8 digital output points, and 1 analog output point configured in the master device, setting the size of the following properties accordingly.
    Table 2. I/O Point and Register Mappings
    Property Size Value
    Discrete Inputs 8
    Coils 8
    Holding Registers - Write 1

Upload Test Program to Master Device

Upload the ladder diagram program to the master VisionFive 2 board. The function of the test program is to flash the corresponding LED light when the key switch is pressed, and set different flashing frequencies for the two LEDs.
Note: Ladder diagram program is located in your computer after you download and unzipped the openplc.zip: \openplc\5.3 Connect slave device.zip.

For details about uploading a program, see Upload a Program.

Verification

After confirming the ladder diagram program on the master board and the slave device configurations are correct, click Start PLC to start the OpenPLC runtime.
  1. Check the Runtime Logs on the Master board's dashboard. A successful Modbus-TCP device connection is indicated by a log message confirming the slave device is connected.

    Figure 4. Successful Connection
  2. Press the button that controls the red LED blinking on the master board. Check whether the red LED on the slave board starts blinking to confirm that the master and slave devices are communicating properly.