LibUsbDotNet Win32Error: Error Code 87

1.2k views Asked by At

I’m having a problem with an USB device. I am developing an application in C# that communicates with this device. To do this communication I am using the LibUsbDotNet library. The problem is the following, my communication works perfectly in many computers here in my company. But there are others (two computers until now) that it just doesn’t work. In these, when I try to write to the device it gives me an Win32Error: Error code 87 (invalid parameter). The strange thing is that I am using the same driver that I am using in the other computers. They even use the same OS (windows 8). This driver I created with the “Driver Install Creator Wizard”, that comes with the UsbK Development Kit. I used the UsbK to create the driver because they use a newer version of the libusb driver. I have other devices that I did this same thing, and they all work fine, just this one is giving me a headache. Researching the problem, I found the Usblyzer software. Running it and analyzing the information of my device when plugged into the PC, I saw that the number of pipes open is 0 in the computers with problem. In the ones where everything works, is 3. Does this have anything to do with the problem? Any idea how can I solve it?

Below are a small code that I am using to reproduce the problem. The original application is very big one.

private void OnUsbDeviceNotify(object sender, DeviceNotifyEventArgs e)
{
    if (e.Device == null)
        return;

    int vid = e.Device.IdVendor;
    int pid = e.Device.IdProduct;

    if (vid == Vid && pid == Pid)
    {
        switch (e.EventType)
        {
            case EventType.DeviceArrival:
                ConnecDevice();
                break;
            case EventType.DeviceRemoveComplete:
                Reader = null;
                Writer = null;
                break;
        }
    }
}

private void ConnecDevice()
{
    UsbDeviceFinder usbDeviceFinder = new UsbDeviceFinder(Vid, Pid);

    UsbDevice usbDevice = UsbDevice.OpenUsbDevice(usbDeviceFinder);

    if (usbDevice == null)
        return;

    if (!usbDevice.IsOpen)
        usbDevice.Open();

    IUsbDevice iUsbDevice = (IUsbDevice)usbDevice;

    iUsbDevice.SetConfiguration(1);
    iUsbDevice.ClaimInterface(1);

    int transferLengthWrite;

    Reader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
    Writer = usbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

    ErrorCode erro = Writer.Write(ReadValuesCommand, 0, 2, WriteTimeout, out transferLengthWrite);
    Console.Write(erro);

    if (erro != ErrorCode.Success)
        return;

    Thread.Sleep(500);

    int transferLengthRead;

    byte[] values = new byte[ValuesSize];

    Reader.Read(Values, 0, ValuesSize, ReadTimeout, out transferLengthRead);

    ProcessValues(values);
}
0

There are 0 answers