您当前的位置:五五电子网电子知识plc技术解决方案vb通过opc连接访问s7-300(2) 正文
vb通过opc连接访问s7-300(2)

vb通过opc连接访问s7-300(2)

点击数:7220 次   录入时间:03-04 11:49:50   整理:http://www.55dianzi.com   解决方案
‘服务器端句柄

Dim abErrors() As Long

Dim i As Long

ItemCount=3

abItemIDs(1) = " S7:[S7 connection_1]IB1"

abItemIDs(2) = " S7:[S7 connection_1]MB1"

abItemIDs(3) = " S7:[S7 connection_1]QB1"

for i="1" t0 3

abClientHandles(i) = i

‘给客户端句柄赋值

Next

MyOPCGroup.OPCItems.AddItems ItemCount, abItemIDs, abClientHandles, abServerHandles, abErrors

‘添加数据项操作

(4) 同步数据读写

OPC数据存取有同步方式和异步方式两种。异步读写数据复杂,需要与事件结合使用,与同步相比速度慢但准确性高。同步读写数据简单,直接使用OPCItem的方法即可。

Dim One As OPCItem

Dim Index As Long

‘Index为标签顺序号

Dim OneRead As String

Dim Xie As String

Set One = MyOPCGroup.OPCItems(Index)

One.Read OPCCache

OneRead = One.Value

‘读数据

MyOPCGroup.SyncWrite ItemCount, ServerHandles, valuess, Errors

‘同步写数据

MyOPCGroup.SyncRead OPCCache, ItemCount, ServerHandles, ReadValue, Errors

‘同步读数据

One.Write (Xie)

‘写数据

若只读取数据,可以使用DataChange事件,当控制器中所要访问的数据一旦发生改变时将会触发该事件,并将该数据自动读到TxtValue文本框。

Private Sub MyOPCGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)

‘自动刷新数据

Dim i As Long

For i = 1 To NumItems

txtValue(ClientHandles(i)) = ItemValues(i)

‘获取项的值

txtTime(ClientHandles(i)) = TimeStamps(i)

‘获取项的时间戳

txtQuantity(ClientHandles(i))=GetQualityString(Qualities(i))

‘获取项的品质

Next i

End Sub

(5) 断开OPC服务器

MyOPCServer.OPCGroups.RemoveAll

‘移除所有OPC Group,空出资源

Set MyOPCGroup = Nothing

MyOPCServer.Disconnect

‘断开连接

注意:在VB中还要引用 siemens opc daautomation 2.0。

本文关键字:暂无联系方式解决方案plc技术 - 解决方案