MelbourneDeveloper / Device.Net

A C# cross platform connected device framework
MIT License
628 stars 121 forks source link

.net 5 to .net 6 #224

Open qmtdlt opened 2 years ago

qmtdlt commented 2 years ago

Describe the issue modify .csproj file old file

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
 <ItemGroup>
    <PackageReference Include="Device.Net" Version="4.2.1" />
    <PackageReference Include="Hid.Net" Version="4.2.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
  </ItemGroup>

modified

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
 <ItemGroup>
    <PackageReference Include="Device.Net" Version="4.2.1" />
    <PackageReference Include="Hid.Net" Version="4.2.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
  </ItemGroup>

Screenshots image

Your Code

internal class Program
    {
        static void Main(string[] args)
        {
            run();
            Console.ReadLine();
        }
        static async Task run()
        {
            try
            {
                var loggerFactory = LoggerFactory.Create((builder) =>
                {
                    _ = builder.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: 0x8888, productId: 0x00D1, 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").
                    CreateWindowsHidDeviceFactory(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);

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
    }

Info

MelbourneDeveloper commented 2 years ago

@qmtdlt dis you mean to do a pull request?