Skip to content

Repository files navigation

Hid.Net, Usb.Net, SerialPort.Net (Device.Net)

diagram

Cross-platform .NET framework for talking to connected devices such as USB, Serial Port and Hid devices

Don't waste your time with the obscurities of connecting to devices and learning their protocol. I've done this plenty of times and I can fast track you to building an app or getting something to work.

Version 4

is live on Nuget.org! Take a look at the 4.0 project to see new features and fixes. Version 4 has public interface changes. You will need to read through the documentation to upgrade from version 3 to version 4.

This framework provides a common Task async programming interface across platforms and device types. This allows for dependency injection to use different types of devices on any platform with the same code. The supported device types are Hid, Serial Port, and USB.

Contribute

This project needs funding. Please sponsor me here so that I can contribute more time to improving this framework.

CoinAddress
Bitcoin33LrG1p81kdzNUHoCnsYGj6EHRprTKWu3U
Ethereum0x7ba0ea9975ac0efb5319886a287dcf5eecd3038e
LitecoinMVAbLaNPq7meGXvZMU4TwypUsDEuU6stpY

This project also needs unit tests, bug fixes and work towards more platforms. Please read this.

Licensing

This framework uses the MIT license. I won't sue you, or your business if you use this for free. If you are developing software for free, I don't expect you to sponsor me. However, if your business makes more than USD 100,000 per year and your software depends on Device.Net, I expect your business to make a serious contribution via sponsorship.

Why Device.Net?

Device communication is fragmented across platforms and device types. If you need to use three different device types across Android, UWP and .NET, you would otherwise need nine different APIs. Device.Net puts a standard layer across all these so that you can share code across all platforms and device types. You don't need to use Windows APIs or learn about Android's API directly. If the device manufacturer decides to switch from USB to Hid, the code remains the same. Write once; run everywhere.

Get Help

Currently supports:

PlatformHidUSBSerial PortBluetooth
.NET FrameworkYesYesYesNo
.NET CoreYesYesYesNo
AndroidYesYesNoNo
UWPYesYesNoNo
Linux, MacOS*No(Via LibUsbDotNet)NoNo
WebAssemblyNoNoNoNo

Note: Bluetooth, Linux, and macOS, WebAssembly (via WebUsb) support are on the radar. If you can sponsor this project, you might be able to help get there faster.

SerialPort.Net and Device.Net.LibUsb are still in alpha mode. You must use the prerelease version

Example Code

usingDevice.Net;usingHid.Net.Windows;usingMicrosoft.Extensions.Logging;usingSystem.Linq;usingSystem.Reactive.Linq;usingSystem.Threading.Tasks;usingUsb.Net.Windows;namespaceUsb.Net.WindowsSample{internalclassProgram{privatestaticasyncTaskMain(){//Create logger factory that will pick up all logs and output them in the debug output windowvarloggerFactory=LoggerFactory.Create((builder)=>{_=builder.AddDebug().SetMinimumLevel(LogLevel.Trace);});//----------------------// This is Windows specific code. You can replace this with your platform of choice or put this part in the composition root of your app//Register the factory for creating Hid devices. varhidFactory=newFilterDeviceDefinition(vendorId:0x534C,productId:0x0001,label:"Trezor One Firmware 1.6.x",usagePage:65280).CreateWindowsHidDeviceFactory(loggerFactory);//Register the factory for creating Usb devices.varusbFactory=newFilterDeviceDefinition(vendorId:0x1209,productId:0x53C1,label:"Trezor One Firmware 1.7.x").CreateWindowsUsbDeviceFactory(loggerFactory);//----------------------//Join the factories together so that it picks up either the Hid or USB devicevarfactories=hidFactory.Aggregate(usbFactory);//Get connected device definitionsvardeviceDefinitions=(awaitfactories.GetConnectedDeviceDefinitionsAsync().ConfigureAwait(false)).ToList();if(deviceDefinitions.Count==0){//No devices were foundreturn;}//Get the device from its definitionvartrezorDevice=awaithidFactory.GetDeviceAsync(deviceDefinitions.First()).ConfigureAwait(false);//Initialize the deviceawaittrezorDevice.InitializeAsync().ConfigureAwait(false);//Create the request buffervarbuffer=newbyte[65];buffer[0]=0x00;buffer[1]=0x3f;buffer[2]=0x23;buffer[3]=0x23;//Write and read the data to the devicevarreadBuffer=awaittrezorDevice.WriteAndReadAsync(buffer).ConfigureAwait(false);}}}

See Also

Human Interface Device Wikipedia Page - Good for understanding the difference between the meaning of the two terms: USB and Hid.

USB human interface device class Wikipedia Page - as above

USB Wikipedia Page - as above

Jax Axelson's USB Page - General C# USB Programming

About

A C# cross platform connected device framework

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages