Table of Contents
We wanted to find a new solution to connect our Hardware to the PC via USB because the experience we made with FTDI Chips was not satisfying at all.
The USB controller of our choice was the Maxim MAX3420E that is connected to the microcontroller through a Serial Peripheral Interface (SPI).
The MAX3420E comes without any drivers or libraries you can use to “speak” with it.
This is the reason why I had to develop my own “driver”.
After a day of reading and googling I decided that the best an easiest way is to use the Human Interface Device Class (HID). The HID-Class is a generic device class which means that all devices in this class are supported by Microsoft Windows and there are already existing DLLs with functions that we can use.
The start point for the “driver” was an article I found in the Microsoft developer network: „Is That You? Writing Better Software for Cool USB Hardware” written by Scott Hanselman. (http://msdn.microsoft.com/coding4fun/someassemblyrequired/isthatyou/default.aspx). Scott’s software is based on the USBSharp Class.
As mentioned above the HID Class is a generic device class so the driver is integrated in the operating system which makes things easy. If a new HID device is plugged in there is no need of any driver installation. The functions to access and control a HID device are included in the Windows hid.dll which is located in the System32 folder.
If you do not know for sure if your device is a HID device you should have a look at this little application. It is a part of windows and you can run it with the start -> run “msinfo32.exe” command. Or under Windows Vista just press the Windows-Key Type msinfo32.exe and hit enter.
Identify your USB device
USB device are identified by their vendor and product id. Those IDs are consisting of a prefix (“vid_” for vendor id or “pid_” for product id) and a 4 digit HEX number. The MAX3420E for example has the vendor id vid_06ba and the product id pid_5346. Usually both values can be changed in the source code of the USB device (assumes that you have access to that code).
Package Size / Communication Speed
HID devices are communicating with the pc through so called hid reports. Those reports consist of 64 bytes of data. Each microsecond one report is send from pc to the USB device and vice versa. This means that theoretical a transfer rate of 64 Kbytes per second can be achieved.
The driver is written in C# and designed as a DLL, this has the benefit so it is very easy to integrate the drive into a new project. Just import the DLL or the project and you are finished.
I tried to keep the interface as simple as possible on the one hand and on the other hand to integrate as much functionality as possible.
At the moment it has the following functions:
This method initializes a new instance of the USBInterface class.
The vendor id of the USB device (e.g. vid_06ba)
The product id of the USB device (e.g. pid_5346)
This method initializes a new instance of the USBInterface class.
The vendor id of the USB device (e.g. vid_06ba).
This method establishes a connection to the USB device. You can only establish a connection to a device if you have used the construct with vendor AND product id. Otherwise it will connect to a device which has the same vendor id is specified, this means if more than one device with these vendor id is plugged in, you can’t be determine to which one you will connect.
False if any error occurs
Disconnects the device
Returns a list of devices with the vendor id (or vendor and product id) specified in the constructor. This function is needed if you want to know how many (and which) devices with the specified vendor id are plugged in.
String list with device paths
This method writes the specified bytes to the USB device. If the array length exceeds 64, the array while be divided into several arrays with each containing 64 bytes. The 0-63 byte of the array is sent first, then the 64-127 byte and so on.
The bytes to send.
Returns true if all bytes have been written successfully
This method is used to activate the “reading-State”. If you execute this command a thread is started which listens to the USB device and waits for data.
This method switches from “reading-State” into “idle-State”. By executing this command the read data thread is stopped and now data will be received.
By calling this method with an event handler (System.EventHandler) the add-event listener of the USB Buffer is enabled. Thus whenever a dataset is added to the buffer (and so received from the USB device) the event handler method will be called.
The event handler (System.EventHandler) method.
There a two ways to integrate the HID USB Library into a Visual Studio project. One is to add the library project to your Visual Studio solution. The other way is to add a reference to the USBHIDDRIVER.dll in the visual basic project you want to use.
Add the library project to a Visual Studio 2005 solution
1. Open your Visual Studio solution. Go to “File > Add > Existing Project”
2. The following Dialog will open. Navigate to the USBHIDDRIVER.csproj and click open.
3. Right click on your Visual Studio project and click « Add Reference »
4. Select the „Projects“-tab. Then select the USBHIDDRIVER and click ok.
Add the USBHIDDRIVER.dll to a Visual Studio project
1. Right click on your Visual Studio project and click « Add Reference »
2. Select the „Browse”-tab. Then navigate to the USBHIDDRIVER.dll.dll and click ok.
Use the USBHIDDRIVER.dll with Visual Basic 6
The .Net runtime allows unmanaged COM aware clients (like Visual Basic 6 applications) to access .Net components through the COM interop and through the tools provided by the framework.
The USBHIDDRIVER.dll is a .NET assembly which can not be accessed by Visual Basic 6. Therefore we have to create a type library which can be used with Visual Basic 6. This can be done with the Tool RegAsm.exe that you will find in the directory of your .Net framework installation.
Create a .bat file in the directory of the USBHIDDRIVER.dll and write the following command into it. Then run the bat file.
“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe” USBHIDDRIVER.dll.dll /tlb: USBHIDDRIVER.dll.tlb
Now you have to copy both the dll and the tlb file in the same directory as the application which is consuming it.
How to use the HID USB Library
Let’s say you want to communicate with an USB device that has the vendor id vid_06ba and multiple products id.
So the first thing you may want to have is a list of all devices connected to the pc which have the specified vendor id. To do this just make a new instance of the USBInterface class with only the vendor id as parameter. Then call the getDeviceList-Mehtod and you will receive a list of the devices that are connected.
USBHIDDRIVER.USBInterface usb = new USBInterface(“vid_06ba”);
String list = usbI.getDeviceList();
For communicating with the device make a new instance of the USBInterface class with the vector id and the product id as parameter. After that, you can call the connect-method and a connection will be established.
USBHIDDRIVER.USBInterface usb = new USBInterface(“vid_0b6a”, “pid_5346″);
Once your device is connected you can read or write data. To write data just call the write method with an array of bytes you want to send as parameter.
For reading you have to options: first without the USB buffer event and second with USB buffer event.
If the event is enabled, the event handler method will be called when a new dataset is received.
Otherwise you can process the data with a timer or something like this.
The library is based on the USBSharp class that imports all needed methods from the kernel32.dll and hid.dll. The HIDUSBDevice class wraps those methods and handles the read thread. The USBInterface is the main interface, which is accessible from outside the dll. In the USBInterface class exists a object of the ListWithEvent, which is basically a ArrayList with the bonus that an event is fired when a new dataset is added. Last but not least in the HidApiDeclaration class you will find some declarations needed for the whole hid-thing.