Main Content

Communicating With a PLC Using the Modbus Interface

This example shows how to initiate communication with a PLC and read and write into holding registers and discrete coils.

Create the Modbus Object

Create a Modbus client object. This example assumes a CLICK Ethernet Standard PLC is connected to RS-232 serial port 'COM10'. The serial port configuration on the device as determined from the CLICK Programming Software is:

Baud Rate: 115200

Parity: odd

Stop Bits: 1 (default)

Data Bits: 8 (default)

modbusObj = modbus("serialrtu", "COM10", BaudRate=115200, Parity="odd")
modbusObj = 
Modbus Serial RTU with properties:

             Port: 'COM10'
         BaudRate: 115200
         DataBits: 8
           Parity: 'odd'
         StopBits: 1
           Status: 'Connected'
       NumRetries: 1
          Timeout: 10 (seconds)
        ByteOrder: 'big-endian'
        WordOrder: 'big-endian'

Write a Coil and Read a 16-bit Timer Register

The PLC has a simple program which initiates a timer when coil 'C1' is closed. The address of this coil is 16385. Set the value of the coil 'C1' to 1 to enable the timer.

coilC1 = 16385;
write(modbusObj, "coils", coilC1, 1);

Read and display the value of the timer and see it increment. You can read the current value of the timer from holding register 'TD1' whose address is 45057. Note that some vendors may document holding registers with an additional number, typically '4', at the start of the register address to indicate that this is a holding register. This number must be dropped before being passed to any Modbus function. The Modbus protocol specifies the address as a 16-bit 1-based integer with a valid range of 1-65535.

regTD1 = 45057;
registerCount = 1; 

Read and display the timer value to see how many milliseconds have elapsed since the coil was energized.

timerValue = read(modbusObj, "holdingregs", regTD1, registerCount)
timerValue = 86

Pause for 1 second and read again; the value should be approximately 1 second more than the previous read value.

pause(1);
timerValue = read(modbusObj, "holdingregs", regTD1, registerCount)
timerValue = 87

Now open the coil 'C1' to disable the timer by setting its value to 0.

write(modbusObj, "coils", coilC1, 0);

Write and Read 32-bit Single Precision Floating Point Registers

In addition to 16-bit integer registers such as 'TD1', this PLC also has 32-bit single precision floating point registers such as 'DF334' and 'DF335'. The addresses for these registers are 29339 and 29341, respectively.

Internal to the PLC a 32-bit value is stored in two consecutive 16-bit registers. For 'DF334' the value is split across addresses 29339 and 29340. The order of the two values will be device dependent. In the case of the CLICK PLC, the values are stored low word first, or little-endian.

modbusObj.WordOrder = "little-endian";
regDF334 = 29339;
data = [919.92 103.64];
write(modbusObj, "holdingregs", regDF334, data, "single");

Now read the values back and display them.

registerCount = 2;
values = read(modbusObj, "holdingregs", regDF334, registerCount, "single")
values = 1×2

  919.9200  103.6400