Skip to main content

Setting Up the Monitoring of I/O Exchange and Device State

Monitoring the ModbusTCPClientSideExample project

  1. In the ModbusTCPClientSideExample project, open the PLC_PRG.prg.st program.

  2. Declare two variables named readValue and writeValue of type WORD.

  3. Define a variable maxValue of type WORD with the value 100.

  4. Insert the following code into your implementation:

    IF writeValue > maxValue THEN
       writeValue := 0;
    END_IF
    IF readValue = writeValue THEN
       writeValue := writeValue + 1;
    END_IF

    Note

    As soon as the Modbus communication is established, this code will continuously increment readValue and writeValue until the limit is reached.

  5. Open the editor of the Modbus_TCP_Server device and switch to the I/O Mapping tab.

  6. Expand the entries for Channel 1 and Channel 2.

  7. Map PLC_PRG.readValue to the Channel 2[0] entry. The variable receives the value read by the request later.

  8. Map PLC_PRG.writeValue to the Channel 1[0] entry. The variable provides the value to write.

    The result looks like this:

    _c4_img_task_modbus_setup_monitoring.png
  9. Switch back to PLC_PRG.prg.st of the ModbusTCPClientSideExample project.

  10. Declare a variable serverComState of type IoDrvModbusTCP.ModbusTCPComState.

  11. Declare a variable serverErrorCode of type IoDrvModbusTCP.MB_ErrorCodes.

  12. Add the following code to your implementation and adjust the server name to match the name used under Devices. In this sample project, the name is Modbus_TCP_Server.

    serverComState := Modbus_TCP_Server.ComState;
    serverErrorCode := Modbus_TCP_Server.byModbusError;

    This will allow you to monitor the communication state and the current error code by using the outputs of the device’s function block.

Monitoring the ModbusTCPServerSideExample project

  1. In the ModbusTCPServerSideExample project, open the PLC_PRG.prg.st program.

  2. Declare two variables named registerValue and tempValue of type WORD.

  3. Insert the following code into your implementation:

    tempValue := registerValue;
  4. Open the editor of the ModbusTCP_Server_Device device and switch to the I/O Mapping tab.

  5. Expand the entry for Holding Registers.

  6. Map PLC_PRG.registerValue to the Holding Registers[0] entry. The variable contains the value of the register.

    The result looks like this:

    _c4_img_task_modbus_setup_monitoring_serer_device.png
  7. Switch back to PLC_PRG.prg.st of the ModbusTCPServerSideExample project.

  8. Declare a variable serverDeviceClientConnections of type UINT.

  9. Declare a variable serverDeviceErrorCode of type UINT.

  10. Insert the following code into your implementation:

    serverDeviceClientConnections := ModbusTCP_Server_Device.uiClientConnections;
    serverDeviceErrorCode := ModbusTCP_Server_Device.ErrorCode;

    This code will allow you to monitor the current number of clients connected to this server and the current error code of the server.

Note

Now the setup for the Modbus communication and the monitoring of the devices are complete.