总述
常见的设备在PC端都只提供C/C++的动态库,当需要使用C#调用时就需要进行封装一下,下面对CP2112/CH341提供的动态库进行封装。
封装操作
首先按一下模式将C/C++的动态库中的函数进行提取和定义
using System.Runtime.InteropServices;
[DllImport("SLABHIDtoSMBus.dll", EntryPoint = "HidSmbus_GetNumDevices")]
public extern static int HidSmbus_GetNumDevices(ref uint lpdwNumDevices, int vid, int pid);
CH341/CP2112
CH341 是可以USB转IIC的芯片,既可以在PC端控制其通过IIC输出数据,例如: 控制IIC OLed屏幕显示灯。
CP2112 是可以USB转SMBUS的芯片,可以用来进行读写电源芯片,例如: TI的bq系列芯片等
编译库链接
接口:
public static uint CP2112_Get_Devices();
public static IntPtr CP2112_Open(uint index);
public static int CP2112_Close(IntPtr device);
public static int CP2112_Send(IntPtr device, byte slaveAddress, byte reg, byte[] buffer, byte sendlen);
public static int CP2112_Read(IntPtr device, byte slaveAddress, byte reg, ref byte[] buffer, byte numBytesToRead);
public static int Ch341_Open(uint iIndex);
public static bool Ch341_Close(uint iIndex);
public static bool IIC_Write_Bytes(uint iIndex, byte slave_addr, byte reg_addr, byte[] buf, uint len);
public static bool IIC_Read_Bytes(uint iIndex, byte slave_addr, byte reg_addr, ref byte buf, uint len);
获取:
关注,并发送 动态库