?? testdlg.cpp
字號:
#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include <process.h>
#include <winioctl.h>
#include "ezusbsys.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
double m_ntime;//時間
double m_nRt,nT1;
ULONG mTmp;
UINT m_nSize;
BYTE tempBuf[6144000];//100*60KB 用來保存讀取過來的數據
CString strDevname;//設備名,在initDialog函數中初始化
BOOL gStopRead;//讀數據和顯示線程啟動和結束全局標志
BOOL gStopBulk;//批量循環讀寫測試啟動和結束全局標志 TRUE結束 FALSE啟動
WORD InUserRegion[1024*1024];
HANDLE ReadEvent ;
HANDLE hEvent;
/*
打開設備
*/
BOOL bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname)
{
char completeDeviceName[64] = "";
char pcMsg[64] = "";
strcat (completeDeviceName,
"\\\\.\\"
);
strcat (completeDeviceName,
devname);
*phDeviceHandle = CreateFile( completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (*phDeviceHandle == INVALID_HANDLE_VALUE) {
return (FALSE);
} else {
return (TRUE);
}
}
BOOL Reset_Device()
{
BOOL bResult=FALSE;
BOOL bDir=DIR_OUT;
UCHAR VendNo=0xb3;
UCHAR pBuffer[10];
HANDLE hDevice;
ULONG ncnt;
ncnt=10;
if (!bOpenDriver (&hDevice, strDevname.GetBuffer(strDevname.GetLength())))
{
return FALSE;
}
for(ULONG i=0;i<ncnt;i++);//復位設備參數
pBuffer[i]=0xff;
bResult=Vend_request(hDevice,VendNo,bDir,pBuffer,ncnt);
CloseHandle(hDevice);
return bResult;
}
/*
讀寫線程函數,改變因為讀寫堵塞而使程序死機
*/
UINT TransferThread(
void * pParam
)
{
PTHREAD_CONTROL threadControl;
threadControl=(PTHREAD_CONTROL)pParam;
// perform the ISO transfer
threadControl->status = DeviceIoControl ( threadControl->hDevice,
threadControl->Ioctl,
threadControl->InBuffer,
threadControl->InBufferSize,
threadControl->OutBuffer,
threadControl->OutBufferSize,
&threadControl->BytesReturned,
NULL);
// if an event exists, set it
if (threadControl->completionEvent)
SetEvent(threadControl->completionEvent);
return 0;
}
/*
固件廠商請求
發出請求初始化參數
*/
BOOL Vend_request(HANDLE hDevice,UCHAR VendNo,BOOL bDir,UCHAR*pdat,ULONG ncnt)
{
if (hDevice==NULL) {
return FALSE;
}
THREAD_CONTROL VendthreadControl;
VENDOR_OR_CLASS_REQUEST_CONTROL myRequest;
HANDLE VendCompleteEvent=NULL;
VendCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
myRequest.requestType=0x02; // request type (1=class, 2=vendor)
myRequest.recepient=0x00; // recipient (0=device,1=interface,2=endpoint,3=other)
myRequest.request = VendNo;//0xb3;//請求號
myRequest.value = 0x00;
myRequest.index = 0x00;
myRequest.direction =bDir;//0x01;//IN transfer direction (0=host to device, 1=device to host)
VendthreadControl.hDevice=hDevice;
VendthreadControl.Ioctl=IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST;
VendthreadControl.InBuffer=(PVOID)&myRequest;
VendthreadControl.InBufferSize=sizeof(myRequest);
VendthreadControl.OutBuffer=pdat;
VendthreadControl.OutBufferSize=ncnt;
VendthreadControl.status=FALSE;
VendthreadControl.completionEvent=VendCompleteEvent;
VendthreadControl.BytesReturned = 0;
// start and wait for transfer threads
CWinThread * TestVend = AfxBeginThread(
TransferThread, // thread function
&VendthreadControl); // argument to thread function
VendthreadControl.hThread = TestVend->m_hThread;
//等待請求回應
WaitForSingleObject(VendCompleteEvent,INFINITE);
return VendthreadControl.status;
}
/*
批量循環測試
先EP2OUT發送1K數據,
再EP6IN接收1K數據,
同時將發送和接收進行數據比較
*/
UINT BulkThread(
void * pParam // thread data
)
{
CThreadParam* ThreadParam;
CTestDlg* TestDlg;
HANDLE hInDevice = NULL;
HANDLE hOutDevice = NULL;
PUCHAR inBuffer = NULL;
PUCHAR outBuffer = NULL;
ULONG CurrentTransferSize;
ULONG InPipeNum;
ULONG OutPipeNum;
ULONG TestPass;
ULONG Errors;
HANDLE ReadCompleteEvent;
HANDLE WriteCompleteEvent;
LARGE_INTEGER lTime;
LONGLONG lT1,lT2;
double dfFreq,dfMinus;
ULONG i;
BULK_TRANSFER_CONTROL inBulkControl,outBulkControl;
THREAD_CONTROL inThreadControl,outThreadControl;
char tempbuff[256];
ThreadParam=(CThreadParam *)pParam;
TestDlg=ThreadParam->testDlg;
if (bOpenDriver (&hInDevice, strDevname.GetBuffer(strDevname.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打開設備失敗,測試線程結束\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
if (bOpenDriver (&hOutDevice, strDevname.GetBuffer(strDevname.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打開設備失敗\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
InPipeNum=1;//輸入批量端點號
OutPipeNum=0;//輸出批量端點號
sprintf(tempbuff,"IN PIPE = %d OUT PIPE = %d\r\n",InPipeNum,OutPipeNum);
ThreadParam->bUpdate=TRUE;
TestDlg->m_strOutput+=tempbuff;
CurrentTransferSize = m_nSize;
outBuffer = (unsigned char *)malloc(CurrentTransferSize);
inBuffer = (unsigned char *)malloc(CurrentTransferSize);
TestPass=0;
Errors=0;
ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
while (!gStopBulk)//全局變量來決定是否退出測試線程
{
// initialize the out buffer
for (i=0;i<CurrentTransferSize;i++)
outBuffer[i] = (UCHAR)i;
// initialize the in buffer
memset(inBuffer, 0, CurrentTransferSize);
// initialize data structures for the read thread
inBulkControl.pipeNum = InPipeNum;
inThreadControl.hDevice = hInDevice;
inThreadControl.Ioctl = IOCTL_EZUSB_BULK_READ;
inThreadControl.InBuffer = (PVOID)&inBulkControl;
inThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
inThreadControl.OutBuffer = inBuffer;
inThreadControl.OutBufferSize = CurrentTransferSize;
inThreadControl.completionEvent = ReadCompleteEvent;
inThreadControl.status = FALSE;
inThreadControl.BytesReturned = 0;
// initialize data structures for the write thread
outBulkControl.pipeNum = OutPipeNum;
outThreadControl.hDevice = hOutDevice;
outThreadControl.Ioctl = IOCTL_EZUSB_BULK_WRITE;
outThreadControl.InBuffer = (PVOID)&outBulkControl;
outThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
outThreadControl.OutBuffer = outBuffer;
outThreadControl.OutBufferSize = CurrentTransferSize;
outThreadControl.completionEvent = WriteCompleteEvent;
outThreadControl.status = FALSE;
outThreadControl.BytesReturned = 0;
QueryPerformanceFrequency(&lTime);
dfFreq=(double)lTime.QuadPart;
//nTime=GetTickCount(); //起始時間
QueryPerformanceCounter(&lTime);
lT1=lTime.QuadPart;
// start and wait for transfer threads
CWinThread * wt = AfxBeginThread(
TransferThread, // thread function
&outThreadControl); // argument to thread function
outThreadControl.hThread = wt->m_hThread;
CWinThread * rd = AfxBeginThread(
TransferThread, // thread function
&inThreadControl); // argument to thread function
inThreadControl.hThread = rd->m_hThread;
WaitForSingleObject(WriteCompleteEvent,INFINITE);
WaitForSingleObject(ReadCompleteEvent,INFINITE);
//m_ntime=GetTickCount()-ntime; //起始時間
QueryPerformanceCounter(&lTime);
lT2=lTime.QuadPart;
dfMinus=(double)(lT2-lT1);
m_ntime=(dfMinus/dfFreq)*1000.0;
// if either the read or write failed, we want to stop the test
if (!inThreadControl.status)
{
TestDlg->m_strOutput+= "循環讀寫測試錯誤:讀失敗\r\n";
gStopBulk = TRUE;
}
if( !outThreadControl.status)
{
TestDlg->m_strOutput+= "循環讀寫測試錯誤:寫失敗\r\n";
gStopBulk = TRUE;
}
// verify the data, unless the user has disabled verify data
// or this was a write only test.
// make sure the correct amount of data was returned
if (inThreadControl.BytesReturned != CurrentTransferSize)
{
sprintf(tempbuff,
"循環讀寫測試錯誤: 期望返回數據長度 = %d 實際數據長度 = %d\r\n",
CurrentTransferSize,
inThreadControl.BytesReturned);
TestDlg->m_strOutput+=tempbuff;
Errors++;
gStopBulk = TRUE;
}
// copmpare the buffers
for (i=0;i<CurrentTransferSize;i++)
{
if (inBuffer[i] != outBuffer[i])
{
sprintf(tempbuff,
"循環讀寫測試錯誤: 數據偏移 0x%x 寫出數據 = 0x%x 讀入數據 = 0x%x\r\n",
i,
outBuffer[i],
inBuffer[i]);
TestDlg->m_strOutput+=tempbuff;
Errors++;
gStopBulk = TRUE;
}
}
// update pass counter
TestPass++;
TestDlg->m_nError=Errors;
TestDlg->m_nTestPass=TestPass;
ThreadParam->bUpdate=TRUE;
}//end while(gStopBulk)
TestDlg->m_nError=Errors;
TestDlg->m_nTestPass=TestPass;
free(inBuffer);
free(outBuffer);
CloseHandle(hInDevice);
CloseHandle(hOutDevice);
ThreadParam->bUpdate=TRUE;
return 0;
}
//讀數據線程
UINT ReadThread(
void * pParam // thread data
)
{
CThreadParam* ThreadParam;
CTestDlg* TestDlg;
HANDLE hInDevice = NULL;
PUCHAR inBuffer = NULL;
ULONG CurrentTransferSize;
ULONG InPipeNum;
ULONG TestPass=0;
ULONG Errors=0;
//ULONG i;
ULONG nCount=0;
BULK_TRANSFER_CONTROL inBulkControl;
THREAD_CONTROL inThreadControl;
// WORD nData[61440];//最大數據大小
//得到線程參數
ThreadParam=(CThreadParam *)pParam;
TestDlg=ThreadParam->testDlg;
//打開設備
if (bOpenDriver (&hInDevice, strDevname.GetBuffer(strDevname.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打開設備失敗,測試線程結束\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
//進行數據傳輸
InPipeNum=1;//端點6,EP6IN
CurrentTransferSize = m_nSize;
//CurrentTransferSize=1024000;
inBuffer = (unsigned char *)malloc(CurrentTransferSize);
// initialize the pass and error counters
TestPass=0;
Errors=0;
BOOL bFirst = TRUE;
WORD wLast = 0;
nT1=GetTickCount();//程序啟動初始化時間
while (!gStopRead)//全局變量來決定是否退出測試線程
{
// initialize the in buffer
//memset(inBuffer, 0, CurrentTransferSize);
// initialize data structures for the read thread
inBulkControl.pipeNum = InPipeNum;
inThreadControl.hDevice = hInDevice;
inThreadControl.Ioctl = IOCTL_EZUSB_BULK_READ;
inThreadControl.InBuffer = (PVOID)&inBulkControl;
inThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
inThreadControl.OutBuffer = inBuffer;
inThreadControl.OutBufferSize = CurrentTransferSize;
inThreadControl.status = FALSE;
inThreadControl.BytesReturned = 0;
inThreadControl.status = DeviceIoControl (inThreadControl.hDevice,
inThreadControl.Ioctl,
inThreadControl.InBuffer,
inThreadControl.InBufferSize,
inThreadControl.OutBuffer,
inThreadControl.OutBufferSize,
&inThreadControl.BytesReturned,
NULL);
// 如果發生錯誤,停止數據傳輸
if ((!inThreadControl.status)||(inThreadControl.BytesReturned != CurrentTransferSize))
{
TestDlg->m_strOutput+= "Error: Read failed\r\n";
Errors++;
gStopRead = TRUE;
}
CopyMemory(InUserRegion, inBuffer, m_nSize);
//拷貝數據緩沖,拷貝數據將花費一段時間,可注釋該語句,測試傳輸速度
SetEvent(hEvent);
// update pass counter
TestPass++;
TestDlg->m_nError=Errors;
TestDlg->m_nTestPass=TestPass;
ThreadParam->bUpdate=TRUE;
}//end while(gStopRead)
TestDlg->m_nError=Errors;
TestDlg->m_nTestPass=TestPass;
free(inBuffer);
CloseHandle(hInDevice);
ThreadParam->bUpdate=TRUE;
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -