First in your Unity program open the configuration and double click on the serial port.
Configure the serial port as needed for your application.
The port must be set to Master for the READ_VAR to function.
In this example the port is set for RS485, 19200,8,E,1
The Rack, Module, Channel (r.m.c) for the Serial port is always 0.0.0 for a M340 processor
Next go to Tools and Project Settings
In the Project Settings that the last 3 items in the Data types field are checked off.
This will allow us to directly enter Arrays into our project rather than having to first declare them as variables.
Next add a ADDM block to your program, link the OUT pin to the ADR pin of the Read_Var block, and enter the appropriate String to the IN pin.
For Modbus communication the syntax for the IN pin of the ADDM block uses the form r.m.c.e where:
r = rack
m = module
c = channel
e = equipment
The equipment is the slave Modbus address.
Remember that this input is a String so you need ' before and after the text.
Next configure the remaining parameters of the Read_Var block where:
OBJ = String declaring the type of object to read (%MW or %M)
NUM = The starting register address in the slave
NB = The # of registers to read
GEST = Array of length 4 where the Management Exchange table for the Read_Var is located
RECP = Array of length NB to where the read information is to be stored.
The Array's are of type INT if declared as variables.
This example will read 5 registers from %MW1-5 in a slave at Modbus address 15 and store them in %MW1-5 in the M340.
The following video demonstrates how to Program READ_VAR Function to Read Modbus Serial Device Data: