Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

modbus

Create MODBUS object

Syntax

m = modbus(Transport,DeviceAddress)
m = modbus(Transport,DeviceAddress,Port)
m = modbus(Transport,DeviceAddress,Name,Value)
m = modbus(Transport,'Port')
m = modbus(Transport,'Port',Name,Value)

Description

example

m = modbus(Transport,DeviceAddress) constructs a MODBUS object, m, over the transport type Transport using the specified 'DeviceAddress'. When the transport is 'tcpip', DeviceAddress must be specified as the second argument. DeviceAddress is the IP address or host name of the MODBUS server.

example

m = modbus(Transport,DeviceAddress,Port) additionally specifies Port. When the transport is 'tcpip', DeviceAddress must be specified. Port is the remote port used by the MODBUS server. Port is optional, and it defaults to 502, which is the reserved port for MODBUS.

example

m = modbus(Transport,DeviceAddress,Name,Value) specifies additional options with one or more name-value pair arguments using any of the previous syntaxes. For example, you can specify a timeout value. The Timeout property specifies the waiting time to complete read and write operations in seconds, and the default is 10.

example

m = modbus(Transport,'Port') constructs a MODBUS object m over the transport type Transport using the specified 'Port'. When the transport is 'serialrtu', 'Port' must be specified. This argument is the serial port the MODBUS server is connected to, such as 'COM3'.

example

m = modbus(Transport,'Port',Name,Value) specifies additional options with one or more name-value pair arguments using any of the previous syntaxes. For example, you can specify NumRetries, the number of retries to perform if there is no reply from the server after a timeout.

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 'Port'. This is the serial port the MODBUS server is connected to.

Create the MODBUS object m using the 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 the list and description of properties you can set for both transport types, see "Configure Properties for MODBUS Communication."

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.

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

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

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

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

There are a number of name-value pairs that can be used when you create the modbus object, including the two shown here. Some can only be used with either TCP/IP or Serial RTU, and some can be used with both transport types. For a list of all the properties and how to set them both during and after object creation, see Configure Properties for MODBUS Communication.

collapse all

Maximum time in seconds to wait for a response from the MODBUS server, specified as the comma-separated pair consisting of 'Timeout' and 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. If using the Serial RTU transport, the message is resent. If using the 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('serialrtu','COM3','NumRetries',5)

Data Types: double

Introduced in R2017a

Was this topic helpful?