Skip to content

Instantly share code, notes, and snippets.

@roboter
Forked from nickfox-taterli/CP2112.cs
Created April 27, 2022 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roboter/5802367cac30af2a0e58f5bd9cec8601 to your computer and use it in GitHub Desktop.
Save roboter/5802367cac30af2a0e58f5bd9cec8601 to your computer and use it in GitHub Desktop.
CP2112 Example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using SLAB_HID_TO_SMBUS;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
const ushort vid = 4292; //10C4
const ushort pid = 60048; //EA90
IntPtr connectedDevice;
const byte RelaySlaveAddress = 0x20;
const byte SensorSlaveAddress = 0x2E;
ushort Relay1Stauts = 0;
ushort Relay2Stauts = 0;
ushort Relay3Stauts = 0;
ushort Relay4Stauts = 0;
public Form1()
{
InitializeComponent();
uint numDevices = 0;
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_GetNumDevices(ref numDevices, vid, pid);
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_Open(ref connectedDevice, 0, vid, pid);
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x01, 0x00 }, 2);
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x02, 0x00 }, 2);
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x03, 0x00 }, 2);
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x04, 0x00 }, 2);
timer_get_temp.Enabled = true;
}
private void Relay1Button_Click(object sender, EventArgs e)
{
timer_get_temp.Enabled = false;
if (Relay1Stauts == 0)
{
Relay1Stauts = 1;
Relay1Button.Text = "关闭一号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x01, 0xFF }, 2);
}
else
{
Relay1Stauts = 0;
Relay1Button.Text = "打开一号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x01, 0x00 }, 2);
}
timer_get_temp.Enabled = true;
}
private void Relay2Button_Click(object sender, EventArgs e)
{
timer_get_temp.Enabled = false;
if (Relay2Stauts == 0)
{
Relay2Stauts = 1;
Relay2Button.Text = "关闭二号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x02, 0xFF }, 2);
}
else
{
Relay2Stauts = 0;
Relay2Button.Text = "打开二号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x02, 0x00 }, 2);
}
timer_get_temp.Enabled = true;
}
private void Relay3Button_Click(object sender, EventArgs e)
{
timer_get_temp.Enabled = false;
if (Relay3Stauts == 0)
{
Relay3Stauts = 1;
Relay3Button.Text = "关闭三号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x03, 0xFF }, 2);
}
else
{
Relay3Stauts = 0;
Relay3Button.Text = "打开三号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x03, 0x00 }, 2);
}
timer_get_temp.Enabled = true;
}
private void Relay4Button_Click(object sender, EventArgs e)
{
timer_get_temp.Enabled = false;
if (Relay4Stauts == 0)
{
Relay4Stauts = 1;
Relay4Button.Text = "关闭四号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x04, 0xFF }, 2);
}
else
{
Relay4Stauts = 0;
Relay4Button.Text = "打开四号继电器";
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_WriteRequest(connectedDevice, RelaySlaveAddress, new byte[] { 0x04, 0x00 }, 2);
}
timer_get_temp.Enabled = true;
}
private void Timer_get_temp_Tick(object sender, EventArgs e)
{
byte bytesRead = 0;
byte status = 0;
byte[] readbuff = new byte[61];
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_AddressReadRequest(connectedDevice, SensorSlaveAddress, 1, 1, new byte[] { 0x01 });
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_ForceReadResponse(connectedDevice, 1);
SLAB_HID_TO_SMBUS.CP2112_DLL.HidSmbus_GetReadResponse(connectedDevice, ref status, readbuff, 61, ref bytesRead);
if(bytesRead == 1)
{
progressBar1.Value = readbuff[0];
label1.Text = "现在温度" + readbuff[0] + ":";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment