VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



usbhidio.vbp

by jan Axelson (1 Submission)
Category: Complete Applications
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (7 Votes)

Demonstrates how to identify and read and write to HID-class USB devices. Uses Windows 98's HID drivers (no additional drivers needed). Includes complete Visual Basic 6 application code.

Inputs
The source code specifies Vendor and Product IDs for the device. Combo boxes enable the user to select two bytes to send to the device.
Assumes
This application requires an attached USB device with compatible firmware. Usbhidio.zip includes example firmware. The code can be adapted for communicating with any HID-class device. (HID = human interface device)
Code Returns
When the device receives the bytes sent by the application, it sends two bytes back. The application displays the received bytes. A list box displays errors and values returned from each API call.
Side Effects
If the application doesn't detect the expected device, it displays a message indicating this.
API Declarations
Public Declare Function CloseHandle _
Lib "kernel32" _
(ByVal hObject As Long) _
As Long
Public Declare Function CreateFile _
Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByRef lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) _
As Long
Public Declare Function FormatMessage _
Lib "kernel32" _
Alias "FormatMessageA" _
(ByVal dwFlags As Long, _
ByRef lpSource As Any, _
ByVal dwMessageId As Long, _
ByVal dwLanguageZId As Long, _
ByVal lpBuffer As String, _
ByVal nSize As Long, _
ByVal Arguments As Long) _
As Long
Public Declare Function HidD_FreePreparsedData _
Lib "hid.dll" _
(ByRef PreparsedData As Long) _
As Long
Public Declare Function HidD_GetAttributes _
Lib "hid.dll" _
(ByVal HidDeviceObject As Long, _
ByRef Attributes As HIDD_ATTRIBUTES) _
As Long
'Declared as a function for consistency,
'but returns nothing. (Ignore the returned value.)
Public Declare Function HidD_GetHidGuid _
Lib "hid.dll" _
(ByRef HidGuid As GUID) _
As Long
Public Declare Function HidD_GetPreparsedData _
Lib "hid.dll" _
(ByVal HidDeviceObject As Long, _
ByRef PreparsedData As Long) _
As Long
Public Declare Function HidP_GetCaps _
Lib "hid.dll" _
(ByVal PreparsedData As Long, _
ByRef Capabilities As HIDP_CAPS) _
As Long
Public Declare Function HidP_GetValueCaps _
Lib "hid.dll" _
(ByVal ReportType As Integer, _
ByRef ValueCaps As Byte, _
ByRef ValueCapsLength As Integer, _
ByVal PreparsedData As Long) _
As Long

Public Declare Function lstrcpy _
Lib "kernel32" _
Alias "lstrcpyA" _
(ByVal dest As String, _
ByVal source As Long) _
As String
Public Declare Function lstrlen _
Lib "kernel32" _
Alias "lstrlenA" _
(ByVal source As Long) _
As Long
Public Declare Function ReadFile _
Lib "kernel32" _
(ByVal hFile As Long, _
ByRef lpBuffer As Byte, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) _
As Long
Public Declare Function RtlMoveMemory _
Lib "kernel32" _
(dest As Any, _
src As Any, _
ByVal Count As Long) _
As Long
Public Declare Function SetupDiCreateDeviceInfoList _
Lib "setupapi.dll" _
(ByRef ClassGuid As GUID, _
ByVal hwndParent As Long) _
As Long
Public Declare Function SetupDiDestroyDeviceInfoList _
Lib "setupapi.dll" _
(ByVal DeviceInfoSet As Long) _
As Long
Public Declare Function SetupDiEnumDeviceInterfaces _
Lib "setupapi.dll" _
(ByVal DeviceInfoSet As Long, _
ByVal DeviceInfoData As Long, _
ByRef InterfaceClassGuid As GUID, _
ByVal MemberIndex As Long, _
ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) _
As Long
Public Declare Function SetupDiGetClassDevs _
Lib "setupapi.dll" _
Alias "SetupDiGetClassDevsA" _
(ByRef ClassGuid As GUID, _
ByVal Enumerator As String, _
ByVal hwndParent As Long, _
ByVal Flags As Long) _
As Long
Public Declare Function SetupDiGetDeviceInterfaceDetail _
Lib "setupapi.dll" _
Alias "SetupDiGetDeviceInterfaceDetailA" _
(ByVal DeviceInfoSet As Long, _
ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
ByVal DeviceInterfaceDetailData As Long, _
ByVal DeviceInterfaceDetailDataSize As Long, _
ByRef RequiredSize As Long, _
ByVal DeviceInfoData As Long) _
As Long

Public Declare Function WriteFile _
Lib "kernel32" _
(ByVal hFile As Long, _
ByRef lpBuffer As Byte, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) _
As Long

Rate usbhidio.vbp

Upload

Download this snippet    Add to My Saved Code

usbhidio.vbp Comments

No comments have been posted about usbhidio.vbp. Why not be the first to post a comment about usbhidio.vbp.

Post your comment

Subject:
Message:
0/1000 characters