c# 调用c++类库控制usb继电器

摘要:
在Internet上找不到调用此类库的文章。简短地写下来以备将来使用。以下是封装的类publicclassUsbRelayDeviceHelper{///<summary>///USBRelayLibrary///</summary>////<returns>ThisfunctionreturnsOnsuccesssand-1错误。</r

网上找不到调用此类库的文章,简单写一下,以备后用。

下面是封装后的调用c++类库的类

    public class UsbRelayDeviceHelper
    {
        /// <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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Exit();

        /// <summary>  
        /// Enumerate the USB Relay Devices.  
        /// </summary>  
        /// <returns></returns>  
        [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr usb_relay_device_enumerate();
        public static UsbRelayDeviceInfo Enumerate()
        {
            IntPtr x = UsbRelayDeviceHelper.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", CallingConvention = CallingConvention.Cdecl)]
        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, CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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", CallingConvention = CallingConvention.Cdecl)]
        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;

        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
    }

查找并打开控制器

            info=UsbRelayDeviceHelper.Enumerate();
            if (info == null){
                MessageBox.Show("没有找到控制器");
            }
            else { state = UsbRelayDeviceHelper.Open(info); }

打开第一路继电器,第二个参数表示第几路

 UsbRelayDeviceHelper.OpenOneRelayChannel(state, 1);

关闭第一路继电器,第二个参数表示第几路

UsbRelayDeviceHelper.CloseOneRelayChannel(state, 1);

退出时记得关闭释放,否则再次调用会失败。

UsbRelayDeviceHelper.Close();
UsbRelayDeviceHelper.Exit();

免责声明:文章转载自《c# 调用c++类库控制usb继电器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用 dumi 打包 React 组件库并生成文档站点时间不同步导致的nova,cinder服务一会up一会down的来回跳跃下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C 工具库8:map

本篇介绍另外一个在C++ stl中常用的容器map.我打算将map的实现容器和map接口分开,创建map的时候可以传递一个实现了interface_map_container接口的对象指针进来,如果这个参数传0,则默认使用红黑树做实际的容器.这样做的好处是用户可以根据性能需求传递自己定制的容器类.例如在游戏程序中常见的数据表.一般通过一个索引查询,并且在程...

cJSON 使用详解

由于c语言中,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json。如果有对应的数据结构就方便一些, 如python中用json.loads(json)就把json字符串转变为内建的数据结构处理起来比较方便。     cjson库文件下载:     sourceforge地址     一个重要概念:         在cjson中,js...

tinyhttp 安装和使用 (代码阅读前期准备)

系统:ubuntu14.04 知识准备: <计算机网路第五版>   (美)特南鲍姆   7.3节 万维网    一:下载安装包  tinyhttpd-0.1.0 .tar.gz 二:解压 tar -zxvf tinyhttpd-0.1.0 .tar.gz 三:安装 cd tinyhttpd-0.1.0 make 出现报错 /usr/inclu...

C++中extern关键字用法小结

总结C++中关于extern关键字的用法。 1.变量的生明和定义中 C++语言支持分离式编译机制,该机制允许将程序分割为若干个文件,每个文件可被独立编译。为了将程序分为许多文件,则需要在文件中共享代码,例如一个文件的代码可能需要另一个文件中中定义的变量。 为了支持分离式编译,C++允许将声明和定义分离开来。变量的声明规定了变量的类型和名字,即使一个名字为程...

c语言中typeof关键字

为什么因为一个关键字而专门写一篇随笔呢?因为这个关键字在Linux里面地位非同一般!这个关键字typeof在linux内核里面使用非常广泛! 下面这两个等效声明,用于声明int类弄的变量atypeof(int) a;typeof('b') a; //相当于获取'b'的类型,定义一个变量a下面用于声明指针和数组typeof(int*) p1,p2;typeo...

EasyPlayerPro(Windows)流媒体播放器开发之跨语言调用

下面我们来讲解一下关于EasyPlayerPro接口的调用,主要分为C++和C#两种语言,C++也可以基于VC和QT进行开发,C++以VC MFC框架为例进行讲解,C#以Winform框架为例进行讲解。 VC开发EasyPlayerPro 首先建一个基于MFC Dialog的工程,取名叫EasyPlayerPro,关于界面逻辑的处理过程就不做过多赘述...