Configure Properties for MODBUS Communication

The modbus object has the following properties.

PropertyTransport TypeDescription
'DeviceAddress'TCP/IP onlyIP address or host name of MODBUS server, for example, '192.168.2.1'. Required during object creation if transport is TCP/IP.

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

PortTCP/IP onlyRemote port used by MODBUS server. The default is 502. Optional during object creation if transport is TCP/IP.

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

'Port'Serial RTU onlySerial port MODBUS server is connected to, for example, 'COM1'. Required during object creation if transport is Serial RTU.

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

TimeoutBoth TCP/IP and Serial RTUMaximum 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 change the value either during object creation, or after you create the object.

m.Timeout = 30;

NumRetriesBoth TCP/IP and Serial RTUNumber 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'Both 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'Both TCP/IP and Serial RTUWord 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';

BaudRateSerial RTU onlyBit transmission rate for serial port communication. Default is 9600 bits per seconds, but the actual required value is device-dependent.

m.Baudrate = 28800;

DataBitsSerial RTU onlyNumber 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;

ParitySerial RTU onlyType of parity checking. Valid choices 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';

StopBitsSerial RTU onlyNumber of bits used 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.

You can create the modbus object using a name-value pair to set a value during object creation.

This example creates the 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 output reflects the Timeout property change from the default of 10 seconds to 20 seconds.

Set a Property After Object Creation

You can change a property anytime by setting the property value using this syntax after you have created a MODBUS object.

<object_name>.<property_name> = <property_value>

This example using the same object named 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';