Main Content

Configure Properties for Modbus Communication

The modbus object has the following properties.

PropertyTransport TypeDescription
'DeviceAddress'TCP/IP only

IP address or host name of Modbus® server, for example, ''. Required during object creation if transport is TCP/IP.

m = modbus('tcpip', '')

PortTCP/IP only

Remote port used by Modbus server. Optional: default is 502.

m = modbus('tcpip', '', 308)

'Port'Serial RTU only

Serial port that Modbus server is connected to, for example, 'COM1'. Required during object creation if transport is Serial RTU.

m = modbus('serialrtu','COM3')

TimeoutTCP/IP and Serial RTU

Maximum time in seconds to wait for a response from the Modbus server, specified as a positive value of type double. The default is 10. You can set the value during object creation or afterward.

m.Timeout = 30;

'NumRetries'TCP/IP and Serial RTU

Number of retries to perform if there is no reply from the server after a timeout. If using the Serial RTU transport, the message is resent. If using the TCP/IP transport, the connection is closed and reopened.

m.NumRetries = 5;

'ByteOrder'TCP/IP and Serial RTU

Byte order of values written to or read from 16-bit registers. Valid choices are 'big-endian' and 'little-endian'. The default is 'big-endian', as specified by the Modbus standard.

m.ByteOrder = 'little-endian';

'WordOrder'TCP/IP and Serial RTU

Word order for register reads and writes that span multiple 16-bit registers. Valid choices are 'big-endian' and 'little-endian'. The default is 'big-endian', and it is device-dependent.

m.WordOrder = 'little-endian';

'BaudRate'Serial RTU only

Bit transmission rate for serial port communication. Default is 9600 bits per second, but the actual required value is device-dependent.

m.Baudrate = 28800;

'DataBits'Serial RTU only

Number of data bits to transmit. Default is 8, which is the Modbus standard for Serial RTU. Other valid values are 5, 6, and 7.

m.DataBits = 6;

'Parity'Serial RTU only

Type of parity checking. Valid options are 'none' (default), 'even', 'odd', 'mark', and 'space'. The actual required value is device-dependent. If set to the default of none, parity checking is not performed, and the parity bit is not transmitted.

m.Parity = 'odd';

'StopBits'Serial RTU only

Number of bits to indicate the end of data transmission. Valid choices are 1 (default) and 2. Actual required value is device-dependent, though 1 is typical for even/odd parity, and 2 for no parity.

m.StopBits = 2;

Set a Property During Object Creation

You can change property values either during object creation or after you create the object. To set property values during object creation, specify name-value pair arguments to the modbus function call.

This example creates a Modbus object and 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 object display in the output shows the specified Timeout property value.

Set a Property After Object Creation

You can change a property value on an existing object with this syntax.

<object_name>.<property_name> = <property_value>

This example using the Modbus object m, increases the Timeout to 30 seconds.

m = modbus('serialrtu','COM3');
m.Timeout = 30

This example changes the Parity from the default of 'none' to 'even'.

m = modbus('serialrtu','COM3');
m.Parity = 'even';