
User Manual
Version 1.18
08/28/07
Copyright (C) 2007
All rights reserved
USB HID API Library
Develop custom USB HID applications
using existing Windows drivers!
for software developers, engineers,
and programmers
by K.A. Delahoussaye
Delahoussaye Consulting
web: www.kadtronix.com
email: info@kadtronix.com
Table of Contents
-
Introduction
-
System Requirements
-
USB Compatibility
-
Features
-
Ordering
-
Demonstrations
-
Installation
-
Linking
-
Header Definition
-
Compiler Compatibility
-
Device Descriptors
-
Function Reference
-
GetList( )
-
Open( )
-
Read( )
-
Write( )
-
GetReportLengths()
-
SetInterface( )
-
GetInterface( )
-
SetCollection( )
-
GetCollection( )
-
CloseRead( )
-
CloseWrite( )
-
GetLibVersion
-
U4x1 Hardware
-
Legal
-
License
-
Warranty
-
Introduction
The USB HID API Library, herein referred to as "UsbHidApi", provides
a set of applications programmer interface (API) functions for accessing
a USB Human Interface Device (HID) class device. The routines are
compiled within a standard Windows dynamic link library (DLL), callable
by applications including C, C++ or Visual Basic.
When using this library to develop your USB HID device, there are no
device drivers to create since the API uses existing Windows HID class
drivers. Used primarily for keyboards, mice,
and joysticks, the HID class is also well-suited for special-purpose applications
including access and control of custom-developed devices. UsbHidApi
encapsulates API calls that give you easy access to your device.
These API calls can be used in any development environment that accepts
DLLs (e.g., Microsoft Visual C++ or Visual Basic).
-
System Requirements
UsbHidApi requires a Windows laptop or desktop PC with the following
minimum capabilities:
-
1 MB RAM
-
USB port
-
Windows 98/2000/XP/Vista
USB Compatibility
There are a number of defined USB device classes as listed in the table
below:
| Device Class |
Typical Use |
| Audio |
Sound card |
| Communications |
Modem, network |
| Human Interface |
Keyboard, mouse |
| Still Image Capture |
Still camera |
| Printer |
Printer |
| Mass Storage |
Disk drive, flash drive |
| Hub |
USB hub |
| Video |
Camera |
| Wireless |
Bluetooth |
There are standard device drivers available for each class. If
a device does not fall into one of these classes or the device has special
requirements, a custom driver must be provided. The UsbHidApi library
uses the HID class and therefore does not require a custom driver.
While the HID class is typically used for devices such as keyboards, mice,
and joysticks, the class is well-suited for custom-developed devices as
well.
The maximum defined transfer rate for the HID class is 64K bytes/sec.
For more information, please refer to the following resource:
http://www.usb.org/developers/hidpage
Features
UsbHidApi library features include the following:
Easy
to use API calls - Open( ), Read( ), and Write( ), etc.
No custom
drivers (uses USB HID drivers already included in Windows)
Develop
PC host applications quickly
Write applications
for USB HID devices
Two available
demonstration applications: Generic and U4x1
Free demo
source code included: Visual C++ and Visual Basic
VC++ demo
source available for either generic demo app or U4x1 demo app.
VB demo
source available for generic demo app only.
Can be used
with ANSI-C compilers such as LabView and LabWindows/CVI
Use implicit-
or explicit-linking
Includes
static (.lib) and dynamic (.dll) libraries
Compatible with
UsbMicro
U4x1 devices
Ordering
UsbHidApi is available at the following source:
Kadtronix
web: http://www.kadtronix.com
email: sales@kadtronix.com
Demonstrations
There are two available applications which demonstrate how to use the
UsbHidApi: generic and U4x1.
The generic demo is applicable to any USB HID device and was developed
and tested under two language environments: Visual C++ 6.0 and Visual
Basic 6.0. The U4x1 demo is applicable only to UsbMicro U4x1 devices.
Pre-built demo executables are included in the installation, allowing you
to run the demo even in the absence of a development environment.
-
Generic Demonstration
To run the Visual C++ generic demo, perform the following Windows command:
Start -> All Programs -> USB HID DEMO -> Usb Hid Demo
When executed, the following dialog screen image will appear on
your monitor display:
Screenshot from Generic demo app - UsbHidDemo
The Visual C++ generic demo project source code is located on your
PC as follows:
Start -> All Programs -> USB HID DEMO -> VC++
To run the Visual Basic generic demo, first open the following folder:
Start -> All Programs -> USB HID DEMO -> VB
Then, run the file titled, UsbHidDemoVB.exe
The Visual Basic generic demo project source code is located as follows:
Start -> All Programs -> USB HID DEMO -> VB
-
U4x1 Demonstration
To run the Visual C++ U4x1 demo, perform the following Windows command:
Start -> All Programs -> USB U4x1 DEMO -> Usb U4x1 Demo
The U4x1 demo is provided only for Visual C++ and was developed
primarily for showing how to access a UsbMicro U4x1 device. When
executed, the following dialog screen image will appear on your monitor
display:
Screenshot from U4x1 demo app - UsbU4x1Demo
The Visual C++ U4x1 demo project source code is located on your PC as
follows:
Start -> All Programs -> USB U4x1 DEMO -> VC++
(There is no available Visual Basic version of this project.)
A trial version of the UsbHidApi is available for free download.
The trial version is unregistered software that is fully operational, but
displays periodic pop-up messages. Visit the Kadtronix website for
download details.
Installation
Before installing the UsbHidApi, you may need to uninstall any earlier
version. To start the installation process, locate and run the self-extracting
setup file (setup.exe) on the install media.
Your device hardware may be plugged into any available USB port using
a compatible cable. Since drivers pre-exist under Windows, you may
plug in the device at any time, before or after installing the UsbHidApi.
Linking
Your application may link the UsbHidApi DLL statically
(.lib) or dynamically (.dll). Dynamic linking differs from static
linking in that it allows your application module to include only
the information needed at run time to locate the executable code for a
DLL function. In static linking, the linker gets all of the referenced
functions from the static link library and places it with your code into
your executable. Using dynamic linking instead of static linking
offers several advantages. DLLs save memory, reduce swapping, save
disk space, upgrade easier, provide after-market support, provide a mechanism
to extend the MFC library classes, support multilanguage programs, and
ease the creation of international versions.
The Generic demo uses static linking while the
U4x1demo uses dynamic linking. You may reference the appropriate
demo for further details.
Header Definition
The header definition file defines the UsbHidApi library class and
lists all properties and features of the library including structure assignments,
exported functions, and parameters. This file, listed below, may
be included in your application for static linking:
UsbHidApi.h
(This file is included in the distribution within the "VC++" sub-folder.)
Compiler Compatibility
TheUsbHidApi library is compatible with Visual C++ 6.0 compilers.
In addition, functions have been exported for compatibility with Visual
Basic 6.0 and certain ANSI C compilers including LabView and LabWindows/CVI.
To use the API with ANSI C compilers, adjustments may be required to the
header file (UsbHidApi.h) before including it within your application project
source. All class references must be commented
or removed from the file. A special macro definition has been provided
in the header file for this purpose:
USING_ANSI_C_COMPILER
By default, this definition is disabled (commented).
ANSI C programmers should enable this definition prior to compiling their
applications. Please view the UsbHidApi.h header for details.
For additional information, see the following online resources:
FAQ:
Using Dynamic Link Libraries with LabWindows/CVI
Integrating
external code using Import Shared Lib. Wizard
Writing
win32 DLLs and Calling them from LabView
(Note: Kadtronix can only provide limited support
for LabView and LabWindows applications. Demo applications for LabView
or LabWindows/CVI are not available.)
Device Descriptors
If you are designing USB firmware, then one of your key considerations
will be definition of descriptors for the device. This is important
for proper device enumeration by the host. There are several types
of descriptors including configuration, device, and report. Developers
will sometimes define a valid set of descriptors, but then fail to properly
classify the device. The device is classified by its "usage" such as a
keyboard, joystick, or mouse. These devices are typically allocated
for exclusive use by the operating system. If you are developing
a custom device (e.g., temperature controller, digital input/output board,
etc.) using the HID class, then your report descriptor should indicate
a "Vendor Defined" usage as indicated in the sample report descriptor below
(courtesy of L. Zadra):
/*------ Report Descriptor Group ---------------------*/
/* This is a basic report descriptor. All data in the */
/* reports are vendor defined and therefore the host */
/* doesn't care what we transfer
*/
BYTE code abromReportDescriptor[SIZEOF_REPORT_DESCRIPTOR] =
{
0x06, 0xA0, 0xFF, // Usage Page (FFA0H = vendor defined)
0x09, 0x01, // Usage (vendor defined)
0xA1, 0x01, // Start Collection (Application)
// INPUT DEFINITION
0x09, 0x01, // Usage (vendor defined)
0x15, 0x00, // Logical Minimum (0)
0x25, 0xff, // Logical Maximum (255)
0x75, 0x08, // Report Size ( 8 BITS)
0x95, EP1_MAX_REPORT_SIZE, // Report Count (16 Bytes)
0x81, 0x00, // Input (Data, Variable, Absolute)
// OUTPUT DEFINITION
0x09, 0x02, // Usage (vendor defined)
0x75, 0x08, // Report Size (8 BITS)
0x95, EP0_MAX_REPORT_SIZE, // Report Count (16 Bytes)
0x91, 0x00, // 0utput (Data, Variable, Absolute)
0xC0 // End Collection
};
For more information, please refer to the following resource:
http://www.usb.org/developers/hidpage
Function Reference
This section presents a description of functions provided by the UsbHidApi
library. The library is encapsulated with a class called CUsbHidApi
for C++ users. To allow for the greatest flexibility and compatibility,
individual functions have been exported for use with Visual Basic and other
languages.
The library defines a device structure called mdeviceList.
Used primarily by the GetList( ) function, this structure defines an entry
in a device list. The mdeviceList structure definition is
listed below.
typedef struct
{
char DeviceName[50];
// Device name
char Manufacturer[50];
// Manufacturer
char SerialNumber[20];
// Serial number
unsigned int VendorID;
// Vendor ID
unsigned int ProductID; // Product
ID
int InputReportLen;
// Size of input report
int OutputReportLen;
// Size of output report
int Interface;
// Interface
int Collection;
// Collection
} mdeviceList;
The UsbHidApi library provides a number of functions for use by
your application. Examples of their use can be found in the demonstrations.
-
GetList( )
This function searches the system for attached USB HID devices and
builds a list of these devices and their availability. The caller
must supply a pointer to a buffer that will hold the list of structure
entries. Must also supply an integer representing maximum no. of
entries the buffer can hold. Returns total number stored.
int CUsbHidApi::GetList(
unsigned
int VendorID,
unsigned
int ProductID,
char
*Manufacturer,
char
*SerialNum,
char
*DeviceName,
mdeviceList
*pList,
int
nMaxDevices);
Parameter descriptions:
VendorID
// Vendor ID to search (0xffff if unused)
ProductID
// Product ID to search (0xffff if unused)
Manufacturer
// Manufacturer
(NULL if unused)
SerialNum
// Serial number to search (NULL if unused)
DeviceName
// Device name to search (NULL if unused)
pList
// Caller's array for storing device(s)
nMaxDevices
// Size of the caller's array list (no.entries)
-
Open( )
This function opens a link to a specified USB HID device. The
caller supplies parameters describing the device, typically a vendor ID
and product ID. Additional optional parameters can be provided to
further describe the device if desired. (See also SetInterface
and SetCollection.) If a successful
open occurs, the function returns TRUE. Otherwise, the function returns
FALSE. If the bAsync flag is TRUE, subsequent read requests will
be non-blocking. A device must be opened before any other actions
can be taken on it.
int CUsbHidApi::Open(
unsigned
int VendorID,
unsigned
int ProductID,
char
*Manufacturer,
char
*SerialNum,
char
*DeviceName,
int
bAsync)
Parameter descriptions:
VendorID
// Vendor ID to search (0xffff if unused)
ProductID
// Product ID to search (0xffff if unused)
Manufacturer
// Manufacturer
(NULL if unused)
SerialNum
// Serial number to search (NULL if unused)
DeviceName
// Device name to search (NULL if unused)
bAsync
// Set TRUE for non-blocking read requests.
-
Read( )
This function reads an input report from an open device. (The
device must be open prior to calling this routine.) The type of read
performed is based on the method used for opening the device (blocking
or non-blocking). If no data is currently available, the function
returns 0. On successful completion, the function returns an integer
representing the number of bytes read (usually the input report length
defined by the device). The function returns -1 if disconnect is
detected. (Note: The caller must supply a pointer to a read
buffer that is large enough to accommodate the report length. This
number can be obtained using GetReportLengths.)
int CUsbHidApi::Read(void
*Buf)
Parameter description:
Buf
// Pointer to the caller's storage buffer
-
Write( )
This function writes an output report to an open device. (The
device must be open prior to calling this routine.) The function
writes the caller's data to the device. The number of bytes is determined
by the output report length (refer to GetReportLengths).
The caller must guarantee the correct byte-count and buffer storage.
(Note: The first byte location is usually a report ID [possibly 0].
The caller must ensure this value is prepended to the buffer.)
int CUsbHidApi::Write(void
*Buf)
Parameter description:
Buf
// Pointer to the caller's storage buffer
-
GetReportLengths( )
This function retrieves the lengths of input- and output-reports for
an open device. (The device must be open prior to calling this routine.)
The caller must supply integer pointers for storing the report lengths.
void CUsbHidApi::GetReportLengths
(
int
*input_len,
int
*output_len)
Parameter descriptions:
input_len
// Pointer for storing input report length
output_len
// Pointer for storing output report length
-
SetInterface( )
This function sets an optional device interface ID (e.g., 0) for search
purposes and is used in instances only where the device has multiple interfaces.
If used, must be called prior to calling the Open() function.
void CUsbHidApi::SetInterface
(int iface)
Parameter description:
iface
// Interface (-1 if unused)
-
GetInterface( )
This function eturns the interface ID that was set using SetInterface().
int CUsbHidApi::GetInterface
(void)
-
SetCollection( )
This function sets an optional collection ID (e.g., 0) for search purposes.
It is used to locate a device to be opened. Use this function when
a the device has multiple collections. Must be called prior
to calling the Open() method.
void CUsbHidApi::SetCollection
(int col)
Parameter description:
col
// Collection (-1 if unused)
-
GetCollection( )
This function returns the optional collection ID that was set using
SetCollection().
int CUsbHidApi::GetCollection
(void)
-
CloseRead( )
This function closes the read pipe. (The device must be open
prior to calling this routine.)
void CUsbHidApi::CloseRead(void)
-
CloseWrite( )
This function closes the write pipe. (The device must be open
prior to calling this routine.)
void CUsbHidApi::CloseWrite(void)
-
GetLibVersion( )
This function retrieves the current library version. A string
pointer must be supplied by the caller for storing the version string.
(The buffer should be at least 10 characters in length.) It
returns 0 on success, -1 otherwise.
int CUsbHidApi::GetLibVersion(LPSTR
buf)
Parameter description:
buf
// Pointer to buffer for storing the version string
U4x1 Hardware
Developers who need a pre-built USB hardware may choose to use the
UsbMicro U4x1. This module provides 16 digital signal lines, configurable
in any combination of inputs and outputs. Available in two form-factors
(U401 and U421), these devices are compatible with the UsbHidApi library.
Demonstration software is available which demonstrates input and output
operations specifically patterned for the U4x1 I/O. Kadtronix offers
these devices pre-bundled with the USB
Digital I/O Commander software.

Visit the following website for detailed hardware information:
http://www.kadtronix.com/digio.htm
The following tables present pin-out information:
U401 Interface Connector Pin Out
| Pin Number |
USB Interface Signal |
| 7 |
+5V USB from PC |
| 9 |
GND |
| 15 |
PA.0 - Port A bit 0 |
| 16 |
PA.1 - Port A bit 1 |
| 17 |
PA.2 - Port A bit 2 |
| 18 |
PA.3 - Port A bit 3 |
| 19 |
PA.4 - Port A bit 4
[SPI SS (slave-mode only)] |
| 20 |
PA.5 - Port A bit 5
[SPI MOSI] |
| 21 |
PA.6 - Port A bit 6
[SPI MISO] |
| 22 |
PA.7 - Port A bit 7
[SPI SCLK] |
| 23 |
PB.0 - Port B bit 0 |
| 24 |
PB.1 - Port B bit 1 |
| 25 |
PB.2 - Port B bit 2 |
| 26 |
PB.3 - Port B bit 3 |
| 27 |
PB.4 - Port B bit 4 |
| 28 |
PB.5 - Port B bit 5 |
| 29 |
PB.6 - Port B bit 6 |
| 30 |
PB.7 - Port B bit 7 |
U421 Interface Connector Pin Out
| Pin Number |
USB Interface Signal |
| 14 |
+5V USB from PC |
| 9 |
GND |
| 1 |
PA.0 - Port A bit 0 |
| 2 |
PA.1 - Port A bit 1 |
| 3 |
PA.2 - Port A bit 2 |
| 4 |
PA.3 - Port A bit 3 |
| 24 |
PA.4 - Port A bit 4
[SPI SS (slave-mode only)] |
| 23 |
PA.5 - Port A bit 5
[SPI MOSI] |
| 22 |
PA.6 - Port A bit 6
[SPI MISO] |
| 21 |
PA.7 - Port A bit 7
[SPI SCLK] |
| 5 |
PB.0 - Port B bit 0 |
| 20 |
PB.1 - Port B bit 1 |
| 6 |
PB.2 - Port B bit 2 |
| 19 |
PB.3 - Port B bit 3 |
| 7 |
PB.4 - Port B bit 4 |
| 18 |
PB.5 - Port B bit 5 |
| 8 |
PB.6 - Port B bit 6 |
| 17 |
PB.7 - Port B bit 7 |
-
Legal
By purchasing this product, you agree to terms of licensing and warranty.
Please read the following sections carefully.
-
License
Kadtronix and Delahoussaye Consulting (the seller) grant the registered
user of the USB HID API library, herein referred to as "the application",
the right to use one copy of the application software on a single computer.
The user may not use the application on more than one computer at the same
time unless expressly intended for this purpose (such as network applications).
The application may NOT be distributed without our express written permission.
-
Warranty
THE SELLER DISCLAIMS ALL WARRANTIES RELATING TO THIS PRODUCT, WHETHER
EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND ALL SUCH WARRANTIES
ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. NEITHER THE SELLER. NOR ANYONE
ELSE WHO HAS BEEN INVOLVED IN THE CREATION, PRODUCTION, OR DELIVERY OF
THIS PRODUCT SHALL BE LIABLE FOR ANY INDIRECT, CONSEQUENTIAL, OR INCIDENTAL
DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE SUCH PRODUCT EVEN IF
THE SELLER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR CLAIMS.
IN NO EVENT SHALL THE SELLER'S LIABILITY FOR ANY SUCH DAMAGES EVER EXCEED
THE PRICE PAID FOR THE PRODUCT, REGARDLESS OF THE FORM OF THE CLAIM. THE
PERSON USING THE PRODUCT BEARS ALL RISK AS TO THE QUALITY AND PERFORMANCE
OF THE PRODUCT.