38.5.2.4 VB6程序示例 - 数据读/写示例(寄存器访问API)

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

Option Explicit

Private Sub Form_Load()

Dim iResult As Long

iResult = EasyInit()
If iResult Then
Dim sErrMsg As String
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
End If

End Sub

' ----------------------------------------------
' WriteDeviceXXX()
' ----------------------------------------------

Private Sub WriteBit_Click()

' 写入数据。
Dim nDataAry(1) As Integer
 nDataAry(0) = CInt(Val(Me.WBuf_Bit.Text))
 
 ' 写入。
Dim iResult As Long
iResult = WriteDeviceBit("#WinGP", "Buf_Bit", nDataAry(0), 1)
' 此处使用了在GP-Pro EX中建立的 "Buf_16" (USR201)符号。
' 您也可以直接指定寄存器地址。
' 38.5.2.2 How to directly specify a device address
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox (sErrMsg)
End If
 
End Sub
 
Private Sub Write16_Click()

' 待加载的数据。
Dim nDataAry(1) As Integer
 nDataAry(0) = CInt(Val(Me.WBuf_16.Text))
 
 '写入。
Dim iResult As Long
iResult = WriteDevice16("#WinGP", "Buf_16", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

End Sub

Private Sub Write32_Click()

' 待加载的数据。
Dim nDataAry(1) As Long
 nDataAry(0) = CLng(Val(Me.WBuf_32.Text))
 
 '写入。
Dim iResult As Long
iResult = WriteDevice32("#WinGP", "Buf_32", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

End Sub

Private Sub WriteFloat_Click()

' 待加载的数据。
Dim nDataAry(1) As Single
 nDataAry(0) = CSng(Val(Me.WBuf_Float.Text))
 
 ' 写入。
Dim iResult As Long
iResult = WriteDeviceFloat("#WinGP", "Buf_Float", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

End Sub

Private Sub WriteDouble_Click()

' 待加载的数据。
Dim nDataAry(1) As Double
 nDataAry(0) = CDbl(Val(Me.WBuf_Double.Text))
 
 '写入。
Dim iResult As Long
iResult = WriteDeviceDouble("#WinGP", "Buf_Double", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

End Sub

Private Sub WriteString_Click()

' 待加载的数据。
Dim nDataAry As String
 nDataAry = Me.WBuf_Str.Text
 
 '写入。
Dim iResult As Long
iResult = WriteDeviceStr("#WinGP", "Buf_Str", nDataAry, 10)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessageEx(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

End Sub

' ----------------------------------------------
' ReadDeviceXXX()
' ----------------------------------------------

Private Sub ReadBit_Click()

' 待读取的数据。
Dim nDataAry(1) As Integer
 
 '读取。
Dim iResult As Long
iResult = ReadDeviceBit("#WinGP", "Buf_Bit", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessage(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

Me.Buf_Bit.Text = CStr(nDataAry(0))

End Sub

Private Sub Read16_Click()

' 待读取的数据。
Dim nDataAry(1) As Integer
 
 '读取。
Dim iResult As Long
iResult = ReadDevice16("#WinGP", "Buf_16", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessage(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

Me.Buf_16.Text = CStr(nDataAry(0))

End Sub

Private Sub Read32_Click()

' 待读取的数据。
Dim nDataAry(1) As Long
 
 '读取。
Dim iResult As Long
iResult = ReadDevice32("#WinGP", "Buf_32", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessage(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

Me.Buf_32.Text = CStr(nDataAry(0))

End Sub

Private Sub ReadFloat_Click()

' 待读取的数据。
Dim nDataAry(1) As Single
 
 '读取。
Dim iResult As Long
iResult = ReadDeviceFloat("#WinGP", "Buf_Float", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessage(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

Me.Buf_Float.Text = CStr(nDataAry(0))

End Sub

Private Sub ReadDouble_Click()

' 待读取的数据。
Dim nDataAry(1) As Double
 
 '读取。
Dim iResult As Long
iResult = ReadDeviceDouble("#WinGP", "Buf_Double", nDataAry(0), 1)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessage(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

Me.Buf_Double.Text = CStr(nDataAry(0))

End Sub

Private Sub ReadString_Click()

' 待读取的数据。
Dim nDataAry As String * 255
 
 '读取。
Dim iResult As Long
iResult = ReadDeviceStr("#WinGP", "Buf_Str", nDataAry, 10)
If iResult Then
Dim sErrMsg As String * 512
Dim iMsgResult As Long
iMsgResult = EasyLoadErrorMessage(iResult, sErrMsg)
MsgBox (sErrMsg)
End If

Me.Buf_Str.Text = nDataAry

End Sub