一聚教程网:一个值得你收藏的教程网站

热门教程

OPC客户程序(VB篇――同步)

时间:2022-07-02 11:16:08 编辑:袖梨 来源:一聚教程网

建立如下窗体:

引用如下:
代码如下:
Option Explicit
Dim WithEvents ServerObj As OPCServer
Dim WithEvents GroupObj As OPCGroup
Dim ItemObj As OPCItem
Private Sub Command_Start_Click()
Dim OutText As String

On Error GoTo ErrorHandler

Command_Start.Enabled = False
Command_Read.Enabled = True
Command_Write.Enabled = True
Command_Exit.Enabled = True
OutText = "连接OPC服务器"
Set ServerObj = New OPCServer
ServerObj.Connect ("XXXSERVER")'XXXSERVER为某OPC服务器名称

OutText = "添加组"
Set GroupObj = ServerObj.OPCGroups.Add("Group")

OutText = "Adding an Item to the group"
Set ItemObj = GroupObj.OPCItems.AddItem("XXXITEM", 1)'XXXITEM为添加的ITEM名称

Exit Sub
ErrorHandler: '如果出现异常,则报出错误。
MsgBox Err.Description + Chr(13) + _
OutText, vbCritical, "ERROR"

End Sub
Private Sub Command_Read_Click()'同步读
Dim OutText As String
Dim myValue As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant

On Error GoTo ErrorHandler
OutText = "读ITEM值"
ItemObj.Read OPCDevice, myValue, myQuality, myTimeStamp
Edit_ReadVal = myValue
Edit_ReadQu = GetQualityText(myQuality)
Edit_ReadTS = myTimeStamp

Exit Sub

热门栏目