同步传输:
public void TransfersThread2() //专门用来传输线程0的
{
int xferLen = XFERSIZE;
FileStream fs = null;
fs = new FileStream(".\\Save.Hex", FileMode.Open);
bool bResult = true;
inEndpoint1.TimeOut = 0xFFFFFFFF; //设置传输超时时间1ms
while (true)
{
// try
// {
//calls the XferData function for bulk transfer(OUT/IN) in the cyusb.dll
xferLen = 1024;
bResult = inEndpoint1.XferData(ref inData1, ref xferLen); //一次传输256个字符
if (bResult)
{
//if (adr > 67108864)
//{
// sw.Stop();
// MessageBox.Show(string.Format("{0}.{1}",sw.Elapsed.Seconds, sw.Elapsed.Milliseconds));
//}
//else
//{
// fs.Write(inData1, 0, 1024);
// adr += 1024;
//}
inCount += 1;
this.Invoke(updateUI);
}
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.ToString(), "线程2错误");
// return;
// }
}
// Call StatusUpdate() in the main thread
//this.Invoke(updateUI);
}
异步传输:
public unsafe void TransfersThread2() //专门用来传输线程0的
{
int xferLen = XFERSIZE; //2048
byte i = 0;
byte[] cmdBufs = new byte[XFERSIZE];
byte[] xferBufs = new byte[XFERSIZE];
byte[] ovLaps = new byte[XFERSIZE];
GCHandle handleOverlap = new GCHandle();
handleOverlap = GCHandle.Alloc(ovLaps, GCHandleType.Pinned);
CyUSB.OVERLAPPED ovLapStatus = new CyUSB.OVERLAPPED();
ovLapStatus = (CyUSB.OVERLAPPED)Marshal.PtrToStructure(handleOverlap.AddrOfPinnedObject(), typeof(CyUSB.OVERLAPPED));
ovLapStatus.hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0);
Marshal.StructureToPtr(ovLapStatus, handleOverlap.AddrOfPinnedObject(), true);
// CyUSB.OVERLAPPED ovLapStatus = new CyUSB.OVERLAPPED();
cmdBufs = new byte[CyConst.SINGLE_XFER_LEN];
xferBufs = new byte[XFERSIZE];
int sz = Math.Max(CyConst.OverlapSignalAllocSize, sizeof(OVERLAPPED));
ovLaps = new byte[sz];
//fixed (byte* tmp0 = ovLaps)
//{
// OVERLAPPED* ovLapStatus = (OVERLAPPED*)tmp0;
// ovLapStatus->hEvent = PInvoke.CreateEvent(0, 0, 0, 0);
//}
CyUSB.OVERLAPPED ovData = new CyUSB.OVERLAPPED();
int len = xferLen;
inEndpoint1.BeginDataXfer(ref cmdBufs, ref xferBufs, ref len, ref ovLaps);
i = 0;
Successes = 0;
Failures = 0;
for (; ; )
{
//fixed (byte* tmp0 = ovLaps)
{
//OVERLAPPED* ovLapStatus = (OVERLAPPED*)tmp0;
ovData = (CyUSB.OVERLAPPED)Marshal.PtrToStructure(handleOverlap.AddrOfPinnedObject(), typeof(CyUSB.OVERLAPPED));
if (!inEndpoint1.WaitForXfer(ovData.hEvent, 500))
{
inEndpoint1.Abort();
PInvoke.WaitForSingleObject(ovData.hEvent, 500);
}
}
if (inEndpoint1.FinishDataXfer(ref cmdBufs, ref xferBufs, ref len, ref ovLaps))
Successes++;
else
Failures++;
// Re-submit this buffer into the queue\
len = XFERSIZE;
inEndpoint1.BeginDataXfer(ref cmdBufs, ref xferBufs, ref len, ref ovLaps);
i++;
this.Invoke(updateUI);
}
}
|