【问题标题】:C# and HID without windows dlls没有 Windows dll 的 C# 和 HID
【发布时间】:2015-05-18 23:09:14
【问题描述】:

我正在使用 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 发行版),我相信 Linux 将无法处理这些调用,因为它基于 Windows 操作系统。

有没有其他方法可以在不使用这些 dll 的情况下将 HID 设备与 C# 集成?或者你知道如何实现这一点吗?

【问题讨论】:

标签: c# .net dll mono hid


【解决方案1】:

您需要做的是想出一个通用界面,该界面适用于您将定位的所有 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2011-11-02
    • 2011-07-24
    • 2021-08-27
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多