简体   繁体   中英

Referencing Hid Library from GitHub library in C# and use it

I am just starting to learn C# so excuse me if this is a basic question. I am trying to develop an application that reads values for a USB-HID scale into Excel. To start I am going to use this github library (already downloaded it): https://github.com/mikeobrien/HidLibrary then use closedxml.codeplex.com to create a spreadsheet and transfer data there.

I am trying to figure out how to reference this library in my C# project and use it to get information from the scale. I don't know much about C# or .NET and would love some help, most google searches haven't helped much so any leads would be much appreciated. Thank you!

The link you provided from GitHub specifies the name of the lib for referenced it on your project. Take a look at the Installation section. This code is for using on the Nuget Package Console on your project.

PM> Install-Package hidlibrary

Take a look on how to use the Nuget Package console for Visual Studio on this link https://docs.nuget.org/consume/package-manager-console

Try out Hid.Net

You can read about Hid transfer in the documentation here .

This reads from a thermometer which would probably be similar to scales.

Reference

    private static async Task DisplayTemperature()
    {
        //Connect to the device by product id and vendor id
        var temperDevice = await new FilterDeviceDefinition(vendorId: 0x413d, productId: 0x2107, usagePage: 65280)
            .CreateWindowsHidDeviceFactory(_loggerFactory)
            .ConnectFirstAsync()
            .ConfigureAwait(false);

        //Create the observable
        var observable = Observable
            .Timer(TimeSpan.Zero, TimeSpan.FromSeconds(.1))
            .SelectMany(_ => Observable.FromAsync(() => temperDevice.WriteAndReadAsync(new byte[] { 0x00, 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 })))
            .Select(data => (data.Data[4] & 0xFF) + (data.Data[3] << 8))
            //Only display the temperature when it changes
            .Distinct()
            .Select(temperatureTimesOneHundred => Math.Round(temperatureTimesOneHundred / 100.0m, 2, MidpointRounding.ToEven));

        //Subscribe to the observable
        _ = observable.Subscribe(t => Console.WriteLine($"Temperature is {t}"));

        //Note: in a real scenario, we would dispose of the subscription afterwards. This method runs forever.
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM