繁体   English   中英

没有Windows dll的C#和HID

[英]C# and HID without windows dlls

我正在用C#开发一个监控系统,它通过一个RS485控制器连接到一些仪器。 该控制器采用微处理器制造,Windows操作系统将其识别为HID设备(USB即插即用)。 也就是说,我开发使用Windows DLL函数(hid.dll),如下面的代码块中所示:

    [DllImport("hid.dll", SetLastError = true)]
    public static extern void HidD_GetHidGuid(ref Guid hidGuid);

    [DllImport("hid.dll", SetLastError = true)]
    public static extern bool HidD_GetNumInputBuffers(SafeFileHandle hidDeviceObject, ref Int32 numberBuffers);

    [DllImport("hid.dll", SetLastError = true)]
    public static extern bool HidD_GetPreparsedData(SafeFileHandle hidDeviceObject, ref IntPtr preparsedData);

今天,我正在使用Mono研究这个应用程序的端口到Linux操作系统(Debian发行版),我相信由于基于Windows操作系统,Linux无法使用这些调用。

有没有其他方法可以在不使用这些dll的情况下将HID设备与C#集成? 或者你是否有任何线索如何实现这一目标?

您需要做的是提供一个通用界面,该界面位于您将要定位的所有Hid平台上。 如果您想出一个通用接口,您可以在每个平台上实现该接口 - 或者包装已经为这些平台编写的库。 这就是你如何实现依赖注入的基础( https://en.wikipedia.org/wiki/Dependency_injection )。

您提到的API调用非常适合Windows,但在其他平台上则完全不同。

这是我的库中的一个基本示例( https://github.com/MelbourneDeveloper/Hid.Net/blob/master/Hid.Net/IHidDevice.cs ):

   public interface IHidDevice : IDisposable
        {
            /// <summary>
            /// Occurs after the device has successfully connected. Note: this can be called multiple times and will occurr every time  InitializeAsync is called successfull. 
            /// </summary>
            event EventHandler Connected;

            /// <summary>
            /// Placeholder. This is not currently being used. Please do not rely on this at the moment.
            /// </summary>
            event EventHandler Disconnected;

            /// <summary>
            /// Checks to see if the device has been successfully connected. Note: check the implementation to see if this method is actually asking the device whether it is still connected or not
            /// </summary>
            Task<bool> GetIsConnectedAsync();

            /// <summary>
            /// Read a page of data
            /// </summary>
            Task<byte[]> ReadAsync();

            /// <summary>
            /// Write a page of data
            /// </summary>
            Task WriteAsync(byte[] data);

            /// <summary>
            /// The device's Vendor Id
            /// </summary>
            int VendorId { get; }

            /// <summary>
            /// The device's Product Id
            /// </summary>
            int ProductId { get; }

            /// <summary>
            /// Dispose of any existing connections and reinitialize the device
            /// </summary>
            Task InitializeAsync();
    }

该库已经支持Windows,Android和UWP。 以下是使用您提到的一些API的示例实现: https//github.com/MelbourneDeveloper/Hid.Net/blob/master/Hid.Net/Windows/WindowsHIDDevice.cs

此外,这个较旧的Hid库支持Linux,但不使用基于任务的异步。 也许你可以从那里获取Linux代码。

https://github.com/treehopper-electronics/HIDSharp/tree/master/HidSharp/Platform/Linux

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM