<< Click to Display Table of Contents >> 手順2VB.NETの基本構築 |
手順2 VB.NETの基本構築
VB.NET側の基本的な構築内容について説明します。
尚、以下は概要のみの説明となっているため、詳細はサンプルと見比べながら確認してください。
本サンプルでは、OPCに関する処理をCommonクラス(Common.vb)に定義し、一通りの接続から値の読み書きの処理コードが含めてあります。以下の作成例では、Commonクラスを利用した構築方法を記載します。 |
本項目で利用する「OpcRcw.Comm」と「OPCRcw.Da」の参照ライブラリは、Panel Serverをインストールすることによって、VB.NETなどから参照できるようになります。 |
1.参照設定を行う
OpcRcw.ComnとOPCRcw.Daの参照を追加ボタンから行ってください。
2.コード内でOpcRcwのインポートを行う
1 | Imports OpcRcw.Da |
3.Commonクラスの定義を行う
1 | Dim WithEvents EvCSvr As Common |
尚、サンプルでは、画面ロード時にインスタンスの生成を行っています。
1 2 3 4 | 'Setting Class If EvCSvr Is Nothing Then EvCSvr = New Common End If |
4.自動的に値を更新するかどうかの設定を行う
「mvCheckState」にTrueを設定した場合、自動更新が有効になります。
サンプルでは画面上でチェックが入っている場合、自動更新を行うようにしています。
自動更新の必要がなければこの処理は必要ありません。
1 2 3 4 5 6 | 'Auto Update Mode Setting If chcAuto.CheckState = 1 Then EvCSvr.mvCheckState = True Else EvCSvr.mvCheckState = False End If |
5.OPCサーバーへのコネクション処理を定義する
コネクションは、「FnConnect」で行います。コネクションの成功とともにAddItemを行い、アイテムの登録を行います。
サンプルではConnectボタン押下で接続を行います。
なお、ProgIDなどが設定されていない場合は処理を行わないなど、エラーチェックも行っています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | '------------------------------------------ ' OPC Connect '------------------------------------------ Private Sub btnConnect_Click(sender As System. Object , e As System.EventArgs) Handles btnConnect.Click 'Configuration Check If cmbProgID.Text = "" Then MessageBox.Show( "ProgID is not set correctly" , "Exclamation" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation) cmbProgID.Focus() Exit Sub End If If txtUpdateRate.Text = "" Then MessageBox.Show( "UpdateRate is not set correctly" , "Exclamation" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation) txtUpdateRate.Focus() Exit Sub End If If Not IsNumeric(txtUpdateRate.Text) Then MessageBox.Show( "UpdateRate set a numerical value" , "Exclamation" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation) txtUpdateRate.Focus() Exit Sub End If 'Auto Update Mode Setting If chcAuto.CheckState = 1 Then EvCSvr.mvCheckState = True Else EvCSvr.mvCheckState = False End If 'Connect If EvCSvr.FnConnect(cmbProgID.Text, txtServerName.Text, (txtUpdateRate.Text)) = True Then For i = 0 To mcItemCount - 1 mvcH(i) = i Next If EvCSvr.FnAddItem(mvItemName, mvcH, mvsH) = False Then MessageBox.Show( "Failed to AddItems!!" , "Error" , MessageBoxButtons.OK, MessageBoxIcon. Error ) EvCSvr.FnDisConnect() Exit Sub End If mvConnectFlg = True 'Change Enabled Call SbChangeEnabled( False , True ) For i = 0 To mcItemCount - 1 txtValue(i).Enabled = True Next Else MessageBox.Show( "Failed to Connect!!" , "Error" , MessageBoxButtons.OK, MessageBoxIcon. Error ) EvCSvr.FnDisConnect() Exit Sub End If End Sub |
6.OPCサーバーへのディスコネクション処理を定義する
ディスコネクションは「FnDisConnect」で行います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | '------------------------------------------ ' OPC DisConnect '------------------------------------------ Private Sub btnDisConnect_Click(sender As System. Object , e As System.EventArgs) Handles btnDisConnect.Click If EvCSvr.FnDisConnect() = False Then MessageBox.Show( "Failed to DisConnect!!" , "Error" , MessageBoxButtons.OK, MessageBoxIcon. Error ) Exit Sub End If mvConnectFlg = False 'Change Enabled Call SbChangeEnabled( True , False ) For i = 0 To mcItemCount - 1 txtValue(i).Enabled = False txtValue(i).Text = "" Next End Sub |
7.自動更新有りの時の設定を行う
「DataChange」イベントの定義を行います。
「mvCheckState」がTrueである場合、DataChangeイベントがUpdateRateの周期で発生します。
自動更新の必要がなければこの処理は必要ありません。
サンプルではエディットボックスに取得したタグの値を設定します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | '------------------------------------------ ' Event - DataChange '------------------------------------------ Private Sub EvCSvr_DataChange(vwTransID As Integer , _ vItemCount As Integer , _ vClientHds() As Integer , _ vValues() As Object , _ vftTimeStamps() As OpcRcw.Da.FILETIME, _ vwQualities() As Short , _ vpErrors() As Integer ) Handles EvCSvr.DataChange Dim i As Integer = 0 Dim j As Integer = 0 For i = 0 To vItemCount - 1 For j = 0 To mcItemCount - 1 If mvcH(j) = vClientHds(i) Then If vpErrors(i) = 0 Then mvoV(j) = vValues(i) End If Exit For End If Next Next For i = 0 To mcItemCount - 1 mvTarget = i txtValue(mvTarget).Invoke( New D_UpdTextBox( AddressOf SbUpdTextBox), CStr (mvoV(mvTarget))) Next End Sub |