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 对象上的接口方法 我 ...
随机推荐
- STS搭建SpringBoot项目
开发工具:推荐IDEA . Spring Tool Suit 虽然很简单,但还是记录一下,图个热闹. 开始 >>> 1. File --> New --> Spring ...
- [SDOI2013]森林 主席树+启发式合并
这题的想法真的很妙啊. 看到题的第一眼,我先想到树链剖分,并把\(DFS\)序当成一段区间上主席树.但是会发现在询问的时候,可能会非常复杂,因为你需要把路径拆成很多条轻链和重链,它们还不一定连续,很难 ...
- Java基础 -- 嵌套类(非静态嵌套类、静态嵌套类)
可以将一个类的定义放在另一个类的内部定义,这样的类就被称为嵌套类,包含嵌套类的类被称为外部类(outer class),也可以叫做封闭类. 嵌套类可以分为两种: 静态嵌套类(Static Nested ...
- redis3.2.10单实例安装测试
redis3.2.10单实例安装测试 主要是实际使用环境中使用,为了方便快速部署,特意记录如下: # root用户 yum -y install make gcc-c++ cmake bison-de ...
- Linux基础整理
命令 说明 chsh 查看和修改当前登录的Shell export 查看和设置Shell环境变量 read 读取从键盘或文件输入的数据 expr 四则远算和字符串运算 tmux 一个窗口操作多个会话 ...
- font-spider问题【已解决】
最近写一个项目,使用了引入的字体,然而字体太大,于是找解决方法,想要把字体压缩一下,然后找到了font-spider;font-spider使用方法这里就不多说了,网上一大把,主要是在node里面安装 ...
- Asp.Net Core 新篇章
一. 二. 三. 系列章节 第一节:.Net Core环境的安装和常用指令 第X节:XXXXXXXXXXXXXXXXXXXXXXXXXXXX 第X节:XXXXXXXXXXXXXXXXXXXXXXXXX ...
- C# MVC分页简单介绍
ASP.NET MVC中进行分页的方式有多种,这里介绍一种简单实用的方法 一:在实现分页之前,先添加“PagedList”和“PagedList.Mvc”两个组件,具体下载直接在NuGet程序包里收索 ...
- C#decimal四舍五入格式化
Console.WriteLine(string.Format("{0:####}", 594884.539)); Console.WriteLine(string.Format( ...
- 移动端,input、textarea滚动至可视区域
1.一般情况下 在移动端,点击input框之后,会弹出输入键盘.而内容input的内容也会自动滚动到可视区域内. 2.当父元素设置了overflow属性之后 在设置了overflow属性之后,点击in ...