如何用C#开发EZ-USB,本人有一段代码,但调试时总是不识别设备,求各位大侠指教。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CyUSB;
using System.Threading;
namespace CSharpUSBTest {
public partial class Form1 : Form {
App_PnP_Callback evHandler;
CyUSBDevice loopDevice = null;
USBDeviceList usbDevices = null;
CyBulkEndPoint inEndpoint = null;
CyBulkEndPoint outEndpoint = null; Thread tXfers;
bool bRunning = false; int value;
long outCount, inCount; const int XFERSIZE = 256;
byte[] outData = new byte[XFERSIZE];
byte[] inData = new byte[XFERSIZE]; // These 2 needed for TransfersThread to update the UI
delegate void UpdateUICallback();
UpdateUICallback updateUI;
public Form1() {
InitializeComponent(); // Setup the callback routine for updating the UI
updateUI = new UpdateUICallback(StatusUpdate); // Setup PnP event handling
evHandler = new App_PnP_Callback(PnP_Event_Handler); // Create a list of CYUSB devices
usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB, evHandler); setDevice();
} public void setDevice() {
if (usbDevices.Count > 0)
loopDevice = usbDevices[0x0547, 0x0080] as CyUSBDevice; StartBtn.Enabled = (loopDevice != null); if (loopDevice != null)
Text = loopDevice.FriendlyName;
else
Text = "Bulkloop - no device"; // Set the in and out endpoints per the selected radio buttons.
EptPair1Btn_Click(this, null);
} public void PnP_Event_Handler(IntPtr pnpEvent, IntPtr hRemovedDevice) {
if (pnpEvent.Equals(CyConst.DBT_DEVICEREMOVECOMPLETE)) {
usbDevices.Remove(hRemovedDevice);
//loopDevice = null;
setDevice();
} if (pnpEvent.Equals(CyConst.DBT_DEVICEARRIVAL)) {
usbDevices.Add();
setDevice();
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// If close was selected while running the loopback, shut it down.
if (bRunning)
StartBtn_Click(this, null); if (usbDevices != null) usbDevices.Dispose();
} private void EptPair1Btn_Click(object sender, EventArgs e) {
if (loopDevice != null) {
if (EptPair1Btn.Checked) {
outEndpoint = loopDevice.EndPointOf(0x02) as CyBulkEndPoint;
inEndpoint = loopDevice.EndPointOf(0x86) as CyBulkEndPoint;
} else {
outEndpoint = loopDevice.EndPointOf(0x04) as CyBulkEndPoint;
inEndpoint = loopDevice.EndPointOf(0x88) as CyBulkEndPoint;
} outEndpoint.TimeOut = 1000;
inEndpoint.TimeOut = 1000;
}
} private void SetOutputData() { if (ConstByteBtn.Checked) {
for (int i = 0; i < XFERSIZE; i++)
outData[i] = (byte)value;
} if (RandomByteBtn.Checked) {
Random r = new Random(value);
r.NextBytes(outData);
} if (IncrByteBtn.Checked) {
for (int i = 0; i < XFERSIZE; i++)
outData[i] = (byte)value++;
} if (IncrWordBtn.Checked) {
for (int i = 0; i < XFERSIZE; i += 4) {
outData[i] = (byte)(value >> 24);
outData[i + 1] = (byte)(value >> 16);
outData[i + 2] = (byte)(value >> 8);
outData[i + 3] = (byte)value; value++;
}
}
} private void StartBtn_Click(object sender, EventArgs e) {
if (!bRunning) {
value = Convert.ToInt32(StartValBox.Text);
outCount = 0;
inCount = 0; bRunning = true;
StartBtn.Text = "Stop";
StartBtn.BackColor = Color.Pink; tXfers = new Thread(new ThreadStart(TransfersThread));
tXfers.IsBackground = true;
tXfers.Priority = ThreadPriority.Highest;
tXfers.Start();
} else {
bRunning = false;
StartBtn.Text = "Start";
StartBtn.BackColor = Color.Aquamarine; if (tXfers == null) return; if (tXfers.IsAlive) {
tXfers.Abort();
tXfers.Join();
tXfers = null;
}
}
} public void StatusUpdate() {
BytesOutLabel.Text = outCount.ToString();
BytesInLabel.Text = inCount.ToString();
Refresh(); StartBtn.Text = bRunning ? "Stop" : "Start";
StartBtn.BackColor = bRunning ? Color.Pink : Color.Aquamarine;
}
// This method runs in a separate thread.
public void TransfersThread() {
int xferLen = XFERSIZE; bool bResult = true; // Loop stops if either an IN or OUT transfer fails
for (; bRunning && bResult; ) {
SetOutputData(); xferLen = XFERSIZE;
bResult = outEndpoint.XferData(ref outData, ref xferLen);
outCount += xferLen; if (bResult) {
bResult = inEndpoint.XferData(ref inData, ref xferLen);
inCount += xferLen;
} // Call StatusUpdate() in the main thread
this.Invoke(updateUI);
} bRunning = false; // Call StatusUpdate() in the main thread
this.Invoke(updateUI);
} } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CyUSB;
using System.Threading;
namespace CSharpUSBTest {
public partial class Form1 : Form {
App_PnP_Callback evHandler;
CyUSBDevice loopDevice = null;
USBDeviceList usbDevices = null;
CyBulkEndPoint inEndpoint = null;
CyBulkEndPoint outEndpoint = null; Thread tXfers;
bool bRunning = false; int value;
long outCount, inCount; const int XFERSIZE = 256;
byte[] outData = new byte[XFERSIZE];
byte[] inData = new byte[XFERSIZE]; // These 2 needed for TransfersThread to update the UI
delegate void UpdateUICallback();
UpdateUICallback updateUI;
public Form1() {
InitializeComponent(); // Setup the callback routine for updating the UI
updateUI = new UpdateUICallback(StatusUpdate); // Setup PnP event handling
evHandler = new App_PnP_Callback(PnP_Event_Handler); // Create a list of CYUSB devices
usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB, evHandler); setDevice();
} public void setDevice() {
if (usbDevices.Count > 0)
loopDevice = usbDevices[0x0547, 0x0080] as CyUSBDevice; StartBtn.Enabled = (loopDevice != null); if (loopDevice != null)
Text = loopDevice.FriendlyName;
else
Text = "Bulkloop - no device"; // Set the in and out endpoints per the selected radio buttons.
EptPair1Btn_Click(this, null);
} public void PnP_Event_Handler(IntPtr pnpEvent, IntPtr hRemovedDevice) {
if (pnpEvent.Equals(CyConst.DBT_DEVICEREMOVECOMPLETE)) {
usbDevices.Remove(hRemovedDevice);
//loopDevice = null;
setDevice();
} if (pnpEvent.Equals(CyConst.DBT_DEVICEARRIVAL)) {
usbDevices.Add();
setDevice();
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// If close was selected while running the loopback, shut it down.
if (bRunning)
StartBtn_Click(this, null); if (usbDevices != null) usbDevices.Dispose();
} private void EptPair1Btn_Click(object sender, EventArgs e) {
if (loopDevice != null) {
if (EptPair1Btn.Checked) {
outEndpoint = loopDevice.EndPointOf(0x02) as CyBulkEndPoint;
inEndpoint = loopDevice.EndPointOf(0x86) as CyBulkEndPoint;
} else {
outEndpoint = loopDevice.EndPointOf(0x04) as CyBulkEndPoint;
inEndpoint = loopDevice.EndPointOf(0x88) as CyBulkEndPoint;
} outEndpoint.TimeOut = 1000;
inEndpoint.TimeOut = 1000;
}
} private void SetOutputData() { if (ConstByteBtn.Checked) {
for (int i = 0; i < XFERSIZE; i++)
outData[i] = (byte)value;
} if (RandomByteBtn.Checked) {
Random r = new Random(value);
r.NextBytes(outData);
} if (IncrByteBtn.Checked) {
for (int i = 0; i < XFERSIZE; i++)
outData[i] = (byte)value++;
} if (IncrWordBtn.Checked) {
for (int i = 0; i < XFERSIZE; i += 4) {
outData[i] = (byte)(value >> 24);
outData[i + 1] = (byte)(value >> 16);
outData[i + 2] = (byte)(value >> 8);
outData[i + 3] = (byte)value; value++;
}
}
} private void StartBtn_Click(object sender, EventArgs e) {
if (!bRunning) {
value = Convert.ToInt32(StartValBox.Text);
outCount = 0;
inCount = 0; bRunning = true;
StartBtn.Text = "Stop";
StartBtn.BackColor = Color.Pink; tXfers = new Thread(new ThreadStart(TransfersThread));
tXfers.IsBackground = true;
tXfers.Priority = ThreadPriority.Highest;
tXfers.Start();
} else {
bRunning = false;
StartBtn.Text = "Start";
StartBtn.BackColor = Color.Aquamarine; if (tXfers == null) return; if (tXfers.IsAlive) {
tXfers.Abort();
tXfers.Join();
tXfers = null;
}
}
} public void StatusUpdate() {
BytesOutLabel.Text = outCount.ToString();
BytesInLabel.Text = inCount.ToString();
Refresh(); StartBtn.Text = bRunning ? "Stop" : "Start";
StartBtn.BackColor = bRunning ? Color.Pink : Color.Aquamarine;
}
// This method runs in a separate thread.
public void TransfersThread() {
int xferLen = XFERSIZE; bool bResult = true; // Loop stops if either an IN or OUT transfer fails
for (; bRunning && bResult; ) {
SetOutputData(); xferLen = XFERSIZE;
bResult = outEndpoint.XferData(ref outData, ref xferLen);
outCount += xferLen; if (bResult) {
bResult = inEndpoint.XferData(ref inData, ref xferLen);
inCount += xferLen;
} // Call StatusUpdate() in the main thread
this.Invoke(updateUI);
} bRunning = false; // Call StatusUpdate() in the main thread
this.Invoke(updateUI);
} } }
解决方案 »
- foreach 急求
- 求winForm批量生成代码
- 利用虚拟打印机将Word、Execl文档转换成PDF,XPS文档出现的问题~解决问题了再加100分
- 发布应用程序时候,检测安装framework的问题
- 完全没人理我没分了,另外还有个没解决的帖子里有100分.求求帮我解决这个问题,去那个帖子里我把分给您!....引用类型的参数,如何让它在类的
- 劳烦大众帮个忙,一个小问题搞了好几天.
- 如果做一个服务端和客户端联系的应用程序..分不多,请原谅
- 求教FTP的使用(在线等)
- 不规则窗体问题 急!~!!
- winxp 系统下 屏幕截图 QQ登陆窗体 不能截取 直接忽略了给
- 程序控制visio流程图中元素的属性
- [跪等]水晶报表页面提示:provider: 命名管道提供程序, error 40
其中[0x0547, 0x0080] 应该是你设备的VID和PID,在设备管理器中可以看到