Send a HID command and reply

USB Library ›› Device ›› Use HID ››
… visits
Parent Previous Next

Use this small exchange to prove that your PIC and PC agree on the byte layout. Once it works, replace the example measurement with your own reading.

HID PC test application connected to Positron8 HID Data, with SEND 100, SEND 200 and SEND 222 buttons and text replies from the PIC.

The HID test app shows a connected device and replies from a simple command demo. This pictured demo returns text; the measurement example on this page returns binary bytes and needs a matching PC reader.

For this example, the PC sends command byte 100. The PIC replies with a measurement and a signed counter.

  1. Select HID data and set both TX and RX reports to 64 bytes.
  2. Use the generated startup from Integration.
  3. Add the variables and main loop below in the application area.

The library already declares TXReport and RXReport; do not declare them again.

Dim bCommand As Byte

Dim bReplyPending As Byte

Dim bPackByte As Byte

Dim wMeasurement As Word

Dim sdCounter As SDword


bReplyPending = 0


Do

    USB_Service()                    ' Required in polling mode


    If bReplyPending = 1 Then

        USB_Write(USB_TX_SIZE)

        If USB_Result = USB_OK Then bReplyPending = 0

        If USB_Configured = 0 Then bReplyPending = 0

    Else

        USB_Read()

        If USB_Result = USB_OK Then

            If USB_RxLength > 0 Then

                bCommand = RXReport[0]

                If bCommand = 100 Then

                    wMeasurement = 1234   ' Replace with your measurement

                    sdCounter = -12345    ' Replace with your counter


                    Clear TXReport

                    TXReport[0] = 100

                    bPackByte = wMeasurement.Byte0

                    TXReport[1] = bPackByte

                    bPackByte = wMeasurement.Byte1

                    TXReport[2] = bPackByte


                    bPackByte = sdCounter.Byte0

                    TXReport[3] = bPackByte

                    bPackByte = sdCounter.Byte1

                    TXReport[4] = bPackByte

                    bPackByte = sdCounter.Byte2

                    TXReport[5] = bPackByte

                    bPackByte = sdCounter.Byte3

                    TXReport[6] = bPackByte

                    bReplyPending = 1

                EndIf

            EndIf

        EndIf

    EndIf

Loop

The loop keeps the reply until USB accepts it for sending. While waiting, it leaves that reply unchanged and does not read another command. In a larger program, also handle USB_HALTED and the other USB results.


Run this loop in the main program in both polling and interrupt modes. Do not put it inside the USB interrupt handler.

Read the reply on the PC

The PC should interpret the reply as follows:

Bytes

Meaning

Example bytes

0

Reply command

64 hexadecimal, meaning decimal 100

1–2

Unsigned 16-bit measurement, low byte first

D2 04, meaning 1234

3–6

Signed 32-bit counter, low byte first, two's complement

C7 CF FF FF, meaning −12345

7–63

Unused padding

Zero

On the PC, combine bytes 1–2 as an unsigned 16-bit number and bytes 3–6 as a signed 32-bit number. Sending text 100 produces three character bytes, 49, 48 and 48; that is different from sending the single command byte 100.

Check the PC buffer size. All 64 PIC report bytes are yours to use. Some PC HID APIs need an extra zero at the start as a Report-ID placeholder. For those APIs, the PC buffer has 65 bytes: zero, then the 64 report bytes. The built-in descriptors do not use Report IDs. Keep your PIC command in TXReport[0]; do not move it to byte 1 to make room for that PC-only zero.

Test with the supplied Windows app

Build the source in HID Data → PC Host App using Visual Studio Community 2026. The HID app guide shows the few settings to match, how to send a test command, and where to change the reply display. Use the matching Framework example first; a binary measurement reply needs the byte interpretation shown on this page rather than plain text display.

See also

Created with the Personal Edition of HelpNDoc: Easy CHM and documentation editor