vb怎么读取或修改opc变量值
opc已经可以与PLC连接,在opcquickclient可以修改plc变量值,但是怎么用VB连接OPC读取opc变量,例如vb中txet1要求显示opc的第一个变量值,...
opc已经可以与PLC连接,在opc quick client可以修改plc变量值,但是怎么用VB连接OPC读取opc变量,例如vb中txet1要求显示opc的第一个变量值,该怎么写程序?
怎么没有人愿意帮我啊 展开
怎么没有人愿意帮我啊 展开
2个回答
2015-08-07 · 知道合伙人互联网行家
关注
展开全部
首先申明OPC对象:
Option Base 1
Dim WithEvents ServerObj As OPCServer 'OPC Server对象,连接OPC服务器
Dim GroupsObj As OPCGroups 'OPC Groups对象,添加OPC组
Dim WithEvents GroupObj As OPCGroup 'OPC Group对象
Dim ItemsObj As OPCItems 'OPC Item集合
Dim ServerHandles() As Long '服务器端OPC Item的句柄
Dim ClientHandles() as Long '客户端OPC Item的句柄
Dim ItemId(2) As String
Dim Errors() As Long
接下来,生成各个对象:
If ServerObj Is Nothing Then Set ServerObj = New OPCServer
'连接OPC服务器
If ServerObj.ServerState = OPCDisconnected Then
ServerObj.Connect ("OPC.SimaticNET") '假设OPC服务器运行在本机
End If
If GroupsObj Is Nothing Then Set GroupsObj = ServerObj.OPCGroups
If GroupObj Is Nothing Then Set GroupObj = GroupsObj.Add
If ItemsObj Is Nothing Then Set ItemsObj = GroupObj.OPCItems
GroupObj.IsActive = True '设置组为活动状态
'假设有两个数据源,一个是8位开关量输入,一个是8位开关量输出
ItemId(1) = "S7:[S7 connection_1]IB0"
ItemId(2) = "S7:[S7 connection_1]QB0"
ClientHandles(1) = 1
ClientHandles(2) = 2
'添加组项目,ServerHandles数组的值为各个OPC Item的服务器句柄,
' ClientHandles数组的值为各个OPC Item的客户端句柄,由应用程序设定
Call ItemsObj.AddItems(2, ItemId, ClientHandles, ServerHandles, Errors)
Option Base 1
Dim WithEvents ServerObj As OPCServer 'OPC Server对象,连接OPC服务器
Dim GroupsObj As OPCGroups 'OPC Groups对象,添加OPC组
Dim WithEvents GroupObj As OPCGroup 'OPC Group对象
Dim ItemsObj As OPCItems 'OPC Item集合
Dim ServerHandles() As Long '服务器端OPC Item的句柄
Dim ClientHandles() as Long '客户端OPC Item的句柄
Dim ItemId(2) As String
Dim Errors() As Long
接下来,生成各个对象:
If ServerObj Is Nothing Then Set ServerObj = New OPCServer
'连接OPC服务器
If ServerObj.ServerState = OPCDisconnected Then
ServerObj.Connect ("OPC.SimaticNET") '假设OPC服务器运行在本机
End If
If GroupsObj Is Nothing Then Set GroupsObj = ServerObj.OPCGroups
If GroupObj Is Nothing Then Set GroupObj = GroupsObj.Add
If ItemsObj Is Nothing Then Set ItemsObj = GroupObj.OPCItems
GroupObj.IsActive = True '设置组为活动状态
'假设有两个数据源,一个是8位开关量输入,一个是8位开关量输出
ItemId(1) = "S7:[S7 connection_1]IB0"
ItemId(2) = "S7:[S7 connection_1]QB0"
ClientHandles(1) = 1
ClientHandles(2) = 2
'添加组项目,ServerHandles数组的值为各个OPC Item的服务器句柄,
' ClientHandles数组的值为各个OPC Item的客户端句柄,由应用程序设定
Call ItemsObj.AddItems(2, ItemId, ClientHandles, ServerHandles, Errors)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询