我得到了需要编程的USB继电器,以便在指定的时间它将打开一段时间,然后关闭。我试图集成提供给我的DLL,但无法做到(或者我做错了)。也试图找到一些指南,但没有成功。
USB继电器:Songle srd-05vdc-sl-c
我正在使用VS 2013,并且正在尝试制作简单的C#应用程序。另外,也许还有其他问题,因为HIDAPI GUI应用程序未将我的中继显示为已连接的设备。如果发生任何更改,我将坐在Windows 8上。
由于您的DLL是带有导出功能的C ++或C库,因此您可以使用平台调用来调用dll中的方法。
使用注释(mirror-16.06.2015)中提供的文件,这就是包装器的外观。可能存在错误,因此请像对待示例一样对待。我基于头文件中的注释添加了XML注释。
请注意,要使其正常工作,您需要将usb_relay_device.dll
与C#可执行文件放在同一文件夹中。最好的方法是将文件添加到项目中,然后在VS的解决方案资源管理器中将“ 复制到输出目录”设置为总是复制或如果较新则复制。
Note:如注释中所述,如果该示例引发有关堆栈不平衡的错误,请将CallingConvention = CallingConvention.Cdecl
添加到DllImport
属性,或尝试另一个calling convention。
using System;
using System.Runtime.InteropServices;
namespace UsbRelay
{
public static class UsbRelayDevice
{
/// <summary>
/// Init the USB Relay Libary
/// </summary>
/// <returns>This function returns 0 on success and -1 on error.</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_init")]
public static extern int Init();
/// <summary>
/// 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.
/// </summary>
/// <returns>This function returns 0 on success and -1 on error.</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_exit")]
public static extern int Exit();
/// <summary>
/// Enumerate the USB Relay Devices.
/// </summary>
/// <returns></returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate")]
public static extern UsbRelayDeviceInfo Enumerate();
/// <summary>
/// Free an enumeration Linked List
/// </summary>
/// <param name="deviceInfo"></param>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_free_enumerate")]
public static extern void FreeEnumerate(UsbRelayDeviceInfo deviceInfo);
/// <summary>
/// Open device that serial number is serialNumber
/// </summary>
/// <param name="serialNumber"></param>
/// <param name="stringLength"></param>
/// <returns>This funcation returns a valid handle to the device on success or NULL on failure.</returns>
[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);
/// <summary>
/// Open a usb relay device
/// </summary>
/// <param name="deviceInfo"></param>
/// <returns>This funcation returns a valid handle to the device on success or NULL on failure.</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open")]
public static extern int Open(UsbRelayDeviceInfo deviceInfo);
/// <summary>
/// Close a usb relay device
/// </summary>
/// <param name="hHandle"></param>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close")]
public static extern void Close(int hHandle);
/// <summary>
/// open a relay channel on the USB-Relay-Device
/// </summary>
/// <param name="hHandle">Which usb relay device your want to operate</param>
/// <param name="index">Which channel your want to open</param>
/// <returns>0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_one_relay_channel")]
public static extern int OpenOneRelayChannel(int hHandle, int index);
/// <summary>
/// open all relay channel on the USB-Relay-Device
/// </summary>
/// <param name="hHandle">which usb relay device your want to operate</param>
/// <returns>0 -- success; 1 -- error</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_all_relay_channel")]
public static extern int OpenAllRelayChannels(int hHandle);
/// <summary>
/// close a relay channel on the USB-Relay-Device
/// </summary>
/// <param name="hHandle">which usb relay device your want to operate</param>
/// <param name="index">which channel your want to close</param>
/// <returns>0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_one_relay_channel")]
public static extern int CloseOneRelayChannel(int hHandle, int index);
/// <summary>
/// close all relay channel on the USB-Relay-Device
/// </summary>
/// <param name="hHandle">hich usb relay device your want to operate</param>
/// <returns>0 -- success; 1 -- error</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_all_relay_channel")]
public static extern int CloseAllRelayChannels(int hHandle);
/// <summary>
/// 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
/// </summary>
/// <param name="hHandle"></param>
/// <param name="status"></param>
/// <returns>0 -- success; 1 -- error</returns>
[DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_get_status")]
public static extern int GetStatus(int hHandle, ref int status);
}
/// <summary>
/// USB relay board info structure
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=8)]
public class UsbRelayDeviceInfo
{
[MarshalAs(UnmanagedType.LPStr)]
public string SerialNumber;
[MarshalAs(UnmanagedType.LPStr)]
public string DevicePath;
public UsbRelayDeviceType Type;
public UsbRelayDeviceInfo Next;
}
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;
}