📄 testdlg.cpp
字号:
//{{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_Control(pDX, IDC_BTNSPEED, m_btnSpeed);
DDX_Control(pDX, IDC_BTNSTOP, m_btnStop);
DDX_Control(pDX, IDC_BTNSTART, m_btnStart);
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_BN_CLICKED(IDC_BTNSTOP, OnBtnstop)
ON_BN_CLICKED(IDC_BTNSTART, OnBtnstart)
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 =8192;
HANDLE hDevice;//设备句柄
UpdateData(FALSE);
if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength())))
{
m_strOutput+="打开设备失败,请检查设备名是正确\r\n";
}
else
{
m_strOutput+="打开设备成功\r\n";
}
CloseHandle(hDevice);
m_btnStart.EnableWindow(TRUE);
m_btnStop.EnableWindow(FALSE);
m_btnSpeed.EnableWindow(FALSE);
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::OnBtnstop()
{
m_btnStart.EnableWindow(TRUE);
m_btnStop.EnableWindow(FALSE);
gStopTest=TRUE;
}
void CTestDlg::OnBtnstart()
{
UpdateData(TRUE);
gStopTest=FALSE;
m_BulkTest.testDlg=this;
m_BulkTest.bUpdate=FALSE;
CWinThread * wt = AfxBeginThread(
TestThread, // thread function
&m_BulkTest); // argument to thread function
m_BulkTest.hThread = wt->m_hThread;
m_btnStart.EnableWindow(FALSE);
m_btnStop.EnableWindow(TRUE);
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;
PUCHAR inBuffer = NULL;
unsigned long mstep,mlength,speed;
BULK_TRANSFER_CONTROL inBulkControl,outBulkControl;//
THREAD_CONTROL inThreadControl, outThreadControl;//
HANDLE hOutDevice=NULL,hInDevice=NULL;
ULONG OutPipeNum,InPipeNum;
char output[256];
ULONG CurrentTransferSize;
ULONG mTotal;
HANDLE WriteCompleteEvent,ReadCompleteEvent;
//打开设备
if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE)
{
m_strOutput+="打开设备失败\r\n";
UpdateData(FALSE);
return ;
}
if (bOpenDriver (&hInDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE)
{
m_strOutput+="打开设备失败\r\n";
UpdateData(FALSE);
return ;
}
//初始化输出数据
CurrentTransferSize=m_nSize;
outBuffer=(PUCHAR) malloc(CurrentTransferSize);
inBuffer=(PUCHAR) malloc(CurrentTransferSize);
srand(CurrentTransferSize);
for(ULONG i=0;i<CurrentTransferSize;i++)
outBuffer[i]=rand();
memset(inBuffer, 0, CurrentTransferSize);
ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
// initialize data structures for the read thread
InPipeNum=2;//输入端点
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;
OutPipeNum=0;//输出端点 Endpoint2 OUT
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 * Testrd = AfxBeginThread(
TransferThread, // thread function
&inThreadControl); // argument to thread function
inThreadControl.hThread = Testrd->m_hThread;
mstep=GetTickCount();
CWinThread * Testwt = AfxBeginThread(
TransferThread, // thread function
&outThreadControl); // argument to thread function
outThreadControl.hThread = Testwt->m_hThread;
WaitForSingleObject(ReadCompleteEvent,INFINITE);
WaitForSingleObject(WriteCompleteEvent,INFINITE);
mlength=GetTickCount();
if(outThreadControl.status)
{
mTotal=CurrentTransferSize;
m_strOutput+="测试成功\r\n";
mlength=mlength-mstep;
speed=1000;
if ( mlength !=0 ) speed=speed*mTotal/mlength;
else speed=9999999;
sprintf( output,"***speed = %5.2f KBytes/Sec, total=%ld bytes, time=%ld mS\r\n", (speed/1000), mTotal, mlength);
}
else
{
m_strOutput+="测试失败\r\n";
}
//关闭设备
free(outBuffer);
CloseHandle(hOutDevice);
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -