Выбрать главу

 DisconnectFromHID

End Sub

'****************************************************************************

' a HID device has been plugged in...

'****************************************************************************

Public Sub OnPlugged(ByVal pHandle As Long)

 If hidGetVendorID(pHandle) = VendorID And _

  hidGetProductID(pHandle) = ProductID Then

  lblstatus = "USB Plugged....."

 End If

End Sub

'****************************************************************************

' a HID device has been unplugged...

'****************************************************************************

Public Sub OnUnplugged(ByVal pHandle As Long)

 If hidGetVendorID(pHandle) = VendorID And _

  hidGetProductID(pHandle) = ProductID Then

  lblstatus = "USB Unplugged...."

 End If

End Sub

'****************************************************************************

' controller changed notification - called

' after ALL HID devices are plugged or unplugged

'****************************************************************************

Public Sub OnChanged()

 Dim DeviceHandle As Long

 ' get the handle of the device we are interested in, then set

 ' its read notify flag to true - this ensures you get a read

 ' notification message when there is some data to read...

 DeviceHandle = hidGetHandle(VendorID, ProductID)

 hidSetReadNotify DeviceHandle, True

End Sub

'****************************************************************************

' on read event...

'****************************************************************************

Public Sub OnRead(ByVal pHandle As Long)

 ' read the data (don't forget, pass the whole array)...

 If hidRead(pHandle, BufferIn(0)) Then

  ' ** YOUR CODE HERE **

  ' first byte is the report ID, e.g. BufferIn(0)

  ' the other bytes are the data from the microcontrolller...

 End If

End Sub

USB1.BAS

' this is the interface to the HID controller DLL - you should not

' normally need to change anything in this file.

'

' WinProc() calls your main form 'event' procedures - these are currently

' set to..

'

' MainForm.OnPlugged(ByVal pHandle as long)

' MainForm.OnUnplugged(ByVal pHandle as long)

' MainForm.OnChanged()

' MainForm.OnRead(ByVal pHandle as long)

Option Explicit

' HID interface API declarations...

Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Long) As Boolean

Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean

Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Long) As Long

Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Long

Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Long, ByRef pData As Byte) As Boolean

Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Long, ByRef pData As Byte) As Boolean

Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Long, ByVal pProductID As Long, ByRef pData As Byte) As Boolean

Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Long, ByVal pProductID As Long, ByRef pData As Byte) As Boolean

Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendorID As Long, ByVal pProductID As Long) As Long

Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Long) As Long

Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Long) As Long

Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Long) As Long

Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long

Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long

Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Long, ByVal pText As String, ByVal pLen As Long) As Long

Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Long) As Long

Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Long) As Long

Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Long, ByVal pValue As Boolean)

Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Long) As Boolean

Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Long, ByVal pProductID As Long) As Boolean

' windows API declarations - used to set up messaging...

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

' windows API Constants

Private Const WM_APP = 32768

Private Const GWL_WNDPROC = -4

' HID message constants

Private Const WM_HID_EVENT = WM_APP + 200

Private Const NOTIFY_PLUGGED = 1

Private Const NOTIFY_UNPLUGGED = 2

Private Const NOTIFY_CHANGED = 3

Private Const NOTIFY_READ = 4

' local variables

Private FPrevWinProc As Long ' Handle to previous window procedure

Private FWinHandle As Long ' Handle to message window

' Set up a windows hook to receive notification

' messages from the HID controller DLL - then connect

' to the controller

Public Function ConnectToHID(ByVal pHostWin As Long) As Boolean

 FWinHandle = pHostWin

 ConnectToHID = hidConnect(FWinHandle)