C# WinForms使用CyUSB.dll訪問USB設備的實現(xiàn)步驟
更新時間:2025年09月01日 08:35:37 作者:leo__520
CYUSB.dll驅(qū)動包為C#開發(fā)者提供了便捷的USB設備操作解決方案,該驅(qū)動包內(nèi)含多個版本的cyusb.dll文件,滿足不同項目的需求,本文給大家介紹了C# WinForms使用CyUSB.dll訪問USB設備的實現(xiàn)步驟,需要的朋友可以參考下
1:環(huán)境配置
- 安裝驅(qū)動與 SDK
- 安裝 Cypress EZ-USB FX3 SDK(確保包含 CyUSB.dll)
- 將 CyUSB.dll 添加到項目引用(路徑示例:
C:\Program Files\Cypress\Cypress USB\Tools\CyUSB\lib\CyUSB.dll)
- 創(chuàng)建 WinForms 項目
- 添加
using CyUSB;命名空間
- 添加
2:核心代碼實現(xiàn)
using System;
using System.Windows.Forms;
using CyUSB;
namespace USB_Communication_Demo
{
public partial class MainForm : Form
{
private CyUSBDeviceList usbDeviceList; // 設備列表
private CyUSBDevice connectedDevice; // 當前設備
private const int TARGET_VID = 0x0483; // 目標設備VID(替換為實際值)
private const int TARGET_PID = 0x5750; // 目標設備PID(替換為實際值)
public MainForm()
{
InitializeComponent();
InitializeUSB();
}
// 初始化USB設備監(jiān)控
private void InitializeUSB()
{
usbDeviceList = new CyUSBDeviceList(CyConst.DEVICES_ALL);
usbDeviceList.DeviceAttached += (s, e) => RefreshDeviceList(); // 設備插入事件
usbDeviceList.DeviceRemoved += (s, e) => RefreshDeviceList(); // 設備拔出事件
RefreshDeviceList(); // 初始刷新
}
// 刷新設備列表(UI更新)
private void RefreshDeviceList()
{
comboBoxDevices.Invoke((MethodInvoker)delegate {
comboBoxDevices.Items.Clear();
foreach (CyUSBDevice dev in usbDeviceList)
{
if (dev.VendorID == TARGET_VID && dev.ProductID == TARGET_PID)
{
comboBoxDevices.Items.Add($"VID:{dev.VendorID:X4} PID:{dev.ProductID:X4}");
}
}
if (comboBoxDevices.Items.Count > 0) comboBoxDevices.SelectedIndex = 0;
});
}
// 選擇設備并連接
private void btnConnect_Click(object sender, EventArgs e)
{
if (comboBoxDevices.SelectedIndex >= 0)
{
connectedDevice = usbDeviceList[comboBoxDevices.SelectedIndex] as CyUSBDevice;
if (connectedDevice.Open())
{
lblStatus.Text = "設備已連接";
btnSend.Enabled = true;
}
}
}
// 發(fā)送數(shù)據(jù)(OUT端點)
private void btnSend_Click(object sender, EventArgs e)
{
if (connectedDevice == null) return;
try
{
byte[] data = { 0x01, 0x02, 0x03 }; // 示例數(shù)據(jù)
int outEndpoint = 0x01; // OUT端點號(需根據(jù)設備手冊調(diào)整)
CyUSBEndPoint endpoint = connectedDevice.EndPointOf(outEndpoint);
endpoint.XferData(data, data.Length);
lblLog.AppendText("數(shù)據(jù)發(fā)送成功\n");
}
catch (Exception ex)
{
lblLog.AppendText($"發(fā)送失敗: {ex.Message}\n");
}
}
// 接收數(shù)據(jù)(IN端點)
private void ReceiveData()
{
if (connectedDevice == null) return;
try
{
int inEndpoint = 0x81; // IN端點號(需根據(jù)設備手冊調(diào)整)
CyUSBEndPoint endpoint = connectedDevice.EndPointOf(inEndpoint);
byte[] buffer = new byte[64]; // 緩沖區(qū)大小需匹配設備配置
int bytesRead = endpoint.XferData(ref buffer, ref 64);
if (bytesRead > 0)
{
string hexData = BitConverter.ToString(buffer, 0, bytesRead);
lblLog.AppendText($"接收數(shù)據(jù): {hexData}\n");
}
}
catch (Exception ex)
{
lblLog.AppendText($"接收失敗: {ex.Message}\n");
}
}
// 窗體關(guān)閉時釋放資源
protected override void OnFormClosing(FormClosingEventArgs e)
{
connectedDevice?.Close();
usbDeviceList.Dispose();
base.OnFormClosing(e);
}
}
}
說明
- 設備枚舉與熱插拔
- 通過
CyUSBDeviceList實時監(jiān)控 USB 設備插入/拔出事件 - 使用
Invoke確保 UI 更新在主線程執(zhí)行(避免跨線程異常)
- 通過
- 數(shù)據(jù)傳輸
- 發(fā)送數(shù)據(jù):通過
EndPointOf獲取 OUT 端點,調(diào)用XferData寫入數(shù)據(jù) - 接收數(shù)據(jù):通過輪詢或事件監(jiān)聽(需擴展)讀取 IN 端點數(shù)據(jù)
- 發(fā)送數(shù)據(jù):通過
- 資源管理
- 使用
Dispose()釋放 USB 設備列表資源 - 關(guān)閉設備句柄防止資源泄漏
- 使用
注意
- VID/PID 配置
- 替換
TARGET_VID和TARGET_PID為實際設備的值(可通過設備管理器查看)
- 替換
- 端點號確認
- 使用工具(如 Cypress Control Center)或設備手冊驗證 IN/OUT 端點號
- 錯誤處理擴展
- 添加重試機制(如發(fā)送失敗時重試 3 次)
- 檢查設備狀態(tài):
if (connectedDevice.IsOpen)
- 數(shù)據(jù)格式
- HID 設備需跳過報告 ID(如
buffer.Skip(1).ToArray())
- HID 設備需跳過報告 ID(如
擴展功能建議
- 異步接收數(shù)據(jù):使用
BeginInvoke實現(xiàn)后臺接收 - 協(xié)議解析:根據(jù)設備協(xié)議解析接收到的二進制數(shù)據(jù)
- 日志記錄:將通信記錄保存到文件
到此這篇關(guān)于C# WinForms使用CyUSB.dll訪問USB設備的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)C# CyUSB.dll訪問USB設備內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#調(diào)用外部程序的三種實現(xiàn)方式的示例
在C#中調(diào)用外部程序通常有幾種方式,本文將介紹其中的三種主要方法:System.Diagnostics.Process?類、System.Shell類以及使用C#的System.Diagnostics.ProcessStartInfo類與System.Diagnostics.Process?類結(jié)合,感興趣的可以了解一下2025-05-05
Visual C#中如何使用IComparable和IComparer接口
這篇文章主要介紹了C#中使用IComparable和IComparer接口,在本例中,該對象被用作第二個參數(shù)被傳遞給Array.Sort的接受IComparer實例的重載方法,需要的朋友可以參考下2023-04-04

