C# 调用继电器api usb_relay_device.dll

C# 调用继电器api usb_relay_device.dll 代码封装

usb_relay_device.dll 为C++编写

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace USBRelayTest
{
    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
    }
}
时间: 2024-11-06 14:23:32

C# 调用继电器api usb_relay_device.dll的相关文章

Unity在Android和iOS中如何调用Native API

本文主要是对unity中如何在Android和iOS中调用Native API进行介绍. 首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调用.利用这一特性,可以扩展unity的功能.例如集成和调用第三方库.同时为了满足对unity接口的一致性,可以考虑在android和iOS上提供相同的接口供C#调用. 这里列举以下两个例子. 1. 1. 以弹出一个覆盖部分屏幕的webview为例来说明如何从C#调用Native接口. 2. 2. 简

名词(A)API与DLL,GUI

API(应用程序编程接口): API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节. 分类:Windows API和linux API---用户编程接口API遵循了UNIX中最流行的应用编程界面标准---POSIX标准. API 声明:API函数包含在位于系统目录下的DLL文件中. 程序功能: 远程过程调用(RPC):通过作

实例365(2)---------调用系统api修改系统时间

一:截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace SetDate { public partial cl

Qt Creator调用VS2008生成的DLL注意事项 good

问题:生成的dll文件QT无法静态/隐式调用 分析:调用的lib库可能是msvc编译的,而我用Qt调用,Qt默认编译器是minGW,两种编译器生成的函数名不一样,所以调用的时候你要用哪个函数,编译结果肯定显示这个函数未定义! 解决1:用VS2008生成DLL文件时,采用__declspec方式导出函数,不要使用def模块文件,这时dll中的函数名称[email protected]规则命名 解决2:函数的调用约定改为__cdecl 导出函数的调用约定和使用这个函数时声明的调用约定必须一致,否则程

C#调用windows API的一些方法

使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2.  调用 COM 对象上的接口方法 我主要讨论从dll中导出函数,基本步骤如下: 1.使用 C# 关键字 static 和 extern 声明方法. 2.将 DllImport 属性附加到该方法.DllImport 属性允许您指定包含该方法的 DLL 的名称. 3.如果需要,为方法的参数和返回值指定

VBS调用windows api函数(postmessage)实现后台发送按键脚本

'=========================================================================='' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0'' NAME: '' AUTHOR: Microsoft , Microsoft' DATE : 2014/8/10'' COMMENT: ''===================定义变量,注册

C#程序调用C++写的dll传递string出现bad ptr

本来是做C/C++的,因为项目需要,所以才搞的C#,说实话,很鄙视做C#的,总结起来,扯淡的DllImport,有本事别用这破玩意,看你C#还能干啥? 参考网上的按照下面的方式来,结果在C++的dll库中打断点,第二个参数怎么都是bad ptr,郁闷至极,耗费了两天的功夫,都没有搞定,也参考了:http://blog.csdn.net/yongshengsilingsa/article/details/7917877 的文章,也无济于事.最后就索性自己再写个简单的dll和exe,只有一个接口,调

用C#调用Windows API向指定窗口发送按键消息

一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式:[DllImport("DLL文件")]方法的声明; [DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("

C#调用WINDOWS API 示例

一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式:[DllImport("DLL文件")]方法的声明; [DllImport("user32.dll")]private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("