📄 form1.cs
字号:
}
}
/*Summary
This is a system event handler, when the selected index changes(end point selection).
*/
private void EndPointsComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the Alt setting
string sAlt = EndPointsComboBox.Text.Substring(4, 1);
byte a = Convert.ToByte(sAlt);
MyDevice.AltIntfc = a;
// Get the endpoint
int aX = EndPointsComboBox.Text.LastIndexOf("0x");
string sAddr = EndPointsComboBox.Text.Substring(aX, 4);
byte addr = (byte)Util.HexToInt(sAddr);
EndPoint = MyDevice.EndPointOf(addr);
// Ensure valid PPX for this endpoint
PpxBox_SelectedIndexChanged(sender, null);
}
/*Summary
Executes on Start Button click
*/
private void StartBtn_Click(object sender, System.EventArgs e)
{
if (MyDevice == null)
return;
if (StartBtn.Text.Equals("Start"))
{
EndPointsComboBox.Enabled = false;
StartBtn.Text = "Stop";
StartBtn.BackColor = Color.Pink;
BufSz = EndPoint.MaxPktSize * Convert.ToUInt16(PpxBox.Text);
QueueSz = Convert.ToUInt16(QueueBox.Text);
PPX = Convert.ToUInt16(PpxBox.Text);
EndPoint.XferSize = BufSz;
if (EndPoint is CyIsocEndPoint)
IsoPktBlockSize = (EndPoint as CyIsocEndPoint).GetPktBlockSize(BufSz);
else
IsoPktBlockSize = 0;
bRunning = true;
tListen = new Thread(new ThreadStart(XferThread));
tListen.IsBackground = true;
tListen.Priority = ThreadPriority.Highest;
tListen.Start();
}
else
{
if (tListen.IsAlive)
{
EndPointsComboBox.Enabled = true;
StartBtn.Text = "Start";
bRunning = false;
t2 = DateTime.Now;
elapsed = t2 - t1;
xferRate = (long)(XferBytes / elapsed.TotalMilliseconds);
xferRate = xferRate / (int)100 * (int)100;
tListen.Abort();
tListen.Join();
tListen = null;
StartBtn.BackColor = Color.Aquamarine;
}
}
}
/*Summary
Data Xfer Thread entry point. Starts the thread on Start Button click
*/
public unsafe void XferThread()
{
// Setup the queue buffers
byte[][] cmdBufs = new byte[QueueSz][];
byte[][] xferBufs = new byte[QueueSz][];
byte[][] ovLaps = new byte[QueueSz][];
ISO_PKT_INFO[][] pktsInfo = new ISO_PKT_INFO[QueueSz][];
int xStart = 0;
try
{
LockNLoad(ref xStart, cmdBufs, xferBufs, ovLaps, pktsInfo);
}
catch (NullReferenceException e)
{
// This exception gets thrown if the device is unplugged
// while we're streaming data
e.GetBaseException();
this.Invoke(handleException);
}
}
/*Summary
This is a recursive routine for pinning all the buffers used in the transfer in memory.
It will get recursively called QueueSz times. On the QueueSz_th call, it will call
XferData, which will loop, transferring data, until the stop button is clicked.
Then, the recursion will unwind.
*/
public unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo)
{
// Allocate one set of buffers for the queue
cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN + IsoPktBlockSize];
xBufs[j] = new byte[BufSz];
oLaps[j] = new byte[20];
pktsInfo[j] = new ISO_PKT_INFO[PPX];
fixed (byte* tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) // Pin the buffers in memory
{
OVERLAPPED* ovLapStatus = (OVERLAPPED*)tL0;
ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0);
// Pre-load the queue with a request
int len = BufSz;
EndPoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]);
j++;
if (j < QueueSz)
LockNLoad(ref j, cBufs, xBufs, oLaps, pktsInfo); // Recursive call to pin next buffers in memory
else
XferData(cBufs, xBufs, oLaps, pktsInfo); // All loaded. Let's go!
}
}
/*Summary
Called at the end of recursive method, LockNLoad().
XferData() implements the infinite transfer loop
*/
public unsafe void XferData(byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo)
{
int k = 0;
int len = 0;
Successes = 0;
Failures = 0;
XferBytes = 0;
t1 = DateTime.Now;
for (; bRunning; )
{
// WaitForXfer
fixed (byte* tmpOvlap = oLaps[k])
{
OVERLAPPED* ovLapStatus = (OVERLAPPED*)tmpOvlap;
if (!EndPoint.WaitForXfer(ovLapStatus->hEvent, 500))
{
EndPoint.Abort();
PInvoke.WaitForSingleObject(ovLapStatus->hEvent, 500);
}
}
if (EndPoint.Attributes == 1)
{
CyIsocEndPoint isoc = EndPoint as CyIsocEndPoint;
// FinishDataXfer
if (isoc.FinishDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k], ref pktsInfo[k]))
{
//XferBytes += len;
//Successes++;
ISO_PKT_INFO[] pkts = pktsInfo[k];
for (int j = 0; j < PPX; j++)
{
if (pkts[j].Status == 0)
{
XferBytes += pkts[j].Length;
Successes++;
}
else
Failures++;
pkts[j].Length = 0;
}
}
else
Failures++;
}
else
{
// FinishDataXfer
if (EndPoint.FinishDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]))
{
XferBytes += len;
Successes++;
}
else
Failures++;
}
k++;
if (k == QueueSz) // Only update displayed stats once each time through the queue
{
k = 0;
t2 = DateTime.Now;
elapsed = t2 - t1;
xferRate = (long)(XferBytes / elapsed.TotalMilliseconds);
xferRate = xferRate / (int)100 * (int)100;
// Call StatusUpdate() in the main thread
this.Invoke(updateUI);
// For small QueueSz or PPX, the loop is too tight for UI thread to ever get service.
// Without this, app hangs in those scenarios.
Thread.Sleep(1);
}
// Re-submit this buffer into the queue
len = BufSz;
EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]);
} // End infinite loop
}
/*Summary
The callback routine delegated to updateUI.
*/
public void StatusUpdate()
{
if (xferRate > ProgressBar.Maximum)
ProgressBar.Maximum = (int)(xferRate * 1.25);
ProgressBar.Value = (int)xferRate;
ThroughputLabel.Text = ProgressBar.Value.ToString();
SuccessBox.Text = Successes.ToString();
FailuresBox.Text = Failures.ToString();
}
/*Summary
The callback routine delegated to handleException.
*/
public void ThreadException()
{
StartBtn.Text = "Start";
bRunning = false;
t2 = DateTime.Now;
elapsed = t2 - t1;
xferRate = (long)(XferBytes / elapsed.TotalMilliseconds);
xferRate = xferRate / (int)100 * (int)100;
tListen = null;
StartBtn.BackColor = Color.Aquamarine;
}
/*Summary
Updates the CPU Load meter.
*/
private void PerfTimer_Tick(object sender, EventArgs e)
{
if (bVista) return;
float cpu = CpuCounter.NextValue();
CpuBar.Value = (int)cpu;
CpuLabel.Text = string.Format("{0} %", (int)cpu);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -