Main Content

modbus

Create Modbus object

Description

TCP/IP

m = modbus(transport,deviceAddress) constructs a Modbus object, m, over the transport type 'tcpip' using the specified deviceAddress. deviceAddress is the IP address or host name of the Modbus server.

example

m = modbus(transport,deviceAddress,tcpipPort) additionally specifies tcpipPort. tcpipPort is the remote port used by the Modbus server. tcpipPort is an optional argument, and it defaults to 502, which is the reserved port for Modbus.

example

m = modbus(___,Name=Value) specifies TCP/IP Transport Properties using optional name-value arguments.

example

Serial

m = modbus(transport,serialPort) constructs a Modbus object m over the transport type 'serialrtu' using the specified serialPort.

example

m = modbus(___,Name=Value) specifies Serial Transport Properties using optional name-value arguments.

example

Examples

collapse all

When the transport is TCP/IP, you must specify the IP address or host name of the Modbus server. You can optionally specify the remote port used by the Modbus server. Port defaults to 502, which is the reserved port for Modbus.

Create the Modbus object m using the host 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'

The object output shows both the arguments you set and the defaults.

When the transport is 'serialrtu', you must specify a Port argument. This is the serial port that the Modbus server is connected to.

Create the Modbus object m specifying a Port of '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'

The object output shows arguments you set and defaults that are used automatically.

You can create the object using a name-value pair to set the 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 a list of properties, see Name-Value Arguments

Create a Modbus object using Serial RTU, but increase 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 output reflects the Timeout property change.

Change the Timeout value to 20 seconds. You can use dot notation to change a property value after object creation.

m.Timeout = 30
m = 

Modbus Serial RTU with properties:

             Port: 'COM3'
         BaudRate: 9600
         DataBits: 8
           Parity: 'none'
         StopBits: 1
           Status: 'open'
       NumRetries: 1
          Timeout: 30 (seconds)
        ByteOrder: 'big-endian'
        WordOrder: 'big-endian'

Input Arguments

collapse all

Physical transport layer for device communication, specified as a character vector or string. Specify transport type as the first argument when you create the modbus object. You must set the transport type as either 'tcpip' or 'serialrtu' to designate the protocol you want to use.

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

Data Types: char | string

IP address or host name of Modbus server, specified as a character vector or string. If transport is TCP/IP, it is required as the second argument during object creation.

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

Data Types: char | string

Remote port used by Modbus server, specified as a double. Optional as a third argument during object creation if transport is TCP/IP. The default of 502 is used if none is specified.

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

Data Types: double

Serial port Modbus server is connected to, e.g. 'COM1', specified as a character vector or string. If transport is Serial RTU, it is required as the second argument during object creation.

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

Data Types: char | string

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: m = modbus('serialrtu','COM3','Timeout',20)

TCP/IP Transport Properties

expand all

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 change the value either during object creation or after you create the object.

Example: m = modbus('tcpip','192.168.2.1',Timeout = 20)

Data Types: double

Number of retries to perform if there is no reply from the server after a timeout, specified as the comma-separated pair consisting of 'NumRetries' and a positive value of type double. For TCP/IP transport, the connection is closed and reopened. You can change the value either during object creation, or after you create the object.

Example: m = modbus('tcpip','192.168.2.1',NumRetries = 5)

Data Types: double

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

Example: m = modbus('tcpip','192.168.2.1',ByteOrder = 'little-endian')

Data Types: char | string

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

Example: m = modbus('tcpip','192.168.2.1',WordOrder = 'little-endian')

Data Types: char | string

Serial Transport Properties

expand all

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 change the value either during object creation or after you create the object.

Example: m = modbus('serialrtu','COM3',Timeout = 20)

Data Types: double

Number of retries to perform if there is no reply from the server after a timeout, specified as the comma-separated pair consisting of 'NumRetries' and a positive value of type double. For Serial RTU transport, the message is resent. You can change the value either during object creation, or after you create the object.

Example: m = modbus('serialrtu','COM3',NumRetries = 5)

Data Types: double

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

Example: m = modbus('serialrtu','COM3',ByteOrder = 'little-endian')

Data Types: char | string

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

Example: m = modbus('serialrtu','COM3',WordOrder = 'little-endian')

Data Types: char | string

Bit transmission rate for serial port communication, specified as a positive value of type double. Default is 9600 bits per second, but the actual required value is device-dependent.

Example: m = modbus('serialrtu','COM3',Baudrate = 28800)

Data Types: double

Number of data bits to transmit, specified as either 5, 6, 7, or 8. Default is 8, which is the Modbus standard for Serial RTU.

Example: m = modbus('serialrtu','COM3',DataBits = 6)

Data Types: double

Type of parity checking, specified as 'none', '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.

Example: m = modbus('serialrtu','COM3',Parity = 'odd')

Data Types: char | string

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

Example: m = modbus('serialrtu','COM3',StopBits = 2)

Data Types: double

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017a

expand all