3

I'm developing a supervisory system with C# and it connects to some instruments through a RS485 controller. This controller was made with a microship processor and Windows OS recognize it as a HID Device (USB plug and play). That said, I developed using the Windows dll functions (hid.dll), as ilustrated in the code block below:

    [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);

Today, I'm studing a port of this application to a Linux OS (Debian distro) with Mono and I believe Linux won't work with these calls due its Windows OS based.

Is there any other way to integrate HID devices with C# without using these dlls? Or even do you have any clue how to achieve this?

tgmoura
  • 70
  • 10
  • I would start poking around UHID. https://dvdhrm.wordpress.com/2012/07/16/uhid-user-space-hid-io-drivers/ – dna Mar 17 '15 at 09:36
  • Hi @dna, thanks for your help.. I found an interesting lib on this link: http://stackoverflow.com/questions/2803890/net-api-for-hid-usb and I'm trying to use it now. – tgmoura Mar 18 '15 at 19:31
  • The lib I'm using is http://www.zer7.com/software/hidsharp – tgmoura Mar 18 '15 at 20:10

1 Answers1

2

What you need to do is come up with a common interface that sits across all Hid platforms that you will be targeting. If you come up with a common interface, you can implement the interface on each platform - or wrap libraries that have already been written for those platforms. This is how you can achieve the rudiments of dependency injection (https://en.wikipedia.org/wiki/Dependency_injection).

The API calls that you have mentioned are great for Windows, but will be totally different on other platforms.

Here is a basic example from my library (https://github.com/MelbourneDeveloper/Device.Net/blob/main/src/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();
}

This library already supports Windows, Android, and UWP. Here is an example implementation using some of the APIs you have mentioned: https://github.com/MelbourneDeveloper/Device.Net/blob/main/src/Hid.Net/Windows/WindowsHidApiService.cs

Also, this older Hid library supports Linux but doesn't use task based async. Perhaps you could grab the Linux code from there.

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

Dai
  • 141,631
  • 28
  • 261
  • 374
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103