Modbus RTU master
To use Modbus RTU master, we need to decide which COM port sends data, and use this COM port to send a Modbus command to an RTU slave.
NOTE: Before you use RTU master, make sure you have already assigned a COM port to RTX64. For further details about how to assign a COM port, see Modbus RTU.
- In the Project pane, add a new program with FBD. You can decide its name and task, or use the settings we provided.
- In Library Tree, drag the following function blocks (FBs) to the source editor and give them meaningful names. COM_Connect is the COM port that the RTU master uses to send data. Mb_ReadCoils_10 is a command that reads a slave's Coil. You can use other commands if you want. After you add the FBs to the source editor, they will also be added to local variables.
- Connectivity > Serial > COM_Connect
- Connectivity > Modbus > Mb_ReadCoils_10
- Add the following variables. Two of them control the Enable and Execute state of COM_Connect and Mb_ReadCoils_10, respectively. The other one displays the value the master reads.
- Add the following inputs and outputs to the FBs as the screen shot shows.
- Compile and run the PLC program. To toggle the BOOL variables, drag them to the Watch panel and set their values. Remember, you need to turn on the Enable and Execute states of the FBs so they can start working.
Language: FBD
Name: mb_rtu_master
Task: Fast
NOTE: For more information about how to add a new program, see PLC programming.
| Variable 1 | Variable 2 | Variable 3 | |
|---|---|---|---|
| Attributes |
Name: connect_target Type: BOOL Address: Auto |
Name: read_coil Type: BOOL Address: Auto |
Name: coil_state Type: BOOL Address: Auto Array: 0..9 |
| Usage | Enable or disable COM_Connect. | Enable or disable Mb_ReadCoils_10. | Display the value read. |
NOTE: To know the range of Coils, Discrete Inputs, Holding Registers, and Input Registers, in Library Tree, expand ModbusSlaveData, double click an item.
If you want to use the RTU master and slave examples in this guide to test Modbus data transmission on the same computer, use an RS-232 cable to connect to COM1 and COM2, assign these COM ports to RTX64 and do the following:
| Step 1 | Step 2 | Step 3 |
|---|---|---|
|
In the RTU slave program, add |
In the RTU master program, add connect_target, read_coil, rtu_target, and rtu_read_coil to the Watch panel. Use connect_target and read_coil to enable the two FBs and check their Enable, Enabled, Execute, and Done states. They will be TRUE if the FBs are enabled successfully. |
Add a global variable |
|
|
|
NOTE: For more information about compiling and running, see Compile and run your PLC program.
NOTE: For more information about toggling a boolean, see Step 8 in Start and stop the KINGSTAR Subsystem through variables.