• Stars
    star
    616
  • Rank 72,837 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A C# cross platform connected device framework

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

Project Update: this project is on pause. Please read the whole story here.

diagram

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

Version 4.x

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.

New in 4.2.1

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.

Coin Address
Bitcoin 33LrG1p81kdzNUHoCnsYGj6EHRprTKWu3U
Ethereum 0x7ba0ea9975ac0efb5319886a287dcf5eecd3038e
Litecoin MVAbLaNPq7meGXvZMU4TwypUsDEuU6stpY

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:

Platform Hid USB Serial Port Bluetooth
.NET Framework Yes Yes Yes No
.NET Core Yes Yes Yes No
Android Yes Yes No No
UWP Yes Yes No No
Linux, MacOS* No (Via LibUsbDotNet) No No
WebAssembly No No No No

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

using Device.Net;
using Hid.Net.Windows;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Usb.Net.Windows;

namespace Usb.Net.WindowsSample
{
    internal class Program
    {
        private static async Task Main()
        {
            //Create logger factory that will pick up all logs and output them in the debug output window
            var loggerFactory = 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. 
            var hidFactory =
                new FilterDeviceDefinition(vendorId: 0x534C, productId: 0x0001, label: "Trezor One Firmware 1.6.x", usagePage: 65280)
                .CreateWindowsHidDeviceFactory(loggerFactory);

            //Register the factory for creating Usb devices.
            var usbFactory =
                new FilterDeviceDefinition(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 device
            var factories = hidFactory.Aggregate(usbFactory);

            //Get connected device definitions
            var deviceDefinitions = (await factories.GetConnectedDeviceDefinitionsAsync().ConfigureAwait(false)).ToList();

            if (deviceDefinitions.Count == 0)
            {
                //No devices were found
                return;
            }

            //Get the device from its definition
            var trezorDevice = await hidFactory.GetDeviceAsync(deviceDefinitions.First()).ConfigureAwait(false);

            //Initialize the device
            await trezorDevice.InitializeAsync().ConfigureAwait(false);

            //Create the request buffer
            var buffer = new byte[65];
            buffer[0] = 0x00;
            buffer[1] = 0x3f;
            buffer[2] = 0x23;
            buffer[3] = 0x23;

            //Write and read the data to the device
            var readBuffer = await trezorDevice.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

More Repositories

1

RestClient.Net

.NET REST Client Framework for all platforms
C#
366
star
2

ioc_container

A lightweight, flexible, and high-performance dependency injection and service location library for Dart and Flutter
Dart
60
star
3

Trezor.Net

Cross platform C# library for talking to the Trezor hardwarewallet
C#
43
star
4

Jayse

Lossless conversion of JSON to and from statically-typed, immutable objects in Dart and .NET
Dart
29
star
5

CryptoCurrency.Net

CryptoCurrency.Net
C#
19
star
6

Samples

C#
19
star
7

Urls

Treat Urls as first-class citizens
C#
19
star
8

Ledger.Net

Cross Platform C# Library For Ledger Hardware Wallet
C#
15
star
9

flutter_ioc_container

A lightweight, flexible, and high-performance dependency injection and service location library for Flutter
C++
13
star
10

firestore_fakes

Fake Firestore Data With Control
Dart
12
star
11

austerity

Dart and Flutter static code analysis, but not for the faint hearted
Dart
9
star
12

papilio_note

Markdown notes anywhere
Dart
8
star
13

COVID-19-DB

COVID-19 database generated from Johns Hopkins CSSE data
C#
8
star
14

navigation_drawer_menu

Flutter Material Design Navigation Drawer Menu
Dart
8
star
15

json_to_dto

Convert JSON to Dart DTOs and back. Automate the model creation and the toJson and fromJson methods
C++
7
star
16

listenable_future_builder

A general purpose Flutter Builder widget that allows you to do async work on controller initialization
Dart
7
star
17

papilio

Build beautiful apps with Flutter
Dart
7
star
18

flutter_location_wakeup

Listens for significant location changes on the device and wakes it up when they arrive
Dart
6
star
19

Hardwarewallets.Net

C# Hardwarewallets
C#
6
star
20

KeepKey.Net

Cross Platform KeepKey Hardware Wallet C# Library
C#
6
star
21

arborio

An elegant, flexible Treeview with Animation. Display hierarchical data in Flutter.
Dart
6
star
22

fixed_collections

Dart collections that you can't modify
Dart
5
star
23

DatabaseLogging

C#
5
star
24

bloobit

Flutter state management, as simple as it gets
C++
5
star
25

Moqesq

C#
4
star
26

notifier_builder

A Builder for Flutter Notifiers
C++
4
star
27

flutter_test_extensions

Dart
3
star
28

fhir_client

Dart/Flutter library for consuming FHIR server APIs. Simple, stateless http extensions that make it easy to consume FHIR
Dart
3
star
29

nadz

Carefully crafted monads, such as Result and Option for exhaustive pattern matching in Dart.
Dart
2
star
30

papilio_bloc

Bloc, but in a different flavor
Dart
2
star
31

AsyncDictionary

C#
2
star
32

benchmarks

I benchmark things and then tell everybody about it
Dart
2
star
33

Cashper

C#
1
star
34

Hardfolio

Hardfolio
1
star
35

AutomatedTesting

A Windows automated testing framework
C#
1
star
36

DBTogRPC

C#
1
star
37

call_and_response

C++
1
star
38

Voom

ViewModels, made easy
C#
1
star
39

fake_httpclient

Fake HTTP calls in Widget Tests without abstractions
Dart
1
star