Option Strict Off
Option Explicit On
Friend Class Dialog1
	Inherits System.Windows.Forms.Form
	Public VendorID As Integer
	Public ProductID As Integer
	Public DeviceName As String
	Public Manufacturer As String
	Public SerialNumber As String
	Public InputReportLen As Integer
	Public OutputReportLen As Integer
	'UPGRADE_NOTE: Interface was upgraded to Interface_Renamed. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Public Interface_Renamed As Integer
	Public Collection As Integer
	
	Private Declare Function Open2 Lib "UsbHidApi.dll"  Alias "Open"(ByVal VendorID As Integer, ByVal ProductID As Integer, ByVal Manufacturer As String, ByVal SerialNumber As String, ByVal DeviceName As String, ByVal bAsync As Integer) As Integer
	Private Declare Sub CloseWrite Lib "UsbHidApi.dll" ()
	Private Declare Sub CloseRead Lib "UsbHidApi.dll" ()
	Private Declare Function Read Lib "UsbHidApi.dll" (ByRef pBuf As Byte) As Integer
	Private Declare Function Write2 Lib "UsbHidApi.dll"  Alias "Write"(ByRef pBuf As Byte) As Integer
	Private Declare Sub SetCollection Lib "UsbHidApi.dll" (ByVal col As Integer)
	Private Declare Function GetCollection Lib "UsbHidApi.dll" () As Integer
	Private Declare Sub SetInterface Lib "UsbHidApi.dll" (ByVal iface As Integer)
	Private Declare Function GetInterface Lib "UsbHidApi.dll" () As Integer
	Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
	
	'private declare Function SendData As Integer
	
	Private Sub Dialog1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		'UPGRADE_NOTE: val was upgraded to val_Renamed. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
		Dim val_Renamed As Short
		
		' Initialize controls
		OutputReportID(0).Text = "0"
		InputReportID(1).Text = "0"
        writebuffertextbox.Text = ""
        ReadBufferTextBox.Text = ""

        ' Assign field values
        ProdID(0).Text = Str(ProductID)
        VendID(0).Text = Str(VendorID)
        SerNum(2).Text = SerialNumber
        DevName(1).Text = DeviceName
        'Mfgr = Manufacturer
        InRptLen(3).Text = CStr(InputReportLen)
        OutRptLen(4).Text = CStr(OutputReportLen)

        ' The following statements are needed in the event that there
        ' are multiple interfaces and/or collections.
        ' added 3/12/07
        SetInterface((Interface_Renamed))
        SetCollection((Collection))

        ' Attempt to Open the device
        val_Renamed = Open2(VendorID, ProductID, Manufacturer, SerialNumber, DeviceName, True)
        If (val_Renamed <> 1) Then
            'MsgBox ("Open() failed! " & val)
            ' Disable controls
            Send.Enabled = False
            WriteBufferTextBox.Enabled = False
            ReadBufferTextBox.Enabled = False
            OutputReportID(0).Enabled = False
            InputReportID(1).Enabled = False
            Status.Text = "Open() failed!"
            Status.Text = Status.Text & "  - This error typically occurs when you have selected a system device that is in use by the operating system.  "
            Status.Text = Status.Text & "For security purposes, Windows 2000/NT/XP retains exclusive access to all system devices including mice, keyboards, etc."
        Else
            Status.Text = "Open Completed!"
        End If
    End Sub

    Private Sub CancelButton_Renamed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CancelButton_Renamed.Click
        Dim Index As Short = CancelButton_Renamed.GetIndex(eventSender)
        ' Close comm links
        Call CloseRead()
        Call CloseWrite()
        ' Terminate the dialog
        Me.Close()
    End Sub

    Private Sub Send_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Send.Click
        If (chkWrite.CheckState = 1) Then
            Call SendData()
        End If
        If (chkRead.CheckState = 1) Then
            Call ReadData()
        End If
    End Sub

    Private Sub SendData()
        Dim idx As Short
        Dim wresult As Short
        Dim rdresult As Short
        Dim retry As Short
        Dim send_buf(1000) As Byte
        ' Clear the buffers
        For idx = 0 To 999
            send_buf(idx) = 0
        Next
        Status.Text = ""
        Call Form1.MyRefreshCmd_Click(Nothing, New System.EventArgs())
        send_buf(0) = Val("&H" & OutputReportID(0).Text)
        Dim a() As String
        a = Split(WriteBufferTextBox.Text) ' Somewhat like sscanf
        ' Populate the write buffer
        For idx = 0 To UBound(a)
            send_buf(idx + 1) = Val("&H" & a(idx)) ' Store the byte
        Next
        ' Clear the fields
        WriteBufferTextBox.Text = ""
        WriteBufferTextBox.Refresh()
        OutputReportID(0).Text = ""
        OutputReportID(0).Refresh()
        ' Re-format the output report ID; ensure the proper
        ' number of hex digits.
        If (Len(Hex(send_buf(0))) = 1) Then
            OutputReportID(0).Text = "0" & Hex(send_buf(0))
        Else
            OutputReportID(0).Text = Hex(send_buf(0))
        End If
        ' Re-format the write-buffer items to make them neat
        ReDim a(1000)
        For idx = 1 To OutputReportLen - 1
            If (Len(Hex(send_buf(idx))) = 1) Then
                a(idx) = "0" & Hex(send_buf(idx))
            Else
                a(idx) = Hex(send_buf(idx))
            End If
            WriteBufferTextBox.Text = WriteBufferTextBox.Text & a(idx) & " "
        Next
        retry = 0
        Do
            wresult = Write2(send_buf(0))
            retry = retry + 1
        Loop While ((retry < 20) And ((rdresult <> 9) Or (wresult <> 9)))
        ' Evaluate write result
        If (wresult = 0) Then
            Status.Text = "Write failed!" & Chr(13) & Chr(10)
        Else
            Status.Text = Status.Text & "Write completed successfully! " & Chr(13) & Chr(10)
        End If

        Status.Text = Status.Text & "Write/Read attempts: " & retry & Chr(13) & Chr(10)

        Call Form1.MyRefreshCmd_Click(Nothing, New System.EventArgs())



    End Sub
    Private Sub ReadData()
        Dim idx As Short
        Dim wresult As Short
        Dim rdresult As Short
        Dim retry As Short

        Dim recv_buf(1000) As Byte

        ' Clear the buffers
        For idx = 0 To 999
            recv_buf(idx) = 0
        Next
        ' Clear the status box
        Status.Text = ""
        Call Form1.MyRefreshCmd_Click(Nothing, New System.EventArgs())
        ' Clear the fields
        ReadBufferTextBox.Text = ""
        ReadBufferTextBox.Refresh()
        InputReportID(1).Text = ""
        InputReportID(1).Refresh()
        retry = 1
        ' Attempt to read the response
        rdresult = Read(recv_buf(0))
        ' Evaluate read result
        If (rdresult <> InputReportLen) Then
            Status.Text = Status.Text & "Read failed!" & Chr(13) & Chr(10)
        Else
            Status.Text = Status.Text & "Read completed successfully!" & Chr(13) & Chr(10)
        End If

        ' Show the number of attempts, just for info
        Status.Text = Status.Text & "Write/Read attempts: " & retry & Chr(13) & Chr(10)

        ' Re-format the read-buffer and report ID fields to make them neat
        ' Populate the read buffer
        ReadBufferTextBox.Text = ""
        Call Form1.MyRefreshCmd_Click(Nothing, New System.EventArgs())

        ' Format & display the input report ID; ensure the proper
        ' number of hex digits.
        If (Len(Hex(recv_buf(0))) = 1) Then
            InputReportID(1).Text = "0" & Hex(recv_buf(0))
        Else
            InputReportID(1).Text = Hex(recv_buf(0))
        End If

        Dim a(1000) As String
        For idx = 1 To InputReportLen - 1

            If (Len(Hex(recv_buf(idx))) = 1) Then
                a(idx) = "0" & Hex(recv_buf(idx))
            Else
                a(idx) = Hex(recv_buf(idx))
            End If

            ReadBufferTextBox.Text = ReadBufferTextBox.Text & a(idx) & " "

        Next
    End Sub

    Private Sub Status_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Status.TextChanged

    End Sub
End Class