38.5.2.3 VB.NET 2003程序示例 - 数据读/写示例(寄存器访问API)

示例程序位置:(GP-Pro EX光盘)\WinGP\SDK\Pro-SDK\DotNet\EasySmpl

Imports ProEasyDotNet
'导入ProEasy对象。
Public Class Form1
 Inherits System.Windows.Forms.Form
 
 #Region “由Windows窗体设计器生成的代码”
 
 Public Sub New()
 MyBase.New()
 
 '此调用对于Windows窗体设计器是必需的。
 InitializeComponent()
 
 ' ProEasy初始化。在调用InitializeComponent()后,运行初始化。
 Dim iResult As Integer = ProEasy.EasyInit() ' WinGP在开始时会初始化SDK一次。
 If iResult Then
 Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
End If
 
End Sub
 
 窗体重写dispose并执行在组建列表上的post处理。
 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窗体设计器设计的代码)-
 
 #End Region
 
 Private Sub ReadBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadBit.Click
 
 End Sub
 
 Private Sub Read16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Read16.Click
 
Try
 
 '读取数据。
   Dim nDataAry(1) As Short
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDevice16("#WinGP", "Buf_16", nDataAry, 1)
 '此处使用了在GP-Pro EX中配置的 "Buf_16" (USR201)符号。
' 也可以直接指定寄存器地址。
   ' 38.5.2.2 如何直接指定寄存器地址
   If iResult Then
    Dim sErrMsg As String
    ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
    MsgBox(sErrMsg)
   End If
 
   Me.Buf_16.Text = CStr(nDataAry(0))
 
   Catch ex As Exception
 
   MsgBox(ex.Message)
 
  End Try
 
 End Sub
 
 Private Sub Read32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Read32.Click
 
  Try
 
   ' 读取数据。
   Dim nDataAry(1) As Integer
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDevice32("#WinGP", "Buf_32", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
Me.Buf_32.Text = CInt(nDataAry(0))
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub ReadBCD16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadBCD16.Click
 
Try
 '读取数据。
   Dim nDataAry(1) As Short
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDeviceBCD16("#WinGP", "Buf_BCD16", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
 End If
 
 Me.Buf_BCD16.Text = CShort(nDataAry(0))
 
 Catch ex As Exception
 
MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub ReadBCD32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadBCD32.Click
 
Try
 '读取数据。
   Dim nDataAry(1) As Integer
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDeviceBCD32("#WinGP", "Buf_BCD32", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
 End If
 
Me.Buf_BCD32.Text = CInt(nDataAry(0))
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub ReadFloat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadFloat.Click
 
Try
 '读取数据。
   Dim nDataAry(1) As Single
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDeviceFloat("#WinGP", "Buf_Float", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
 End If
 
 Me.Buf_Float.Text = CSng(nDataAry(0))
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub ReadDouble_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadDouble.Click
 
Try
 '读取数据。
   Dim nDataAry(1) As Double
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDeviceDouble("#WinGP", "Buf_Double", nDataAry, 1)
If iResult Then
 Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
 End If
 
 Me.Buf_Double.Text = CDbl(nDataAry(0))
 
 Catch ex As Exception
 
MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub ReadStr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadStr.Click
 
Try
 '读取数据。
   Dim nDataAry As String
 
 '读取。
   Dim iResult As Integer = ProEasy.ReadDeviceStr("#WinGP", "Buf_Str", nDataAry, 10)
 If iResult Then
Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
 Me.Buf_Str.Text = nDataAry
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub ReadVariant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadVariant.Click
 
 End Sub
 
 Private Sub ReadSymbol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
ReadSymbol.Click
 
 End Sub
 
 Private Sub WriteBit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteBit.Click
 
Try
 '读取数据。
   Dim nDataAry(1) As Short
 nDataAry(0) = CShort(Val(Me.WBuf_Bit.Text))
 
 '读取。
   Dim iResult As Integer = ProEasy.WriteDeviceBit("#WinGP", "Buf_16", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
 End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub Write16_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Write16.Click
 
Try
 '写入数据。
   Dim nDataAry(1) As Short
 nDataAry(0) = CShort(Val(Me.WBuf_16.Text))
 
 '写入。
   Dim iResult As Integer = ProEasy.WriteDevice16("#WinGP", "Buf_16", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub Write32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Write32.Click
 
 Try
 '写入数据。
   Dim nDataAry(1) As Integer
 nDataAry(0) = CInt(Val(Me.WBuf_32.Text))
 
 '写入。
   Dim iResult As Integer = ProEasy.WriteDevice32("#WinGP", "Buf_32", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub WriteBCD16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteBCD16.Click
 
 Try
 写入数据。
   Dim nDataAry(1) As Short
 nDataAry(0) = CShort(Val("&h" + Me.WBuf_BCD16.Text))
 
'写入。
   Dim iResult As Integer = ProEasy.WriteDevice16("#WinGP", "Buf_BCD16", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub WriteBCD32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteBCD32.Click
 
Try
 '写入数据。
   Dim nDataAry(1) As Integer
 nDataAry(0) = CInt(Val("&h" + Me.WBuf_BCD16.Text))
 
 '写入。
   Dim iResult As Integer = ProEasy.WriteDeviceBCD32("#WinGP", "Buf_BCD32", nDataAry, 1)
 If iResult Then
 Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub WriteFloat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteFloat.Click
 
Try
'写入数据。
   Dim nDataAry(1) As Single
 nDataAry(0) = CSng(Val(Me.WBuf_Float.Text))
 
 ' 写入。
   Dim iResult As Integer = ProEasy.WriteDeviceFloat("#WinGP", "Buf_Float", nDataAry, 1)
If iResult Then
Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
 End Sub
 
 Private Sub WriteDouble_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteDouble.Click
 
 Try
 '写入数据。
   Dim nDataAry(1) As Double
 nDataAry(0) = CDbl(Val(Me.WBuf_Double.Text))
 
 '写入。
   Dim iResult As Integer = ProEasy.WriteDeviceDouble("#WinGP", "Buf_Double", nDataAry, 1)
If iResult Then
 Dim sErrMsg As String
ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox(sErrMsg)
 End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub WriteString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteString.Click
 
Try
'写入数据。
   Dim nDataAry As String
 nDataAry = Me.WBuf_Str.Text
 
 '写入。
   Dim iResult As Integer = ProEasy.WriteDeviceStr("#WinGP", "Buf_Str", nDataAry, 10)
If iResult Then
Dim sErrMsg As String
 ProEasy.EasyLoadErrorMessageEx(iResult, sErrMsg)
 MsgBox(sErrMsg)
 End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 End Sub
 
 Private Sub WriteVariant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles WriteVariant.Click
 
'在VB.NET中不再使用Variant变量类型。而是使用Object变量类型。
'随着这一改变,WriteDeviceVariant()已改为WriteDeviceEasyObject()。
 
 End Sub
 
 Private Sub WriteSymbol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
WriteSymbol.Click
 
 '仅系统找到的WriteSymbol为WriteSymbolVariant()。
 
 End Sub
 
End Class