Instrument Control Toolbox™ supports the MODBUS interface over TCP/IP or Serial RTU. You can use it to communicate with MODBUS servers, such as a PLC. The typical workflow is:
Create a MODBUS connection to a server or hardware.
Configure the connection if necessary.
Perform read and write operations, such as communicating with a temperature controller.
Clear and close the connection.
To communicate over the MODBUS interface, you first create a
MODBUS object using the modbus function. Creating
the object also makes the connection. The syntax is:
<objname> = modbus('Transport', 'DeviceAddress')
or
<objname> = modbus('Transport', 'Port')You must set the transport type as either 'tcpip' or 'serialrtu' to
designate the protocol you want to use. Then set the address and port,
as shown in the next sections. You can also use name-value pairs in
the object creation to set properties such as Timeout and ByteOrder.
When you create the MODBUS object, it connects to the server
or hardware. If the transport is 'tcpip', then DeviceAddress must
be specified. Port is optional and defaults to 502 (reserved port
for MODBUS). If the transport is 'serialrtu', then 'Port' must
be specified.
Create Object Using TCP/IP Transport
When the transport is 'tcpip', you must specify DeviceAddress.
This is the IP address or host name of the MODBUS server. Port is
the remote port used by the MODBUS server. Port is optional and defaults
to 502, which is the reserved port for MODBUS.
This example creates the MODBUS object m using
the device address shown and port of 308.
m = modbus('tcpip', '192.168.2.1', 308)
m =
Modbus TCPIP with properties:
DeviceAddress: '192.168.2.1'
Port: 308
Status: 'open'
NumRetries: 1
Timeout: 10 (seconds)
ByteOrder: 'big-endian'
WordOrder: 'big-endian'Create Object Using Serial RTU Transport
When the transport is 'serialrtu', you must
specify 'Port'. This is the Serial port the MODBUS
server is connected to.
This example creates the MODBUS object m using
the port of 'COM3'.
m = modbus('serialrtu','COM3')
m =
Modbus Serial RTU with properties:
Port: 'COM3'
BaudRate: 9600
DataBits: 8
Parity: 'none'
StopBits: 1
Status: 'open'
NumRetries: 1
Timeout: 10 (seconds)
ByteOrder: 'big-endian'
WordOrder: 'big-endian'Create Object and Set a Property
You can create the object using a name-value pair to set the
properties such as Timeout. The Timeout property
specifies the maximum time in seconds to wait for a response from
the MODBUS server, and the default is 10. You can
change the value either during object creation or after you create
the object.
For the list and description of properties you can set for both transport types, see Configure Properties for MODBUS Communication.
This example creates a MODBUS object using Serial RTU, but increases
the Timeout to 20 seconds.
m = modbus('serialrtu','COM3','Timeout',20)
m =
Modbus Serial RTU with properties:
Port: 'COM3'
BaudRate: 9600
DataBits: 8
Parity: 'none'
StopBits: 1
Status: 'open'
NumRetries: 1
Timeout: 20 (seconds)
ByteOrder: 'big-endian'
WordOrder: 'big-endian'The output reflects the Timeout property
change.