C#封装USB继电器C++操作类

要控制的东西:


淘宝上面有很多,1路到8路都有。可以通过控制继电器的开关来实现电路管理。

有C++调用的示例。

以下代码百度来的,C#封装了调用的代码:

namespace usb_relay_device
{
    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 IntPtr usb_relay_device_enumerate();
        public static UsbRelayDeviceInfo Enumerate()
        {
            IntPtr x = UsbRelayDevice.usb_relay_device_enumerate();
            UsbRelayDeviceInfo a = (UsbRelayDeviceInfo)Marshal.PtrToStructure(x, typeof(UsbRelayDeviceInfo));
            return a;
        }

        /// <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;// { get; set; }

        //[MarshalAs(UnmanagedType.LPStr)]
        public IntPtr DevicePath{ get; set; }

        public UsbRelayDeviceType Type { get; set; }

        public IntPtr Next { get; set; }
    }

    public enum UsbRelayDeviceType
    {
        OneChannel = 1,
        TwoChannel = 2,
        FourChannel = 4,
        EightChannel = 8
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello World,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值