MATLAB - Modbus TCP客户端的使用
发布日期:2021-07-01 04:03:32
浏览次数:2
分类:技术文章
本文共 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
参考链接:
- https://ww2.mathworks.cn/help/instrument/modbus.read.html#mw_bfb0478b-ea6e-42dc-94a5-88a54a3a55f9
- https://blog.csdn.net/u011273970/article/details/92705421
- https://ww2.mathworks.cn/help/instrument/modbus-communication.html?s_tid=CRUX_lftnav
- https://ww2.mathworks.cn/help/instrument/configure-properties-for-modbus-communication.html
转载地址:https://miracle.blog.csdn.net/article/details/113876832 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月16日 20时11分55秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
17、QT布局类
2019-05-02
18、 QStackedWidget
2019-05-02
20、Qt 之 QTemporaryFile
2019-05-02
24 Qt 之 JSON小结
2019-05-02
25 QT之QSetting
2019-05-02
26 QT坐标系统
2019-05-02
27 Qt 之图形(QPainter 的基本绘图)
2019-05-02
28、Qt 之图形(渐变填充)
2019-05-02
29 Qt 之图形(转换)
2019-05-02
30 Qt 之图形之QPainterPath及QPainterPathStroker
2019-05-02
31 Qt 之绘图之绘制一个漂亮的圆及圆弧
2019-05-02
32 Qt 之绘图之绘制一个漂亮的西瓜
2019-05-02
33 Qt 之绘图之绘制卡通蚂蚁
2019-05-02
34 Qt 之绘图之绘制时钟
2019-05-02
35 Qt 之绘制闪烁文本
2019-05-02
QT知识点总结(一)
2019-05-02
QT知识点总结(二)
2019-05-02
QT知识点总结(三)
2019-05-02
一劳永逸的解除ByondCompare4注册问题
2019-05-02
Unix环境变量--文件操作
2019-05-02