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

usb_relay_device.dll 为C++编写

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. namespace USBRelayTest
  8. {
  9. public class UsbRelayDeviceHelper
  10. {
  11. /// <summary>
  12. /// Init the USB Relay Libary
  13. /// </summary>
  14. /// <returns>This function returns 0 on success and -1 on error.</returns>
  15. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_init", CallingConvention = CallingConvention.Cdecl)]
  16. public static extern int Init();
  17.  
  18. /// <summary>
  19. /// Finalize the USB Relay Libary.
  20. /// This function frees all of the static data associated with
  21. /// USB Relay Libary. It should be called at the end of execution to avoid
  22. /// memory leaks.
  23. /// </summary>
  24. /// <returns>This function returns 0 on success and -1 on error.</returns>
  25. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_exit", CallingConvention = CallingConvention.Cdecl)]
  26. public static extern int Exit();
  27.  
  28. /// <summary>
  29. /// Enumerate the USB Relay Devices.
  30. /// </summary>
  31. /// <returns></returns>
  32. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate", CallingConvention = CallingConvention.Cdecl)]
  33. public static extern IntPtr usb_relay_device_enumerate();
  34. public static UsbRelayDeviceInfo Enumerate()
  35. {
  36. IntPtr x = UsbRelayDeviceHelper.usb_relay_device_enumerate();
  37. UsbRelayDeviceInfo a = (UsbRelayDeviceInfo)Marshal.PtrToStructure(x, typeof(UsbRelayDeviceInfo));
  38. return a;
  39. }
  40.  
  41. /// <summary>
  42. /// Free an enumeration Linked List
  43. /// </summary>
  44. /// <param name="deviceInfo"></param>
  45. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_free_enumerate", CallingConvention = CallingConvention.Cdecl)]
  46. public static extern void FreeEnumerate(UsbRelayDeviceInfo deviceInfo);
  47.  
  48. /// <summary>
  49. /// Open device that serial number is serialNumber
  50. /// </summary>
  51. /// <param name="serialNumber"></param>
  52. /// <param name="stringLength"></param>
  53. /// <returns>This funcation returns a valid handle to the device on success or NULL on failure.</returns>
  54. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_with_serial_number", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  55. public static extern int OpenWithSerialNumber([MarshalAs(UnmanagedType.LPStr)] string serialNumber, int stringLength);
  56.  
  57. /// <summary>
  58. /// Open a usb relay device
  59. /// </summary>
  60. /// <param name="deviceInfo"></param>
  61. /// <returns>This funcation returns a valid handle to the device on success or NULL on failure.</returns>
  62. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open", CallingConvention = CallingConvention.Cdecl)]
  63. public static extern int Open(UsbRelayDeviceInfo deviceInfo);
  64.  
  65. /// <summary>
  66. /// Close a usb relay device
  67. /// </summary>
  68. /// <param name="hHandle"></param>
  69. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close", CallingConvention = CallingConvention.Cdecl)]
  70. public static extern void Close(int hHandle);
  71.  
  72. /// <summary>
  73. /// open a relay channel on the USB-Relay-Device
  74. /// </summary>
  75. /// <param name="hHandle">Which usb relay device your want to operate</param>
  76. /// <param name="index">Which channel your want to open</param>
  77. /// <returns>0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device</returns>
  78. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_one_relay_channel", CallingConvention = CallingConvention.Cdecl)]
  79. public static extern int OpenOneRelayChannel(int hHandle, int index);
  80.  
  81. /// <summary>
  82. /// open all relay channel on the USB-Relay-Device
  83. /// </summary>
  84. /// <param name="hHandle">which usb relay device your want to operate</param>
  85. /// <returns>0 -- success; 1 -- error</returns>
  86. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_all_relay_channel", CallingConvention = CallingConvention.Cdecl)]
  87. public static extern int OpenAllRelayChannels(int hHandle);
  88.  
  89. /// <summary>
  90. /// close a relay channel on the USB-Relay-Device
  91. /// </summary>
  92. /// <param name="hHandle">which usb relay device your want to operate</param>
  93. /// <param name="index">which channel your want to close</param>
  94. /// <returns>0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device</returns>
  95. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_one_relay_channel", CallingConvention = CallingConvention.Cdecl)]
  96. public static extern int CloseOneRelayChannel(int hHandle, int index);
  97.  
  98. /// <summary>
  99. /// close all relay channel on the USB-Relay-Device
  100. /// </summary>
  101. /// <param name="hHandle">hich usb relay device your want to operate</param>
  102. /// <returns>0 -- success; 1 -- error</returns>
  103. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_all_relay_channel", CallingConvention = CallingConvention.Cdecl)]
  104. public static extern int CloseAllRelayChannels(int hHandle);
  105.  
  106. /// <summary>
  107. /// status bit: High --> Low 0000 0000 0000 0000 0000 0000 0000 0000, one bit indicate a relay status.
  108. /// the lowest bit 0 indicate relay one status, 1 -- means open status, 0 -- means closed status.
  109. /// bit 0/1/2/3/4/5/6/7/8 indicate relay 1/2/3/4/5/6/7/8 status
  110. /// </summary>
  111. /// <param name="hHandle"></param>
  112. /// <param name="status"></param>
  113. /// <returns>0 -- success; 1 -- error</returns>
  114. [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_get_status", CallingConvention = CallingConvention.Cdecl)]
  115. public static extern int GetStatus(int hHandle, ref int status);
  116. }
  117.  
  118. /// <summary>
  119. /// USB relay board info structure
  120. /// </summary>
  121. [StructLayout(LayoutKind.Sequential, Pack = )]
  122. public class UsbRelayDeviceInfo
  123. {
  124. [MarshalAs(UnmanagedType.LPStr)]
  125. public string SerialNumber;
  126.  
  127. public IntPtr DevicePath { get; set; }
  128.  
  129. public UsbRelayDeviceType Type { get; set; }
  130.  
  131. public IntPtr Next { get; set; }
  132. }
  133.  
  134. public enum UsbRelayDeviceType
  135. {
  136. OneChannel = ,
  137. TwoChannel = ,
  138. FourChannel = ,
  139. EightChannel =
  140. }
  141. }

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

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

    本文主要是对unity中如何在Android和iOS中调用Native API进行介绍. 首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调 ...

  2. C#调用windows API的一些方法

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...

  3. C#调用Windows API函数截图

    界面如下: 下面放了一个PictureBox 首先是声明函数: //这里是调用 Windows API函数来进行截图 //首先导入库文件 [System.Runtime.InteropServices ...

  4. 【转】用C#调用Windows API向指定窗口发送

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  5. c++builder调用VC的dll以及VC调用c++builder的dll

    解析__cdecl,__fastcall, __stdcall 的不同:在函数调用过程中,会使用堆栈,这三个表示不同的堆栈调用方式和释放方式. 比如说__cdecl,它是标准的c方法的堆栈调用方式,就 ...

  6. MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...

  7. C#动态调用C++编写的DLL函数

    C#动态调用C++编写的DLL函数 动态加载DLL需要使用Windows API函数:LoadLibrary.GetProcAddress以及FreeLibrary.我们可以使用DllImport在C ...

  8. C#中调用Windows API的要点 .

    介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...

  9. 使用C#调用windows API入门

    一:入门,直接从 C# 调用 DLL 导出   其实我们的议题应该叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2.  调用 COM 对象上的接口方法 我 ...

随机推荐

  1. webp图片实践之路(转载)

    最近,我们在项目中实践了webp图片,并且抽离出了工具模块,整合到了项目的基础模板中.传闻IOS10也将要支持webp,那么使用webp带来的性能提升将更加明显.估计在不久的将来,webp会成为标配. ...

  2. [PKUWC2019]Day1 T2 你和虚树的故事

    选择k个颜色,使得颜色的虚树有交的方案数 肯定要考虑连通块的贡献. 法一 https://www.cnblogs.com/xzz_233/p/10292983.html 枚举连通块还是不可行的. 枚举 ...

  3. PMP是什么,PMP最难的是哪些内容?

    目前在国内很多人还不了解PMP是什么,甚至不知道PMP认证的内容是哪些,考来有什么用,今天我这边就普及一下. PMP认证是美国项目管理协会发起的一项针对项目管理专业人士资格认证.取得认证需要学习项目管 ...

  4. 转载:Centos升级gcc

    一.检查centos 里面是否安装了gcc g++ 输入命令:rpm -qa|grep gcc*有看到就出来gcc的东西就是装了没有的话就yum install gcc* -y 二.升级gcc 对于C ...

  5. 如何在Linux中使用命令行卸载软件

    您可以使用“dpkg”命令来查看您的计算机,按“Ctrl + Alt + T”的所有已安装包的列表,打开一个终端窗口. 在提示符下键入以下命令,然后按Enter键.dpkg -- list 要卸载程序 ...

  6. python之路(8)常用模块

    目录 os模块 sys模块 json模块 pickle模块 xml模块 re模块 logging模块 configparser模块 hashlib模块 time模块 random模块 subproce ...

  7. java 代码

    java 里的 pandas tablesaw DataFrame 再有就是 spark 了 java 代码规范 Java8特性详解 lambda表达式 Stream Sonar 规则检测 sprin ...

  8. PMP知识点(三)——挣值计算汇总表

    在新标签页打开. 附参考图 资料地址:http://pan.baidu.com/s/1bMNroq

  9. sketch格式文件转换成psd

    在做响应式页面的时间需要把px单位转换成rem才可以,但是sketch文件的格式不能随意转换成rem,最高只能到CSS rem 16px,不能满足我们的需求,因此需要一个工具来转换成psd格式文件,他 ...

  10. java操作mongodb & springboot整合mongodb

    简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...