create a modbus connection -凯发k8网页登录

main content

create a modbus connection

industrial communication toolbox™ supports the modbus® interface over tcp/ip or serial rtu. you can use it to communicate with modbus servers, such as a plc. the typical workflow is:

  • create a modbus connection to a server or hardware.

  • configure the connection if necessary.

  • perform read and write operations, such as communicating with a temperature controller.

  • clear and close the connection.

to communicate over the modbus interface, you first create a modbus object using the function. creating the object also makes the connection. the syntax is:

 = modbus('transport','deviceaddress')

or

 = modbus('transport','port')

you must set the transport type as either 'tcpip' or 'serialrtu' to designate the protocol you want to use. then set the address and port, as shown in the next sections. you can also use arguments in the object creation to set properties such as timeout and byteorder.

when you create the modbus object, it connects to the server or hardware. if the transport is 'tcpip', then deviceaddress must be specified. port is optional and defaults to 502 (reserved port for modbus). if the transport is 'serialrtu', then 'port' must be specified.

create object using tcp/ip transport

when the transport is 'tcpip', you must specify deviceaddress. this is the ip address or host name of the modbus server. port is the remote port used by the modbus server. port is optional and defaults to 502, which is the reserved port for modbus.

this example creates the modbus object m using the device 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'

create object using serial rtu transport

when the transport is 'serialrtu', you must specify 'port'. this is the serial port the modbus server is connected to.

this example creates the modbus object m using port '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'

create an object with a property setting

you can create the object using a name-value pair to set 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 .

this example creates a modbus object using serial rtu, with an increased timeout of 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.

网站地图