C# 调用继电器api usb_relay_device.dll

时间:2021-01-01 01:22:04

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的更多相关文章

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

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

  2. C&num;调用windows API的一些方法

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

  3. C&num;调用Windows API函数截图

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

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

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

  5. c&plus;&plus;builder调用VC的dll以及VC调用c&plus;&plus;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&num;动态调用C&plus;&plus;编写的DLL函数

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

  8. C&num;中调用Windows API的要点 &period;

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

  9. 使用C&num;调用windows API入门

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

随机推荐

  1. 【代码笔记】iOS-UILable高度自适应&lpar;sizeWithFont&rpar;

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  2. Source Insight 基本使用&lpar;1&rpar;-使用Source Insight查看Android Framework 源码

    一.下载framework源码: google已经把framework源码托管在了gitHub上: https://github.com/android/platform_frameworks_bas ...

  3. MYSQL自动备份策略的选择

    目前流行几种备份方式: 1.逻辑备份:使用mysql自带的mysqldump工具进行备份.备份成sql文件形式.优点:最大好处是能够与正在运行的mysql自动协同工作,在运行期间可以确保备份是当时的点 ...

  4. 学习 ExtJS 4 面板与布局

    原文 http://www.cnblogs.com/codealone/archive/2013/06/04/3091325.html 面板Panel Ext.panel.Panel拓展自Ext.co ...

  5. abstract class 与interface

    一.抽象类(absteact class) 特点: 1.抽象方法只作说明,而不包含实现,可以看成是没有实现体的虚方法 2.抽象类不能被实例化.除此之外,具有类的其他特性 3.抽象类可以但不是必须有抽象 ...

  6. meta 是什么??

    META http-equiv 大全HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正确和精确地显示网页内容.常用的HTTP-EQUIV类型有: 1.Content- ...

  7. python3 random模块

    import random '''随机获取从0-1之间的小数'''print(random.random())print(format(random.random(), ".2f" ...

  8. Sunscreen POJ - 3614(贪心)

    To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with s ...

  9. 要继续看Python写算法的内容请到那里去

    由于在这里发文章的时候.莫名其妙的出现公布出去的问题.客服告知是由于链接或者敏感词. 能不能告诉我哪里出了问题?我能够改动,以便再发. 可是,没有人告诉我.仅仅是告诉我不能发. 另外,能不能公布一下敏 ...

  10. vue&period;js精讲02

    2017-09-17 笔记及源码地址 : https://github.com/wll8/vue_note vue 中的事件深入. 事件: @click/mouseover…事件简写: @ 如 @cl ...