DSC模块之Modbus通讯(以PLC为例)

http://digital.ni.com/public.nsf/allkb/F716ABC0F75705ED86257AF5002E1451




主要软件: LabVIEW Modules>>LabVIEW DSC Module
主要软件版本: 2011 SP1
主要软件修正版本: N/A
次要软件: Driver Software>>Comedi Drivers

问题:

我有几台自动化设备,通过Modbus通讯的,我可以用LabVIEW来做上位机程序控制吗?



解答:
这是完全可以的,本篇文档主要用于对DSC模块中的Modbus的使用做一个详述,以PLC为例来说明基于Modbus协议怎样与自动化工业现场中的各类设备交换数据,从而简化和扩大LabVIEW的使用。
1.       Modbus通讯协议详述
Modbus是OSI模型第七层上即应用层的报文传输协议,它在连接至不同类型总线或网络的设备之间提供客户机/服务器通信。自从1979年出现工业串行链路的事实标准以来,Modbus使成千上万的自动化设备能够通信。目前,互联网组织能够使TCP/IP栈上的保留系统端口502 访问Modbus,扩大了Modbus的使用。因此,目前标准中有两个通讯规程,串行链路上的标准:232-F 和485-A;TCP/IP上的标准RFC793和RFC791。
Modbus协议以简单而雅观著称,是一个请求/应答协议,并且提供功能码规定的服务。一个通用的帧结构如下图所示:
那么对于使用Modbus来进行通讯和传递数据的工程师来说,就关注几个部分即可,地址域、功能码和数据。
先说数据
再来看看功能码
至于地址,则需要根据具体设备的说明书或者厂家定义来确定,
以西门子公司的PLC为例。
以某公司全自动温控箱为例
我们清楚了通用的数据帧之后,就可以进行一次简单的Modbus通讯了,因为客户机/服务器的机制,在一个正确接收的通讯过程,不出现与请求功能相关的差错,服务器就将向客户机相应数据域包括请求的数据本身。
至此,我已经将厚厚一本的Modbus规范和标准抽丝剥茧,对于我们应用工程师来说,了解了以上内容就可以开始应用了。
2.       NI DSC模块中的Modbus特点
NI的LabVIEW从8.0开始引入了本地ModbusTCP和ModbusSerial对任何以太网或串行端口的支持,通过DSC模块,采用窗口配置的方式就能创建Modbus的服务器或者客户端,并能指定不同的寄存器用于读写。如果客户使用的较早版本的LabVIEW或者没有安装DSC模块,依然可以使用LabVIEW的Modbus库在任意以太网或者串行端口上创建Modbus主从设备应用。
3.       连接实例(以西门子PLC S7-200 CPU221为例)
1)      先为PLC正确连接电源,设置好通讯参数,并烧录程序
2)      在LabVIEW中创建好一个Modbus的主设备
进行相应的配置,需要关注的几个参数为模式(Serial或者TCP/IP)、地址(根据设备手册)、报文格式(RTU或者ASCII)这几个。
3)      创建相应的变量,根据本文之前的西门子PLC的地址列表,000001就代表了PLC中的Q0.0,400001就代表了VB0,但是我们可以看到有些差别,中间会多出一个零,原因可以参考 http://digital.ni.com/public.nsf/allkb/41F3B1558F671521862577BA002C500F
4)      做完这些设置就可以开始编程了,编写一个简单的测试程序来监控和改写PLC中Q0.0、Q0.1以及VB0和VB1的值,可以正常读写和监测。


相关链接:

http://sine.ni.com/nips/cds/view/p/lang/zhs/nid/201711
http://digital.ni.com/public.nsf/allkb/9289275474D402678625795D0040CC02
http://digital.ni.com/public.nsf/allkb/41F3B1558F671521862577BA002C500F




附件: 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
与西门子PLC通讯的Labview库,还不错。 以下是英文声明: Warning, the attached files are experimental VIs provided for educational purposes only. No warrantee is expressed or implied. You should test your code and completely understand the implications of writing to or reading from an operating PLC. PLCs are often used to control hazardous processes and/or equipment. Writing to or reading from a PLC in active control of equipment or process can result in the disruption of the PLC program or data areas, potentially causing economic loss, property damage, generation or release of hazardous substances and/or personal injury up to and including death. Test your software in a controlled environment and qualify it before using it on active equipment or processes. To my knowledge, Siemens has never released the details of the S7 protocol. Much of what is publicly available on S7 is based on observations of the protocol by others. There is an open source S7 data exchange package available at http://libnodave.sourceforge.net/ that documents many S7 features. These VIs were based upon the example posted at http://decibel.ni.com/content/docs/DOC-5467. They were modified by observing and mimicking a S7 data exchange between a protocol converter and a S7-300 series PLC. The observations were accomplished with the wireshark network protocol analyzer, available at http://www.wireshark.org/ and the Wireshark Plugin f黵 S7-Protokoll, available at http://sps-forum.de/showthread.php?p=202763. The Address Area parameter determines which PLC memory area is the target for the reads and writes. I have only tested reads and writes to the Data Block (DB) Area. Your application should read and write to separate read and write DBs dedicated for transfer only. By confining reads and writes to dedicated Data Blocks, the risk of unintended overwrites may be reduced. The data type: S7Com_Transport_Size.ctl is a ring variable that contains the parameter for the size or type of the transfer. I have only had success with the BYTE, INT and DINT transport sizes on an S7-300 series PLC. The other sizes remain in the ring variable for testing on other PLCs. I do not know if the bytes of a multiple-byte variable are read or written atomically. The example contains two top-level VIs: S7Com_Once.vi and S7Com_W+R_Loop.vi. S7Com_Once.vi performs one read or write per execution. It writes to the target PLC from an array of I32, it reads from the target PLC into an array of I32. The number of bytes written is proportional to the number of elements in the array to be written and the transport size parameter. S7Com_W+R_Loop.vi regularly writes to and reads from a pair of DBs in the target PLC. In order to use it, your Step7 PLC project should provide two Data Blocks, DB11 and DB12. See the screen capture image db11&12.PNG for their layout. After downloading the DBs to your PLC, monitor and change DB VAlues with a VAT. As can be seen in the VI, the transfer size is DWORD. Included are example VIs (S7Com_to_PLC(SubVI).vi and S7Com_from_PLC(SubVI).vi) to map variables between Labview and the S7 PLC DBs. In the example, they use the same cluster type definition (S7Com_PLC_Data.ctl) but that is not a requirement. When you change the mappings, you must calculate the size of the variables to be read from the PLC in order to use the correct Read Length parameter. The read length parameter is in transport size units. The write length parameter is taken from the size of the write data array, so if the write data array is larger than you anticipate, data may be overwritten. The hex byte and hex byte array type definitions were created to format the internal data structures to show hex values so that they could be easily compared to the wireshark packet dumps. These VIs have been minimally tested on a NI 9072 cRIO. This is not finished. Needed are better error checking, cleaner S7 Response decoding, and stress testing with malformed data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值