VB版 同步和异步通讯有所不一样 根据你的数据量选择模式
这个是我的AB通讯程序不需要更改可以直接使用,RSLinx必须有授权否则无法通讯。
如果被挂起来可以考虑放到线程里面运行比较稳定,具体使用如有不清楚的可以打电话给我,后面有Delphi版和C版都不需要更改只要将PLC变量填入指定数组就可以读出和写入数据,Delphi版和C版相对复杂点但稳定性可移植性比较强适合做大程序。
Option Explicit
'OPC对象的声明
Public objServer As OPCServer 'WithEvents(开启所有事件可用,异步通讯时使用)
Public objGroups As OPCGroups
Public objTestGrp As OPCGroup
Public objItems As OPCItems
Public lServerHandles() As Long
Public Sub Connect(strProgID As String, Optional strNode As String)
If objServer Is Nothing Then '建立一个OPC服务器对象
Set objServer = New OPCServer
End If
If objServer.ServerState = OPCDisconnected Then '连接OPC服务器
objServer.Connect strProgID, strNode
End If
If objGroups Is Nothing Then '建立一个OPC组集合
Set objGroups = objServer.OPCGroups
End If
If objTestGrp Is Nothing Then '添加一个OPC组
Set objTestGrp = objGroups.Add(stcOPCConfig.strGroupName)
End If
End Sub
Public Sub Disconnect()
Dim lErrors() As Long
If Not objItems Is Nothing Then
If objItems.Count > 0 Then ' 清除OPC项
objItems.Remove objTestGrp.OPCItems.Count, lServerHandles, lErrors
End If
Set objItems = Nothing
End If
If Not objTestGrp Is Nothing Then ' 清除OPC组
If (objGroups.Count > 0) Then
objGroups.Remove stcOPCConfig.strGroupName
End If
Set objTestGrp = Nothing
End If
If Not objGroups Is Nothing Then
Set objGroups = Nothing
End If
If Not objServer Is Nothing Then
If objServer.ServerState <> OPCDisconnected Then ' 断开OPC服务器.
objServer.Disconnect
End If
Set objServer = Nothing
End If
stcOPCConfig.lngConnectState = 0
End Sub
Public Sub AddItem()
Dim strItemIDs(MAX_TAG) As String
Dim lClientHandles(MAX_TAG) As Long
Dim lErrors() As Long
Dim i As Integer
If objTestGrp Is Nothing Then
Exit Sub
End If
If Not objItems Is Nothing Then
If objItems.Count > 0 Then
Exit Sub
End If
End If
'设置组活动状态
objTestGrp.IsActive = True
'取消组非同期通知
objTestGrp.IsSubscribed = False
'建立OPC项集合
Set objItems = objTestGrp.OPCItems
'加载所有标记名
For i = 1 To stcOPCConfig.lngMaxTagCnt
strItemIDs(i) = stcTagConfig(i).strTagName
lClientHandles(i) = stcTagConfig(i).lngTagHandle
Next
'添加OPC项
objItems.DefaultAccessPath = stcOPCConfig.strAccessPath
Call objItems.AddItems(stcOPCConfig.lngMaxTagCnt, strItemIDs, lClientHandles, lServerHandles, lErrors)
End Sub
Public Sub SyncRead(nSource As Integer, ByRef vtItemValues() As Variant, ByRef lErrors() As Long)
If objTestGrp Is Nothing Or (stcOPCConfig.lngConnectState = 0) Then
Exit Sub
End If
If objTestGrp.OPCItems.Count > 0 Then '同期读取
Call objTestGrp.SyncRead(nSource, stcOPCConfig.lngMaxTagCnt, lServerHandles, vtItemValues, lErrors)
End If
End Sub
Public Sub SyncWrite(nIndex As Integer, ByRef vtItemValues() As Variant, ByRef lErrors() As Long)
Dim lHandle(1) As Long
If objTestGrp Is Nothing Or (stcOPCConfig.lngConnectState = 0) Then
Exit Sub
End If
If objTestGrp.OPCItems.Count = stcOPCConfig.lngMaxTagCnt Then '同期写入
lHandle(1) = lServerHandles(nIndex)
objTestGrp.SyncWrite 1, lHandle(), vtItemValues, lErrors
End If
End Sub
Public Sub ConnectToOPCServer()
Call Connect(stcOPCConfig.strProgID, stcOPCConfig.strNode)
End Sub
Public Sub AddItemToOPCServer()
Dim tmCNT As Long
tmCNT = basWinAPI.GetTickCount
Call AddItem
If Not objTestGrp Is Nothing Then
If objTestGrp.OPCItems.Count = stcOPCConfig.lngMaxTagCnt Then
objTestGrp.UpdateRate = stcOPCConfig.lngUpdateRate
If ((basWinAPI.GetTickCount - tmCNT) <= 5000) Then '加项时间过长算失败
stcOPCConfig.lngConnectState = 1
End If
Else
MsgBox "可编程控制器变量名已更改", vbOKOnly, "加载变量表失败!!!"
End If
End If
End Sub
Public Sub UpdateOPCData()
Dim vtItemValues() As Variant
Dim lErrors() As Long
Dim i As Integer
'同期读取
SyncRead OPCCache, vtItemValues, lErrors
For i = 1 To stcOPCConfig.lngMaxTagCnt
stcTagConfig(i).strData = vtItemValues(i)
Next
End Sub
Public Sub WriteDataToOPCServer(TagHandle As Integer, Data As Variant)
Dim vtItemData(1) As Variant
Dim lError() As Long
vtItemData(1) = Data ' 同期写入
SyncWrite TagHandle, vtItemData, lError
End Sub
评论0