Você está na página 1de 3

Option Base 1 Dim ErrCode As Integer 'used for returned ErrCode of EX92026 functions Dim DoData As Long '

used for readback relay port data Private Sub Btn_Close_Click() W_2026_Close 'Close all cards Unload Form1 'Close the form End Sub Private Sub Form_Unload(Cancel As Integer) 'before close the form ,Close all EX92026 cards W_2026_Close 'Close all EX92026 cards End Sub

Private Sub Form_Load() 'initialize all ComboBox Cmb_ResetBit_BitNo.ListIndex = 0 Cmb_SetBit_BitNo.ListIndex = 0 End Sub Private Sub Btn_Open_Click() 'Open all cards installed Dim Cards_Num As Long 'card number installed Dim ID(4) As Long Dim i As Integer ErrCode = W_2026_Open(Cards_Num) 'Initialize EX92026 card. If ErrCode = 0 And Cards_Num > 0 Then 'if open cards success ErrCode = W_2026_GetCardsID(ID(1)) For i = 1 To Cards_Num Cmb_CardNo.AddItem (Str(ID(i))) 'Fill the ComboBox with card number Next i Cmb_CardNo.ListIndex = 0 Else 'if open cards fail MsgBox ("Open Device Failed!" & Chr(13) & "Error Code=" & ErrCode) 'show error messagebox End If End Sub Private Sub Btn_ReadDi_Click() 'Read Digital Input Dim DiData As Long 'store read di data ErrCode = W_2026_Read_Di(Val(Cmb_CardNo.Text), DiData) 'Read Digital Input If ErrCode <> 0 Then 'if Read Di fail MsgBox ("Read Digital Input Fail!" & Chr(13) & "ERROR Code=" & ErrCode) 'show error messagebox End If Txt_ReadDi_ReadBack.Text = Hex(DiData) ' show di data End Sub Private Sub Btn_ResetBit_Click() 'Reset Do Bit ErrCode = W_2026_Reset_Do_Bit(Val(Cmb_CardNo.Text), Cmb_ResetBit_BitNo.ListIndex) 'Reset Do Bit If ErrCode <> 0 Then 'if Reset Do bit fail MsgBox ("Reset Do Bit Fail!" & Chr(13) & "ERROR Code=" & ErrCode) 'show error messagebox End If Call Sleep(100) ErrCode = W_2026_Read_Do(Val(Cmb_CardNo.Text), DoData) 'read back the relay data Txt_ResetBit_ReadBack.Text = Hex(DoData) 'show the readback relay data End Sub Private Sub Btn_SetBit_Click() 'set do bit ErrCode = W_2026_Set_Do_Bit(Val(Cmb_CardNo.Text), Cmb_SetBit_BitNo.ListIndex) 'write data to relay port If ErrCode <> 0 Then 'if set do bit fail

MsgBox ("Set Do Bit Fail!" & Chr(13) & "ERROR Code=" & ErrCode) 'show error messagebox End If Call Sleep(100) ErrCode = W_2026_Read_Do(Val(Cmb_CardNo.Text), DoData) 'read back the relay data Txt_SetBit_ReadBack.Text = Hex(DoData) 'show the readback relay data End Sub

Private Sub Btn_WriteDo_Click() 'Write Relay Ports ErrCode = W_2026_Write_Do(Val(Cmb_CardNo.Text), StrHexToInt(Txt_WriteDo_Data.Text)) 'write data to relay port If ErrCode <> 0 Then 'if write Do fail MsgBox ("Write Relay Fail!" & Chr(13) & "ERROR Code=" & ErrCode) 'show error messagebox End If Call Sleep(100) ErrCode = W_2026_Read_Do(Val(Cmb_CardNo.Text), DoData) 'read back the relay data Txt_WriteDo_ReadBack.Text = Hex(DoData) 'show the readback relay data End Sub Public Function StrHexToInt(p As String) As Integer 'used for convert hex string to int data Dim a As Integer a = 0 p = UCase(p) For i = Len(p) To 1 Step -1 Select Case Mid(p, i, 1) Case "0" a = a + 0 Case "1" a = a + 1 * (16 ^ (Len(p) - i)) Case "2" a = a + 2 * (16 ^ (Len(p) - i)) Case "3" a = a + 3 * (16 ^ (Len(p) - i)) Case "4" a = a + 4 * (16 ^ (Len(p) - i)) Case "5" a = a + 5 * (16 ^ (Len(p) - i)) Case "6" a = a + 6 * (16 ^ (Len(p) - i)) Case "7" a = a + 7 * (16 ^ (Len(p) - i)) Case "8" a = a + 8 * (16 ^ (Len(p) - i)) Case "9" a = a + 9 * (16 ^ (Len(p) - i)) Case "A" a = a + 10 * (16 ^ (Len(p) - i)) Case "B" a = a + 11 * (16 ^ (Len(p) - i)) Case "C" a = a + 12 * (16 ^ (Len(p) - i)) Case "D" a = a + 13 * (16 ^ (Len(p) - i)) Case "E"

a = a + 14 * (16 ^ (Len(p) - i)) Case "F" a = a + 15 * (16 ^ (Len(p) - i)) Case Else MsgBox ("Invalid Number!") a = 0 Exit For End Select Next StrHexToInt = a End Function

Você também pode gostar