Error when trying to use class/method of a library.

Asked

Viewed 52 times

0

I am trying to connect a WPF to an external hardware via usb. I installed the Hidsharp Nuget and declared the USB Devices as below:

HidDevice _device;
HidStream _deviceStream;
IEnumerable<HidDevice> _deviceList;

That within the public partial class MainWindow : Window. I created the method ConnectToUsb and within it use DeviceList from the library Hidsharp which has been declared as follows using HidSharp;

But VS is reporting me 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 within the main class. Someone has already been 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;
            }

        }
  • What is exactly the error that is presented?

  • the name 'Devicelist' does not exist in the Current context.

  • And this DeviceList is a library class/namespace?

  • Yes, class Hidsharp.DeviceList. Provides a list of all available.

  • What version of the package you are using?

  • I’m using version 2.0.0-alpha.

  • Installed by Nuget? If so, you can post the contents of the file Packages.config?

Show 2 more comments

2 answers

3


This class is not available in version 1.5.0. That, according to this previous question, is the version you have referenced.

In the section Assembly, in the class documentation you can check

Namespace: Hidsharp
Assembly: Hidsharp (in Hidsharp.dll) Version: 2.0.0-alpha

You can install this version, but keep in mind that it is a version alpha. It’s at your own risk.

To install it just use the command

PM> Install-Package HidSharp -Version 2.0.0-alpha
  • I was using version 1.5. When I upgraded the version to 2.0.0-alpha it worked, thank you.

1

For the error message:

the name 'Devicelist' does not exist in the Current context.

I think maybe the problem is in your class, where you call:

 _deviceList = DeviceList.Local.GetHidDevices();

If you have more than one script/document that is using Hiddevice, then maybe in one of them you have forgotten to declare the using HidSharp;, check all documents that will use the class.

  • At the beginning of the question he says he has the statement using HidSharp

  • @LINQ could have declared in one script and another not, if it is using in more than one file.

  • Absolutely. But note that he says he’s doing everything in the same class (MainWindow). Of course it can be confused and misspelled, I’m only guided by what is written in the question (and also by the history, where it gets an answer that says to install version 1.5.0 of the library - version that does not contain the class cited in the error).

  • @LINQ but it may be that in Mainwindow is ok and the console stated that the problem is elsewhere, it has not posted the whole message, it may simply be a mess, I will wait for the AP feedback on this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.