Skip to content

Latest commit

 

History

History
118 lines (85 loc) · 2.19 KB

README.md

File metadata and controls

118 lines (85 loc) · 2.19 KB

Hid Test

Copyright © Bill Wang

All rights reserved.

Gui

Requirements

📦 NuGet Packages

📬 IDE

  • Visual Studio 2022
  • .net 6.0

🙌 Getting Started

Initialize Hid Device

MyHidClass myHidDevice;
myHidDevice = new MyHidClass(TestSetting.UsbVid, TestSetting.UsbPid);

Add USB Plug/Unplug Event

MyHidClass.localDeviceList.Changed += DeviceListChangedHandler;

// Deal with the USB plug & unplug
void DeviceListChangedHandler(object? sender, EventArgs e)
{        
    myHidDevice.CheckHidDevice();

    if (myHidDevice.hidDevice == null)
    {
        DeviceRemoved();
    }
    else
    {
        DeviceConnected();
    }
}

Add HID Input Report Callback

myHidDevice.InputReportReceived = InputReportReceived;

// Parse the HID Input Report
void InputReportReceived(byte[] bytes, int length)
{
    // Decode bytes
}

Check USB at startup

DeviceListChangedHandler(null, null); // Check USB

Complete Example

MyHidClass myHidDevice;
myHidDevice = new MyHidClass(TestSetting.UsbVid, TestSetting.UsbPid);
myHidDevice.InputReportReceived = InputReportReceived;
MyHidClass.localDeviceList.Changed += DeviceListChangedHandler;
DeviceListChangedHandler(null, null); // Check USB

// Parse the HID Input Report
void InputReportReceived(byte[] bytes, int length)
{
    // Decode bytes
}

// Deal with the USB plug & unplug
void DeviceListChangedHandler(object? sender, EventArgs e)
{        
    myHidDevice.CheckHidDevice();

    if (myHidDevice.hidDevice == null)
    {
        DeviceRemoved();
    }
    else
    {
        DeviceConnected();
    }
}

Write to HID

Send string to HID

public async Task WriteStringAsync(string command)

Send byte array to HID

async Task WriteBytesAsync(byte[] buffer, int timeout = 1)

References