版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、OPC客户程序(VB篇异步) 来源: 责任编辑: 作者: 41建立如下窗体: 引用如下: 代码如下: Option Explicit Option Base 1 Const WRITEASYNC_ID = 1 Const READASYNC_ID = 2 Const REFRESHASYNC_ID = 3 '- ' Interface Objects '- Public WithEvents ServerObj As OPCServer Public WithEvents GroupObj As OPCGroup
2、 Dim ItemObj1 As OPCItem Dim ItemObj2 As OPCItem Dim Serverhandle(2) As Long Private Sub chkGroupActive_Click() If chkGroupActive = 1 Then GroupObj.IsActive = 1 Else GroupObj.IsActive = 0 End If End Sub Private Sub Command_Start_Click() Dim OutText As String On Error GoTo ErrorHandler Command_Start.
3、Enabled = False Command_Read.Enabled = True Command_Write.Enabled = True Command_Exit.Enabled = True chkGroupActive.Enabled = True OutText = "连接OPC服务器" Set ServerObj = New OPCServer ServerObj.Connect ("XXXSERVER") OutText = "添加组" Set GroupObj = ServerObj.OPCGroups.Add(&
4、quot;Group") GroupObj.IsSubscribed = True chkGroupActive_Click OutText = "添加ITEM" Set ItemObj1 = GroupObj.OPCItems.AddItem("XXXITEM1", 1) Set ItemObj2 = GroupObj.OPCItems.AddItem("XXXITEM2", 2) Serverhandle(1) = ItemObj1.Serverhandle Serverhandle(2) = ItemObj2.Serv
5、erhandle 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 Dim ClientID As Long Dim ServerID As Long Dim
6、 ErrorNr() As Long Dim ErrorString As String On Error GoTo ErrorHandler OutText = "读值" ClientID = READASYNC_ID GroupObj.AsyncRead 1, Serverhandle, ErrorNr, ClientID, ServerID If ErrorNr(1) <> 0 Then ErrorString = ServerObj.GetErrorString(ErrorNr(1) MsgBox ErrorString, vbCritical, &qu
7、ot;Error AsyncRead()" End If Erase ErrorNr Exit Sub ErrorHandler: MsgBox Err.Description + Chr(13) + _ OutText, vbCritical, "ERROR" End Sub Private Sub Command_Write_Click() '异步写 Dim OutText As String Dim Serverhandles(1) As Long Dim MyValues(1) As Variant Dim ErrorNr() As Long Di
8、m ErrorString As String Dim Cancel_id As Long OutText = "Writing Value" On Error GoTo ErrorHandler MyValues(1) = Edit_WriteVal GroupObj.AsyncWrite 1, Serverhandle, MyValues, ErrorNr, WRITEASYNC_ID, Cancel_id If ErrorNr(1) <> 0 Then ErrorString = ServerObj.GetErrorString(ErrorNr(1) Ms
9、gBox ErrorString, vbCritical, "Error AsyncRead()" End If Erase ErrorNr Exit Sub ErrorHandler: MsgBox Err.Description + Chr(13) + _ OutText, vbCritical, "ERROR" End Sub Private Sub Command_Exit_Click() '停止 Dim OutText As String On Error GoTo ErrorHandler Command_Start.Enabled
10、= True Command_Read.Enabled = False Command_Write.Enabled = False Command_Exit.Enabled = False chkGroupActive.Enabled = False OutText = "Removing Objects" Set ItemObj1 = Nothing Set ItemObj2 = Nothing ServerObj.OPCGroups.RemoveAll Set GroupObj = Nothing ServerObj.Disconnect Set ServerObj =
11、 Nothing Exit Sub ErrorHandler: MsgBox Err.Description + Chr(13) + _ OutText, vbCritical, "ERROR" End Sub '异步读回调 Private Sub GroupObj_AsyncReadComplete(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps
12、() As Date, Errors() As Long) Dim ErrorString As String If (TransactionID = READASYNC_ID) Then If Errors(1) = 0 Then Edit_ReadVal = ItemValues(1) Edit_ReadQu = GetQualityText(Qualities(1) Edit_ReadTS = TimeStamps(1) Else ErrorString = ServerObj.GetErrorString(Errors(1) MsgBox ErrorString, vbCritical
13、, "Error AsyncReadComplete()" End If End If End Sub '异步写回调 Private Sub GroupObj_AsyncWriteComplete(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, Errors() As Long) Dim ErrorString As String If (TransactionID = WRITEASYNC_ID) Then If Errors(1) = 0 Then Edi
14、t_WriteRes = ServerObj.GetErrorString(Errors(1) Else ErrorString = ServerObj.GetErrorString(Errors(1) MsgBox ErrorString, vbCritical, "Error AsyncWriteComplete()" End If End If End Sub '回调 Private Sub GroupObj_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandle
15、s() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date) Dim i As Long For i = 1 To NumItems Edit_OnDataVal(i - 1) = ItemValues(i) Edit_OnDataQu(i - 1) = GetQualityText(Qualities(i) Edit_OnDataTS(i - 1) = TimeStamps(i) Next i End Sub Private Function GetQualityText(Quality) A
16、s String Select Case Quality Case 0: GetQualityText = "BAD" Case 64: GetQualityText = "UNCERTAIN" Case 192: GetQualityText = "GOOD" Case 8: GetQualityText = "NOT_CONNECTED" Case 13: GetQualityText = "DEVICE_FAILURE" Case 16: GetQualityText = "SENSOR_FAILURE" Case 20: GetQualityText = "LAST_KNOWN" Case 24: GetQualityText = "COMM_FAILURE" Case 28: GetQualityText = "OUT_OF_SERVICE" Case 132: GetQualityText = "LAST_USABLE" Case 144: GetQuali
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2030年中国废镍利用项目可行性研究报告
- 2024至2030年无纺布挂历袋项目投资价值分析报告
- 2024至2030年中国大象六人转椅数据监测研究报告
- 2024年摩托车交易协议样本2
- 2024公司入伙及退伙规范协议
- 2024年木工施工承包协议模板
- 2024年中国自攻钻床市场调查研究报告
- 2024年中国电子闪光警号市场调查研究报告
- 2024年跨境电子商贸履行服务协议
- 国际贸易信息安全管理制度
- 2024年8月CCAA国家注册审核员《管理体系认证基础》考试题目含解析
- 2024新版(北京版)三年级英语上册单词带音标
- 户外广告安装施工方案
- 钠离子电池低成本硬碳负极关键技术开发-2024-10-技术资料
- 2025届广东省珠海市紫荆中学高二数学第一学期期末教学质量检测试题含解析
- 期中测试卷(1-3单元)(试题)2024-2025学年六年级上册数学苏教版
- 现代农业课件教学课件
- 2024年专业技术人员继续教育公需科目-职业幸福感的提升考试近5年真题集锦(频考类试题)带答案
- 农业无人机物流行业发展方向及匹配能力建设研究报告
- 2024年深圳市中考英语试题及解析版
- 《自制信封》教学设计-小学劳动苏教版《劳动与技术》三年级上册
评论
0/150
提交评论