News:

;) This forum is the property of Proton software developers

Main Menu

VB.NET HID Application Framework to send and receive to and from PIC

Started by trastikata, Apr 11, 2021, 03:03 PM

Previous topic - Next topic

trastikata

PIC mcu with USB capabilities is only useful if we can do something with it. Therefore we need a PC host application to receive from and send information to the PIC microcontroller.

If the data transfer speed is not critical then for simple applications the HID USB protocol is proffered because of its simplicity of implementation (since WinXP, the USB HID driver is standardized in Windows.

So I am sharing one way to built HID-capable applications with VB.NET, far from being the only or the best way to do it, this is simply one possibility.

1. First download the required DLL files USB_Framework_DLL.zip and the Windows Form example USB_Framework_Solution.zip.
- The mcHID.dll needs to be in the same folder as the application's exe file

DLL.jpg

- The mcHIDinterface.dll need to be referenced in your VB.NET solution as shown here:

REF.jpg

-- 1. In the "Solution Explorer" right click on your solution and select "Properties"
-- 2. Click on "Add" and browse to "mcHIDinterface.dll" and select it to add.
-- 3. and 4. It should appear in you "References" as shown in 3 and 4

2. Make sure in your project properties, "Target CPU" is set as x86

x86.jpg

3. Design your Windows form to fit your project. This is the example Project, included in USB_Framework_Solution.zip:

VB_HID.jpg

In this example:
- The top Label is changed to "MY USB CONNECTED" and "MY USB NOT CONNECTED" when a device with the preset VID and PID is connected and disconnected from the PC respectively.
- Button "SEND" sends a simple command to the PIC MCU to turn on the LED.
- The PIC MCU switches on the LED and responds with a string saying "LED ON"
- After 1 second the PIC switches off the LED and sends a string "LED OFF"
- As idle process the PIC constantly sends the status of the button and the green/red button in the form changes color accordingly.

To set up the application correctly, make sure the VID and PID, as well as the BufferIN and BufferOUT sizes match in both, the VB.NET application and Proton.

All functions and events are commented in the VB.NET solution and how information is being handled. However how to deal with the data received and sent to the PIC MCU is entirely up to the user and there is always a rudimentary protocol required that will avoid infinity loops (i.e. waiting for data, that is never being sent) or time collisions and overflows. 

Again, there are probably many and better ways to achieve the same result, this is just a basic to start and build on.



John Lawton

Trastikata, I've been using your excellent mcHID information as a start and have been able to create a nice App for configuring my Joystick interface board design.

I followed your example code but I now want to utilise some of the other functionality available with mcHIDusb, e.g.

mcHIDusb.hidGetProductID(pHandle) which I was able to use, but I can't find the API (if that's the right term) for any other commands available, (e.g. USB Serial Number?) despite searching. Can you point me in the right direction please?

trastikata

Hello John,

this is the entire API as shown in the solution explorer:

' HID interface API declarations...
 Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Integer) As Boolean
 Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean
 Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Integer) As Integer
 Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Integer
 Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
 Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
 Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
 Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
 Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendoID As Integer, ByVal pProductID As Integer) As Integer
 Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Integer) As Integer
 Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Integer) As Integer
 Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Integer) As Integer
 Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
 Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
 Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
 Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Integer) As Integer
 Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Integer) As Integer
 Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Integer, ByVal pValue As Boolean)
 Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Integer) As Boolean
 Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Integer, ByVal pProductID As Integer) As Boolean

Example how to get the Product name by clicking a button:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'New string variable with preset lenght of 32
        Dim ProductName As String = "                                "
        'Get the product name
        mcHIDusb.hidGetProductName(pHandle1, ProductName, 20)
        'Post ProductName
        TextBox1.Text = ProductName
    End Sub
End Class

John Lawton

Thank you very much. It looks like I still have a lot to learn as it looks like I could have found this myself in Visual Studio if I had known how to... :)

John

Stephen Moss

If using Visual Studio an alternative may be to use one of the USB library packages, I have not tried them but if you go to the Project menu - Manage NuGet Package and enter USB in the search textbox there appears to be several options available.
The downside is they may produce a lot of files you need to distribute with your application (WebView2 does), but the upside may be that it is possible to use the same library to communicate with both HID and non HID devices so you won't have to use different methods for different device types.

John Lawton

Hi Stephen,

thanks for that info, but I'll stick with what's working for now...

John

DaveC

Hi Trastikata, any chance you could share your positron code.

trying to get my head round this USB stuff.

John Lawton