CP2112 + .NET 开发入门

/ 0评 / 3

DockerPi-H 基于CP2112实现的HID转I2C功能,而CP2112提供很多各种的驱动层对接,所以开发起来会轻松加愉快.

当然首要前提是安装VS,其实VSC也可以,因为是HID的,也不存在安装驱动一说,另外.NET是跨平台的,在Mac和Linux下可以用mono部分代替.NET框架运行.

引用的DLL和头文件.

打开设备.

const ushort vid = 4292;  //10C4

const ushort pid = 60048; //EA90

IntPtr connectedDevice;



InitializeComponent();

uint numDevices = 0;

SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_GetNumDevices(ref numDevices, vid, pid);



if (numDevices < 1)

{

    MessageBox.Show("No devices Connected");

    this.Close();

}



if (numDevices > 1)

{

    MessageBox.Show("More than one device connected. Abort");

    this.Close();

}



try

{

    SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_Open(ref connectedDevice, 0, vid, pid);

    SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_SetSmbusConfig(connectedDevice, 100000, 0x02, 0, 100, 100, 0, 2);

    TickTimer.Start();

}

catch (Exception)

{

    MessageBox.Show("Connecting fail");

    this.Close();

}

然后读取一下寄存器.

byte status = 0;

byte[] readbuff = new byte[61];



byte bytesRead = 1;

SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_AddressReadRequest(connectedDevice, 0x2E, 1, 1, new byte[] { 0x01 });

SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_ForceReadResponse(connectedDevice, 30);

SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_GetReadResponse(connectedDevice, ref status, readbuff, 61, ref bytesRead);

就是这么简单.