Getting timed-out sometimes when reading 16-bit joystick/hardware using HidSharp

27 views Asked by At

I currently have a problem dealing with a 16-bit joystick here using HidSharp. Sometimes it gives me a timeout exception, sometimes it does not. Basically, trying to read the input of the joystick's buttons/axes is inconsistent. I'm trying to make it consistent.

Here is my code below:

static void Main()
{
    var list = DeviceList.Local;
    
    var joystick = list.GetHidDeviceOrNull(productID: 12345);

    if (joystick != null)
    {
        using (var stream = joystick.Open())
        {
            var inputReportBuffer = new byte[joystick.GetMaxInputReportLength()];
            while (true)
            {
                try
                {
                    int length = stream.Read(inputReportBuffer, 0, inputReportBuffer.Length);
                    Console.Write($"Worked!\n");
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Console.Write($"Crashed!\n");
                    Console.WriteLine($"Exception: {ex.Message}\n{ex.StackTrace}");
                }

            }
        }
    }
    else
    {
        Console.WriteLine("Joystick not found.");
    }
}

The timeout happens at the line int length = stream.Read(inputReportBuffer, 0, inputReportBuffer.Length); where it returns byte[64] and times out my code or any number that is not 64 here and it works, somehow. It makes the code inconsistent.

Here is the exception message and stack trace that I get (It's not accurate due to the structure of the code above but this is what I'm getting from it):

Exception: Operation timed out.
at HidSharp.Platform.Windows.NativeMethods. OverlappedOperation (IntPtr ioHandle, IntPtr eventHandle, Int32 eventTimeout, IntPtr closeEventHandle, Boolean overlapResult, NativeOverlapped overlapped, UInt32& bytesTransferred)
at HidSharp.Platform.Windows.WinHidStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at Program. RunProgram() in \16BitHidSharp\Program.cs:line 39
at Program.Main() in \16BitHidSharp\Program.cs:line 12

Any help is appreciated.

I was expecting the program would work just fine until I keep getting either time-outs or not. This code is working fine with 8-bit joysticks but not consistently working with 16-bit joysticks.

0

There are 0 answers