USB HID and Python



This demo includes two files. The function wrapper is in (figure 1). It provides a simple interface to the AHid.dll.

Figure 1: implements the interface to the AHid.dll.

Figures 2 and 3 show the source code of the demo application. In a first step, the driver is initialized. After that, the keyboard input is processed in an endless loop. Press 'x' and 'Enter' to shut down the program.

Figure 2: Initialization of AHid.dll and definition of constants and variables.

Figure 4 shows the python output on the console.

Figure 3: Main loop.

The function return values are always zero in case that no error occures (Figure 4). The return value of -1 signals an error. These errors are mostly based on wrong parameters passed to the register() function. Check back VID, PID and the Report values first. They all must match your device.

Figure 4: Data output after a successful initialization.

Figure 4 shows the data output. You can see that the DLL was initialized first (init library = 0). After that the registration of the data handles is made by the register() function. The Handles counter starts from 1 and is counting up.

The device was found on USB (find device = 0) and starts sending and receiving data. In this case, 32 bytes are send and 64 bytes are read from the HID device. These values may differ and depend on the setup of your HID device, done in the so called Report Descriptor.