USB HID Template for Visual Basic 2005/2008/2010

Published on 29 November, 2010

Introduction

With the decline of serial and parallel ports from modern computers, electronics hobbyists are turning more to utilizing USB (or stick with old computers for their legacy ports). Unfortunately, unlike serial and parallel ports, USB is far from simple and so it can be daunting to try to develop hardware and software for it. However, there are many hardware and software solutions that make developing USB device much simpler.

Some PIC microcontrollers now come with a USB Serial Interface Engine (SIE) that handles the very low level parts of the interface. However, writing firmware to work with the SIE can still be a difficult task. Luckily, many PIC compilers come with USB libraries that work out of the box and are dead easy to use. The code generated by these compilers tends to produce a USB Human Interface Device (HID) as these devices do not require custom drivers to be written because Windows has them preinstalled. However, you still need to write your own PC software to read and write data from your USB device. This article shows you how to do this.

Visual Basic Template

The Visual Basic template, which you can download at the bottom of the page, generates the basic code framework that is needed to interface with your USB device. All you need to do is set the VID, PID and buffer sizes going into and out from the PC. After that you’re ready to read and write data.

To give credit where credit is due, I did not write the code that is in the template – the code is based on the code generated by the EasyHID application from Mecanique and modified for Visual Basic 2005 by Steve Monfette. I modified his code a little, wrote some documentation and packaged it into a VB template.

mcHID.dll on 64-bit Windows

mcHID.dll is a 32-bit DLL and so can only be linked with 32-bit applications. Since all 64-bit versions of Windows come with the WOW64 emulator to run 32-bit applications, mcHID.dll will work on a 64-bit operating system but only if your application is compiled as 32-bit/x86. If you receive runtime errors such as “An attempt was made to load a program with an incorrect format” or similar messages, then make sure that you are not compiling your program to the “x64” or “Any CPU” configurations. “Any CPU” configures your program so that it runs as a native 32-bit application on 32-bit Windows and runs as a native 64-bit application on 64-bit Windows. This means that the same executable compiled using “Any CPU” will work fine with mcHID.dll on 32-bit Windows, but will fail on 64-bit Windows. Make sure to compile using the x86 configuration.

Installing

Installing the template is simple. Download and extract the archive and you will find two files: USBTemplate.zip andmcHID.dll. Copy mcHID.dll to your windows\system32 folder and copy USBTemplate.zip (copy the actual zip file – do not extract it) to your My Documents\Visual Studio [2005/2008/2010]\Templates\ProjectTemplates.

Using the Template

After installation, load up Visual Basic 2005/2008/2010 and create a new project. When the New Project dialog opens, you should see the USBTemplate option at the bottom.

Select it, give your project a name and then click on OK. Visual Basic creates the basic code framework for you.

The following files are created and can be found in the Solution Explorer:

  1. frmUSB: This is the main form where USB communication takes place.
  2. mcHIDInterface: Contains the underlying code.
  3. HOT TO USE: Contains instructions on how to use the template.

The only file which you need to modify is frmUSB.

Updates

2011/03/06: Modified the declaration of the PID and VID in the main form from Short to Integer (Thanks for Fred Schader).

Download

The template and the DLL file can be downloaded below:

USB HID Template for Visual Basic 2005/2008/2010

232 Responses to USB HID Template for Visual Basic 2005/2008/2010

  1. Fred Schader says:

    I believe that there is an error in the code. The product ID is specified as a short but it will not take a product ID higher than 32767. I recently got a PID from Micrichip that was in the 64k range and to get the program to work had to declare the PID as an integer. Not sure if this also applies to the VID.
    Thanks for the help getting this to run on W7 with the x86 vs any CPU comment

    • Amr Bekhit says:

      Hi Fred,

      Thanks for pointing that out. Yes, there is indeed a problem with the code. The PID and VIDs are both 16-bit values, hence the use of Short for the variable declaration. However, because Short is a signed 16-bit value, its range is 32767 to -32768. Changing it to UShort or Integer (as you have done) solves the problem.

      I will update the template with the corrected code.

  2. Mike says:

    I can’t get this to communicate.

    I have set VendorID, ProductID and buffers correctly. When I check it alwasys returns a handle of “0”. I am trying to connect to a Pic 18F4550. I can talk to it through the Hid terminal that comes with MikroE development environment. I have your template running in Visual Studio 2005. It runs without error but it doesn’t communicate.

    Thank you in advance for your comments.
    Mike

  3. John in Ottawa says:

    Just started using this template, a great resource! In my application, I will be tracking up to 10 identical HID devices plugged into a USB hub. I don’t mind tinkering with the code, but would appreciate any advice you can share or gotchas to avoid.

    Thanks
    -john

    • Amr Bekhit says:

      Hi John,

      Sorry for not getting back to you earlier. It is possible to use the template to talk to multiple identical devices.

      At the moment, the code assumes that there is only one device. In any function calls that require a device handle (e.g. hidWrite, hidSetReadNotify) the code simply retrieves the handle by calling hidGetHandle. This will always retrieve the handle of the first device with a matching VID and PID. If you have a look at the OnChanged function in frmUSB, you will see that the hidSetReadNotify function uses the handle returned by hidGetHandle, which means that the OnRead event will only get called when data is received from the first device you plugged in.

      In order to modify the code to allow you to communicate to multiple devices, modify the OnPlugged function to add the matching handle to an array or list somewhere in your code. Do the same for the OnUnplugged function but remove the matching handle from your array. That way, your array will always contain a list of the handles of the currently plugged in devices. For example, declare a list at the top of your form somewhere:

      Dim USBHandles As New List(Of Integer)

      Now modify the OnPlugged and OnUnplugged functions as follows:

      Public Sub OnPlugged(ByVal pHandle As Integer)
      If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
      ' ** YOUR CODE HERE **
      USBHandles.Add(pHandle)
      End If
      End Sub

      Public Sub OnUnplugged(ByVal pHandle As Integer)
      If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
      hidSetReadNotify(pHandle, False)
      ' ** YOUR CODE HERE **
      USBHandles.Remove(pHandle)
      End If
      End Sub

      Finally, modify the OnChanged function to enable read notifications for all your plugged in devices:

      Public Sub OnChanged()
      For Each pHandle As Integer In USBHandles
      hidSetReadNotify(pHandle, True)
      Next
      End Sub

      You should now be able to send data to your devices individually by using hidWrite (not hidWriteEx). When your devices respond with data, OnRead will be called.

      I will be modifying the USB Template to incorporate these changes as this makes it much more generic and useful.

  4. Robert Lowe says:

    I am having trouble sending data from PC to the PIC.
    I use ‘hidWrite(VendorID, ProductID, BufferOut(0))’ but the VB program hangs. (stops working)
    Passing data from Pic to VB works OK.
    Any ideas would be appreciated.

    Thank you
    -Robert

    PS: Could not find ‘hidWrite’ routine in the template.

    • Amr Bekhit says:

      Are you still having problems? Can you get in touch using the Contact page? I’d like to have a look at your code.

      Regarding hidWrite, the function is available in the template and does indeed work – you can get the handle of the device from the OnPlugged routine.

  5. John in Ottawa says:

    Thanks Amr. Actually, your timing was great as your response confirmed the solution I had cobbled together.

    Due to the number of devices (6), I created a class for the device, placed the buffers and DataIn in that class as well as the pHandle, Serial and related stuff, and created a list to add and remove the elements with plug and unplug respectively. That way I don’t mix up data streams when interrupts come in. I had also modified away from the ‘Ex’ versions of the calls, which works as advertised.

    My thinking ran that, as long as I had separate datain and buffers, I should not have collisions when multiple devices were responding during a multi-part message – I would just route the data to the data in and buffer for the class instance and it should work.

    I did find that I needed to use a timer running a polling routine so I can hear from more than just the head of the pack. It works great as long as all of the devices are connected before I start the application and I don’t remove one until I shut down. For some reason notification gets lost for the device next to the port that has a plug or unplug event. I’ve not sure if the handles are subject to change at this time.

    Is it possible for the pHandle for a connected device to change when another is disconnected?

    I also note that my devices are dual registered (Windows XP). One set of handles has the serial number, the other the long integer Handle assigned by the system. Is there a way to access the serial number based handle, or do we need a different DLL for this? Alternately, can you help me decode the text and length requirements of the hidGetSerialNumber method?

    Thanks again for your support. Great to have this resource available?

    -john

    • Amr Bekhit says:

      Hi John,

      Glad to hear that you’re making progress. I’ve done some more testing and what I’ve found is that every time you plug in or unplug a device, the OnChanged event gets called and the read notifications for all devices get removed. If you use hidGetItemCount and hidGetItem functions to list all of the HID handles you will find that they will completely change everytime a device is plugged or unplugged. If you modify the OnChanged function to scan through the list of new handles, identify the ones that match your VID and PID (remove the code that does that from OnPlugged) and reenable their read notifications, then you will continue to get read interrupts for all your devices.

      Assuming that everything described above is working as it should, then it’s obvious that you can’t use the handles as unique identifiers. It certainly seems possible to use serial numbers if you can modify your descriptor to add one in (I’m looking into that).

  6. John in Ottawa says:

    Oops – wrong keyboard – the ? should have been a !!

  7. John in Ottawa says:

    I have a bit of an update. It appears that, though I can see the new HID handle, set its readnotify to true and test that it is indeed true, I can’t get the interface to pass data from it.

    I have found a workaround, though. In the event of an unplug, I clear all of my buffers, clear my device list, then disconnect from HID, followed by a reconnect. The reconnect triggers the Plugged routine which populates my screen and seems to find all of the new handles.

    Now, this might pose problems for a history log, but I think I can deal with that by simply referencing the history log in a csv file by serial number.

    Does this approach raise any flags with you?

    • Amr Bekhit says:

      Hi John,

      I too have experienced the same behaviour as you: if you clear your list of handles and repopulate it with new ones you can continue to communicate with your devices. I’m currently looking into adding a serial number to the descriptor in mikroC so that I can continue to identify my devices when the handles change.

    • Matthew says:

      First off, thank you to Amr for giving me a starting point! I’m trying to implement a quiz bowl lockout system on the cheap. I have 10 identical USB keypads and I need to identify which keypad is pressed first.

      JOHN, is there any chance this is what you were trying to accomplish? If so, do you have a project you might be willing to share? You can find my contact information on my web site, which is linked to my name.

      Thanks,
      Matthew

  8. Jonathan says:

    Hi,

    I have the same problem of Robert Lowe (message 4.)

    I can’t write on my PIC from the VB interface…

    What’s the solution ?
    Thanks

  9. luis says:

    Hi,
    in case of n. 1 Hid with different endpoint
    -Endpoint 81, in 1
    -Endpoint 01, out 1
    -Endpoint 82, in 2
    -Endpoint 02, out 2
    -Endpoint 83, in 3
    -Endpoint 03, in 3
    WHAT TO DO ????
    What’s the solution?
    Thanks

  10. Luis says:

    Sorry if I talk about something elsr, but, referring to reply n. 3 (multiply hid): Every hid can have different endpoints. Every endpoints can transmit packets of different bites. Usually with mcHID.dll you can read and write only one endpoint. The question is: if you have more endpoints, how should you modify mcHID.dll in order to manage all of them? Many greetings, thanks a lot! Luis

    • Amr Bekhit says:

      Hi Luis,

      HID devices with multiple endpoints are not something that I have worked with previously. Looking through the list of functions in mcHID.dll, there does not appear to be any way of specifying which endpoint to communicate with.

      However, If you have a look at the Visual Basic code for reading and writing, you will notice that the first byte of the read and write buffers is always 0 (report ID). Perhaps changing this number could allow you to communicate with multiple endpoints, with the number specifying the endpoint number?

  11. Luis says:

    Hi Amr,
    I noticed the “report ID always set to 0”.
    If Report ID 0 is not sent, the transmission is not performed.
    Using a USB sniffer, the microcontroller sets correctly the following endpoints:
    – Endpoint-81, in 1
    – Endpoint-01, out1
    – Endpoint-82, in 2
    – Endpoint-02, out2
    – Endpoint-83, in 3
    – Endpoint-03, out3
    81,01,82,02,83,03 are the addresses.
    I just have to do some testing and I will tell you the result.

    Thanks

    Luis

  12. Luis says:

    hi,
    …unfortunately are not able to work with different endpoint….

    Luis

  13. Dear Amr,
    I am trying to use your template and am having problems. I seem to be able to connect ok. I am getting OnChanged events and OnPlugged events. However, I can’t seem to be able to write to the device.
    When I execute “hidWriteEx(VendorID, ProductID, BufferOut(0))” and use a USB device monitor, I see nothing happen. When I execute “hidWriteEx(VendorID, ProductID, BufferOut(1))”, I see some activity, but not enough – I see 2 bytes go out, not the 3 I had thought I was passing. Also, I don’t get a response (probably because not enough is being passed out…)
    So I have a few pointed questions beyond “help”.
    1) How do I determine the correct value for BufferOutSize? Is it the number of bytes I am sending + 1? (ie. I need to send the device “C0 03 12”).
    2) How do I determine the BufferInSize? Is this based on the expected response from the device?
    3) Do I need to interate through the BufferOut array?

    Thanks

    Kevin

    • Amr Bekhit says:

      Hi Kevin,

      – 1&2: The size of the buffers transferring data in and out of the PC are fixed and should match the sizes of the IN and OUT buffers of your USB device, as determined by the device’s descriptor.
      – 3: I’m not really sure what you mean. You will probably need to iterate through it to fill it with data, but then you just need a single call to hidWriteEx to transmit the buffer contents.

      What is your USB device? Did you develop it yourself or are you using an existing HID device?

      Amr

  14. Amr,
    Thanks for the response. I am trying to work with a USB/HID RFID reader. I have the API manual for the reader, so know what to send and what to expect back… My code at present (to send soemthing) is:

    BufferInSize = 64
    BufferOutSize = 3

    ReDim BufferIn(BufferInSize)
    ReDim BufferOut(BufferOutSize)

    BufferOut(0) = 0
    BufferOut(1) = CByte(Convert.ToInt32(“31”, 16))
    BufferOut(2) = CByte(Convert.ToInt32(“03”, 16))
    BufferOut(3) = CByte(Convert.ToInt32(“01”, 16))

    hidWriteEx(VendorID, ProductID, BufferOut(0))

    When I do the above, nothing happens (I am using the “Device Monitoring Studio” to watch comms between the device and the computer.

    If I change the hidWriteEX index from 0 to 1, I see the 2nd and 3rd bytes of data go out, but not the 1st.

    By putting in a message box at the end of the code, I can show that the “stall” is occuring at the hidwriteex code, because I never get the message box to show.

    Thanks

    Kevin

  15. Oh, and it is a purchased device, I am using and when I say hidwrite index, I am referring to the index number of “BufferOut” in the hidWriteEX function.

    Kevin

    • Amr Bekhit says:

      Hi Kevin,

      Hmm…I’m wondering if the configuration of your RFID reader might not be compatible with mchid.dll. Can you give me the specific part number of this reader? I’d like to have a quick skim through the datasheet myself.

      Also, there should be no need to ReDim the buffer arrays – simply set the constants at the top of From1.vb to the sizes of your buffers, although there should be nothing wrong with what you’ve done above.

      • kevin berisso says:

        Amr

        The unit is called the RFID Me ( http://www.mti.com.tw/rfidme/). If you send me an email to autoid@ohio.edu, I van send you the API doc for the reader.

        I am wondering if the unit has something weird as well. I know that their software, and another third party have successfully worked with it, but I am not a super strong programmer and wonder if it is just that.

        The resin was because the different commands result in different sized responses from the device. I was hoping that the wproblemwas the code was waiting for more from the buffer.

        Kevin

  16. Mike says:

    I really like mcHID.dll. I’m using it to communicate with several different devices. I would like, however to write a VB class library to communicate with my devices, rather then a form class. I’m not real strong with VB, and was wondering how I can set a ‘hook’ to my class library rather then a form (phostwin)? Any hellp would be appreciated.

    • Amr Bekhit says:

      Hi Mike,

      Hmm…I’m not sure you’re going to be able to get away without using a form. I think that mcHID needs a form handle so that it can perhaps capture and send Windows messages to the form as a way of sending and receiving data from it (have a look at WinProc).

      I would be very interested to hear from you if you find otherwise.

      Amr

      • space says:

        simply create your class with the mcHID.DLL definitions and, in your main, use something like that :

        Dim WithEvents MyUSB_IO As New ClassUSB_IO(VendorID, ProductID, Me)

        regards

  17. Nawar says:

    Hi Amr,
    I just wanted to say thanks a lottttt
    I’ve been trying to communicate with the USB for two weeks now using C#
    then I decided to switch to VB and I found your site and it’s the best

    Thank you
    Nawar

  18. Dustin says:

    Hey Amr,
    I am a student at the University of Colorado at Colorado Springs. I am using your template to communicate my vb 2010 program to an arduino uno via series 1 XBee modules. Looking at your template it shows how to read data into the pc but not send data out. My vb program will be sending out data not reading any incoming data. (This is my first time using vb) Is there anyway you can show me an example code of a sub module for writing data from the pc to anything?

    Thanks for your time
    Dustin

    • Amr Bekhit says:

      Hi Dustin,

      If you want to send data to your USB device, use the HIDWrite or HIDWriteEx functions. The readme file in the template should show an example of their use.

      Amr

      • Dustin says:

        Hey Amr,
        Thanks for the response last time, I went back and read the read me file again and again and came up with this:
        Private Sub CamPostion1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        hidWriteEx(ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
        ‘This function is used to send data to the USB device. It’s usage is very simple:
        ‘the BufferOut array (see above)
        ‘is filled with the data that needs to be sent (make sure to set BufferOut(0)=0).
        ‘After that, the function is called as follows:

        BufferOut(0) = 0
        BufferOut(1) = 0I
        BufferOut(2) = 1I

        hidWriteEx(VendorID, ProductID, BufferOut(0))
        End Sub

        The problem is that it is giving me expected errors in the hidWriteEx() expression. What I am trying to do is send a single number (when a button is clicked) with XBee to an Arduino Uno that is why the sub CameraPostion1_Click() called then hidWriteEx() is called after that. Any advice would be wonderful. Thanks for your time!

        Dustin

  19. Gc says:

    I was having a lot of trouble until I started using the template. I have been able to successfully create a form that can communicate with my microcontroller.

    Is it possible for you to make a template for console application or provide instructions to use mcHID.dll in a console application? I am not an advanced programmer in Visual Studio so I was unable to figure it out. You current template works mostly based on events which is great when using it as part of a form. I am trying to get it to work in a linear fashion such as:
    Check if device connected->Send Data->Receive Data

  20. luca says:

    Wonderful, incredible and amazing.
    Only to download and very easy to use. It works always. Thank you.
    I use from vb.net express 2010. Only since connectToHID is a boolean function with no return value I have a warning in compilation.
    Please suggest me an idea to fix this (transforming in subroutine could be?)
    Best Regards
    Luca

  21. Manal says:

    i need to transmit data from micro controller pic18f4550 to laptop via usb. at first just run ur program i had build errors like
    Error 2 ‘Sub Main’ was not found in ‘$safeprojectname$.My.MyApplication’.

    Error 3 Character is not valid. C:\Users\MMM\Downloads\USBTemplate-2011-03-06-16-34-00\USBTemplate\My Project\Application.Designer.vb 35 34 USBTemplate1

    Error 4 ‘Settings’ is not a member of ‘My’. C:\Users\MMM\Downloads\USBTemplate-2011-03-06-16-34-00\USBTemplate\My Project\usb_vbb.vb 34 13 USBTemplate1

    Error 5 Character is not valid. C:\Users\MMM\Downloads\USBTemplate-2011-03-06-16-34-00\USBTemplate\My Project\usb_vbb.vb 67 55 USBTemplate1

    please help me to understand how solve these problems

  22. azer says:

    hi if i want to create a button that send “111” to my pic
    and an ather button that read continously what the pic send
    what is the code that will let me do that

    thank you for your eventual help

  23. rony galo says:

    excelent
    I´m trying to build a vendor device,,,,will be usefull

  24. lcpoon says:

    Hi,

    I have two HID devices (same PID and same VID) connected to the host. I want to send the command to one of the HIDs. Which API to be used?

  25. alfonso says:

    hi, i didn’t understand how to send a byte on usb port if iwont to do it which function have i to use?can you make an example for me tanks

  26. alfonso says:

    hi i didn’t undrstand how to use hidusb.
    If i want to send a byte on usb port which function have i to use hidwriteex or hidwrite? can you write for me an exsample code please? tanks

  27. Erika says:

    Hi, I’m Erika, I’m peruvian so i hope that you can understand this.
    I’m trying to comunicate a freescale whith my computer by USB port, and i need to get data from the freescale using Visual Basic, maybe you can help my with this, jejeje , im recently knowing more about VB, and i need help please =D

  28. Julito 3A says:

    Wow!!! i was looking for this for a long time!!! time to study vb 2010 and migrate from vb6 ^^ … btw does this lib supports bulk transmissions ??

  29. Vincent says:

    This is a v useful sample.
    I am trying to interface with a PC mouse
    I can connect and disconnect ok.
    Am not sure I have set up the right buffer size.
    I don’t seem to be able to read Buffer(1). The OnRead is never called.
    Is this because Windows takes control of the actual device and so block other app to read from it?
    Any idea,
    Cheers.

  30. reltkaine says:

    thans for these tutorial i was able to connect pic18f4550 mikroC pro

  31. stephen says:

    Hi and thanks for the great tutorials.
    I used your template to try and connect to a PIC 18F2550.
    I can’t seem to be able to connect to the PIC although both VID and PID match the descriptor in my MikroC project.

    Do you have any idea what could cause that?

    Also, I am getting an error in VB 2010 when I try to send some data to the PIC. I create a simple button in the form which only does:
    BufferOut(1) = 255
    BufferOut(0) = 0
    hidWriteEx(VendorID, ProductID, BufferOut(0))

    the error (badformatimage..) says: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

    It seemed pretty straight forward while reading your tutorial but I can’t figure out what I am doing wrong.
    Other than the same buffer size (which is 64 bytes) for both my descriptor and the VB form, is there anything else that must match in order for the communication to work?

    Thanks!
    Stephen

    • Amr Bekhit says:

      Hi Stephen,

      The error you’re getting is because your application is running as a 64-bit process and is trying to load the 32-bit mcHID.dll. Simply change your compile platform in Visual Studio from x64 or AnyCPU to x86.

      Amr

  32. Andrzej says:

    Hi – could you, please, help me to fix the following problem?
    I connected USB scanner the USB port and I modified VendorID to &HC2E and ProductID to &H206 as determined by USB scanner.
    When I unplug or plug this device, OnPlugged or OnUnPlugged functions are called.
    When scanning any code bar – OnRead function is never called.
    What should be the pMsg value to be tested in WinProc function instead of WM_HID_EVENT (which is good for NOTIFY_PLUGGED, or NOTIFY_UNPLUGGED and other cases)?
    Thank you in advance for your help,
    Andrzej

  33. TImothy says:

    I am battling with same problem as Andrzej. I can read from HID RFID reader. I can detect plugging and unplugging of the device. Whenever an RFID card is scanned, the Onread event is not triggered.

    I’ll appreciate your assistance.

    Regards,

    Tim.

  34. Fernando says:

    Hi, I have a problem. I cant get this to work under W7. When I test the solution under XP, it works ok. But under W7, when I add the reference to mchid.dll the VS popsup a message “Cant add reference… verify its a valid COM component” (this is a translation, something like that it says). What can be de the problem? I’m working with VB2008 Express.
    Thank you.

  35. Fernando says:

    Thank you Amr! That did it! Thanks for your fast response!

  36. Salman says:

    Hi,

    I am beginner at VB and I am using your code to communicate with PIC18F4550. As a previous comment said, I am facing problems.

    Public Sub hidWriteEx(ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean

    hidWriteEx(VendorID, ProductID, BufferOut(0))

    My compiler gives an error “end of statement expected” at “AS Boolean” statement. Please help me figure this out.

    Regards,
    Salman

  37. Arun Sharma says:

    First of all i want to thanks you..

    Your USB tempelate Saves my huge time..

    I had successfully sent data from PC TO USB

    but what i want is to receive data.
    How can i do so…
    I am not a good Programmer of Visual Basic.

    But still trying
    Here is my Code to send data from PC to usb

    Private Sub btn_Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Send.Click
    BufferOut(0) = 0
    For Me.i = 1 To Len(txt_Data_to_Send.Text)
    BufferOut(i) = Asc(Mid(txt_Data_to_Send.Text, i))
    Next

    hidWriteEx(VendorID, ProductID, BufferOut(0))

    This is working absolutely fine…
    But My OnRead Function is not Working..

    I am Echoing Data what i sent but my Sent portion is working Properly but not the receive one..
    But doing some Research i came to a conclusion that my OnRead function never gets called..
    It gets called when i remove my USB Device

    Here is the Receive Function

    Public Sub OnRead(ByVal pHandle As Integer)
    ‘ read the data (don’t forget, pass the whole array)…
    MsgBox(“Reading From PIC”)
    If hidRead(pHandle, BufferIn(0)) Then
    ‘** YOUR CODE HERE **
    ‘first byte is the report ID, e.g. BufferIn(0)
    ‘the other bytes are the data from the microcontroller…
    For Me.i = 1 To BufferInSize
    MsgBox(USB_Data_Received)
    USB_Data_Received(i) = BufferIn(i)
    txt_Receive.Text = txt_Receive.Text & Chr(USB_Data_Received(i))
    Next
    ‘Data gets Printed in the Receve Window
    ‘Now clear the Buffer USB_Data_Receive
    For Me.i = 1 To BufferInSize
    USB_Data_Received(i) = 0
    ‘Clear the Buffer Before Exiting
    Next
    MsgBox(BufferIn)
    End If
    End Sub

    I have msgbox to know whether onread function gets called or not,
    it gets called when i remove my device..

    Pls reply me soon

    • Amr Bekhit says:

      Hi Arun,

      If your OnRead function is never being called, then your USB device is not transmitting data to the PC. Please make sure that your HID device is transmitting data to the PC. You could use the HID Terminal that comes with mikroC to test this.

      Amr

  38. Arun Sharma says:

    Hi!! Amr
    Thanks for your reply…

    I Checked my Code and it is working properly(mikroC one).

    And after few experiments i got this result..
    My OnRead() function is called but,
    what ever data i am receive from USB, i want it to be displayed on a Text_Box Present at VB Form..

    For this i have declared a array

    Dim USB_Data_Received(BufferInSize) As Byte

    Now My OnRead Function is as follow:-


    Public Sub OnRead(ByVal pHandle As Integer)
    ' read the data (don't forget, pass the whole array)...
    MsgBox("Reading From PIC")
    If hidRead(pHandle, BufferIn(0)) Then
    ' ** YOUR CODE HERE **
    ' first byte is the report ID, e.g. BufferIn(0)
    ' the other bytes are the data from the microcontroller...
    For Me.i = 1 To BufferInSize
    USB_Data_Received(i) = BufferIn(i)
    MsgBox(USB_Data_Received)
    'txt_Receive.Text = txt_Receive.Text & Chr(USB_Data_Received(i))
    Next
    'Data gets Printed in the Receve Window
    'Now clear the Buffer USB_Data_Receive
    For Me.i = 1 To BufferInSize
    USB_Data_Received(i) = 0
    'Clear the Buffer Before Exiting
    Next

    End If

    My this part is not working

    USB_Data_Received(i) = BufferIn(i)
    i.e i am not able to copy data from the USB Port, But i am getting the Data successfully

    Because when i Transmit “S” from the USB and use this statement in Visual Basic

    MsgBox(BufferIn(1)) –> get 83 ascii value of S

    MsgBox(Chr(BufferIn(1)) ) –> get “S”

    This means i am getting data propely,
    But i am not able to display that data on the Receive_Text_Box..

    I am new to Visual Basic thats why i don’t know how to do that..

    Pls reply me as soon as possible

    Thanks

    • -tE says:

      I have just begun to add an HID to a software I am developing. I am getting it to read just fine. You have to set the “Focus” to the textbox of the form you want to be filled.
      My question is reguarding only using the data I want from the string that is produced. I haven’t tried this download yet, but my textbox is being filled just by setting the focus to it. I want to be able to only use the name from the card for instance. Any ideas? Would the template that is here help do this?

  39. Hero7 says:

    I’m somehow lost I try an easy task with the template but nothing seems to work

    could someone show me a code for the onread section

    I don’t know if my 64bit OS is the problem I just need a valid code to test the template if not I have to find a 32bit OS.

    • Amr Bekhit says:

      What problems are you having? If you are getting error messages when trying to run the program on 64-bit Windows, then please read the section titled “mcHID.dll on 64-bit Windows” at the top of this page.

      Regarding reading data, the template already does most of the work for you. The OnRead function saves the data inside the BufferIn array – you just need to process the contents of the array as your application requires.

  40. Hero7 says:

    thanks for the answer
    I’m using VB express and for what I read ,I haven’t the possibility to choose something else then ANYCPU;I have to upgrade to an higher version of visual ; I’ll try with a 32 bit OS.
    thanks for the good work and support.

  41. Hero7 says:

    Finally I found how to change the target CPU; I was wrong in the end, sorry.

    for those who may be in the same case

    ” To target x86 in the express editions:

    Tools –> Options –> Projects and Solutions–>General Check “Show advanced build configurations”
    If “Configuration Manager” doesn’t show on the Buid menu, add it and click it.
    Active Solution Platform –> New –> Type or select the new platform x86 ”

    http://go4answers.webhost4life.com/Example/visual-basic-2008-express-edition-45272.aspx

    http://www.vbforfree.com/?p=418

  42. Hero7 says:

    a complement for 64 bit user’s
    don’t forget to Copy mcHID.dll to your C:\Windows\SysWOW64

  43. pablo says:

    HI, i hope somebody help me about a mcHID.dll problem.

    When I open de windows form. the usb connect correctly.
    When i close de windows form. the usb disconnect correctly.

    the problem, is when im trying close the connection in an other part of my code.
    the program is blocked and i have to stop debug and start again.

    does somebody knows what could be happened?

    thanks for help

    • Amr Bekhit says:

      How are you closing the connection? I wrote a test program that calls DisconnectFromHID() inside a button and that seems to work OK.

      • Pablo says:

        sorry Amr Bekhit.

        im use the same function than you DisconnectFromHID()

        it’s so strange cause when im close the form (with close button form) and call DisconnectFromHID fuction, works perfectly.

        but, when i just want to disconnect after some processes finished. the aplication blocked.

  44. need2know says:

    How to use mcHID.dll to send zero packet length data to the device?

    • Amr Bekhit says:

      I’m not sure about this. The hidWrite and hidWriteEx commands do not take any parameters that specify the size of the message. This would lead me to guess that they determine the buffer size from the device descriptor, thus meaning that the size of the messages sent to and from the device are fixed.

      I tried passing a single byte to the hidWrite command as follows:

      Dim ZeroMessage As Byte = 0 'Report ID is 0
      hidWrite(handle, ZeroMessage)

      Although this seemed to work (my devices indicated that they received a USB packet), I think hidWrite is sending more than one byte. For my test devices, the Buffer Out size is 64, and I reckon that after transmitting the contents of ZeroMessage, the function also transmitted the next 64 bytes in RAM that happen to be after ZeroMessage.

      Try it for yourself and let me know what you find.

      • need2know says:

        The codes do not work. I used USB trace software to capture the event and it does not send out zero-length packect. BTW, I’ll try using hid.dll and check if it works.

  45. need2know says:

    Is that possible to use mcHID.dll to send zero-length packet out to the device? If yes, can I have the codes?

  46. Anthony Lee says:

    Hi there,

    I just try out the template, so far it work fine for me, its my 1st try to use MCU USB function for my Project, really thanks a lot.

    Just some small info to share, I’m using a win 7 64bit, VB 2010, on my first try, I copy the dll file into my system32, all the code are unable to work, than I try to copy it into SysWOW64 folder, everything works fine.

    • Amr Bekhit says:

      Hi Anthony, glad you found the template useful.

      Regarding the dll, it will also work if you ensure the DLL is in the same directory as your executable. This is the method I recommend as all the files that your program needs to run are kept in the same place and not scattered about the system.

  47. daniel menendez says:

    Hi, I have found this material very interesting.
    I have followed the procedure to install the template, but when I create a New Project in Visual Basic 2008, I can´t see the USBTemplate icon in “My Templates”.
    Could you help me?
    Thanks you very much and best regards, Daniel

  48. daniel menendez says:

    In other words, I can´t see the USBTemplate option at the bottom.

  49. Phil Middleton says:

    Hi, I’m running 64bit windows and vs2010 but when I try to run the template I get the following error ‘An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)’ in the ConnectToHID function at line ‘pHostWin = hidConnect(FWinHandle)’. I Have copied the mcHID.dll to both the system32 and sysWOW64 folders. Any help would most appreciated.

  50. Csquire says:

    Hi, just want to say thanks so much for this code, I established USB comm and sent data on first try using VB 2010 express and PIC 18F4550 on a Mikroe Easypic6 development board.

  51. karthik says:

    how can i read a data from Buffer[1]

  52. gaul1 says:

    Hi!
    tnx for this stable working, easy to use dll.I use it with VB6, it works great for writing, also reading raw data from EP0. But I don’t get the FeatureReport data.
    2 Questions:
    – can you add the getFeatureReport function?
    – I’ve got 2 devices with same pid, diff. ser#. I’ve choosen devices by GetItem(), and got a handle, but there is still only the last device used. How do I change the device? what shall I do with the handle, it’s not compatible with the usbdevicehandle.

    tnx in advance
    gaul1

  53. Kornum says:

    Hi,

    I’ve been trying to connect and get a response from a HID device, but when using your template, I get an error which says: “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

    What does this mean?

  54. S3r___ says:

    Thank you again for this great source !
    Also i have some question concerning the OnRead function. Actually i would like to capture data coming from a joystick.

    I understand that data from BufferIn(1) are the one i need, but how can i decode these. Because until now it just something such as 131 71 132 71 etc…

    Do you have any example data parsing. I can find many resources about reading raw data, but nothing about what i need.

    It would be really helpfull, thank you ~

    • Amr Bekhit says:

      You’ll need to have a look at your joystick’s descriptor in order to figure out how the data is formatted. Have a look at my USB joystick article at http://helmpcb.com/electronics/usb-joystick. I explain how the joystick’s descriptor relates to the data that it sends.

      • kambon says:

        Umh, then could you let me know, and knowing the descriptor, how i get this data. For example with your joystick, what would be the code if you are using this template ?

        • kambon says:

          Its really easy to understand the case of writing bytes on simple PIC.
          I really also understand the descriptor you developed for this joystick, but i dont arrive to find the way to convert this into data. I’m still confused with the buffer size and data of descriptor.

          If you could give me some hint, it would be really great ~

  55. S3r___ says:

    Thank you for such a fast reply.

    Actually i’m still stucked, i guess this is because this is my first work related to hid ^^

    Right now i’m getting 8bit info from my joystick and i can see clearly info from each buffer(0), buffer(1) buffer(2) buffer(3), … buffer(8).
    Actually my question is related to the data itself.
    To take the example of your joystick, i guess i would guess something like this :
    buffer(2) is Throttle
    buffer(3) is X-axis
    buffer(4) is Y-axis
    buffer(5) is Button 4 Button 3 Button 2 Button 1 POV Hat

    Is it correct ? In that case how can i convert buffer(2) to its real value, and also how to extract buffer(5) knowing that 5 values are inside.

    I guess it sounds pretty dumb, but i’m having trouble to find answer about this.

    Would love to get some help !
    Thank you

    • kambon says:

      Hello ~
      Same here, i’m also really interested in this.
      How to convert buffer(1),(2),(3)… into data in this joystick example. I’m totally stocked 😉

  56. Joerg says:

    The DLL works only on 32 bit windows, right???
    I have problem to make it work on my 64 bit windows 🙁

  57. tinashe says:

    THIS WEBSITE IS WONDERFUL. THANK YOU VERY MUCH

  58. tinashe says:

    THANK U

  59. lrh says:

    I need to be able to tell the difference between someone scanning their prox card and typing in their card number on the keyboard. Can I do that with your code? If so, please tell me how. I am using VS2005 on a Windows 7 computer.
    Thank you for any direction you can give me.

  60. lrh says:

    I’m having the same issue reported by Andrzej and Timothy back in January 2012. If I open Notepad and scan my card, the number is displayed. In my VB app, the OnPlugged and OnUnplugged subroutines execute, but the OnRead subroutine doesn’t. Any ideas?

  61. phil says:

    Hello all,

    i already use this DLL on many project with VisualStudio 2005 2008 and 2010

    now i reopen an old project under VS2010/win 7 x64 and framework 4.0
    i verify to compile only for x86 target as explain but now the project run and rapidly hang .. i can exchange maybe 10 or 20 bytes between my board and the application and it hang with a “vshost32.exe” error message // nothing more !

    please anybody can confirm that my setup (win7 X64 / VS2010 / NET4.0) is working ?

    thanks for any help
    regards

    • phil says:

      EDIT ..
      if i run directly the executable file under /Release folder, it seem working right !

      but if i run by the debug mode (green arrow) it hang quickly !

      any idea ?

      thanks
      phil

  62. Dominic says:

    Hi Amr,
    Thanks for the code, its a life savor. Been searching for code like this for a week!. I got everything working with the template. Was very easy. Would it be possible to have the same but for C#? I have tried some online converter but im getting all sorts of error.(‘System.Windows.Forms.Form’ does not contain a definition for ‘OnPlugged’ and no extension method ‘OnPlugged’ accepting a first argument of type ‘System.Windows.Forms.Form’ could be found (are you missing a using directive or an assembly reference?) and some others. I tried adding some ‘using’ but still no go. Would you have any pointer on how it could be converted? I could send you what i already have if it helps.

    Thanks
    Dominic

  63. Pavel says:

    Hi!
    Can you give me an advance about dll functions. Which one what to do.
    For example, hidIsAvailable(pVendorID, pProductID) should check connected device to USB or not? But it always return false. And so on…

    • Amr Bekhit says:

      Hi Pavel,

      As mentioned in my article, mcHID.dll was not written by me, but by mechanique. If you need more detailed information about the dll, you’re better off contacting them, or experimenting yourself.

  64. Mark says:

    I use mcHID.dll on xp, vista and win7 with no problems. But I recently updated to my unique USB PID, and now the USB no longer works on win7. As soon as I plug in the USB, win7 gives a low base beep indicating a problem with it. Hence I think it needs a .inf file with the driver GUID inside. Does the mcHID have a GUID? Or does anyone no of any workaround?

    • Amr Bekhit says:

      Try reverting back to your original PID so that the device enumerates correctly, then go to device manager and uninstall the device. After that, change the PID and see if it enumerates successfully.

  65. YY says:

    Hi, I have follow your instruction, but when I open the frmUSB and run it, the form is blank?how to solve this, I amusing ver2010. If I click to run the project file directly, it shows that there is a need in conversion and there are many conversion errors. thanks

  66. Kmil says:

    Hi everyone, im trying to send a string like a “Hello”, but when you run, the program say “can’t convert string to byte” in bufferout, someone sent to string?????, thanx for any help.

    • Amr Bekhit says:

      You’re going to need to convert every character in the string to its ASCII code and send the array of ASCII codes. Do a Google search on converting a string to a byte array in VB

  67. kmil says:

    Amr Hello, thanks for your help, I did what you suggested and so far I managed to send the letter “E”, I will try to send a string, again thanks.

  68. kmil says:

    Hi Amr, thanks for the tips, i did,i sent the string: “Hola a todos”, sorry for my english, i speak spanish XD

  69. Win says:

    HI,

    Can I use it in my Silverlight project? if this dll can support for SL project, how could I use it?
    Or do you have a dll for Silverlight version?

  70. jose javier says:

    hi a need to do a communication hid with pic how can i do or do you have some one tutorial or manual help me please

  71. Danilo says:

    Hello, I noticed that if I connect my HID device and then I open my program (that uses mcHID.dll)) the HID device is properly recognized as connected but if I remove my HID device does not notice the change in the software. This problem only happens when I connect my device before opening the program. It’s just my problem or it happens to others?
    Many thanks in advance!!
    Here some code of my program:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ‘ do not remove!
    ConnectToHID(Me)
    If hidIsAvailable(VendorID, ProductID) Then
    lblStatus.Text = “HID DEVICE CONNECTED!”
    Else
    lblStatus.Text = “HID DEVICE DISCONNECTED!”
    End If
    End Sub

    ‘*****************************************************************
    ‘ a HID device has been plugged in…
    ‘*****************************************************************
    Public Sub OnPlugged(ByVal pHandle As Integer)
    If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
    ‘ ** YOUR CODE HERE **
    lblStatus.Text = “HID DEVICE CONNECTED!”
    End If
    End Sub

    ‘*****************************************************************
    ‘ a HID device has been unplugged…
    ‘*****************************************************************
    Public Sub OnUnplugged(ByVal pHandle As Integer)
    If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
    hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
    ‘ ** YOUR CODE HERE **
    lblStatus.Text = “HID DEVICE DISCONNECTED!”
    End If
    End Sub

  72. alawode basit says:

    Good morning. I’m a new visual basic learner and I’m working on a scrolling project display.
    Please sir, how can i send a string using the template, store the string in a PIC that supports USB and then send a string back to the template from the PIC to be displayed by the visual basic program. I’m using mikroC pro for writing the PIC code and Visual basic 2010 to design the user interface.
    Thanks.

    • Amr Bekhit says:

      Hello,

      You’re going to need to convert the string to an array of bytes, with each byte representing the ASCII code of each letter in the string. You can then pass that array to the hidWrite/hidWriteEx functions. Likewise, when the PIC sends data back, it will be sent as an array of bytes, and you’ll need to convert that to a string so that you can display it on your program.

      • Tim says:

        You wrote:
        You’re going to need to convert the string to an array of bytes, with each byte representing the ASCII code of each letter in the string. You can then pass that array to the hidWrite/hidWriteEx functions. Likewise, when the PIC sends data back, it will be sent as an array of bytes, and you’ll need to convert that to a string so that you can display it on your program.
        *****************************************
        First of all thanks for your template posted here. I tried and everything is working now but one thing I may need your help.
        I want to send a command of string to the microcontroller so I can get a response from it. I converted it to an array of Byte as you recommended and got an response of zero. It means that the micro controller got the command but wrong command. I think that is because it is a ascii command not the string command and the micro controller doesn’t understand. Do you have any advice here? How can I make the micro controller understand the ascii commands? Thanks in advance for your help

        • Amr Bekhit says:

          Hi Tim,

          If you want to send a string to the microcontroller, then you need to convert each character of the string into its ASCII equivalent, put those values into the buffer array and then send that to the PIC, making sure to place a ‘0’ value at the end of the string. For example, if you want to send the string “hello” to the microcontroller, then you need to fill your BufferOut array as follows:

          BufferOut(0) = 0 ‘<-- Always put 0 in the first element BufferOut(1) = 104 '<-- h BufferOut(2) = 101 '<-- e BufferOut(3) = 108 '<-- l BufferOut(4) = 108 '<-- l BufferOut(5) = 111 '<-- o BufferOut(6) = 0 'Null terminating character You then have to write the relevant code on the microcontroller to correctly process that information.

          • Tim says:

            Hi Amr. Thanks for your response.
            I tried with the value 0 at the end of the string but it didn’t work either.
            I am trying to send a string command to read the value of a micro controller. I converted it to ascii code, put them in bufferOut() with bufferOut(0)= 0. When I called the function hidWriteEx(VendorID, ProductID, BufferOUt(0)), I got the value 0 from ReadOn Function. My question is:
            – How is the bufferOUt(1,2,3…) sent out to the device? Do I need the codes for them? Or they are sent automatically after BufferOut(0) is sent?
            Thanks for your help.

          • Amr Bekhit says:

            Both the PC software and microcontroller firmware need to be working properly in order for this to work. Are you sure that your microcontroller firmware is working correctly? What data are you receiving on the microcontroller?

          • Tim says:

            Hi Amr,
            I am working with a thermo temperature sensor TMP006EVM using SM-USB-DIG from TI. They came in with data sheet and an interface software that I can install, run and get data. I can modify value of registers also. But I want to play more with HID USB communication using VB to read data.
            So far I used a terminal interface to send command and get the data to make sure I have the right commands.(using I2C)For example, command “I2C CH1 ACKS OO ACKS S 81 ACKS R ACKM R ACKS P” and get response “FFC2” like I expected. Using your VB template I send this command and get a read of “0000” (I set BufferInSize = 4). I converted this command to ascci codes, put them in BufferOut(), and call function ExWrite(VendorID, ProductID,
            BufferOut(0)) but only get values of 0000.
            I will review all codes and some spec from TI but want to understand the functions
            ExWrite(VendorID, ProductID,BufferOut(0))
            I just call this function and all elements in BufferOut() are automatically set out to the device or I need more code to send out BufferOut(1, 2, 3…). If you have any advice please let me know. Thank you very much

          • Amr Bekhit says:

            Hi Tim. So I’ve had a quick look at the SM-USB-DIG, and I did a google search for “sm-usb-dig usb”. I came up with the following forum post on the ti.com website: http://e2e.ti.com/support/sensor/temperature_sensors/f/243/p/117449/422148. The thread author is trying to communicate with the SM-USB-DIG on Linux and wants some information on the USB protocol it uses. One of the TI staff has posted the source code for the device, which you can download from the forum post.

            Now, if you have a look through the source code (particularly usb.c), you’ll find that the device does not appear to be receiving plain string commands (such as “I2C CH1 ACKS OO ACKS S 81 ACKS R ACKM R ACKS P”) directly over the USB. Rather, it seems to be receiving a binary packet. My guess is that the terminal program you are using to test the device is not simply passing your string directly to the SM-USB-DIG – rather it’s parsing it, converting it to a binary packet format and then sending *that* to the device. And that’s probably why you sending the strings directly to the device is having no effect.

            In any case, have a look through the code yourself and see if you come to the same conclusion.

          • Tim says:

            Thank you very much for your explanations. I opened that link from TI before but didn’t go through the codes. I will open it again and spend more time reading them to figure out how to send these commands. I will post the solution when I find it. I really appreciate your help.

  73. alawode basit says:

    I’ll try that out n I’ll let you know the outcome. Thanks.

  74. alawode basit says:

    I tried, but I don’t seem to get it working. Probably because I’m new to visual basic. Can you help me with the code that can do it. I will really appreciate if you can. Thanks.

  75. alawode basit says:

    I’ve successfully gotten it to send data to the pic. But, when I tried to use it to read the data sent from the pic, I got an error message and the visual basic program will abort. Can you help me with this. Thanks.

  76. alawode basit says:

    Good Morning,
    I wrote the code that will receive the message sent by the pic but I get the following error message “Error vshost32-clr2.exe has stopped working”.
    When I tried to debug, Visual Studio Just-In-Time Debugger tells me that “A debugger is attached to myusb.vshost.exe but not configured to debug this unhandled exeption. Myusb is the name of my project.
    When I clicked ok and try to use the Visual Studio Just-In-Time debugger, I get the message “unable to attach to the crashing process. A debugger is already attached.”
    How can I fix this unhandled exception?
    How can I dettach the vshost debugger?
    How can I use the Visual Studio Just-In-Time debugger?
    Or simply, how can I fix the problem?
    Please help me. Thanks.

  77. alawode basit says:

    Finally! After a lot of troubles, I finally got my code to work. Both pic and vb are now sending and receiving data perfectly. Thanks to everyone that helped me especially all those in the previous comments here, I say thank u. Your comments really helped.
    Thanks so much Amr for making this template. Really appreciate your work.
    Quitters never win and Winners never Quit…

  78. alawode basit says:

    Thank you, Amr.
    Phase one of the project is almost done.
    I’m faced with phase two now, scrolling message display. The message which the pic receives is meant to be scrolled on an 8×64 matrix led display.
    1) Please, do you have any leads as to how I can possibly get this done?
    2) Do you have any idea how i can use the internal eeprom of a pic microcontroller to store and retrieve data using C (I’m using mikroC Pro)?
    3) A little problem with the vb code, when i send the first message, it will be sent back okay, now, if i send a second message whose length is less than the first message, the second message will be sent back plus the remainder of the first message e.g
    1st message = between, received message = between,
    2nd message = meal, received message = mealeen, please, how can i rectify this?
    Thanks a lot for your support.

  79. alawode basit says:

    Good Morning
    Problems 1 and 2 above has been solved after a lot of trial and error, leaving me with problem 3 which still remain a problem in its own. I need your help on this. Thanks.

  80. alawode basit says:

    I’ve finally found a way around the problem even though it hasn’t been totally solved but at least, i can still work without getting it solved.

  81. alawode basit says:

    Finally, I’m almost through, but I need to send data to more than one device whose vendorID and ProductID may not be known. Pls, how can I achieve this. Thanks.

  82. Daan says:

    Hi,

    I love your work, great help.
    But i do have a problem

    I’m able to send data to my controller and receive an answer. But this only works if the USB is already connected before starting the program. If I connect the device while the program is already running, the onplugged routine starts, but it doesn’t send my data.

    • Daan says:

      It has been solved.

      I use the MINI-32 board from MikroE. When powered up, the MCU loads a boot-loader for 5 seconds and then starts the program if no connection is made to the boot-loader. The device disconnects and then connects to fast, which messed up the VB program. I put a delay of 1sec between the boot-loader disconnect and my programmed connect and now it works.

  83. Dan says:

    Is there any place I can see all the VB source code? I don’t have VB, but I understand VB and want to translate into another language (LabVIEW). THANKS!

  84. Marouane says:

    Thanks for this work , i made all steps but when i run the project i have this message “badimage exception” i can’t resolve it.
    i use windows 7 64 bits.

    • Amr Bekhit says:

      Hi Marouane,
      The error you’re getting is because your application is running as a 64-bit process and is trying to load the 32-bit mcHID.dll. Simply change your compile platform in Visual Studio from x64 or AnyCPU to x86.
      Amr

  85. alawode basit says:

    Hi, I designed a VB interface to send data to pic16F8550 and I changed the BufferInSize and BufferOutSize to 500 because I’m going to send more than 64 bytes of data.
    I then wrote a mikroC pro code to read the data sent from the VB interface and also to send it back to the VB interface.
    After testing, all the data was sent to the pic but the pic only returned the first 64 bytes of the data.
    What can I do so that all the data would be sent back by the PIC. Thanks.

    • Amr Bekhit says:

      Hi Alawode,

      64 bytes is the maximum packet size you can send and receive. You’ll have to devise a method of splitting large data into chunks and then reconstructing the data from the chunks.

  86. alawode basit says:

    Hi Amr,
    Thanks, I’ve also done some google searching on the problem and found out the same thing that you’ve said that usb data are sent in 64 byte per packet.
    The problem is I don’t really know how I can get this done. Please, do you have any idea as to how I can get it done? For example, 200 bytes of data.
    Thanks.

  87. Mahnke says:

    I cannot get this to work for me. I downloaded magtek’s demo and the reader works and spits out the data, but this apparently doesn’t even detect my reader:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ‘ do not remove!
    ConnectToHID(Me)
    If hidIsAvailable(VendorID, ProductID) Then
    MsgBox(“Is”)
    Else
    MsgBox(“Is not.”)
    End If
    End Sub

    • Mahnke says:

      “Is not.” Always pops up. My only guess is that the IDs are off but I’m new to this and have no real idea.

  88. nesrine says:

    HI,
    i am trying to connect pc to microcontroller 18f2550 via usb i tried the code but i could not send any thing through the interface?
    can anyone tell me where could be the problem?

    • Amr Bekhit says:

      It’s hard to tell from your question what the problem could be. What id advise is to make sure your PIC is running USB code that you know works as a HID device, then try experimenting with the unmodified VB template and see if you get anywhere

      • nesrine says:

        hi Amr thaks for replying,
        this my whole code i found similar thing in the net i tried to send data but no thing happening
        i could not make it work,also i could not get a Product ID and vendor ID. hope that you can help.
        Public Class Form1
        ‘ vendor and product IDs
        Private Const VendorID As Integer = &H1234 ‘Replace with your device’s
        Private Const ProductID As Integer = &H1 ‘product and vendor IDs

        ‘ read and write buffers
        Private Const BufferInSize As Integer = 64 ‘Size of the data buffer coming IN to the PC
        Private Const BufferOutSize As Integer = 65 ‘Size of the data buffer going OUT from the PC
        Dim BufferIn(BufferInSize) As Byte ‘Received data will be stored here – the first byte in the array is unused
        Dim BufferOut(BufferOutSize) As Byte ‘Transmitted data is stored here – the first item in the array must be 0

        Public Function AsciiByteToChar(ByVal b As Byte) As Char

        Dim barr() As Byte = New Byte() {b}

        Dim carr() As Char = Encoding.ASCII.GetChars(barr)

        Return carr(0)

        End Function

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectToHID(Me)
        End Sub

        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        DisconnectFromHID()
        End Sub

        ‘ a HID device has been plugged in…********
        Public Sub OnPlugged(ByVal pHandle As Integer)
        If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
        MsgBox(“Device connected”, vbOKOnly, “USB”)
        End If
        End Sub

        ‘*****************************************************************
        ‘ a HID device has been unplugged…
        ‘*****************************************************************
        Public Sub OnUnplugged(ByVal pHandle As Integer)
        If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
        hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
        MsgBox(“Device unplugged”, vbOKOnly, “USB”)
        End If
        End Sub

        ‘*****************************************************************
        ‘ controller changed notification – called
        ‘ after ALL HID devices are plugged or unplugged
        ‘*****************************************************************
        Public Sub OnChanged()
        ‘ get the handle of the device we are interested in, then set
        ‘ its read notify flag to true – this ensures you get a read
        ‘ notification message when there is some data to read…
        Dim pHandle As Integer
        pHandle = hidGetHandle(VendorID, ProductID)
        hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
        End Sub

        ‘*****************************************************************
        ‘ on read event…
        ‘*****************************************************************
        Public Sub OnRead(ByVal pHandle As Integer)
        Dim S As String
        If hidRead(pHandle, BufferIn(0)) Then
        For i = 1 To 64
        S += AsciiByteToChar(BufferIn(i))
        Next
        TextBox2.Text = S
        End If
        End Sub

        Public Shared Function StrToByteArray(ByVal str As String) As Byte()
        Dim encoding As New System.Text.UTF8Encoding()
        Return encoding.GetBytes(str)
        End Function ‘StrToByteArray

        Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
        Dim i = 0
        BufferOut(0) = 0
        Dim k = TextBox1.Text.Length
        Dim datatosend As Byte() = StrToByteArray(TextBox1.Text)
        For i = 0 To k – 1
        BufferOut(i + 1) = datatosend(i)
        Next
        hidWriteEx(VendorID, ProductID, BufferOut(0))
        End Sub
        End Class

        thanks.

  89. alawode basit says:

    Hi, Nesrine

    Which lang are you using to program the pic? You will need a descriptor file that will hav the same vendor and product I’d as the one above. This will enable the vb application to detect that the device has been connected.
    Without the descriptor file, you may not be able to send anything from the vb app to the pic.
    If you are using mikroC pro, go to help and read about usb library, it will tell you how you will generate the descriptor file and will also tell you where to find the app that you will use to create the file.

    Hope that helps?

  90. Jayanth D says:

    I am having problem reading data that comes from uC. The dats sending from PC to uC is working but receive is not working. Here is my codes.

    while(1){

    kk=HID_Read();

    if(kk != 0)
    {
    PORTB = readbuff[0]; //write received data to PORTB
    TRISB = 0xFF;
    writebuff[0] = 0xFF; //write PORTB value to writebuff
    TRISB = 0x00;
    HID_Write(&writebuff,1); // send back via HID class !
    kk = 0;
    }
    }

    vb.net code

    Public Sub OnRead(ByVal pHandle As Integer)
    ‘ read the data (don’t forget, pass the whole array)…
    If hidRead(pHandle, BufferIn(0)) Then
    ‘ ** YOUR CODE HERE **
    ‘ first byte is the report ID, e.g. BufferIn(0)
    ‘ the other bytes are the data from the microcontroller…
    RPORTBVAL = BufferIn(1)
    read_back()

    End If
    End Sub

    read_back…

    If RPORTBVAL And &H1 Then
    OvalShape1.BackColor = Color.Red
    Else
    OvalShape1.BackColor = Color.Blue
    End If

    In the readback function I am toggling the LED but LEDs never turn ON. I found that data is not being received at PC.

  91. harpreet kaur says:

    I WANT TO GET THE FULL PROGRAMMING USING VISUAL BASIC FOR FINGER PRINT ATTENDENCE SYSTEM

    • Jayanth D says:

      Hi Harpreeth

      Please explain about your project in detail. I have done 3 projects using mcHID.dll and VB.net

      What data you send from the finger print attendance system through USB. I will try to write a vb.net program. Contact me at internetuser2k11@gmail.com

  92. zombie says:

    Hi,

    I try to read data from 2 HID devices using mchid.dll in vb.net

    I modified Onread() function but it doesn’t work, the 2 textbox are changed in a same time

    Can you held me?

    Public Sub OnRead(ByVal pHandle As Integer)
    ‘*****************************************************************
    ‘ on read event…
    ‘*****************************************************************
    ‘ read the data (don’t forget, pass the whole array)…
    ‘If hidRead(pHandle, BufferIn(0)) Then
    ‘ ** YOUR CODE HERE **
    ‘ first byte is the report ID, e.g. BufferIn(0)
    ‘ the other bytes are the data from the microcontroller…
    ‘End If

    If hidReadEx(VendorID_dev1, ProductID_dev1, BufferIn(0)) Then
    TextBox1.Text = BufferIn(1)
    End If

    If hidReadEx(VendorID_dev2, ProductID_dev2, BufferIn(0)) Then
    TextBox2.Text = BufferIn(1)
    End If

    BufferIn(1) = 0
    End Sub

  93. Jayanth D says:

    How can your VB.net code have two VIDs and PIDs. In the top of the code you mention VID and PID. It can’t be same for two devices.

    • zombie says:

      hi,
      I just declared 2 different constants and 2 handles

      ‘ vendor and product IDs
      Private Const VendorID_dev1 As Short = &H1234 ‘Replace with your device’s
      Private Const ProductID_dev1 As Short = &H1 ‘product and vendor IDs

      Private Const VendorID_dev2 As Short = &H1234 ‘Replace with your device’s
      Private Const ProductID_dev2 As Short = &H2 ‘product and vendor IDs

      Dim pHandle_dev1 As Integer
      Dim pHandle_dev2 As Integer

  94. I use the mcHID.dll as in the USB template.
    After upgrading from windows 8 to windows 8.1 it does not work anymore.
    I did put questions on there to forums :
    http://social.msdn.microsoft.com/Forums/silverlight/en-US/599e42ff-5157-4b26-a45d-6e5fd065b623/mchiddll-and-windows-81?forum=windowsgeneraldevelopmentissues
    http://digital-diy.com/forum/visual-basic/mchid-dll-and-windows-8-1-t2905.html
    The first one says that the mcHID.dll is the problem
    Is there a newer version for windows 8.1 ?

    Thanks in advance,

    Hans Niekus

    • Romain Borderies says:

      Hi,

      I use a USB/Parallel Adapter.
      How can I use your program to send a byte on this adapter ? I am little lost.

      Can you help me ?

  95. Csquire says:

    Hi I am using USB template unsuccessfully with win7. I create an executable and install on PC with mcHID.dll in system32 directory and .NET framework installed. Application fails to run. Although when I then install Visual Basic Express onto same PC application then runs. Have not figured out what is going on any help appreciated. Thanks

  96. Ralph says:

    Hi,

    I love this HID dll and fits right in with my project.
    I do have one problem. When I unplug the device the unplugged event takes a while to recognize it was unplugged and run the code.

    Can this be made faster?

    Ralph

  97. dtvonly says:

    Hi. I can’t communicate with PIC18F4550 FSUSB demo board. I am running Microchip’s HID-USB sample firmware code with this template. I changed vendor ID to 0x12 and product ID to 0x1 on both sides. I then added a simple text box on the form to inform me of a plugged and unplugged device by adding textbox1.text = “Device plugged in” in the OnPlugged subroutine. Nothing is happening. furthermore, when I exited this template VB app, I got an unhandled exception at the DisconnectFromHID function. Please advice. Thank you.

    • dtvonly says:

      If any of you could send a sample working VB project that would be great – especially with the PIC18F4550. Thank you.

  98. paddy says:

    What about non-integer IDs? My usb device has a VendorID of 05F3 and ProductID of 00FF. tried to convert.toint32 no cigar.

    and code execution enters ConnectToHID but does not go past this line and the form displays…
    pHostWin = hidConnect(FWinHandle)

    help.

    • Midday says:

      I have the same issue as yours! After the VID and HID are setting well, the program will stuck in the ConnectToHID(Me) in Form1_load. Does anyone could help us? My OS is win7 64bits and VB 2010.

  99. I am using the mchid.dll to control a MCP2210, the code I am using is Visual Studio 2013 VB. I have run into a weird issue. I have been unable to get the correct data in the SPI Read buffer by just using the HIDREADEX command…until during debug I placed a “messagebox.show” command in the code just before the HIDREADEX statement execution. Timers, dealys, stoping the thread for countless seconds in the code do not work, only halting execution and requiring user interface seems to work. Any suggestions on how I could change my usage of the HIDREADEX function to avoid having to do this? Many thanks in advance.

  100. Midday says:

    Hello, this template is great and complete. But could you provide a simple and workable code?

    In this template, I am just trying to test the Form1_load functionality, but it always stuck in ConnectToHID(Me). Could you hit me something about it?

  101. Bahadur Kar says:

    Can you upload a temple & guide line so multiple PIC Hid can share (receive & Transmit)
    data among them with the temple.

    And thanks for the current temple. It is best and very easy for use, the beginner like me.

  102. Giorgi says:

    Hello

    I have two USB HID barcode scanner (they has same VID and PID) and I wont
    catch code from both devices separately.
    Can I do that by your class mcHID.dll?

    • Amr Bekhit says:

      I think you won’t be able to. The reason being is that often these devices are designed to appear as keyboards, and as such the operating system will take control of them and you won’t be able to access them using MCHID.DLL.

      Have you had a go at trying to use the template to communicate with them?

  103. Steve says:

    I have tried everything to get the onread to work, but nothing ever triggers the onread, I can identify when my device is plugged in and unplugged and the handlers have been added through the dll, but nothing triggers the onread. I am using a standard barcode reader, which I simply want to capture the data. I am using 64bit windows 7. Everything seems to work with exception to the onread never gets triggered. I have even attempted to capture reads from my keyboard and again the onread never gets called. Any help would be much appreciated.

  104. Simon HIckman says:

    Hi,

    I am using Windows 7 64bit and VB 2013.

    I am using a standard symbol LS2208 usb barcode reader with your application.

    I can detect the device ok in terms of onplub, onchange and unplug however I can’t get a read from it.

    The reader works ok on the actual laptop but never seems to trigger a read event through the program.

    Any help you could provide would me more than greatly appreciated.

    Cheers
    Simon

  105. Chering Parson says:

    Hello Amr,

    I am experiencing a problem similar to the one described in message 104: I am trying to read data from a RFID reader (DORSET LID575 Trovan), but till now without success.

    The Onplugged, OnUnplugged and OnChanged events work perfectly, but nothing happens when I read the transponder. And when I put a MsgBox after each event in WinProc, only NOTIFY_READ remains silent.

    I know that my reader transmits correctly the tranponder IDs to the computer because I can read them on the DORSET’s program and with YAT too (like 00074F2C95 or 00074C07E7).

    I obtained the full device descriptor with USBTreeView (very useful free app.), giving a packet length (BufferInSize) of 8 bytes. So my code to read is:

    ‘*****************************************************************
    ‘ on read event…
    ‘*****************************************************************
    Public Sub OnRead(ByVal pHandle As Integer)
    ‘ read the data (don’t forget, pass the whole array)…
    If hidRead(pHandle, BufferIn(0)) Then
    ‘ ** YOUR CODE HERE **
    ‘ first byte is the report ID, e.g. BufferIn(0)
    ‘ the other bytes are the data from the microcontroller…

    Dim i As Integer
    For i = 1 To BufferInSize
    lstReport.Items.Add(Chr(Val(BufferIn(i))))
    Next

    End If
    End Sub

    Do you have any idea why OnRead doesn’t answer. My OS is Windows 8.1, could it be the problem? Thank you advance for your help.

    Chering Parson

  106. localcrack says:

    for windows 8 or 8.1 64bit you have to place
    mcHID.dll
    in following folder

    C:\Windows\SysWOW64

    more information given on following page

    http://www.forceflow.be/2012/04/04/the-3264-bit-dll-confusion-on-windows-64-bit/

    • Amr Bekhit says:

      Thanks for the information. I always prefer to just put the DLL in the same folder as the executable, that way it’s always there, it’s simple and consistent and doesn’t require admin access.

  107. Soheila says:

    Please help me…
    I used the above program in visual basic 2005 on windows 7 and 64 bits.
    I placed the vendor ID and Product ID which is set on micro-controller program.
    But, Unfortunately, When I run the UsbTemplate1 program there are some error on mcHIDInterface.vb:

    Error1: Option Strict On disallows implicit conversions from ‘Boolean’ to ‘Integer’. on line pHostWin = hidConnect(FWinHandle)

    Error2, 3, 4, 5:Option Strict On disallows late binding. on line HostForm.OnPlugged(lParam) and HostForm.OnUnplugged(lParam) ad the line HostForm.OnUnplugged(lParam) and HostForm.OnChanged()..

    Please help me…I am really confused and I am beginner in visual basic 2005..

    Thanks in advance….

    • Amr Bekhit says:

      Hi Soheila,

      Easiest thing would be to disable Option Strict in Visual Studio. Do a Google search on how to turn Option Strict Off.

      • Soheila says:

        Thanks Dear Amr. Now there is an error “An attempt was made to load a program with an incorrect format.”
        What do you suggest me?
        I am really confused….I wanted to use from build tab in visual basic 2005 and change any CPU to x86 from platform…but it doesnit exist! Please…please help me..

        • Soheila says:

          Thanks..Thanks…
          It work properly….

          • Soheila says:

            No there is a problem…
            Why program runs but It doesn’t show in texbox1. text=”Device is attached”..
            I added this code to above program in Form1_Load event:
            If hidIsAvailable(VendorID, ProductID) Then
            TextBox1.Text = “Device is attached!”
            Else
            TextBox2.Text = “Device is Not attached!”
            End If

            and

            Public Sub OnPlugged(ByVal pHandle As Integer)
            If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
            TextBox1.Text = “Device is attached!”
            End If
            End Sub

            Public Sub OnUnplugged(ByVal pHandle As Integer)
            If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
            hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
            TextBox2.Text = “Device is Not attached!”
            End If
            End Sub

            but when it runs it shows “Device is Not attached!” in textbox2.

            I have a vendor ID &H16C0 and productId &H5DC…..
            Why it happens?!!!
            Could you help me…??
            Thanks in advance

  108. Soheila says:

    I think I have a problem in phandle…in my program its value is zero in Sub OnChanged….

    Public Sub OnChanged()

    Dim pHandle As Integer
    pHandle = hidGetHandle(VendorID, ProductID)
    hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)

    End Sub

    Does everybody know about it and why? I am baffled…

  109. salah says:

    When executing this template to check wether connected or not, function connectTOHID of mcHIDInterface.vb doesn’t return a value on all code paths.
    Also, asks if a return statement is missing?
    I use win7, 64 bits with vb express 2010

    • Soheila says:

      Thanks for your reply, how can I check the value of connectTOHID(Me)?
      I think there is a problem here…..because the value of pHandle is 0….

  110. […] 1.5 kilo ohm register, Two diodes and… for software which I can apply in PC, I used from http://helmpcb.com/software/usb-hid-…ual-basic-2005. fortunately, when I connect my hardware to PC, It can be recognize…I matched my hardware vendor […]

  111. Rob Beck says:

    Hi,
    Started using the lib. Seems all good. One of my customers is using Win 10.
    My application is very simple. All I am using the lib for is to play and pause media when headphones are connected and disconnected.
    OnUnplugged is called when the device is disconnected but when the return data from hidGetVendorId() or hidGetProductId() is random data. The correct VID and PID is not returned. The data is random. I get the occassional correct data, but 99% of the time it is random.
    I realise this is Windows 10 and a new OS, but has anyone had a chance to use on Windows 10?
    Many thanks
    Rob.

    • Rob Beck says:

      PS. When calling the same calls for PID and VID within the OnPlugged() callback always returns correctly. Only the OnUnplugged() is showing this issue.

      • Amr Bekhit says:

        So as I understand it, you get the correct VID and PID when you try to retrieve them during OnPlugged(), but you get problems when you try to retrieve those values in OnUnplugged(). Is that correct?

        Have you tried this on other versions of Windows? Does it work in Windows 7/8?

        • Rob Beck says:

          Hi Amr,
          Thanks for your reply. Works fine in Win7. I’m not sure about Win8 as I don’t have a Win8 (or 8.1) handy.
          Yes, OnPlugged() works fine, it is just OnUnplugged() that I have a problem.
          Cheers
          Rob.

    • Indika says:

      I have tested this with a Teensy 2.0 on freshly installed Windows 10 laptop. Works perfectly…

  112. Kornum says:

    I’ve noticed that it does not work in Windows 10. Has there been a change in the USB drivers. I can get it to notice if I disconnect and connect, but nothing happens when I scan my card.

    Looking forward to hear from you
    Thanks..

  113. Mrk31 says:

    Hi,
    if I’d like to do a Synchronous connection like this:
    – prepare buffer to write
    – write buffer
    – get response
    – check response
    how should I modify the HID interface in the linked template?
    In that template read method is Asynchronuos and generated by WinProc Event…can I execute hidRead when I want…what value of pHandle params coul I pass to it?

    P.S. sorry for my bad english…

  114. Mahesh Prasath says:

    Hi Amr,
    I am using win8.1 & VB6.0 to read the HID device, Device plug and Unplug is working fine. OnRead event is not generating at all. For your information: I am trying to read the Data from “In-size Gap Gauge instrument” that detects as HID device. When I focus the cursor on the text box the data is capturing. But programmatically unable to capture the data. I am using Vb6, the mcHID.dll and code from your web site.
    Note: I had read your post above regarding 32bit and 64 bit OS. Is there solution to over come this because I need to use win8.1. Please help me in solving this problem.
    i.e VendorID = &H4D9 and ProductID = &H1702

    • Amr Bekhit says:

      I don’t know what this device is. Can you share some more information about it? When you plug it in, how does it normally appear in Windows? Is it a HID Keyboard/Mouse? Does it appear as a generic HID device? Have a look in Device Manager and see what category it falls under. The HID Template can only work with generic HID devices that fall under the Human Interface Devices category in Device Manager. If Windows sees the device as a keyboard, mouse etc, then it will handle reading and writing data to the device and the template won’t be able to.

    • Amr Bekhit says:

      According to the VID and PID you provide (&H4D9 and &H1702), this is a USB keyboard: http://www.linux-hardware-guide.com/uk/2015-06-14-holtek-lks02-keyboard-usb. Is that the case? If so, the HID library won’t be able to communicate with it, since Windows is already handling data transfers to and from the keyboard.

  115. Mahesh Prasath says:

    Hi Amr,
    Sorry for the late response, I was trying the same on win XP 32 bit OS but all my attempts failed. In device manager I could not find out the device type i.e Keyboard or generic HID USB. Link for the device I am using its an Gap Measurement Gauge( i.e Digital vernier Caliper ) http://www.insize.com/products/tools/caliper/01%20electronic%20caliper.php. My guess is its emulated as HID Key board. Because the data appear where every the cursor blinks. Is there any other way I can capture the data in Vb6.0

  116. Mahesh Prasath says:

    For now even Win XP would be fine

  117. Mahesh Prasath says:

    Hi Amr,
    Is there a way to disable and enable only the data communication for HID USB device (In mean device is connected, just enable/disable the HID device only when data is needed ), I am back on this project again. Need to read from the HID device as mentioned above in my previous conversions.

  118. Mahesh Prasath says:

    Hi Amr,
    Could finally solve my problem, below link helped me a lot. Thanks a lot for your time. Now I am able to connected and read multiple Keyboards and also the Gap Measure Gauge as well from Vb6.0. Tested the program on Win8.1, need to test on other OS as well(XP and win 7).

    http://luisulloa.blogspot.de/2011/01/blog-post.html#gpluscomments

    Thank you once again..

  119. WIJAYA says:

    PLEACE HELP ME TO COMMINICAT WITH PIC 18F2550 VIA USB FORM VISUAL BASIC 2010

  120. CristobAlonso says:

    Hi, I’m working in my thesis and it consist in send and image frome one PC to another but with a LASER. I have to extract the image from the PC by the USB port. I think this template could help me. I understand tha this template help to send and recive information?? But I don’t see where in the template is the part to send it, I just see the part to read, that is the last one. Could somebody help me??.

  121. Ahmed says:

    Salut
    Can I used this application in visual c#2013 “Windows 8”
    1000000 Thank’s

  122. ahmed says:

    How to get this library (c#)?

  123. Recep TÜRKYILMAZ says:

    I have difficulty in sending data from the computer to the microprocessor . I in that the transmission code ;

    bufferout (0) = 0
    bufferout (1) = 82
    bufferout (2) = 69
    bufferout (3) = 67
    bufferout (4) = 69
    bufferout (5) = 80
    hidwriteex ( VendorID , ProductID , bufferout (0))

    Only bufferout (1) = 82 out of the computer gönderily . Why not just bufferout (1) = 82 sent. Help. THANK YOU

    • Amr Bekhit says:

      Merhaba Recep, you appear to be sending the data correctly, but are you sure the microcontroller side is working fine? Is the HID descriptor set up correctly? Do you have any way of testing the microcontroller? For example, if you are using the mikroC compiler, can you send data using their HID tool and check that you are receiving all of the data on the microcontroller?

  124. Romeo mougnol says:

    Hello Amr n odas!
    I really appreciate d great job done over here. I have a prob, i want to communicate with my hid to control home appliances, i can knw whn my device is connected or disconnected but im unable to control d appliances. Im using buttons to control d appliances but i dnt knw how to prog it. Please i need ur help.
    Thanks

  125. Joshua Wright says:

    Could this be integrated into Microsoft Excel?

  126. Neelix says:

    First I want to say how thankful I am for your amazing post!

    I am able to receive connect and disconnect events and send data but the OnRead() event is not firing.

    Vendor ID:07C0
    Product ID:1589

    It’s connected using the HID Compliant Device Driver.

    Any help is appreciated, thanks!

    • Amr Bekhit says:

      Have you tried using a USB HID test program, such as the one integrated into MikroC to make sure everything’s working fine? What device are you working with?

      • Neelix says:

        Thank you for the fast reply! I’m using USBlyzer (I will try MikroC). When I press the buttons on the keypad I get a sequence of 8 byte packets for the up and down events which show up in the output window of USBlyzer so it appears to be sending data to the PC. The devices is an Axis Keypad T8312
        Thanks!

        • Amr Bekhit says:

          Do you know if this keypad appears as a regular keyboard or joystick in Windows? Or do you need to install a special driver for it? In my experience, if the device gets handled by Windows automatically (such as with keyboards, joysticks and mice), you’re not going to be able to use the HID library to communicate with it. You’ll need to use Windows’ own APIs

        • Amr Bekhit says:

          I’ve just had a look at the user manual. It looks like it appears as a standard joystick/gamepad. You won’t be able to use the HID library to communicate with it, since Windows is already taking care of that. You’ll need to use whatever Windows APIs are available for communicating with joysticks.

          • Neelix says:

            There is a family of devices T8311,T8312,T8313. I actually got confused by that at first as well but they are referring to the T8311 which is a Joystick. The T8312 is a keypad that does not enumerate as a normal keyboard in windows. That may be the issue for the keypad as well. Windows uses a generic HID driver so I may have to use the native calls. Thanks!

          • Neelix says:

            Your suggestion lead me in the right direction and I was able to get it working. I was using the Axis software to sniff out the initialization commands and after I was done I closed the program but the process was still running in the background. This must have been taking over the read event and I wasn’t able to see it in my program. I killed all the Axis processes then cycled the keypad and sent the initialization commands from my program and VOILA it worked! Thanks again for all your help!

          • Amr Bekhit says:

            Nice one! Good luck with your project.

  127. Jack says:

    If will used the mcHID.dll make my Program , sale to my customer , need the Authorization?

  128. Jack says:

    thank you for the answer

  129. Fred Schader says:

    Hi Amr

    It has been a long time since I posted (back in Feb. of 2011). I recently tried to load an application in my Windows 10 Laptop and got an error message. “Unable to load DLL ‘mcHID.dll’ the specified module count not be found”

    Of course, I checked and it is located in Windows/System32 and has the correct size and date (324k 7/27/2007). I was wondering if the latest revision of Windows 10 wants this in a different location?
    thanks
    Fred

  130. Fred Schader says:

    Amr

    It figures, as soon as I posted my question I got an idea from your comment about WOW64. I did a search and found this link
    http://stackoverflow.com/questions/949959/why-do-64-bit-dlls-go-to-system32-and-32-bit-dlls-to-syswow64-on-64-bit-windows

    By placing the dll in sysWOW64 it now works correctly

    Fred

    • Amr Bekhit says:

      Hi Fred,

      Glad to hear you got it working! So, does it not work if the DLL is in the same folder as the EXE? Usually, Windows searches for the DLL in the same directory as the executable, otherwise it looks in the system directories.

  131. azri says:

    my Vid is 08ff and Pidc is 0009
    i cannot detect..how i can fix it

    • Amr Bekhit says:

      Hi Azri,

      What type of device are you trying to detect? The HID library only works with plain HID devices – it won’t work with device such as keyboards and mice, because Windows already handles those.

      Device 08FF 0009 is an AuthenTec RFID reader. This device enumerates as a regular keyboard, and as such is handled by Windows. You will not be able to communicate with this device using the HID library.

  132. Rubén says:

    Hi! I need to use HIDCOMM with LABVIEW and windows 64bits. Could you help me? All the thing that I have readed are for Visual basic.

    • Amr Bekhit says:

      Hi Ruben, this page doesn’t deal with HIDCOMM. If you want to use the MCHID.DLL file with Labview, you’ll need to look into how to communicate with standard dll files in Labview code.

  133. Luc Beaulieu says:

    Hello, I bought a USB relay from miniinthebox.com. There is no documentation for it. The hardware ID is USB\VID_1A86&PID_7523&REV_0254. How do I enter a vendor ID of 1A86 when that variable is supposed to be an integer? I also don’t know which command to send this device to turn it on and off. If anybody can help that would be great. Thanks.

  134. Ju says:

    Wonder if micronucleus on a ATtiny85 as Hid would work?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This blog is kept spam free by WP-SpamFree.