本文共 4319 字,大约阅读时间需要 14 分钟。
函数 | 说明 |
---|---|
modbus | 创建MODBUS对象 |
read | 从MODBUS服务器读取数据 |
write | 对连接的MODBUS服务器执行写操作 |
writeRead | 在单个MODBUS事务中对一组保持寄存器执行写然后读操作 |
maskWrite | 在保持寄存器上执行掩码写入操作 |
instrhwinfo | 有关可用硬件的信息 |
clear | 从MATLAB工作区中删除仪器对象 |
Property | Transport Type | Description |
---|---|---|
‘DeviceAddress’ | TCP/IP only | IP address or host name of MODBUS server, for example, ‘192.168.2.1’. Required during object creation if transport is TCP/IP.m = modbus('tcpip', '192.168.2.1') |
Port | TCP/IP only | Remote port used by MODBUS server. The default is 502. Optional during object creation if transport is TCP/IP.m = modbus('tcpip', '192.168.2.1', 308) |
‘Port’ | Serial RTU only | Serial port MODBUS server is connected to, for example, ‘COM1’. Required during object creation if transport is Serial RTU.m = modbus('serialrtu','COM3') |
Timeout | Both 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 double. The default is 10. You can change the value either during object creation, or after you create the object.m.Timeout = 30; |
NumRetries | Both 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.m.NumRetries = 5; |
‘ByteOrder’ | Both TCP/IP and Serial RTU | Byte order of values written to or read from 16-bit registers. Valid choices are ‘big-endian’ and ‘little-endian’. The default is ‘big-endian’, as specified by the MODBUS standard.m.ByteOrder = 'little-endian'; |
‘WordOrder’ | Both TCP/IP and Serial RTU | Word order for register reads and writes that span multiple 16-bit registers. Valid choices are ‘big-endian’ and ‘little-endian’. The default is ‘big-endian’, and it is device-dependent.m.WordOrder = 'little-endian'; |
BaudRate | Serial RTU only | Bit transmission rate for serial port communication. Default is 9600 bits per seconds, but the actual required value is device-dependent.m.Baudrate = 28800; |
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.m.DataBits = 6; |
Parity | Serial RTU only | Type of parity checking. Valid choices are ‘none’ (default), ‘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.m.Parity = 'odd'; |
StopBits | Serial RTU only | Number of bits used to indicate the end of data transmission. Valid choices are 1 (default) and 2. Actual required value is device-dependent, though 1 is typical for even/odd parity and 2 for no parity.m.StopBits = 2; |
使用Modbus Slave模拟服务器,数值如下设置:
注意:Modbus Slave中每个寄存器是16位!
数据类型有:'int16', 'uint16', 'int32', 'uint32', 'single', 'double', 'int64', 'uint64'
% 创建客户端m = modbus('tcpip','127.0.0.1',502)% 属性查看>> m.ByteOrderans = 'big-endian'>> m.Portans = 502% 设置属性>> m.Timeout = 3m = Modbus TCPIP with properties: DeviceAddress: '127.0.0.1' Port: 502 Status: 'Connected' NumRetries: 1 Timeout: 3 (seconds) ByteOrder: 'big-endian' WordOrder: 'big-endian' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% read(obj,'holdingregs',address,count)% read(obj,target,address,count,serverId) % 指定Server ID% read(obj,target,address,count,precision) % 指定数据读取格式% read(obj,target,address,count,serverId,precision) % 同时指定Server ID和数据读取格式% 读取23,以保持寄存器的方式读取,read列表为modbus对象、寄存器类型、开始地址(从1开始)、读取的个数、读取的类型>> read(m,'holdingregs',3,1,'int16')ans = 23% 单精度浮点型数据的读取,注意MATLAB默认的读取字序对应的Slave中的字序为Float AB CD,即大端>> read(m,'holdingregs',5,1,'single')ans = 10.1100% 断开并清除Modbus连接clear m
% 创建客户端m = modbus('tcpip','127.0.0.1',502)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% write(m,target,address,values)% write(m,target,address,values,serverId,'precision')write(mbConnector,'holdingregs',10,1235,'int16')write(mbConnector,'holdingregs',1,[1 2 3],'int16')% 断开并清除Modbus连接clear m
参考链接: