📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using CyUSB;
namespace BulkLoop
{
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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -