USB中继DLL问题
我有USB继电器,我需要编程,以便在指定的时间它会打开一段时间然后关闭。 我试图整合提供给我的DLL,但是无法做到(或者我做错了)。 还试图找到一些指导如何做到这一点,也没有成功。
USB继电器:songle srd-05vdc-sl-c
我正在使用VS 2013,我正在尝试制作简单的C#应用程序。 也许还有其他错误,因为HIDAPI GUI应用程序不会将我的继电器显示为连接设备。 如果改变了什么,我坐在Windows 8上。
由于您的DLL是带有导出的C ++或C库,因此您可以使用平台调用来调用dll中的方法。
使用您在评论中提供的文件( 镜像 – 16.06.2015) ,这是您的包装器的样子。 可能有错误所以请将此视为一个例子。 我根据头文件中的注释添加了XML注释。
请注意,为了使其正常工作,您需要将usb_relay_device.dll
放在与C#可执行文件相同的文件夹中。 执行此操作的最佳方法是将文件添加到项目中,并在VS的解决方案资源管理器中将“ 复制到输出目录 ”设置为“ 始终复制”或“如果更新则复制” 。
using System; using System.Runtime.InteropServices; namespace UsbRelay { public static class UsbRelayDevice { /// /// Init the USB Relay Libary /// /// This function returns 0 on success and -1 on error. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_init")] public static extern int Init(); /// /// Finalize the USB Relay Libary. /// This function frees all of the static data associated with /// USB Relay Libary. It should be called at the end of execution to avoid /// memory leaks. /// /// This function returns 0 on success and -1 on error. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_exit")] public static extern int Exit(); /// /// Enumerate the USB Relay Devices. /// /// [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate")] public static extern UsbRelayDeviceInfo Enumerate(); /// /// Free an enumeration Linked List /// /// [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_free_enumerate")] public static extern void FreeEnumerate(UsbRelayDeviceInfo deviceInfo); /// /// Open device that serial number is serialNumber /// /// /// /// This funcation returns a valid handle to the device on success or NULL on failure. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_with_serial_number", CharSet = CharSet.Ansi)] public static extern int OpenWithSerialNumber([MarshalAs(UnmanagedType.LPStr)] string serialNumber, int stringLength); /// /// Open a usb relay device /// /// /// This funcation returns a valid handle to the device on success or NULL on failure. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open")] public static extern int Open(UsbRelayDeviceInfo deviceInfo); /// /// Close a usb relay device /// /// [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close")] public static extern void Close(int hHandle); /// /// open a relay channel on the USB-Relay-Device /// /// Which usb relay device your want to operate /// Which channel your want to open /// 0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_one_relay_channel")] public static extern int OpenOneRelayChannel(int hHandle, int index); /// /// open all relay channel on the USB-Relay-Device /// /// which usb relay device your want to operate /// 0 -- success; 1 -- error [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_all_relay_channel")] public static extern int OpenAllRelayChannels(int hHandle); /// /// close a relay channel on the USB-Relay-Device /// /// which usb relay device your want to operate /// which channel your want to close /// 0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_one_relay_channel")] public static extern int CloseOneRelayChannel(int hHandle, int index); /// /// close all relay channel on the USB-Relay-Device /// /// hich usb relay device your want to operate /// 0 -- success; 1 -- error [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_all_relay_channel")] public static extern int CloseAllRelayChannels(int hHandle); /// /// status bit: High --> Low 0000 0000 0000 0000 0000 0000 0000 0000, one bit indicate a relay status. /// the lowest bit 0 indicate relay one status, 1 -- means open status, 0 -- means closed status. /// bit 0/1/2/3/4/5/6/7/8 indicate relay 1/2/3/4/5/6/7/8 status /// /// /// /// 0 -- success; 1 -- error [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_get_status")] public static extern int GetStatus(int hHandle, ref int status); } /// /// USB relay board info structure /// [StructLayout(LayoutKind.Sequential, Pack=8)] public class UsbRelayDeviceInfo { [MarshalAs(UnmanagedType.LPStr)] public string SerialNumber { get; set; } [MarshalAs(UnmanagedType.LPStr)] public string DevicePath { get; set; } public UsbRelayDeviceType Type { get; set; } public UsbRelayDeviceInfo Next { get; set; } } public enum UsbRelayDeviceType { OneChannel = 1, TwoChannel = 2, FourChannel = 4, EightChannel = 8 } }
要在设备中打开中继通道1,您可以执行以下操作:
if (UsbRelayDevice.Init() != 0) { Console.WriteLine("Couldn't initialize!"); return; } string serial = "serial number here"; int deviceHandle = UsbRelayDevice.OpenWithSerialNumber(serial, serial.Length); int openResult = UsbRelayDevice.OpenOneRelayChannel(deviceHandle, 1); if (openResult == 1) { Console.WriteLine("Got error from OpenOneRelayChannel!"); return; } else if (openResult == 2) { Console.WriteLine("Index is out of range on the usb relay device"); return; }