User Manual

 

Version 1.20
 

06/14/11
 

Copyright (C) 2003 - 2011
 

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

  1. Introduction
    1.  
    1. System Requirements
    2. USB Compatibility
    3. Features
    4. Ordering
    5. Demonstrations
    6. Installation
    7. Linking
      1.  
  2. Header Definition

  3.  
  4. Compiler Compatibility

  5.  
  6. Device Descriptors

  7.  
  8. Function Reference
  9.  
    1. GetList( )
    2. Open( )
    3. Read( )
    4. Write( )
    5. GetReportLengths()
    6. SetInterface( )
    7. GetInterface( )
    8. SetCollection( )
    9. GetCollection( )
    10. CloseRead( )
    11. CloseWrite( )
    12. GetLibVersion
       
  1. U4x1 Hardware
    1.  
  2. Legal
    1. License
    2. Warranty




 
  1. Introduction

  2. 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). 

    1. System Requirements

    2. UsbHidApi requires a Windows laptop or desktop PC with the following minimum capabilities:
           
        1. 1 MB RAM
        2. USB port
        3. Windows XP or higher
       
    3. USB Compatibility

    4. 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

         
    5. Features

    6. UsbHidApi library features include the following:
           
        1.  Easy to use API calls - Open( ), Read( ), and Write( ), etc.
        2.  No custom drivers (uses USB HID drivers already included in Windows)
        3.  Develop PC host applications quickly
        4.  Write applications for USB HID devices
        5.  Two available demonstration applications: Generic and U4x1
        6.  Free demo source code included:  Visual C++ and Visual Basic
        7.  VC++ demo source available for either generic demo app or U4x1 demo app.
        8.  VB demo source available for generic demo app only.
        9.  Can be used with ANSI-C compilers such as LabView and LabWindows/CVI
        10.  Use implicit- or explicit-linking
        11.  Includes static (.lib) and dynamic (.dll)  libraries
        12. Compatible with UsbMicro U4x1 devices

           
    7. Ordering

    8. UsbHidApi is available at the following source:

          Kadtronix
          web: http://www.kadtronix.com
          email: sales@kadtronix.com
       

    9. Demonstrations

    10. 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 includes demonstration source code for use with the following development environments: 

      1. Visual C++ 2008
      2. Visual C++ 6.0
      3. Visual Basic 6.0

The U4x1 demo is applicable only to UsbMicro U4x1 devices and includes demonstration source code for use with the following development environments: Pre-built demo executables are included in the installation, allowing you to run the demo even in the absence of a development environment.
       
      1. Generic Demonstration

      2. 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++ 2008 generic demo project source code is located on your PC as follows:
         
          Start -> All Programs -> USB HID DEMO -> VC2008

         
        The Visual C++ 6 generic demo project source code is located on your PC as follows:
         

          Start -> All Programs -> USB HID DEMO -> VC6


        To run the Visual Basic generic demo, first open the following folder:

           
          Start -> All Programs -> USB HID DEMO -> VB6

         
      1. U4x1  Demonstration

      2. 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.
       

    1. Installation

    2. 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.
       

    3. Linking

    4. 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.

       
  1. Header Definition

  2. 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.)
     

  3. Compiler Compatibility

  4. TheUsbHidApi library is compatible with Visual C++ 2008, Visual C++ 6.0, and Visual Basic 6.0 compilers.  To meet the needs of developers who use other compilers, the API has been adapted for .NET (2003) and ANSI C use.  For .NET, use the header file named: UsbHidApi_NET.h.  (Refer to the "NET" folder of your installation for details.)

    In addition, functions have been exported for compatibility with certain ANSI C compilers such as LabView and LabWindows/CVI.  For these situations, a special header file has been created named UsbHidApi_2.h and is located in the "ANSI C" folder.  This is a specially modified version of the original header (UsbHidApi.h) and is intended only for use with ANSI C applications.  To use, copy the file to your project space and add the following statement to your C source file:

        #include "UsbHidApi_2.h"

     This statement serves to include or add the file to your application.  Also add the following library file to your list of build objects
     (located in the "VC++" folder):   

      UsbHidApi.lib

     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.)
     

  5. Device Descriptors

  6. 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
     

  7. Function Reference

  8. 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.
     

    1. GetList( )

    2. 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)
       

    3. Open( )

    4. 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.
       

    5. Read( )

    6. 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
       

    7. Write( )

    8. 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
       

    9. GetReportLengths( )

    10. 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
       

    11. SetInterface( )

    12. 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)
       

    13. GetInterface( )

    14. This function eturns the interface ID that was set using SetInterface().

      int CUsbHidApi::GetInterface (void)
       

    15. SetCollection( )

    16. 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)
       

    17. GetCollection( )

    18. This function returns the optional collection ID that was set using SetCollection().

      int CUsbHidApi::GetCollection (void)
       

    19. CloseRead( )

    20. This function closes the read pipe.  (The device must be open prior to calling this routine.)

      void CUsbHidApi::CloseRead(void)
       

    21. CloseWrite( )

    22. This function closes the write pipe.  (The device must be open prior to calling this routine.)

      void CUsbHidApi::CloseWrite(void)
       

    23. GetLibVersion( )

    24. 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

     
  9. U4x1 Hardware

  10. 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 devices include the following:

    1. U401
    2. U421
    3. U451

    Demo software is available which demonstrates input and output operations specifically patterned for the U4x1 I/O.  U4x1 hardware is available at USBMicro (www.USBmicro.com).  Kadtronix also offers the USB Digital I/O Commander software which is compatible with U4x1 devices.

    Refer to the following resources for additional information:  

      http://www.kadtronix.com/digio.htm
      http://www.usbmicro.com
      http://www.circuitgizmos.com

    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

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

U451 Interface Connector Pin Out
 
Pin Number USB Interface Signal
J1-10
+5V USB from PC
J1-9
GND
J1-1 PA.0 - Port A bit 0
J1-2 PA.1 - Port A bit 1
J1-3 PA.2 - Port A bit 2
J1-4 PA.3 - Port A bit 3
J1-5 PA.4 - Port A bit 4   [SPI SS (slave-mode only)]
J1-6 PA.5 - Port A bit 5   [SPI MOSI]
J1-7 PA.6 - Port A bit 6   [SPI MISO]
J1-8 PA.7 - Port A bit 7   [SPI SCLK]
n/a PB.0 - Port B bit 0   [Relay 1]
n/a PB.1 - Port B bit 1   [Relay 2]
J4-1 PB.2 - Port B bit 2
J4-2 PB.3 - Port B bit 3
J4-3 PB.4 - Port B bit 4
J4-4 PB.5 - Port B bit 5
J4-5 PB.6 - Port B bit 6
J4-6 PB.7 - Port B bit 7

  1. Legal

  2. By purchasing this product, you agree to terms of licensing and warranty.  Please read the following sections carefully.
  1. License

  2. 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.
     
  3. Warranty

  4. 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.