MelbourneDeveloper / Device.Net

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

Can't write to HID device #250

Open Simone1223 opened 2 months ago

Simone1223 commented 2 months ago

Hi, I am trying to communicate with an HID device (STM32). At the moment I have to transmit 256 bytes (including ReportID) from my PC, which represents the device's status request. The device must in turn respond with another 256 bytes. I manage to initialise everything without any problems, the error occurs when I try to send the 256 bytes, telling me:

System.IO.IOException: ‘An error occurred while attempting to write to the device’
This exception was originally generated in the following call stack:
System.IO.Strategies.BufferedFileStreamStrategy.WriteToNonSeekableAsync(System.ReadOnlyMemory<byte>, System.Threading.CancellationToken)
Hid.Net.Windows.WindowsHidHandler.WriteReportAsync(byte[], byte, System.Threading.CancellationToken)
Hid.Net.HidDevice.WriteReportAsync(byte[], byte, System.Threading.CancellationToken).

image

Full code

using Device.Net;
using Hid.Net.Windows;
using Microsoft.Extensions.Logging;

namespace MauiApp_Test_Usb
{
    public class UsbManager
    {
        private static IDevice markDevice;
        private TransferResult readBuffer;
        public async Task InitializeUsbAsync()
        {
            var loggerFactory = LoggerFactory.Create((builder) =>
            {
                _ = builder.AddDebug().SetMinimumLevel(LogLevel.Trace);
            });

            // Definisci i device che l'app deve cercare
            var hidFactory = new FilterDeviceDefinition(vendorId: 0x0483, productId: 0x5750).CreateWindowsHidDeviceFactory(loggerFactory);

            //Get connected device definitions
            var deviceDefinitions = (await hidFactory.GetConnectedDeviceDefinitionsAsync().ConfigureAwait(false)).ToList();
            if (deviceDefinitions.Count == 0)
            {
                //No devices were found
                return;
            }

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

            MainPage.timer.Start();
        }

        public bool GetIsInitialized()
        {
            return markDevice.IsInitialized;
        }
        public TransferResult GetReadBuffer()
        {
            return readBuffer;
        }

        public async void sendStatus()
        {
            //Create the request buffer
            var buffer = new byte[256];
            buffer[0] = 0x02;
            buffer[1] = 0x01;

            //Write and read the data to the device
            var readBuffer = await markDevice.WriteAndReadAsync(buffer).ConfigureAwait(false); // <-- ERROR HERE
        }
    }
}
System.IO.IOException
  HResult=0x80131620
  Messaggio=An error occurred while attempting to write to the device
  Origine=Hid.Net
  Analisi dello stack:
   in Hid.Net.HidDevice.<WriteReportAsync>d__22.MoveNext()
   in Device.Net.DeviceBase.<WriteAndReadAsync>d__15.MoveNext()
   in MauiApp_Test_Usb.UsbManager.<sendStatus>d__5.MoveNext() in C:\Users\simonebenincasa\Documents\Progetti visual studio\MauiApp Test Usb\MauiApp Test Usb\usbManager.cs: riga 53

  Questa eccezione è stata generata in origine nello stack di chiamate seguente:
    System.IO.Strategies.BufferedFileStreamStrategy.WriteToNonSeekableAsync(System.ReadOnlyMemory<byte>, System.Threading.CancellationToken)
    Hid.Net.Windows.WindowsHidHandler.WriteReportAsync(byte[], byte, System.Threading.CancellationToken)
    Hid.Net.HidDevice.WriteReportAsync(byte[], byte, System.Threading.CancellationToken)

Eccezione interna 1:
IOException: Parametro non corretto.

The app must be cross-platform for Android, Windows, MacOS and iOS. To do this I am using the .net MAUI platform.

Thank you in advance for your support

Info