?? testdlg.cpp
字號:
// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include <process.h>
#include <winioctl.h>
#include "ezusbsys.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char PIPE_TYPE_STRINGS[4][4] =
{
"CTL",
"ISO",
"BLK",
"INT"
};
char PIPE_DIRECTION[2][4] =
{
"OUT",
"IN"
};
BOOL gStopTest;
/*
purpose:
打開設備函數
input:
devname 設備名
output:
phDeviceHandle 設備句柄
return value:
BOOLEAN變量 成功或者失敗
*/
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);
}
}
UINT TransferThread(
void * pParam
)
{
PTHREAD_CONTROL threadControl;
threadControl=(PTHREAD_CONTROL)pParam;
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;
}
UINT TestThread(
void * pParam // thread data
)
{
CThreadParam* ThreadParam;
CTestDlg* TestDlg;
HANDLE hInDevice = NULL;
HANDLE hOutDevice = NULL;
PUCHAR inBuffer = NULL;
PUCHAR outBuffer = NULL;
ULONG MaxTransferSize, CurrentTransferSize;
ULONG InPipeNum;
ULONG OutPipeNum;
ULONG TestPass;
ULONG Errors;
HANDLE ReadCompleteEvent;
HANDLE WriteCompleteEvent;
ULONG i;
BULK_TRANSFER_CONTROL inBulkControl,outBulkControl;
THREAD_CONTROL inThreadControl,outThreadControl;
char tempbuff[256];
FILE *dumpfile;//保存測試數據
FILE *binfile;
ThreadParam=(CThreadParam *)pParam;
TestDlg=ThreadParam->testDlg;
if (bOpenDriver (&hInDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打開設備失敗,測試線程結束\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
if (bOpenDriver (&hOutDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE)
{
TestDlg->m_strOutput+="打開設備失敗\r\n";
ThreadParam->bUpdate=TRUE;
return 0;
}
InPipeNum=2;
OutPipeNum=0;
sprintf(tempbuff,"IN PIPE = %d OUT PIPE = %d",InPipeNum,OutPipeNum);
ThreadParam->bUpdate=TRUE;
TestDlg->m_strOutput+=tempbuff;
//
// get the transfer size and allocate the transfer buffers
//
MaxTransferSize = TestDlg->m_nSize;
CurrentTransferSize = MaxTransferSize;
outBuffer = (unsigned char *)malloc(MaxTransferSize);
inBuffer = (unsigned char *)malloc(MaxTransferSize);
//
// seed the random number generator, and set our starting pattern
//
srand(TestDlg->m_nSize);
//
// initialize the pass and error counters
//
TestPass=0;
Errors=0;
//
// Set up our event objects
//
ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
//
// main test loop
//
while (!gStopTest)//全局變量來決定是否退出測試線程
{
// initialize the out buffer
for (i=0;i<CurrentTransferSize;i++)
outBuffer[i] = rand();
// initialize the in buffer
memset(inBuffer, 0, MaxTransferSize);
// 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;
// start and wait for transfer threads
CWinThread * rd = AfxBeginThread(
TransferThread, // thread function
&inThreadControl); // argument to thread function
inThreadControl.hThread = rd->m_hThread;
CWinThread * wt = AfxBeginThread(
TransferThread, // thread function
&outThreadControl); // argument to thread function
outThreadControl.hThread = wt->m_hThread;
WaitForSingleObject(ReadCompleteEvent,INFINITE);
WaitForSingleObject(WriteCompleteEvent,INFINITE);
// if either the read or write failed, we want to stop the test
if (!inThreadControl.status)
{
TestDlg->m_strOutput+= "Error: Read failed\r\n";
TestDlg->m_strOutput+="Dumping OUT packet to file\r\n";
dumpfile = fopen("dumpfile.txt","w");
for (i=0;i<CurrentTransferSize;i++)
fprintf(dumpfile,"%X\n",outBuffer[i]);
fclose(dumpfile);
binfile = fopen("binfile.bix","wb");
fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile);
fclose(binfile);
gStopTest = TRUE;
}
if( !outThreadControl.status)
{
TestDlg->m_strOutput+= "Error: Write failed\r\n";
gStopTest = 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,
"Error: Length expected = %d actual = %d\r\n",
CurrentTransferSize,
inThreadControl.BytesReturned);
TestDlg->m_strOutput+=tempbuff;
Errors++;
// gStopTest = TRUE;
}
// copmpare the buffers
for (i=0;i<CurrentTransferSize;i++)
{
if (inBuffer[i] != outBuffer[i])
{
sprintf(tempbuff,
"Error: Data offset 0x%x expected = 0x%x actual = 0x%x\r\n",
i,
outBuffer[i],
inBuffer[i]);
TestDlg->m_strOutput+=tempbuff;
Errors++;
// gStopTest = TRUE;
}
}
//如果有錯誤發生
if (Errors && (gStopTest == TRUE))
{
TestDlg->m_strOutput+="Dumping Error packet to file\r\n";
dumpfile = fopen("dumpfile.txt","w");
for (i=0;i<CurrentTransferSize;i++)
fprintf(dumpfile,"%X\n",outBuffer[i]);
fclose(dumpfile);
binfile = fopen("binfile.bix","wb");
fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile);
fclose(binfile);
}
// dumpfile = fopen("dumpfile.txt","w");
// for (i=0;i<CurrentTransferSize;i++)
// fprintf(dumpfile,"%X\n",outBuffer[i]);
// fclose(dumpfile);
// update pass counter
TestPass++;
TestDlg->m_nError=Errors;
TestDlg->m_nTestPass=TestPass;
ThreadParam->bUpdate=TRUE;
}//end while(gstoptest)
TestDlg->m_nError=Errors;
TestDlg->m_nTestPass=TestPass;
free(inBuffer);
free(outBuffer);
CloseHandle(hInDevice);
CloseHandle(hOutDevice);
ThreadParam->bUpdate=TRUE;
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_strName = _T("");
m_strOutput = _T("");
m_nSize = 0;
m_nTestPass = 0;
m_nError = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Text(pDX, IDC_EDTNAME, m_strName);
DDX_Text(pDX, IDC_EDTOUTPUT, m_strOutput);
DDX_Text(pDX, IDC_EDTSIZE, m_nSize);
DDX_Text(pDX, IDC_EDTPASS, m_nTestPass);
DDX_Text(pDX, IDC_EDTERROR, m_nError);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTNLIST, OnBtnlist)
ON_BN_CLICKED(IDC_BTNCLEAR, OnBtnclear)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BTNSPEED, OnBtnspeed)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_strName = _T("EZUSB-0");
m_strOutput = _T("");
m_nSize =60000;
HANDLE hDevice;//設備句柄
UpdateData(FALSE);
if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength())))
{
m_strOutput+="打開設備失敗,請檢查設備名是正確\r\n";
}
else
{
m_strOutput+="打開設備成功\r\n";
}
CloseHandle(hDevice);
gStopTest=FALSE;
SetTimer(1, 1000, 0);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDlg::OnBtnlist()
{
HANDLE hDevice = NULL;
ULONG nBytes;
char tempbuff[256];
PUSBD_INTERFACE_INFORMATION pInterfaceInfo;
PUSBD_PIPE_INFORMATION pPipeInfo;
ULONG i;
UCHAR gInterfaceInfo [1024];
UpdateData(FALSE);
if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength())))
{
m_strOutput+="打開設備失敗,請檢查設備名是正確\r\n";
}
DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_PIPE_INFO,
NULL,
0,
&gInterfaceInfo,
sizeof(gInterfaceInfo),
&nBytes,
NULL);
CloseHandle(hDevice);
pInterfaceInfo = (PUSBD_INTERFACE_INFORMATION) &gInterfaceInfo;
m_strOutput+="Pipe Endpoint Direction Type Size\r\n";
for (i=0;i<pInterfaceInfo->NumberOfPipes;i++)
{
pPipeInfo = &pInterfaceInfo->Pipes[i];
sprintf(tempbuff,"%2d %2d %s %s %4d",
i,
pPipeInfo->EndpointAddress & 0x0F,
pPipeInfo->EndpointAddress & 0x80 ? "IN " : "OUT",
PIPE_TYPE_STRINGS[pPipeInfo->PipeType],
pPipeInfo->MaximumPacketSize);
m_strOutput+=tempbuff;
m_strOutput+="\r\n";
}
UpdateData(FALSE);
}
void CTestDlg::OnBtnclear()
{
m_strOutput="";
UpdateData(FALSE);
}
void CTestDlg::OnTimer(UINT nIDEvent)
{
if(m_BulkTest.bUpdate)
{
m_BulkTest.bUpdate=FALSE;
UpdateData(FALSE);
}
CDialog::OnTimer(nIDEvent);
}
void CTestDlg::OnBtnspeed()
{
UpdateData(TRUE);
PUCHAR outBuffer = NULL;
unsigned long mstep,mlength;
double speed;
BULK_TRANSFER_CONTROL outBulkControl;//inBulkControl,
HANDLE hOutDevice=NULL,hInDevice=NULL;
char output[256];
double mTotal;
if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE)
{
m_strOutput+="打開設備失敗\r\n";
UpdateData(FALSE);
return ;
}
outBuffer=(PUCHAR) malloc(m_nSize);
srand(m_nSize);
for(ULONG i=0;i<m_nSize;i++)
outBuffer[i]=rand();
BOOLEAN status=FALSE;
ULONG BytesReturned=0;
ULONG nTestCount=100;//共循環測試100次
outBulkControl.pipeNum=0;//端點選擇EP2
mstep=GetTickCount(); //起始時間
for( i=0;i<nTestCount;i++)//多次循環測試
{
status = DeviceIoControl (hOutDevice,
IOCTL_EZUSB_BULK_WRITE,
(PVOID)&outBulkControl,
sizeof(BULK_TRANSFER_CONTROL),
outBuffer,//輸出緩沖區
m_nSize,//字節數,在對話框中可以設置,60000為較佳值
&BytesReturned,//返回字節數據
//這里為了測試速度,沒有測試返回字節數
NULL);
}
mlength=GetTickCount();//結束時間
if(status==TRUE)
{
mTotal=m_nSize*nTestCount;
m_strOutput+="測試成功\r\n";
mlength-=mstep;
if ( mlength !=0 ) speed=mTotal/mlength;
else speed=9999999;
sprintf( output,"***speed = %10.2f KBytes/Sec, total=%10.2f bytes, time=%ld mS\r\n", speed, mTotal, mlength);
m_strOutput+=output;
}
else
{
m_strOutput+="測試失敗\r\n";
}
//關閉設備
free(outBuffer);
CloseHandle(hOutDevice);
UpdateData(FALSE);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -