Closed wankey closed 9 months ago
Hey,
Which operating system are you using?
Can you try to run this code in a minimal console app without using any threads and show the code here?
If the problem persists in your minimal sample I would recommend writing a minimal C application as I would assume the same problem is present there.
MacOS 14.3.1 and Windows 11 (22H2) both occurred problem.
run without thread also occurred.
I will try writing a c application, but it's seems maybe a dot net runtime error caused by MemoryMarshal.GetReference(data) in Read METHOD, i'm not sure.
UPDATE: I recreate a minimal sample project just running Read from certain device, It works fine. I don't know what's happen and why.
I have one idea which could be verified. Please do the following:
Internal/NativeMethods.cs
file with the following starting in line 49:public static unsafe int Read(DeviceSafeHandle device, ReadOnlySpan<byte> data)
{
fixed (byte* ptr = data)
{
return Read(device, ptr, (nuint) data.Length);
}
}
Internal/NativeMethods.cs
file with the following in Line 123/124[LibraryImport(Library, EntryPoint = "hid_read")]
private static unsafe partial int Read(DeviceSafeHandle device, byte* data, nuint length);
Internal/NativeMethods.Net6.cs
file with the following in Line 32/33[DllImport(Library, EntryPoint = "hid_read")]
private static extern unsafe int Read(DeviceSafeHandle device, byte* data, nuint length);
ProjectRefernece
in your application csproj
fileI have one idea which could be verified. Please do the following:
- Clone the HidApi.Net repository on your development machine
- Update the
Internal/NativeMethods.cs
file with the following starting in line 49:public static unsafe int Read(DeviceSafeHandle device, ReadOnlySpan<byte> data) { fixed (byte* ptr = data) { return Read(device, ptr, (nuint) data.Length); } }
- Update the
Internal/NativeMethods.cs
file with the following in Line 123/124[LibraryImport(Library, EntryPoint = "hid_read")] private static unsafe partial int Read(DeviceSafeHandle device, byte* data, nuint length);
- Update the
Internal/NativeMethods.Net6.cs
file with the following in Line 32/33[DllImport(Library, EntryPoint = "hid_read")] private static extern unsafe int Read(DeviceSafeHandle device, byte* data, nuint length);
- Compile the HidApi.Net project
- Include the HidApi.Net project as a
ProjectRefernece
in your applicationcsproj
file- Verify if the problem persists
I have make a sample to try this, but the problem persists.
after some test, maybe the problem is not caused by HidApi.Net.
private void UpdateDataCommunicationThread(Device device)
{
var childThread = new Thread(_readDataFromDevice);
childThread.Start(device);
}
the call method as below works fine. but if uncomment these code,first code especially, the problem persists again. I'm not familiar with c#, so don't known what's happened here.
var device = hid.ConnectToDevice();
// var controller = new Controller(device)
// {
// isConnected = true
// };
// _connectedControllers.Add(controller);
// OnHidChangedListener?.OnHidChanged(true, controller);
UpdateDataCommunicationThread(device);
To help you I need some minimal compileable sample code. Some snippets don't help.
The lines you mentioned look like some custom code from your own project so the problem seems to be caused by your own code.
because it's not a library fault, so the issue closed.
thanks for you help
I'm seeing this issue also in my project when we have a situation where we have parallel read/write operation with the HID device. As we had no issues with another HID library I think it's not an application fault in my case. As it is exactly the same issue as above I decided to add my post to this already closed isse.
I wan't to change to this library in the future becuase our currently used HID library has some issues and the project is abandond. In addition I like the approach of using the hidapi library in behind as this works also on .NET on Linux (tested already witha Raspberry Pi).
I'm aware that debugging such an issue that happens on other systems is nearly impossible. So I'll investigate the issue myself or try to provide a demo project that allows reproduction.
The issue occured with the latest 1.0.3 release of the library. After I've found this issue here I did the steps you proposed above. I replaced the NuGet package with the code from the repository and changed the pointer handling as proposed. The issue did not disappear by this change.
Another thing I've tried is to block the read/write calls to the library so that no threding issue should occur.
My next step would be to simplify the case where it happens, as by now it requires our full application and a specific HID device that is not commercialy available.
Question: Do you know a good HID device that allows testing things? What devices are you using for tests? That will allow me to use the same devices. Any ideas or hints what to try are very welcome.
@AdrianEggenberger It is unlikely (but not impossible) that the issue is based in this library as I just call the native methods: No threading is involved by HidApi.Net.
Please be aware that HIDAPI is not thread safe.
So if you experience issues please create a minimal sample calling the C HIDAPI directly via PInvoke where the issue is not occurring to be sure that there is some issue which can be fixed as part of this project.
If needed I can try to assist you with my PInvoke knowledge but I cant write all the code for you or do the testing.
Then I can check what is different between your working sample and this project to fix the problem.
I don't have a specific test device. During my search for an app I found that there was no modern C# cross platform library available. So I wrote this library to be able to communicate with my Mouse under Linux. So my testing is extremely limited.
Any actionable feedback is very welcome to improve this library.
If you want to work on this I kindly ask you to open a new discussion and we can try to narrow down the problem there.
I see your point that the Hidapi.Net is probably not the reason for the issue. There is effectively not much more than the call to the native Library for the write or read. After some reading in the issues of the native hidapi library it could also be something related to the native library.
There someone also mentions that read and write calls need to be made from the same thread. As my code did only ensure that no read and write calls are made in parallel, I thought this might be the reason. So I gave this a try and modified my code to do all calls from one thread, but the result remained the same.
Anyhow I’ll try to find out more about the issue. As soon as I have more information, I’ll open a new issue to share my findings.
Thanx for your input.
Von: Marcel Tiede @.> Gesendet: Montag, 10. Juni 2024 18:23 An: badcel/HidApi.Net @.> Cc: Adrian Eggenberger @.>; Comment @.> Betreff: Re: [badcel/HidApi.Net] Fatal error. Internal CLR error. (0x80131506) when read data (Issue #84)
It is unlikely (but not impossible) that the issue is based in this library as I just call the native methods: No threading is involved by HidApi.Net.
Please be aware that HIDAPI is not thread safehttps://github.com/libusb/hidapi/issues/45.
So if you experience issues please create a minimal sample calling the C HIDAPI directly via PInvoke where the issue is not occurring to be sure that there is some issue which can be fixed as part of this project.
If needed I can try to assist you with my PInvoke knowledge but I cant write all the code for you or do the testing.
Then I can check what is different between your working sample and this project to fix the problem.
I don't have a specific test device. During my search for an app I found that there was no modern C# cross platform library available. So I wrote this library to be able to communicate with my Mouse under Linux. So my testing is extremely limited.
Any actionable feedback is very welcome to improve this library.
If you want to work on this I kindly ask you to open a new discussion and we can try to narrow down the problem there.
— Reply to this email directly, view it on GitHubhttps://github.com/badcel/HidApi.Net/issues/84#issuecomment-2158811542, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AGIDI52IPKLMF6OBCB3T5UDZGXHGRAVCNFSM6AAAAABJBXXTMKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJYHAYTCNJUGI. You are receiving this because you commented.Message ID: @.**@.>>
after long time running, Internal CLR error. occurred.
.net version: 8.0.101 and 6.0.321 BOTH Tried