0

I am trying to write to a USB device using the LibUsbDotNet library, however, when I try to write it returns a Win32 error.

So far it finds the device, sets up the interface, and sets up the writer endpoint (as far as I can tell). But, when I try to write to the writer it returns a Win32 error. I can't figure out how to get a more descriptive error message from it, let alone what's going wrong. Can anyone help me with this?

private static void USBPortTest()
        {
            //USB is identified by it's serial number
            UsbDevice LI7000;
            string userInput = string.Empty;
            UsbEndpointReader readerLI7000;
            UsbEndpointWriter writerLI7000;
            int bytesWritten;

            try
            {
                UsbDeviceFinder usbFinder = new UsbDeviceFinder(0x1509);

                LI7000 = UsbDevice.OpenUsbDevice(usbFinder);

                IUsbDevice wholeLI7000 = LI7000 as IUsbDevice;

                if (!ReferenceEquals(wholeLI7000, null))
                {
                    wholeLI7000.SetConfiguration(1);
                    wholeLI7000.ClaimInterface(0);
                }

                readerLI7000 = LI7000.OpenEndpointReader(ReadEndpointID.Ep01);
                writerLI7000 = LI7000.OpenEndpointWriter(WriteEndpointID.Ep01);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            if (LI7000 != null)
            {
                Console.WriteLine("Device Found!");
            }
            else
            {
                Console.WriteLine("Device NOT Found!");
                return;
            }



            while(true)
            {
                Console.WriteLine("Menu");
                Console.WriteLine("------------------------------------");
                Console.WriteLine("1: Input command");
                Console.WriteLine("2: Quit");

                userInput = Console.ReadLine();
                switch(userInput)
                {
                    case "1":
                        ErrorCode ec = ErrorCode.None;
                        Console.WriteLine("Input command:");
                        userInput = Console.ReadLine();
                        ec = writerLI7000.Write(Encoding.Default.GetBytes(userInput), 2000, out bytesWritten);

                        break;
                    case "2":
                        return;
                    default:
                        Console.WriteLine("Invalid Command");
                        break;
                }
                
            }
        }
  • Is the problem converting a Windows error code into english? - https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- – Dave S Jun 13 '23 at 22:38
  • @DaveS No the problem is when I run it the writer returns "Win32 error", but I can't figure out which Win32 error it is. – Zach Hall Jun 14 '23 at 16:47

0 Answers0