########################################
Visual Basic 2005 HID Functions
Compiled and modified by Vladimir 
########################################

-----------------------------
--How to use
-----------------------------

Before using the code, you need to make sure that the mcHID.dll file is present in either the SYSTEM32 folder of your PC
OR in the same directory as your application's executable file. If you plan to distribute your application, the mcHID.dll
file must also be included in order for the application to work.

Using the code is very simple. First, all the following variables need to set to their correct values, which will
be determined by your USB hardware:

- VendorID
- ProductID
- BufferInSize
- BufferOutSize

The variables can be found at the top of the form code.

If you need to write code that responds to your USB device being plugged or unplugged, simply place that code where shown
in the OnPlugged and OnUnplugged events respectively in the main form.

When the USB device sends data to the PC, the OnRead event will be called and the BufferIn array will be populated with
the received data. Take note that the received data starts from BufferIn(1) onwards. BufferIn(0) is unused.

If you want to transmit data to the USB device, simply fill the BufferOut array with data, then call the hidWriteEx
function to transmit the data to your USB device. Take note that your transmitted data must start from BufferOut(1)
and that BufferOut(0) must always be set to 0.

If you want to integrate the code into an existing application, you need to add the mcHIDInterface.vb fileto your 
existing project and copy the code in the template's form into the form in your project that will be doing the USB 
communication.

-----------------------------
--Variables
-----------------------------

The following is a brief description of the variables used in the main form that allow USB communication:

- VendorID
	Integer value specifying the Vendor ID of the USB device.

- ProductID
	Integer value specifying the Product ID of the USB device.
	
- BufferInSize
	Non-zero integer value specifying the size (in bytes) of the data packet that the USB device will send to the PC.

- BufferOutSize
	Non-zero integer value specifying the size (in bytes) of the data packet that the PC will send to the USB device.

- BufferIn()
	Byte array which contains the data packet received from the USB device. The first byte of the array, BufferIn(0), 
	is unused. Your data will start from BufferIn(1)

- BufferOut()
	Byte array which contains the data packet that will be sent to the USB device. The first byte of the array,
	BufferOut(0), must always be 0. Your data is placed in BufferOut(1) and onwards.

-----------------------------
--Functions
-----------------------------

The following is a brief description of the important functions that allow USB communication:

- hidWriteEx(ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
	This function is used to send data to the USB device. It's usage is very simple: the BufferOut array (see above)
	is filled with the data that needs to be sent (make sure to set BufferOut(0)=0). After that, the function is called
	as follows:
	
	hidWriteEx(VendorID, ProductID, BufferOut(0))
	
	VendorID, ProductID and BufferOut are all declared at the top of the form

-----------------------------
--Events
-----------------------------

The following is a brief description of the important events in the main form that allow USB communication:

- Form1_Load
	When the form is loading, ConnectToHID is called to initialize the USB functions.
	This line must be present in order for the USB communication to function properly.
	
- Form1_FormClosed
	When the form is closing, any USB connections are cleaned up and released.
	
- OnPlugged
	This function gets called when your device is plugged into a USB port.
	
- OnUnplugged
	This function gets called when your device has been unplugged from a USB port.
	
- OnChanged
	Not sure what it does, but you don't need to mess with it in order to use USB. Leave it alone and you'll be fine.
	
- OnRead
	This function gets called when data is sent to the PC from your USB device. The data is placed in the BufferIn array
	declared at the top of the form.




Example in Koshava 5 Hid Module:






Attribute VB_Name = "HID_Interface"
Option Explicit

' VendorID= 0x4530
' ProductID= 0x7531

'######################################################
Public Const VendorID             As Long = &H4530 ' VendorID
Public Const ProductID            As Long = &H7531 ' ProductID
Public BufferIn(0 To 40)          As Byte    
Public BufferOut(0 To 40)          As Byte   
'######################################################
Public VendorName                 As String * 255 
Public ProductName                As String * 18 
Private SerialNumber              As String * 255 
Public HID_Handle                 As Long    
Private InRptLngt                 As Byte    
Private OutRptLngt                As Byte   

Private Const WM_APP              As Long = 32768
Private Const GWL_WNDPROC         As Integer = -4

Private Const WM_HID_EVENT        As Double = WM_APP + 200
Private Const NOTIFY_PLUGGED      As Integer = 1
Private Const NOTIFY_UNPLUGGED    As Integer = 2
Private Const NOTIFY_CHANGED      As Integer = 3
Private Const NOTIFY_READ         As Integer = 4

Private FPrevWinProc              As Long    
Private FWinHandle                As Long   

Private Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Long) As Boolean
Private Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean
''Private Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Long) As Long
''Private Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Long
''Private Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Long, ByRef pData As Byte) As Boolean
''Private Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Long, ByRef pData As Byte) As Boolean
Public Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Long, _
                                                                  ByVal pProductID As Long, _
                                                                  ByRef pData As Byte) As Boolean
Public Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Long, _
                                                                    ByVal pProductID As Long, _
                                                                    ByRef pData As Byte) As Boolean
Public Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendoID As Long, _
                                                                        ByVal pProductID As Long) As Long
Public Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Long) As Long
Public Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Long) As Long
Private Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Long) As Long
Public Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Long, _
                                                                                ByVal pText As String, _
                                                                                ByVal pLen As Long) As Long
Public Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Long, _
                                                                                  ByVal pText As String, _
                                                                                  ByVal pLen As Long) As Long
Private Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long'Private Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Long) As Long
Private Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Long) As Long
Public Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Long, _
                                                                           ByVal pValue As Boolean)
''Private Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Long) As Boolean
Public Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Long, _
                                                                            ByVal pProductID As Long) As Boolean
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
                                                                              ByVal hWnd As Long, _
                                                                              ByVal Msg As Long, _
                                                                              ByVal wParam As Long, _
                                                                              ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _
                                                                            ByVal nIndex As Long, _
                                                                            ByVal dwNewLong As Long) As Long


Public Function ConnectToHID(ByVal pHostWin As Long) As Boolean
    FWinHandle = pHostWin
    ConnectToHID = hidConnect(FWinHandle)
    FPrevWinProc = SetWindowLong(FWinHandle, GWL_WNDPROC, AddressOf winproc)
End Function
Public Function DisconnectFromHID() As Boolean
    DisconnectFromHID = hidDisconnect ' raskidanje veze sa HID kontrolerom
    SetWindowLong FWinHandle, GWL_WNDPROC, FPrevWinProc  ' Gasenje window hook-a
End Function

Private Function winproc(ByVal pHWnd As Long, _
                         ByVal pMsg As Long, _
                         ByVal wParam As Long, _
                         ByVal lParam As Long) As Long
    If pMsg = WM_HID_EVENT Then
        Select Case wParam

        Case NOTIFY_PLUGGED
            frmMain.OnPlugged (lParam)

        Case NOTIFY_UNPLUGGED
            frmMain.OnUnplugged (lParam)

        Case NOTIFY_CHANGED
            frmMain.OnChanged
' ...Read event
        Case NOTIFY_READ
            frmMain.OnRead (lParam)
            
        End Select
    End If
' next...
    winproc = CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam)
End Function






IMPORTANT NOTICE: 


For the communication with Hid I use Timer control with interval 55ms(stable) and in code in VB it looks like this:


Private Sub tmrUSB_Timer()
    BufferOut(0) = 0
    BufferOut(1) = &H1D
    If hidIsAvailable(VendorID, ProductID) Then
        hidWriteEx VendorID, ProductID, BufferOut(0)
    End If
    
End Sub


Where BufferOut(1) is command byte and it contains number in hexa format which represents command at hid device.

Commands for sending and receiving buffer from and to device

First of all:

-BufferIn and BufferOut, now has a new values
And it is:
In Visual Basic:
Public BufferIn(0 To 40)          As Byte   
Public BufferOut(0 To 40)          As Byte    


-I will show you how I send data to device

I put timer control in form with interval of 200ms

And in code(VB) it looks like this

Private Sub someTimer_Timer()
Dim i As Byte

        BufferOut(0) = 0
        For i = 0 To 40
         BufferOut(i) = BufferIn(i)
        Next i
        BufferOut(1) = &H2A
        BufferOut(5) = range
        BufferOut(6) = mGrupa
        BufferOut(7) = blnAC
        BufferOut(9) = Jezik
       
        If hidIsAvailable(VendorID, ProductID) Then
          hidWriteEx VendorID, ProductID, BufferOut(0)
          tmrKomanda.Enabled = False
    
        End If
    
End Sub


Where BufferOut(1) is command buffer, and as you see it has some value &H2A(hexa)

Instead of this value there could be 
1.   &H1D This command tells device to send data to computer and it sends whole Buffer 


2.  &H2A This command tells device to receive and store changed or unchanged parameters:

        BufferOut(5) = Is measurement range 
        BufferOut(6) = Is units parameter
        BufferOut(7) = Change to AC/DC parameter
        BufferOut(9) =Is Language parameter


When we receive BufferIn(5) ,those are values 1,2,3 or 4
Also BufferIn(6) are 0,1,2 ,3 or 4
Then accordingly to table in our code represent display units and position of a dot




BufferIn(5) =1, is 0.000  “T”(Tesla)
	       =2, is 000.0  “mT”(milliTesla)
	       =3, is 00.00 “mT”
	       =4, is 0.000  “mT”

This can be other unit predefined in bufferIn(6)


BufferIn(6)=0, is “T” Tesla
                   =1, is “G” Gaus
                   =2, is “kA/m” 
                   =3, is “A/cm” 
                   =4, is “Oe” Oersted


Buffer(7)=0 is “AC”
               =&HFF is “DC”


BufferIn(9)=0 is English
                =1 is German



IMPORTANT NOTICE: Only these three paramters should be changed, all ohers cannot be changed. 

