C# 调用继电器api usb_relay_device.dll
2017-09-28 11:37
686 查看
C# 调用继电器api usb_relay_device.dll 代码封装
usb_relay_device.dll 为C++编写
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 = 8)] 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 = 1, TwoChannel = 2, FourChannel = 4, EightChannel = 8 } }
相关文章推荐
- C#调用SCL2008 动态库SCL_API_cdecl.dll or SCL_API_Stdcall.dll,提示找不到指定模块
- C++调用libusb,继电器(usbrelay)开发
- 从.NET平台调用Win32 API(C#调用的dll动态链接库)
- WinPcap中Pocket.dll里的PacketGetAdapterNames这个API在C#里面的调用
- C#调用api,或C++生成的dll
- C#连接android时,pc端检测便携设备PortableDeviceApi调用的问题
- c#调用API 和c++dll的参数问题
- ET99 采用C#直接调用API的DLL文件
- IronPython脚本调用C#dll示例
- C# DLL库自动注册的两种方法(调用Regsvr32法、调用DllRegisterServer函数法)
- SQL Server 调用 C#.net 写的Dll
- C#与DLL和COM的混合编程(1)-C#调用C++写的非托管的DLL中导出的函数
- Java调用C#dll全过程
- 智能设备 C#调用MFC DLL 传参值为汉字的解决方案 CString转存 unsigned char数组
- C++通过DLL调用C#代码
- C# 调用C++ DLL
- C#(9)——API调用
- C#写好的类库dll在别人调用的时候也能看到注释的方法
- VS2017创建DLL动态库------C#调用
- C#程序实现动态调用DLL的研究