四、利用VB开发DDE 客户端
VB作为Microsoft 公司软件产品的重要一员,理所当然的对Windows操作系统下的DDE技术给予支持。VB为一些控件提供了用于DDE动态数据交换的linktopic、linkitem、linktimeout、linkmode等属性,以及linkrequest、linkpoke、linkexecute、linksend等方法。其中,linktopic、linkitem两项属性分别对应于DDE通讯架构中的主题名和数据项名,linktimeout、linkmode分别表示DDE 客户端与DDE服务器的尝试连接时间和连接模式(数据交换方式),linkmode有Manual、Notify、Automatic三种方式,而Automatic方式最为常用,它主要用于和DDE server建立“热连接(hot link)”,即在DDE服务器数据发生变化时主动发送数据给客户端。linkrequest、linkpoke分别用于DDE 客户端对DDE服务器数据的读写操作。
(一)、 在VB中实现与A-B PLC的 “热连接”
在Visual Basi建立一个新工程,添加几个label控件,并建立和RSLinx的DDE通讯连接,使其随上一步所设置好的连接的A-B PLC数据“B01主系统”数值的变化而变化。
其代码如下:
Private Sub Form_Load()
Label1.LinkTopic = 'rslinx|' & 'B01主系统'
Label1.LinkItem = 's:42'
Label1.LinkMode = 1
Label1.LinkRequest
Label3.LinkTopic = 'rslinx|' & 'B01主系统'
Label3.LinkItem = 's:41'
Label3.LinkMode = 1
Label3.LinkRequest
Label5.LinkTopic = 'rslinx|' & 'B01主系统'
Label5.LinkItem = 's:40'
Label5.LinkMode = 1
Label5.LinkRequest
Label10.LinkTopic = 'rslinx|' & 'B01主系统'
Label10.LinkItem = 'N60:11'
Label10.LinkMode = 1
Label10.LinkRequest
Label12.LinkTopic = 'rslinx|' & 'B01主系统'
Label12.LinkItem = 'N60:130'
Label12.LinkMode = 1
Label12.LinkRequest
Label14.LinkTopic = 'rslinx|' & 'B01主系统'
Label14.LinkItem = 'N60:131'
Label14.LinkMode = 1
Label14.LinkRequest
End Sub
其中,代码中的“rslinx”和“B01主系统”分别表示应用程序名和主题名,“linkmode”值为1表示连接方式为automatic(即“热连接”)方式。这样当该应用代码被执行时,label的值便随A-B PLC 内存值的变化而变化。具体执行如图6:

图6
(二)、在Excel中实现通过 DDE 方式访问A-B PLC
首先必须先安装RS Linx创建DDE topic. 比如我们定义一个topic名叫“B01主系统”
打开Excel,创建宏命令 ,编写VBA脚本如何读取和写入数据,从而完成与PLC的数据交换.
Sub Start()
Dim lngRow As Long
Dim varCycle As Variant
Dim varLogging As Variant
Dim varResults As Variant
On Error GoTo Error
RSIchan = DDEInitiate('RSLinx', ' B01主系统')
varLogging = DDERequest(RSIchan, 'N60/163')
varCycle = DDERequest(RSIchan, 'N60/161')
l/?
If varCycle(1) = '1' And varLogging(1) = '1' Then
lngRow = 3
If Range('INDATA!A3').Value > 3 Then
lngRow = Range('INDATA!A3').Value
End If
For lngRow = lngRow To 65500
If Cells(lngRow, 1) = '' Then Exit For
Range('INDATA!A3').Value = lngRow + 1
Next
RSIchan = DDEInitiate('RSLinx', 'N1')
f810data = DDERequest(RSIchan, 'F8:10')
f811data = DDERequest(RSIchan, 'F8:11')
f812data = DDERequest(RSIchan, 'F8:12')
f816data = DDERequest(RSIchan, 'F8:16')
f818data = DDERequest(RSIchan, 'F8:18')
f817data = DDERequest(RSIchan, 'F8:17')
f820data = DDERequest(RSIchan, 'F8:20')
f821data = DDERequest(RSIchan, 'F8:21')
f822data = DDERequest(RSIchan, 'F8:22')
f823data = DDERequest(RSIchan, 'F8:23')
f824data = DDERequest(RSIchan, 'F8:24')
varResults = DDERequest(RSIchan, 'F8:25')
DDETerminate (RSIchan)
Cells(lngRow, 1).Value = f810data
Cells(lngRow, 2).Value = f811data
Cells(lngRow, 3).Value = f812data
Cells(lngRow, 4).Value = f816data
Cells(lngRow, 5).Value = f818data
Cells(lngRow, 6).Value = f817data
Cells(lngRow, 7).Value = f820data
Cel 2/3 首页 上一页 1 2 3 下一页 尾页 |