Main Content

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 themodbusfunction. Creating the object also makes the connection. The syntax is:

 = modbus('Transport','DeviceAddress')

or

 = 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 asTimeoutandByteOrder.

When you create the Modbus object, it connects to the server or hardware. If the transport is'tcpip', thenDeviceAddressmust 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 specifyDeviceAddress. This is the IP address or host name of the Modbus server.Portis the remote port used by the Modbus server. Port is optional and defaults to502, which is the reserved port for Modbus.

This example creates the Modbus objectmusing the device address shown andportof308.

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 objectmusingport'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

您可以创建对象使用一个名称-值对to set properties such asTimeout. TheTimeoutproperty specifies the maximum time in seconds to wait for a response from the Modbus server, and the default is10. You can change the value either during object creation or after you create the object.

你可以列表和描述的属性set for both transport types, seeConfigure Properties for Modbus Communication.

This example creates a Modbus object using Serial RTU, with an increasedTimeoutof20seconds.

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 specifiedTimeoutproperty value.