I'm trying to connect a WPF with external hardware through the usb. I installed NuGet HIDSharp and declared USB Devices as below:
HidDevice _device;
HidStream _deviceStream;
IEnumerable<HidDevice> _deviceList;
That's within public partial class MainWindow : Window
.
I created the ConnectToUsb
method and inside it DeviceList
use coming from the HidSharp library that was declared as using HidSharp;
But VS is reporting an error in DeviceList
saying that it does not exist in that context, even though I have declared the library and even the method being inside the main class. Has anyone gone through this and would have a possible solution?
private async Task<bool> ConnectToUsb()
{
await Task.Delay(1000);
try
{
//manda conectar no dispositivo
_deviceList = DeviceList.Local.GetHidDevices();
_device = _deviceList.FirstOrDefault(dev => dev.ProductID == _Pid && dev.VendorID == _Vid && dev.DevicePath.Contains("col01"));
//inicia o background worker caso encontre
if (_device != null)
{
//Conecta na placa encontrada
_device.Open();
if (_device.TryOpen(out _deviceStream))
{
// TODO Mostrar a capacidade do HID
WriteLog($"Placa {cmbBoard.SelectedItem.ToString()} encontrada!", Brushes.Gray);
WriteLog("Comunicação estabelecida com sucesso", Brushes.Gray);
return true;
}
else
{
return false;
}
}
else
{
//Não encontrou nenhum dispositivo
// WriteMessage("Erro na Conexão USB!", Brushes.Red);
WriteLog("Não foi possível encontrar nenhuma Placa!", Brushes.Red);
return false;
}
}
catch (Exception ex)
{
MessageBox.Show("Erro ao tentar conectar ao dispositivo! " + ex.ToString());
WriteLog("Erro ao tentar conectar ao dispositivo: " + ex.ToString(), Brushes.Gray);
return false;
}
}