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

下面是封装后的调用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 = )]
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 =
}

查找并打开控制器

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

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

 UsbRelayDeviceHelper.OpenOneRelayChannel(state, );

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

UsbRelayDeviceHelper.CloseOneRelayChannel(state, );

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

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

c# 调用c++类库控制usb继电器的更多相关文章

  1. VFP调用API来控制USB摄像头,实现拍照或录像

    *--前提:VFP7.0以上;Windows 2K及以上*--控件:AVICAP32.DLL *--定义:一般放到主程序或表单(集)的Load事件中Public WM_CAP_DRIVER_DISCO ...

  2. java 调用 C# 类库搞定,三步即可,可以调用任何类及方法,很简单,非常爽啊

    java 调用 C# 类库搞定,三步即可,可以调用任何类及方法,很简单,非常爽啊 java 调用 C# 类库搞定,可以调用任何类及方法,很简单,非常爽啊 总体分三步走: 一.准备一个 C# 类库 (d ...

  3. Asp.Net MVC ajax调用 .net 类库问题

    如果你还在为 ajax 调用 .net 类库还束手无策的话,相信这篇博客将帮助你解决这个世纪问题! 因为Visual Studio 内置了asp.net mvc ,不过当你添加asp.net mvc项 ...

  4. 利用C#的反射机制动态调用DLL类库

    最近由于业务要求,需要动态调用DLL类库,所以研究了一下,感觉还好也不太难,今天就把自己理解的写了一个小例子(已经通过VS2005跑通),供大家一起研究和探讨,有理解不当的地方还请高手们多多指正,谢谢 ...

  5. 在SQL Server中使用CLR调用.NET类库中的方法 (转载)

    在SQL Server中调用 .NET 类库的方法要分为下面几步来实现: 在.NET中新建一个类库项目,并在这个项目中添加一个类文件,并把要被SQL Server调用的方法定义为公有的,静态的方法. ...

  6. asp.net调用opencv类库,实现图像处理显示

    asp.net调用opencv类库,实现图像处理显示     ​      原理上来说,通过dll的调用,无论是asp.net还是winform都可以调用opencv及其类库.但是在实现的过程还是有许 ...

  7. java 调用 C# 类库 实战视频, 非常简单, 通过 云寻觅 javacallcsharp 生成器 一步即可!

    java 调用 C# 类库 实战视频, 非常简单, 通过 云寻觅 javacallcsharp 生成器 一步即可! 通过 云寻觅 javacallcsharp 生成器 自动生成java jni类库,  ...

  8. php通过JavaBridge调用Java类库和不带包的自定义java类成功 但是调用带包的自定义Java类报错,该怎么解决

    php通过JavaBridge调用Java类库和不带包的自定义java类成功 但是调用带包的自定义Java类报错,Class.forName("com.mysql.jdbc.Driver&q ...

  9. C#调用C++类库的几种方式

    1.  直接调用C++类库中的公共方法 使用DllImport特性对方法进行调用,比如一个C++类库SampleCppWrapper.dll中的公共方法: extern "C" _ ...

随机推荐

  1. Nginx简介入门

    买了极客时间上陶辉的Nginx核心知识100讲,正在学.链接 Nginx 4个组成部分 二进制可执行文件 nginx.conf 配置文件 access.log error.log nginx 版本 M ...

  2. 杭电-------2045不容易系列之(3)—— LELE的RPG难题(C语言写)

    /* 当最后一个块可以和第一个块染相同颜色时,答案为:3*pow(2,n-1);但是最后一块不能和第一块颜色相同,则减去和第一 块颜色相同的染色种数即可 3*pow(2,n-1)-ranse(n-1) ...

  3. 论文翻译:Speech Enhancement Based on the General Transfer Function GSC and Postfiltering

    论文地址:基于通用传递函数GSC和后置滤波的语音增强 博客作者:凌逆战 博客地址:https://www.cnblogs.com/LXP-Never/p/12232341.html 摘要 在语音增强应 ...

  4. go程序基于阿里云CodePipeline的一次devops实践

    背景 最近朋友有个项目代码托管用的码云,测试服务器(阿里云ECS)只有一台,三四个人开发,于是想基于阿里云的CodePipeline快速打造一套自动化cicd的流程,使用docker来进行多套环境部署 ...

  5. Mock模拟数据,前后端分离

    安装 使用npm安装: npm install mockjs; 或直接<script src="http://mockjs.com/dist/mock.js">< ...

  6. html基本介绍,了解html与css,html语法和结构

    一般来说,制作自己第一个网页通常书写的文字是"hello world!你好,全世界",代码如下展示: <!DOCTYPE html> <html lang=&qu ...

  7. Android中使用Notification在状态栏上显示通知

    场景 状态栏上显示通知效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新 ...

  8. 虚拟机VMware官网最新版附密钥,kali,ubuntu,centos,deepin迅雷下载地址。

    以下全部都是官网的迅雷复制链接 版本都是当前时间可下载的最新版本 VMware官网迅雷下载链接: https://download3.vmware.com/software/wkst/file/VMw ...

  9. DFA与NFA的等价性,DFA化简

    等价性 对于每个NFA M存在一个DFA M',使得L(M)=L(M')--------等价性证明,NFA的确定化 假定NFA M=<S, Σ, δ, S 0 , F>,我们对M的状态转换 ...

  10. 【seata源码学习】001 - seata-server的配置读取和服务注册

    github, seata vergilyn seata-fork seata.io zh-cn docs (PS. 随缘看心情写,坚持不了几天.文章还是写的超级的烂,排版也奇差无比~~~~ 脑壳疼~ ...