Configure Properties for Modbus Communication
The modbus
object has the following properties.
Property | Transport Type | Description |
---|---|---|
'DeviceAddress' | TCP/IP only | IP address or host name of Modbus® server, for example,
|
Port | TCP/IP only | Remote port used by Modbus server. Optional: default is 502.
|
'Port' | Serial RTU only | Serial port that Modbus server is connected to, for example,
|
Timeout | TCP/IP and Serial RTU | Maximum time in seconds to wait for a response from the Modbus server, specified as a positive value of type
|
'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.
|
'ByteOrder' | TCP/IP and Serial RTU | Byte order of values written to or read from 16-bit registers. Valid
choices are
|
'WordOrder' | TCP/IP and Serial RTU | Word order for register reads and writes that span multiple 16-bit
registers. Valid choices are
|
'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.
|
'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.
|
'Parity' | Serial RTU only | Type of parity checking. Valid options are
|
'StopBits' | Serial RTU only | Number of bits to indicate the end of data transmission. Valid choices are
|
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';