博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MATLAB - Modbus TCP客户端的使用
阅读量:2010 次
发布时间:2019-04-28

本文共 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
你可能感兴趣的文章
C# 数据库并发的解决方案(通用版、EF版)
查看>>
layui定时刷新表格不闪动并停留在当前页(不重新渲染)
查看>>
C#中Invoke的用法
查看>>
zend_parse_parameters函数
查看>>
Flex4/Flash多文件上传(带进度条)实例分享
查看>>
博客中 Flex4/Flash mp3音乐播放器实例 含演示地址
查看>>
JAVA与DOM解析器提高(DOM/SAX/JDOM/DOM4j/XPath) 学习笔记二
查看>>
在自己网站中引入CU3ER/Flash 3D幻灯片效果和照片画框
查看>>
JAVA中 XML与数据库互转 学习笔记三
查看>>
JAVA与DOM解析器基础 学习笔记
查看>>
JAVA 利用Dom4j实现英语六级词汇查询 含演示地址
查看>>
到网上收集了一个“高大上”的CSS3登入表单和大家分享一下
查看>>
CSS3无前缀脚本prefixfree.js与Animatable使用介绍
查看>>
自制“低奢内”CSS3登入表单,包含JS验证,请别嫌弃哦。
查看>>
自制“低奢内”CSS3注册表单,包含JS验证哦。请别嫌弃,好吗?。
查看>>
Wookmark-jQuery-master 瀑布流插件使用介绍,含个人测试DEMO
查看>>
Font Awesome 4.0.3 提供了369个网页常用的矢量字体图标,新浪、人人 的矢量图标也到其中哟...
查看>>
Flex4/Flash开发在线音乐播放器 , 含演示地址
查看>>
利用:before和:after伪类制作CSS3 圆形按钮 含demo
查看>>
超漂亮的CSS3按钮制作教程分享
查看>>