'============================================================================== ' TITLE: Form1.vb - Ver2.00(DotNetDAautoSample) ' ' CONTENTS: ' ' ' (c) Copyright 2004-2010 Takebishi Electric Sales Corporation ' ALL RIGHTS RESERVED. ' ' DISCLAIMER: ' This code is provided by the Takebishi Corporation solely to assist ' in understanding and use of the appropriate OPC Specification(s). ' This code is provided as-is and without warranty or support of any sort. ' ' MODIFICATION LOG: ' ' Version Date By Notes ' -------- -------- -------- -------- ' 1.00 2004/02/27 Mike First release. ' 1.01 2004/12/27 Mike Revised memory leak. ' 1.02 2005/02/08 Mike Fixed to support array type. ' 1.03 2005/03/10 Mike Fixed that if object is nothing, do not read value. ' Fixed to change value to string. ' 1.04 2005/04/21 Mike Used method is changed from 'Read' of OPCItem to 'SyncRead' of OPCGroup. ' 1.05 2006/09/21 Mike Modified for VS2005. ' Fixed to add product version. ' 2.00 2010/02/05 ykishimo Fixed ini file path for Windows Vista and Windows7. '============================================================================== Option Strict Off Option Explicit On Imports System.Runtime.InteropServices Imports System.Threading ' 06/09/21 for VS2005 Public Class Form1 Inherits System.Windows.Forms.Form ' This delegate enables asynchronous calls for setting ' the text property on a TextBox control. Delegate Sub SetValueCallback(ByVal i As Integer) ' 06/09/21 for VS2005 Delegate Sub SetTimeCallback(ByVal i As Integer) ' 06/09/21 for VS2005 Delegate Sub SetQualityCallback(ByVal i As Integer) ' 06/09/21 for VS2005 ' This thread is used to demonstrate both thread-safe and ' unsafe ways to call a Windows Forms control. Private threadCopy As Thread = Nothing ' 06/09/21 for VS2005 Const ITEMMAX As Integer = 8 Const STR_OPC_INI As String = "DAAutoDotNET.ini" Const VAL_CTRL_SPACE As Integer = 8 Const VAL_CTRL_TOP As Integer = 192 Const VAL_CTRL_LEFT As Integer = 16 Const VAL_ITEMNAME_HEIGHT As Integer = 24 Const VAL_ITEMNAME_WIDTH As Integer = 88 Const VAL_VALUE_HEIGHT As Integer = VAL_ITEMNAME_HEIGHT Const VAL_VALUE_WIDTH As Integer = 48 Const VAL_TIME_HEIGHT As Integer = VAL_ITEMNAME_HEIGHT Const VAL_TIME_WIDTH As Integer = 128 Const VAL_QUALITY_HEIGHT As Integer = VAL_ITEMNAME_HEIGHT Const VAL_QUALITY_WIDTH As Integer = 40 Dim WithEvents OPCMyServer As OPCAutomation.OPCServer Dim WithEvents OPCMyGroups As OPCAutomation.OPCGroups Dim WithEvents OPCMyGroup As OPCAutomation.OPCGroup Dim OPCMyItems As OPCAutomation.OPCItems Dim OPCMyItem As OPCAutomation.OPCItem Dim sItemName(ITEMMAX) As String Dim cH(ITEMMAX) As Integer Dim sH(ITEMMAX) As Integer Dim oVal(ITEMMAX) As Object Dim dTime(ITEMMAX) As Date Dim wQuality(ITEMMAX) As Short Dim bConnect As Boolean Dim csSpFolder As String Dim csIniPath As String Dim txtItemName(ITEMMAX - 1) As System.Windows.Forms.TextBox Dim txtValue(ITEMMAX - 1) As System.Windows.Forms.TextBox Dim txtTime(ITEMMAX - 1) As System.Windows.Forms.TextBox Dim txtQuality(ITEMMAX - 1) As System.Windows.Forms.TextBox Enum COPY_DIR DISP_TO_MEM ' display to memory MEM_TO_DISP ' memory to display End Enum #Region " Windows フォーム デザイナで生成されたコード " Public Sub New() MyBase.New() ' この呼び出しは Windows フォーム デザイナで必要です。 InitializeComponent() ' InitializeComponent() 呼び出しの後に初期化を追加します。 End Sub ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ' Windows フォーム デザイナで必要です。 Private components As System.ComponentModel.IContainer ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 'Windows フォーム デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 Public WithEvents btnAsyncWrite As System.Windows.Forms.Button Public WithEvents btnAsyncRead As System.Windows.Forms.Button Public WithEvents txtSvrName As System.Windows.Forms.TextBox Public WithEvents btnAdvise As System.Windows.Forms.Button Public WithEvents txtUpdateRate As System.Windows.Forms.TextBox Public WithEvents btnWrite As System.Windows.Forms.Button Public WithEvents btnRead As System.Windows.Forms.Button Public WithEvents btnConnect As System.Windows.Forms.Button Public WithEvents Label6 As System.Windows.Forms.Label Public WithEvents Label5 As System.Windows.Forms.Label Public WithEvents Label4 As System.Windows.Forms.Label Public WithEvents Label3 As System.Windows.Forms.Label Public WithEvents Label2 As System.Windows.Forms.Label Public WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label7 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.btnAsyncWrite = New System.Windows.Forms.Button Me.btnAsyncRead = New System.Windows.Forms.Button Me.txtSvrName = New System.Windows.Forms.TextBox Me.btnAdvise = New System.Windows.Forms.Button Me.txtUpdateRate = New System.Windows.Forms.TextBox Me.btnWrite = New System.Windows.Forms.Button Me.btnRead = New System.Windows.Forms.Button Me.btnConnect = New System.Windows.Forms.Button Me.Label6 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.Label7 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'btnAsyncWrite ' Me.btnAsyncWrite.BackColor = System.Drawing.SystemColors.Control Me.btnAsyncWrite.Cursor = System.Windows.Forms.Cursors.Default Me.btnAsyncWrite.Enabled = False Me.btnAsyncWrite.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnAsyncWrite.ForeColor = System.Drawing.SystemColors.ControlText Me.btnAsyncWrite.Location = New System.Drawing.Point(240, 120) Me.btnAsyncWrite.Name = "btnAsyncWrite" Me.btnAsyncWrite.RightToLeft = System.Windows.Forms.RightToLeft.No Me.btnAsyncWrite.Size = New System.Drawing.Size(104, 32) Me.btnAsyncWrite.TabIndex = 60 Me.btnAsyncWrite.Text = "Async Write" ' 'btnAsyncRead ' Me.btnAsyncRead.BackColor = System.Drawing.SystemColors.Control Me.btnAsyncRead.Cursor = System.Windows.Forms.Cursors.Default Me.btnAsyncRead.Enabled = False Me.btnAsyncRead.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnAsyncRead.ForeColor = System.Drawing.SystemColors.ControlText Me.btnAsyncRead.Location = New System.Drawing.Point(128, 120) Me.btnAsyncRead.Name = "btnAsyncRead" Me.btnAsyncRead.RightToLeft = System.Windows.Forms.RightToLeft.No Me.btnAsyncRead.Size = New System.Drawing.Size(104, 32) Me.btnAsyncRead.TabIndex = 59 Me.btnAsyncRead.Text = "Async Read" ' 'txtSvrName ' Me.txtSvrName.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.txtSvrName.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtSvrName.Location = New System.Drawing.Point(96, 8) Me.txtSvrName.Name = "txtSvrName" Me.txtSvrName.Size = New System.Drawing.Size(136, 20) Me.txtSvrName.TabIndex = 58 Me.txtSvrName.Text = "Takebishi.Dxp.1" ' 'btnAdvise ' Me.btnAdvise.BackColor = System.Drawing.SystemColors.Control Me.btnAdvise.Cursor = System.Windows.Forms.Cursors.Default Me.btnAdvise.Enabled = False Me.btnAdvise.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnAdvise.ForeColor = System.Drawing.SystemColors.ControlText Me.btnAdvise.Location = New System.Drawing.Point(16, 120) Me.btnAdvise.Name = "btnAdvise" Me.btnAdvise.RightToLeft = System.Windows.Forms.RightToLeft.No Me.btnAdvise.Size = New System.Drawing.Size(104, 32) Me.btnAdvise.TabIndex = 57 Me.btnAdvise.Text = "Advise" ' 'txtUpdateRate ' Me.txtUpdateRate.AcceptsReturn = True Me.txtUpdateRate.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.txtUpdateRate.BackColor = System.Drawing.SystemColors.Window Me.txtUpdateRate.Cursor = System.Windows.Forms.Cursors.IBeam Me.txtUpdateRate.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtUpdateRate.ForeColor = System.Drawing.SystemColors.WindowText Me.txtUpdateRate.Location = New System.Drawing.Point(96, 40) Me.txtUpdateRate.MaxLength = 0 Me.txtUpdateRate.Name = "txtUpdateRate" Me.txtUpdateRate.RightToLeft = System.Windows.Forms.RightToLeft.No Me.txtUpdateRate.Size = New System.Drawing.Size(64, 20) Me.txtUpdateRate.TabIndex = 54 Me.txtUpdateRate.Text = "1000" Me.txtUpdateRate.TextAlign = System.Windows.Forms.HorizontalAlignment.Right ' 'btnWrite ' Me.btnWrite.BackColor = System.Drawing.SystemColors.Control Me.btnWrite.Cursor = System.Windows.Forms.Cursors.Default Me.btnWrite.Enabled = False Me.btnWrite.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnWrite.ForeColor = System.Drawing.SystemColors.ControlText Me.btnWrite.Location = New System.Drawing.Point(240, 80) Me.btnWrite.Name = "btnWrite" Me.btnWrite.RightToLeft = System.Windows.Forms.RightToLeft.No Me.btnWrite.Size = New System.Drawing.Size(104, 32) Me.btnWrite.TabIndex = 49 Me.btnWrite.Text = "Write" ' 'btnRead ' Me.btnRead.BackColor = System.Drawing.SystemColors.Control Me.btnRead.Cursor = System.Windows.Forms.Cursors.Default Me.btnRead.Enabled = False Me.btnRead.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnRead.ForeColor = System.Drawing.SystemColors.ControlText Me.btnRead.Location = New System.Drawing.Point(128, 80) Me.btnRead.Name = "btnRead" Me.btnRead.RightToLeft = System.Windows.Forms.RightToLeft.No Me.btnRead.Size = New System.Drawing.Size(104, 32) Me.btnRead.TabIndex = 48 Me.btnRead.Text = "Read" ' 'btnConnect ' Me.btnConnect.BackColor = System.Drawing.SystemColors.Control Me.btnConnect.Cursor = System.Windows.Forms.Cursors.Default Me.btnConnect.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnConnect.ForeColor = System.Drawing.SystemColors.ControlText Me.btnConnect.Location = New System.Drawing.Point(240, 16) Me.btnConnect.Name = "btnConnect" Me.btnConnect.RightToLeft = System.Windows.Forms.RightToLeft.No Me.btnConnect.Size = New System.Drawing.Size(104, 32) Me.btnConnect.TabIndex = 47 Me.btnConnect.Text = "Connect" ' 'Label6 ' Me.Label6.BackColor = System.Drawing.SystemColors.Control Me.Label6.Cursor = System.Windows.Forms.Cursors.Default Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label6.ForeColor = System.Drawing.SystemColors.ControlText Me.Label6.Location = New System.Drawing.Point(16, 40) Me.Label6.Name = "Label6" Me.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No Me.Label6.Size = New System.Drawing.Size(80, 16) Me.Label6.TabIndex = 56 Me.Label6.Text = "Update Rate" Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label5 ' Me.Label5.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.Label5.BackColor = System.Drawing.SystemColors.Control Me.Label5.Cursor = System.Windows.Forms.Cursors.Default Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label5.ForeColor = System.Drawing.SystemColors.ControlText Me.Label5.Location = New System.Drawing.Point(16, 8) Me.Label5.Name = "Label5" Me.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No Me.Label5.Size = New System.Drawing.Size(80, 16) Me.Label5.TabIndex = 55 Me.Label5.Text = "Server Name" Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Label4 ' Me.Label4.BackColor = System.Drawing.SystemColors.Control Me.Label4.Cursor = System.Windows.Forms.Cursors.Default Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label4.ForeColor = System.Drawing.SystemColors.ControlText Me.Label4.Location = New System.Drawing.Point(304, 168) Me.Label4.Name = "Label4" Me.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No Me.Label4.Size = New System.Drawing.Size(48, 16) Me.Label4.TabIndex = 53 Me.Label4.Text = "Quality" ' 'Label3 ' Me.Label3.BackColor = System.Drawing.SystemColors.Control Me.Label3.Cursor = System.Windows.Forms.Cursors.Default Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label3.ForeColor = System.Drawing.SystemColors.ControlText Me.Label3.Location = New System.Drawing.Point(168, 168) Me.Label3.Name = "Label3" Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No Me.Label3.Size = New System.Drawing.Size(65, 16) Me.Label3.TabIndex = 52 Me.Label3.Text = "Date/Time" ' 'Label2 ' Me.Label2.BackColor = System.Drawing.SystemColors.Control Me.Label2.Cursor = System.Windows.Forms.Cursors.Default Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText Me.Label2.Location = New System.Drawing.Point(112, 168) Me.Label2.Name = "Label2" Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No Me.Label2.Size = New System.Drawing.Size(41, 16) Me.Label2.TabIndex = 51 Me.Label2.Text = "Value" ' 'Label1 ' Me.Label1.BackColor = System.Drawing.SystemColors.Control Me.Label1.Cursor = System.Windows.Forms.Cursors.Default Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText Me.Label1.Location = New System.Drawing.Point(16, 168) Me.Label1.Name = "Label1" Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No Me.Label1.Size = New System.Drawing.Size(65, 16) Me.Label1.TabIndex = 50 Me.Label1.Text = "Item Name" ' 'Label7 ' Me.Label7.Location = New System.Drawing.Point(232, 448) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(128, 16) Me.Label7.TabIndex = 61 Me.Label7.Text = "DAAutoDotNET ver2.0" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(360, 470) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.btnAsyncWrite) Me.Controls.Add(Me.btnAsyncRead) Me.Controls.Add(Me.txtSvrName) Me.Controls.Add(Me.btnAdvise) Me.Controls.Add(Me.txtUpdateRate) Me.Controls.Add(Me.btnWrite) Me.Controls.Add(Me.btnRead) Me.Controls.Add(Me.btnConnect) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i% ' create TextBox For i = 0 To ITEMMAX - 1 txtItemName(i) = New System.Windows.Forms.TextBox txtItemName(i).AutoSize = False txtItemName(i).Top = VAL_CTRL_TOP + (VAL_ITEMNAME_HEIGHT + VAL_CTRL_SPACE) * i txtItemName(i).Left = VAL_CTRL_LEFT txtItemName(i).Height = VAL_ITEMNAME_HEIGHT txtItemName(i).Width = VAL_ITEMNAME_WIDTH Me.Controls.Add(txtItemName(i)) txtValue(i) = New System.Windows.Forms.TextBox txtValue(i).AutoSize = False txtValue(i).Top = VAL_CTRL_TOP + (VAL_VALUE_HEIGHT + VAL_CTRL_SPACE) * i txtValue(i).Left = VAL_CTRL_LEFT + (VAL_ITEMNAME_WIDTH + VAL_CTRL_SPACE) txtValue(i).Height = VAL_VALUE_HEIGHT txtValue(i).Width = VAL_VALUE_WIDTH Me.Controls.Add(txtValue(i)) txtTime(i) = New System.Windows.Forms.TextBox txtTime(i).AutoSize = False txtTime(i).Top = VAL_CTRL_TOP + (VAL_TIME_HEIGHT + VAL_CTRL_SPACE) * i txtTime(i).Left = VAL_CTRL_LEFT + (VAL_ITEMNAME_WIDTH + VAL_CTRL_SPACE) + (VAL_VALUE_WIDTH + VAL_CTRL_SPACE) txtTime(i).Height = VAL_TIME_HEIGHT txtTime(i).Width = VAL_TIME_WIDTH Me.Controls.Add(txtTime(i)) txtQuality(i) = New System.Windows.Forms.TextBox txtQuality(i).AutoSize = False txtQuality(i).Top = VAL_CTRL_TOP + (VAL_QUALITY_HEIGHT + VAL_CTRL_SPACE) * i txtQuality(i).Left = VAL_CTRL_LEFT + (VAL_ITEMNAME_WIDTH + VAL_CTRL_SPACE) + (VAL_VALUE_WIDTH + VAL_CTRL_SPACE) + (VAL_TIME_WIDTH + VAL_CTRL_SPACE) txtQuality(i).Height = VAL_QUALITY_HEIGHT txtQuality(i).Width = VAL_QUALITY_WIDTH Me.Controls.Add(txtQuality(i)) Next Try ' Get My document Path csSpFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal) csSpFolder = csSpFolder + "\\TAKEBISHI\\DeviceXPlorer OPC Server\\Sample\\" csIniPath = csSpFolder + STR_OPC_INI ' import ini file If System.IO.File.Exists(csIniPath) = True Then FileOpen(1, csIniPath, OpenMode.Input) Input(1, txtSvrName.Text) Input(1, txtUpdateRate.Text) i = 0 Do While Not EOF(1) ' loop until end of file If i > ITEMMAX Then Exit Do End If Input(1, txtItemName(i).Text) i = i + 1 Loop FileClose(1) Else txtSvrName.Text = "Takebishi.Dxp.1" ' 07/06/23 for DXP2007 txtUpdateRate.Text = "1000" For i = 1 To ITEMMAX txtItemName(i - 1).Text = "Device1.D" + Format$(i - 0) Next i End If Catch ex As Exception Trace.WriteLine(ex.ToString()) End Try End Sub Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed Dim i As Short If bConnect = True Then btnConnect_Click(btnConnect, New System.EventArgs) End If Try ' Check ini file path If System.IO.File.Exists(csIniPath) = False Then ' create ini file Dim hStream As System.IO.FileStream System.IO.Directory.CreateDirectory(csSpFolder) hStream = System.IO.File.Create(csIniPath) ' Close If Not hStream Is Nothing Then hStream.Close() End If End If FileOpen(1, csIniPath, OpenMode.Output) PrintLine(1, txtSvrName.Text) PrintLine(1, txtUpdateRate.Text) For i = 1 To ITEMMAX PrintLine(1, txtItemName(i - 1).Text) Next i FileClose(1) Catch ex As Exception Trace.Write(ex.ToString()) End Try End Sub Protected Overrides Sub Finalize() MyBase.Finalize() End Sub Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click Dim i% Dim ServerHandles As Array Dim Errors As Array If txtSvrName.Text = "" Then MsgBox("Server Name is not registed.") Exit Sub End If If IsNumeric(txtUpdateRate.Text) = False Then MsgBox("Update Rate is unsuitable.") Exit Sub End If If bConnect = False Then '--- connect OPCServer OPCMyServer = New OPCAutomation.OPCServer OPCMyServer.Connect(txtSvrName.Text, "") OPCMyGroups = OPCMyServer.OPCGroups OPCMyGroup = OPCMyGroups.Add("Group1") OPCMyGroup.UpdateRate = Val(txtUpdateRate.Text) ' UpdateRate is 1000msec OPCMyGroup.IsActive = False OPCMyGroup.IsSubscribed = OPCMyGroup.IsActive OPCMyItems = OPCMyGroup.OPCItems For i = 1 To ITEMMAX sItemName(i) = txtItemName(i - 1).Text cH(i) = i Next OPCMyItems.AddItems(ITEMMAX, sItemName, cH, ServerHandles, Errors) For i = 1 To ITEMMAX If Errors(i) = 0 Then sH(i) = ServerHandles(i) Else MsgBox("Registing Items is failed : " + sItemName(i)) End If Next bConnect = True txtSvrName.Enabled = False txtUpdateRate.Enabled = False btnConnect.Text = "Disconnect" btnAdvise.Enabled = True btnAdvise.Text = "Advise" btnRead.Enabled = True btnWrite.Enabled = True btnAsyncRead.Enabled = False btnAsyncWrite.Enabled = False For i = 1 To ITEMMAX txtItemName(i - 1).Enabled = False Next i ' read data btnRead_Click(btnRead, New System.EventArgs) Else '--- disconnect OPCServer OPCMyGroup.IsActive = False OPCMyGroups.Remove(OPCMyGroup.ServerHandle) OPCMyItems = Nothing OPCMyItem = Nothing OPCMyGroups = Nothing OPCMyGroup = Nothing OPCMyServer.Disconnect() 'ret = Marshal.ReleaseComObject(OPCMyServer) OPCMyServer = Nothing System.GC.Collect() 'Execute Garbage Collection compulsorily. bConnect = False txtSvrName.Enabled = True txtUpdateRate.Enabled = True btnConnect.Text = "Connect" btnAdvise.Enabled = False btnRead.Enabled = False btnWrite.Enabled = False btnAsyncRead.Enabled = False btnAsyncWrite.Enabled = False For i = 1 To ITEMMAX txtItemName(i - 1).Enabled = True Next i End If End Sub Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click '05/04/21 Used method is changed from 'Read' of OPCItem to 'SyncRead' of OPCGroup. Dim ItemValues As Array Dim Qualities As Array Dim TimeStamps As Array Dim Errors As Array Dim i% OPCMyGroup.SyncRead(OPCAutomation.OPCDataSource.OPCDevice, ITEMMAX, sH, ItemValues, Errors, Qualities, TimeStamps) For i = 1 To ITEMMAX oVal(i) = ItemValues(i) dTime(i) = TimeStamps(i) wQuality(i) = Qualities(i) Next i Copy(COPY_DIR.MEM_TO_DISP) 'Dim anItem As OPCAutomation.OPCItem 'For Each anItem In OPCMyGroup.OPCItems ' anItem.Read(OPCAutomation.OPCDataSource.OPCDevice) ', value, qual, time ' If subscribed, don't do this! ' oVal(anItem.ClientHandle) = anItem.Value ' dTime(anItem.ClientHandle) = anItem.TimeStamp ' wQuality(anItem.ClientHandle) = anItem.Quality 'Next anItem 'anItem = Nothing 'Copy(COPY_DIR.MEM_TO_DISP) End Sub Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click Dim Errors As Array Copy(COPY_DIR.DISP_TO_MEM) OPCMyGroup.SyncWrite(ITEMMAX, sH, oVal, Errors) End Sub Private Sub btnAdvise_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdvise.Click OPCMyGroup.IsActive = Not OPCMyGroup.IsActive OPCMyGroup.IsSubscribed = OPCMyGroup.IsActive If OPCMyGroup.IsActive = True Then btnAdvise.Text = "Unadvise" btnAsyncRead.Enabled = True btnAsyncWrite.Enabled = True ' read data btnRead_Click(btnRead, New System.EventArgs) Else btnAdvise.Text = "Advise" btnAsyncRead.Enabled = False btnAsyncWrite.Enabled = False End If End Sub Private Sub btnAsyncRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAsyncRead.Click Static Dim wTransID As Integer = 1000 Dim wCancelID As Integer Dim Errors As Array wTransID = wTransID + 1 OPCMyGroup.AsyncRead(ITEMMAX, sH, Errors, wTransID, wCancelID) 'wTransID = wTransID + 1 'OPCMyGroup.AsyncRead(ITEMMAX, sH, Errors, wTransID, wCancelID) 'OPCMyGroup.AsyncCancel(wCancelID) End Sub Private Sub btnAsyncWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAsyncWrite.Click Static Dim wTransID As Integer = 2000 Dim wCancelID As Integer Dim Errors As Array Copy(COPY_DIR.DISP_TO_MEM) wTransID = wTransID + 1 OPCMyGroup.AsyncWrite(ITEMMAX, sH, oVal, Errors, wTransID, wCancelID) End Sub Private Sub OPCMyGroup_DataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles OPCMyGroup.DataChange Dim i% For i = 1 To NumItems oVal(ClientHandles(i)) = ItemValues(i) dTime(ClientHandles(i)) = TimeStamps(i) wQuality(ClientHandles(i)) = Qualities(i) Next i Me.threadCopy = New Thread(New ThreadStart(AddressOf Me.ThreadProcCopy)) ' 06/09/21 for VS2005 Me.threadCopy.Start() 'Copy(COPY_DIR.MEM_TO_DISP) End Sub Private Sub OPCMyGroup_AsyncReadComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array, ByRef Errors As System.Array) Handles OPCMyGroup.AsyncReadComplete Dim i% For i = 1 To NumItems oVal(ClientHandles(i)) = ItemValues(i) dTime(ClientHandles(i)) = TimeStamps(i) wQuality(ClientHandles(i)) = Qualities(i) Next i Me.threadCopy = New Thread(New ThreadStart(AddressOf Me.ThreadProcCopy)) ' 06/09/21 for VS2005 Me.threadCopy.Start() 'Copy(COPY_DIR.MEM_TO_DISP) End Sub Private Sub OPCMyGroup_AsyncWriteComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef Errors As System.Array) Handles OPCMyGroup.AsyncWriteComplete End Sub Private Sub OPCMyGroup_AsyncCancelComplete(ByVal CancelID As Integer) Handles OPCMyGroup.AsyncCancelComplete End Sub '05/02/08 Support array type Private Sub Copy(ByVal Direction As COPY_DIR) Dim i% Dim j% Dim sBufGet As String Dim sBufPut() As String Select Case Direction Case COPY_DIR.DISP_TO_MEM For i = 1 To ITEMMAX If IsArray(oVal(i)) Then Erase sBufPut sBufPut = Split(txtValue(i - 1).Text, ",") For j = 0 To UBound(oVal(i)) If j > UBound(sBufPut) Then Exit For If IsNumeric(sBufPut(j)) Then oVal(i)(j) = sBufPut(j) Next j Else If IsNumeric(txtValue(i - 1).Text) Then oVal(i) = txtValue(i - 1).Text End If If IsDate(txtTime(i - 1).Text) = True Then dTime(i) = txtTime(i - 1).Text dTime(i) = dTime(i).ToUniversalTime() 'dTime(i) = Time(i - 1).Text End If If IsNumeric(txtQuality(i - 1).Text) = True Then wQuality(i) = txtQuality(i - 1).Text End If Next Case COPY_DIR.MEM_TO_DISP For i = 1 To ITEMMAX If Not IsNothing(oVal(i)) Then '05/03/10 If object is nothing, do not read value. If IsArray(oVal(i)) Then sBufGet = "" For j = 0 To UBound(oVal(i)) sBufGet = sBufGet & oVal(i)(j).ToString & "," '05/03/10 Change value to string 'sBufGet = sBufGet & oVal(i)(j) & "," Next j txtValue(i - 1).Text = sBufGet Else txtValue(i - 1).Text = oVal(i).ToString 'txtValue(i - 1).Text = oVal(i) End If End If If Not IsNothing(dTime(i)) Then txtTime(i - 1).Text = dTime(i).ToLocalTime().ToString 'txtTime(i - 1).Text = dTime(i).ToString If Not IsNothing(wQuality(i)) Then txtQuality(i - 1).Text = wQuality(i).ToString Next End Select End Sub '************************ Private Sub ThreadProcCopy() ' 06/09/21 for VS2005 Dim i As Integer For i = 1 To ITEMMAX Me.SetValue(i) Me.SetQuality(i) Me.SetTime(i) Next End Sub Private Sub SetValue(ByVal [i] As Integer) ' 06/09/21 for VS2005 '// InvokeRequired required compares the thread ID of the '// calling thread to the thread ID of the creating thread. '// If these threads are different, it returns true. If Me.txtValue(i - 1).InvokeRequired Then Dim d As New SetValueCallback(AddressOf SetValue) Me.Invoke(d, New Object() {[i]}) Else Dim j As Integer Dim sBufGet As String If Not IsNothing(oVal(i)) Then '05/03/10 If object is nothing, do not read value. If IsArray(oVal(i)) Then sBufGet = "" For j = 0 To UBound(oVal(i)) sBufGet = sBufGet & oVal(i)(j).ToString & "," '05/03/10 Change value to string 'sBufGet = sBufGet & oVal(i)(j) & "," Next j txtValue(i - 1).Text = sBufGet Else txtValue(i - 1).Text = oVal(i).ToString 'txtValue(i - 1).Text = oVal(i) End If End If End If End Sub Private Sub SetTime(ByVal [i] As Integer) ' 06/09/21 for VS2005 '// InvokeRequired required compares the thread ID of the '// calling thread to the thread ID of the creating thread. '// If these threads are different, it returns true. If Me.txtTime(i - 1).InvokeRequired Then Dim d As New SetTimeCallback(AddressOf SetTime) Me.Invoke(d, New Object() {[i]}) Else If Not IsNothing(dTime(i)) Then txtTime(i - 1).Text = dTime(i).ToString End If End Sub Private Sub SetQuality(ByVal [i] As Integer) ' 06/09/21 for VS2005 '// InvokeRequired required compares the thread ID of the '// calling thread to the thread ID of the creating thread. '// If these threads are different, it returns true. If Me.txtQuality(i - 1).InvokeRequired Then Dim d As New SetQualityCallback(AddressOf SetQuality) Me.Invoke(d, New Object() {[i]}) Else If Not IsNothing(wQuality(i)) Then txtQuality(i - 1).Text = wQuality(i).ToString End If End Sub End Class