modbus
Create Modbus object
Syntax
Description
TCP/IP
m = modbus(
constructs a Modbus object, transport,deviceAddress)m, over the transport type
'tcpip' using the specified deviceAddress.
deviceAddress is the IP address or host name of the Modbus
server.
m = modbus(
additionally specifies transport,deviceAddress,tcpipPort)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.
m = modbus(___,Name=Value) specifies TCP/IP Transport Properties using optional
name-value arguments.
Serial
m = modbus(
constructs a Modbus object transport,serialPort)m over the transport type
'serialrtu' using the specified serialPort.
m = modbus(___,Name=Value) specifies Serial Transport Properties using optional
name-value arguments.
Examples
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: 'Connected'
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: 'Connected'
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: 'Connected'
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: 'Connected'
NumRetries: 1
Timeout: 30 (seconds)
ByteOrder: 'big-endian'
WordOrder: 'big-endian'Input Arguments
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
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
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
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2017aBefore R2022a, the Modbus functionality, including this function, was part of the Instrument Control Toolbox™ product.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)