USB HID and LabVIEW



Downloads



Application

AHid.dll allows you to transfer data between an USB Host and a HID device. Based on a sample application, we show you the required steps to connect your device to LabVIEW. Figure 1 presents the GUI of the sample application.

The buttons are used to connect to the USB HID device and to send data to it.

The application is partitioned in three sections. The Device part displays values that are used for Input and Output Reports as well. It includes the Vendor ID and the Product ID of your device. All values are given decimal.

Often an USB HID device has only a single interface and therefore does not make use of the Interface ID. This is expressed by setting the Interface ID to -1 (no interface).

Figure 1: GUI to send and receive data from PC to USB HID device and back.


The Output part is all about transferring data from Host to device. In figure 1, the Report ID is set to 0 and the value for the Report Size is 32. The Report Size is part of the Report Descriptor . The Report Descriptor describes the data transferred by any HID device. It is part of the device's firmware. The Report ID is also found in the Report Descriptor . Sometimes it is omitted. In such cases a value of 0 is chosen.

The Input section is responsible for data reception. Report Size and ID follows the criteria listed in the Output section.

Workflow

The functions of AHid.dll are called within a state machine. The following states are used:

- Disconnected: No usage of AHid.dll.
- Init: Driver initialization.
- Register: Creation of the transfer pipes.
- Idle: Data reception and tracking the connection state.
- Write: Write to device.
- Deregister: Cleanup.

The application starts in the Disconnected state and stays here until the Connect button is pushed (figure 2).

Figure 2: Disconnected state: nothing to do.


Figure 3 shows the Init state. AHid.dll is initialized by a call to AHid_init().

Figure 3: Call AHid_init() first.


The next state is Register (figure 4). The data pipes for the Input and Output transfers are created by a call to AHid_register(). The Interface ID is set to -1 (no interfaces). All other parameters are taken from the graphical user interface (GUI).

The state machine immediately switches to the Idle state (figure 5).

Figure 4: Register Input and Output pipes for transferring data with AHid_register().


AHid_find() is called by a timer event. The connection state is less relevant in most cases and therefore it is no problem to set the timer interval to a few seconds (figure 5).

Figure 5: Read available data with AHid_read() and track connection state with AHid_find().


AHid_read() reads data from the HID device. The data transfer from device to Host is made in two steps. First, your device sends data to the PC where AHid.dll collects it. The collected data can then be transferred to your application using AHid_read(). Doing it this way, it could not happen to miss some data, because AHid.dll acts like a data buffer.

Sending data to the HID device is done by AHid_write() (figure 6).

Figure 6: Write to your device with AHid_write().


The application is always switching from Idle to Write state during normal operation. This transfer loop is left by pushing the Connect button again. The state machine moves on to Deregister state in figure 7.

Figure 7: Cleanup pipe handles using AHid_deregister().


The connection to the HID device is stopped now. All left to do is a cleanup. Call AHid_deregister() to free the pipe handles and their dedicated memory used by AHid.dll.