Use the HID test app

… visits
Parent Previous Next

Use this Windows app to exchange small commands and replies with a PIC running HID data firmware. It is a starting point for your own buttons, indicators and measurements.


The source is in HID Data > PC Host App. Open the supplied solution, or its HID_Data_VS_WPF.vbproj project. Follow Build the Windows test apps for the Visual Studio setup and first build.



This template uses fixed-length HID messages, called reports, without numbered Report IDs. Start with the matching HID data example in Framework. Keyboard, mouse and other HID formats need their own handling.

Match the app to the PIC

Open MainWindow.xaml.vb and find User settings and arrays. Change the existing settings there; do not add a second set of declarations.

Setting in the PC app

What to enter

USB_VID

The VID used by your PIC firmware

USB_PID

The PID used by your PIC firmware

USB_UsagePage

The usage page selected for the firmware's HID data profile

USB_Usage

The application usage for that profile

USB_SerialNumber

Leave empty for the first matching device, or enter the exact device serial to select one particular board

TX_REPORT_LENGTH

Number of data bytes the PC sends to the PIC

RX_REPORT_LENGTH

Number of data bytes the PC receives from the PIC

USB_AutoConnect

Use True for automatic connection and reconnection

USB_ReceiveEvents

Leave True for the normal receive display

Copy the values from your PIC configuration. Usage page and usage must match as well as VID and PID.


VB.NET uses &H before a hexadecimal number. For example, &H20 is the same number as decimal 32. Do not copy a Positron $ prefix into a VB.NET setting. Keep a serial number as text so any leading zeros remain.

Get the two report sizes the right way round

Transmit and receive swap sides between the PC and PIC:

Direction

PC app setting and array

PIC-side setting

PC sends to PIC

TX_REPORT_LENGTH, TXReport

OUT / RX bytes, USB_RX_SIZE

PIC sends to PC

RX_REPORT_LENGTH, RXReport

IN / TX bytes, USB_TX_SIZE

If the PIC sends 32 bytes and receives 16, use PC RX length 32 and PC TX length 16. The window supports 1–64 data bytes each way, matching the firmware.

Count data bytes only. For a 64-byte payload, use 64, not 65. Windows has one extra position for the Report ID, but this template handles that position for you. Do not add an extra zero byte at the start of TXReport or RXReport.

Changing an app setting does not change the PIC. If you change report sizes in the configuration tool, rebuild and program the firmware, then update and rebuild the PC app to match.

Build, connect and try one command

  1. Save your settings and build the project in Release mode, keeping .NET Framework 4.8 as its target.
  2. Run the built app from Visual Studio or from bin\Release\net48. Keep the normal build output together.
  3. Program the board with the matching HID data firmware and connect the USB cable.
  4. With USB_AutoConnect = True, wait for the app to find the device. The connection display changes when a matching device opens.
  5. Check the displayed device information, then press one command button that the firmware understands.
  6. Check the expected reply. A connected indicator or a completed send does not prove the PIC carried out the command.
  7. Unplug and reconnect once. Automatic mode should look for the device again and open a new connection when it becomes available.

HID connects directly; there is no COM port to choose.


The demo buttons shown in the HID command example use commands 100, 200 and 222. A command button puts its numeric value in TXReport(0) and sends a complete report. It does not send the text printed on the button. The firmware decides what each command means.

Understand the receive display

The supplied ProcessReceivedData routine copies incoming data to RXReport and shows it as ASCII text up to the first zero byte. It keeps received line endings, but does not add its own.


For binary measurements, change the receive routine to read those fields. An empty or strange text display need not mean that communication failed. Longer messages need your own rule for joining reports.


The manufacturer, product and serial fields help identify the board. If a field reports a read failure, use Refresh descriptors and inspect its details. A missing optional name is different from a failed data connection.

Adapt the demo a little at a time

What you want to change

Where to start

Command button or window layout

MainWindow.xaml: change the label and numeric Tag, or add your own control.

Device IDs, report sizes or automatic connection

User settings and arrays in MainWindow.xaml.vb.

What happens on connection

USB_Connected: enable your controls or update the display.

What happens on disconnection

USB_Disconnected: disable device actions and clear pending commands.

Bytes sent by your new button

User send routines and the existing USB_SendDataAsync() helper.

Meaning of received bytes

ProcessReceivedData; the USB_DataReceived handler already calls it.

Glass colours and control appearance

Styles.xaml.

Edit existing handlers rather than pasting duplicate methods. Keep the transport classes and window-closing cleanup for now. Save, rebuild and repeat the same test after each small change.


Send the exact agreed report length; shorter arrays are not padded and longer ones are not split. Keep Await in adapted examples so the window can respond while USB work finishes.

If it does not work

What you see

First check

No connection

VID/PID, hexadecimal notation, usage page, usage and any serial filter.

Report-length error

Match PC TX to PIC RX and PC RX to PIC TX. Exclude the extra Report-ID position.

Connected but no reply

Check the command value, firmware reply code and continued USB servicing. Keep receive events enabled.

Data seems shifted by one byte

Remove any Report-ID byte you added to the application data.

Text is incomplete or unreadable

Check whether the reply is binary, contains zero padding or spans several reports.

See also

Created with the Personal Edition of HelpNDoc: What is a Help Authoring tool?