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 = )]
- 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 = ,
- TwoChannel = ,
- FourChannel = ,
- EightChannel =
- }
- }
C# 调用继电器api usb_relay_device.dll的更多相关文章
- Unity在Android和iOS中如何调用Native API
本文主要是对unity中如何在Android和iOS中调用Native API进行介绍. 首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调 ...
- C#调用windows API的一些方法
使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1. 直接调用从 DLL 导出的函数. 2. ...
- C#调用Windows API函数截图
界面如下: 下面放了一个PictureBox 首先是声明函数: //这里是调用 Windows API函数来进行截图 //首先导入库文件 [System.Runtime.InteropServices ...
- 【转】用C#调用Windows API向指定窗口发送
一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...
- c++builder调用VC的dll以及VC调用c++builder的dll
解析__cdecl,__fastcall, __stdcall 的不同:在函数调用过程中,会使用堆栈,这三个表示不同的堆栈调用方式和释放方式. 比如说__cdecl,它是标准的c方法的堆栈调用方式,就 ...
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)
转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...
- C#动态调用C++编写的DLL函数
C#动态调用C++编写的DLL函数 动态加载DLL需要使用Windows API函数:LoadLibrary.GetProcAddress以及FreeLibrary.我们可以使用DllImport在C ...
- C#中调用Windows API的要点 .
介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...
- 使用C#调用windows API入门
一:入门,直接从 C# 调用 DLL 导出 其实我们的议题应该叫做C#如何直接调用非托管代码,通常有2种方法: 1. 直接调用从 DLL 导出的函数. 2. 调用 COM 对象上的接口方法 我 ...
随机推荐
- webp图片实践之路(转载)
最近,我们在项目中实践了webp图片,并且抽离出了工具模块,整合到了项目的基础模板中.传闻IOS10也将要支持webp,那么使用webp带来的性能提升将更加明显.估计在不久的将来,webp会成为标配. ...
- [PKUWC2019]Day1 T2 你和虚树的故事
选择k个颜色,使得颜色的虚树有交的方案数 肯定要考虑连通块的贡献. 法一 https://www.cnblogs.com/xzz_233/p/10292983.html 枚举连通块还是不可行的. 枚举 ...
- PMP是什么,PMP最难的是哪些内容?
目前在国内很多人还不了解PMP是什么,甚至不知道PMP认证的内容是哪些,考来有什么用,今天我这边就普及一下. PMP认证是美国项目管理协会发起的一项针对项目管理专业人士资格认证.取得认证需要学习项目管 ...
- 转载:Centos升级gcc
一.检查centos 里面是否安装了gcc g++ 输入命令:rpm -qa|grep gcc*有看到就出来gcc的东西就是装了没有的话就yum install gcc* -y 二.升级gcc 对于C ...
- 如何在Linux中使用命令行卸载软件
您可以使用“dpkg”命令来查看您的计算机,按“Ctrl + Alt + T”的所有已安装包的列表,打开一个终端窗口. 在提示符下键入以下命令,然后按Enter键.dpkg -- list 要卸载程序 ...
- python之路(8)常用模块
目录 os模块 sys模块 json模块 pickle模块 xml模块 re模块 logging模块 configparser模块 hashlib模块 time模块 random模块 subproce ...
- java 代码
java 里的 pandas tablesaw DataFrame 再有就是 spark 了 java 代码规范 Java8特性详解 lambda表达式 Stream Sonar 规则检测 sprin ...
- PMP知识点(三)——挣值计算汇总表
在新标签页打开. 附参考图 资料地址:http://pan.baidu.com/s/1bMNroq
- sketch格式文件转换成psd
在做响应式页面的时间需要把px单位转换成rem才可以,但是sketch文件的格式不能随意转换成rem,最高只能到CSS rem 16px,不能满足我们的需求,因此需要一个工具来转换成psd格式文件,他 ...
- java操作mongodb & springboot整合mongodb
简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...