Create a Modbus Connection
Industrial Communication 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 arguments 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 port
'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 an Object with a Property Setting
You can create the object using a name-value pair to set 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, with an increased Timeout
of
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 object display in the output shows the specified Timeout
property
value.