📄 d12testdlg.cpp
字号:
/*
//*************************************************************************
//
// P H I L I P S P R O P R I E T A R Y
//
// COPYRIGHT (c) 1998 BY PHILIPS SINGAPORE.
// -- ALL RIGHTS RESERVED --
//
// File Name: D12TestDlg.CPP
// Author: Wenkai Du
// Created: 14 Jan 98
// Modified:
// Revision: 1.4
//
//*************************************************************************
//
//*************************************************************************
*/
// D12TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include <mmsystem.h>
#include "D12Test.h"
#include "D12TestDlg.h"
#include "devioctl.h"
#include "process.h"
#include "rwbulk.h"
#include "ioctl.h"
#include <afxmt.h>
#include "timer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BOOL DevIOCtrl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer,
DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize,
LPDWORD lpBytesReturned);
BOOL Read(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead)
{
OVERLAPPED os;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=::CreateEvent(NULL,TRUE,FALSE,NULL);
int err=0;
BOOL bResult = ReadFile(hFile,lpBuffer,nNumberOfBytesToRead,
lpNumberOfBytesRead,&os);
*lpNumberOfBytesRead=0;
if(!bResult)
{
if(::GetLastError()==ERROR_IO_PENDING)
{
switch(::WaitForSingleObject(os.hEvent,2000))
{
case WAIT_OBJECT_0:
if(!::GetOverlappedResult(hFile,&os,lpNumberOfBytesRead,FALSE))
{
err=::GetLastError();
}
break;
case WAIT_TIMEOUT:
::CancelIo(hFile);
err=ERROR_TIMEOUT;
break;
default:
err=::GetLastError();
break;
}
}
else
{
*lpNumberOfBytesRead=-1;
err=1;
}
}
CloseHandle(os.hEvent);
DWORD nBytes;
if(err)
DevIOCtrl(hFile,IOCTL_D12_RESET_PIPE,0,0,0,0,&nBytes);
return err==0;
}
BOOL Write(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWrite)
{
OVERLAPPED os;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=::CreateEvent(NULL,TRUE,FALSE,NULL);
int err=0;
BOOL bResult = WriteFile(hFile,lpBuffer,nNumberOfBytesToWrite,
lpNumberOfBytesWrite,&os);
if(!bResult)
{
if(::GetLastError()==ERROR_IO_PENDING)
{
switch(::WaitForSingleObject(os.hEvent,2000))
{
case WAIT_OBJECT_0:
if(!::GetOverlappedResult(hFile,&os,lpNumberOfBytesWrite,FALSE))
{
err=::GetLastError();
}
break;
case WAIT_TIMEOUT:
::CancelIo(hFile);
err=ERROR_TIMEOUT;
break;
default:
err=::GetLastError();
break;
}
}
else
err=1;
}
CloseHandle(os.hEvent);
DWORD nBytes;
if(err)
DevIOCtrl(hFile,IOCTL_D12_RESET_PIPE,0,0,0,0,&nBytes);
return err==0;
}
CCriticalSection m_sec;
BOOL DevIOCtrl(HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer,
DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize,
LPDWORD lpBytesReturned)
{
m_sec.Lock();
OVERLAPPED os;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=::CreateEvent(NULL,TRUE,FALSE,NULL);
int err=0;
BOOL bResult = DeviceIoControl(hDevice,
dwIoControlCode,
lpInBuffer,
nInBufferSize,
lpOutBuffer,
nOutBufferSize,
lpBytesReturned,
&os);
if(!bResult)
{
if(::GetLastError()==ERROR_IO_PENDING)
{
switch(::WaitForSingleObject(os.hEvent,1000))
{
case WAIT_OBJECT_0:
if(!::GetOverlappedResult(hDevice,&os,lpBytesReturned,FALSE))
{
err=::GetLastError();
}
break;
case WAIT_TIMEOUT:
::CancelIo(hDevice);
err=ERROR_TIMEOUT;
break;
default:
err=::GetLastError();
break;
}
}
else
err=1;
}
CloseHandle(os.hEvent);
m_sec.Unlock();
return err==0;
}
UINT InterruptIn(void * pParam)
{
char cc[180], c;
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
HANDLE hFile, hDevice;
CD12TestDlg* testDlg;
IO_BLOCK ioBlock;
threadParam=(struct CThreadParam *)pParam;
threadParam->bInThread = TRUE;
testDlg = threadParam->testDlg;
threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16);
do
{
do
{
Sleep(2000);
hFile = open_file(threadParam->pipe_name);
if(hFile != INVALID_HANDLE_VALUE)
{
hDevice = open_dev();
ioBlock.uOffset = 0;
ioBlock.uLength = 1;
ioBlock.pbyData = (PUCHAR)&c;
ioBlock.uIndex = GET_FIRMWARE_VERSION;
//DWORD t=IOCTL_READ_REGISTERS;
bResult=DevIOCtrl(hDevice,
IOCTL_READ_REGISTERS,
(PVOID)&ioBlock,
sizeof(IO_BLOCK),
(PVOID)&c,
1,
&nBytes);
// bResult = DeviceIoControl(hDevice,
// IOCTL_READ_REGISTERS,
// (PVOID)&ioBlock,
// sizeof(IO_BLOCK),
// (PVOID)&c,
// 1,
// &nBytes,
// NULL);
if (bResult != TRUE)
{
CloseHandle(hFile);
CloseHandle(hDevice);
threadParam->bInThread = FALSE;
wsprintf (cc, "固件版本不支持本测试。\r\n请升级仿真板固件。");
testDlg->MessageBox((LPCSTR)cc, "固件不兼容", MB_ICONSTOP);
testDlg->EndDialog(0);
return 0;
}
}
} while(hFile == INVALID_HANDLE_VALUE && threadParam->bInThread == TRUE);
threadParam->ulData[0] = D12_DRIVERLOAD;
threadParam->ulData[1] = c;
threadParam->bUpdate = TRUE;
if (hFile != INVALID_HANDLE_VALUE)
{
threadParam->threadFile1=hFile;
while(threadParam->bInThread == TRUE)
{
bResult=Read(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes);
// bResult = ReadFile(hFile,
// threadParam->pcIoBuffer,
// threadParam->uiLength,
// &nBytes,
// NULL);
if (bResult == TRUE)
{
threadParam->ulData[0] |= D12_KEYSTATUS;
threadParam->ulData[2] = *(threadParam->pcIoBuffer);
if(threadParam->ulData[2])
threadParam->testDlg->m_KeyStatus = _T("已松开按键");
else
threadParam->testDlg->m_KeyStatus = _T("已按下按键");
threadParam->testDlg->SendMessage(0x405,0,0);
}
else if(nBytes==(DWORD)-1)
{
break;
// DevIOCtrl(hFile,IOCTL_D12_RESET_PIPE,0,0,0,0,&nBytes);
//// DeviceIoControl(hFile,
//// IOCTL_D12_RESET_PIPE,
//// 0,
//// 0,
//// 0,
//// 0,
//// &nBytes,
//// NULL);
//
// threadParam->ulData[0] |= D12_DRIVERUNLOAD;
// threadParam->bUpdate = TRUE;
//
// break;
}// else ioctl failed
};
CloseHandle(hFile);
CloseHandle(hDevice);
} // if valid hDevice
} while(threadParam->bInThread == TRUE);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
UINT GenericOut(void * pParam)
{
char temp [80],cc[80];
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
HANDLE hFile;
CD12TestDlg* testDlg;
threadParam=(struct CThreadParam *)pParam;
threadParam->bInThread = FALSE;
testDlg = threadParam->testDlg;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE) {
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
strcpy(temp, "驱动程序打开失败! 不能写数据到 ");
strcat(temp, threadParam->pipe_name);
strcat(temp, "。");
testDlg->MessageBox((LPCSTR)temp, "测试错误");
return 0;
}
if (hFile != INVALID_HANDLE_VALUE) {
threadParam->bInThread = TRUE;
bResult=Write(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes);
// bResult = WriteFile(hFile,
// threadParam->pcIoBuffer,
// threadParam->uiLength,
// &nBytes,
// NULL);
if (bResult != TRUE) {
DevIOCtrl(hFile,IOCTL_D12_RESET_PIPE,0,0,0,0,&nBytes);
// DeviceIoControl(hFile,
// IOCTL_D12_RESET_PIPE,
// 0,
// 0,
// 0,
// 0,
// &nBytes,
// NULL);
wsprintf (cc, "写 ");
strcat( cc, threadParam->pipe_name);
strcat( cc, " 失败! 已放弃.");
testDlg->MessageBox((LPCSTR)cc, "测试错误");
}/* else ioctl failed */
}// if valid hDevice
//The thread terminated by itself, delete resources
CloseHandle(hFile);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
UINT ReadPipe(void * pParam)
{
char temp [80],cc[80];
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
CD12TestDlg* testDlg;
HANDLE hFile, hDevice;
DWORD time0, time1;
IO_BLOCK ioBlock;
IO_REQUEST ioRequest;
CTIMER tmcount;
threadParam=(struct CThreadParam *)pParam;
threadParam->bInThread = FALSE;
testDlg = threadParam->testDlg;
threadParam->bUpdate = FALSE;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE) {
strcpy(temp, "驱动程序打开失败! 不能从 ");
strcat(temp, threadParam->pipe_name);
strcat(temp, " 中读出数据。");
testDlg->MessageBox((LPCSTR)temp, "测试错误");
return 0;
}
hDevice = open_dev();
if(hDevice == INVALID_HANDLE_VALUE) {
CloseHandle(hFile);
strcpy(temp, "设备句柄打开失败! 不能读 ");
strcat(temp, threadParam->pipe_name);
strcat(temp, "。");
testDlg->MessageBox((LPCSTR)temp, "Test Error");
return 0;
}
threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16);
threadParam->ulCount = 0;
threadParam->total_time = 0;
threadParam->cur_rate = 0;
threadParam->max_rate = 0;
if (hFile != INVALID_HANDLE_VALUE) {
threadParam->bInThread = TRUE;
threadParam->threadFile1=hFile;
testDlg->m_InStartBtn.EnableWindow(FALSE);
testDlg->m_InStopBtn.EnableWindow(TRUE);
testDlg->m_LoopStartBtn.EnableWindow(FALSE);
testDlg->m_LoopStopBtn.EnableWindow(FALSE);
threadParam->Last_time=timeGetTime();
do{
ioRequest.uAddressL = 0;
ioRequest.bAddressH = 0;
ioRequest.uSize = threadParam->uiLength;
ioRequest.bCommand = 0x81; //start, read
ioBlock.uOffset = 0;
ioBlock.uLength = sizeof(IO_REQUEST);
ioBlock.pbyData = (PUCHAR)&ioRequest;
ioBlock.uIndex = SETUP_DMA_REQUEST;
TRACE("READPIPE:before DeviceIoControl1\n");
bResult=DevIOCtrl(hDevice,
IOCTL_WRITE_REGISTERS,
(PVOID)&ioBlock,
sizeof(IO_BLOCK),
NULL,
0,
&nBytes);
TRACE("READPIPE:after DeviceIoControl1\n");
if (bResult != TRUE) {
threadParam->bInThread = FALSE;
wsprintf (cc, "在 ");
strcat( cc, threadParam->pipe_name);
strcat( cc, " 上请求数据传送失败! 已放弃。");
testDlg->MessageBox((LPCSTR)cc, "测试错误");
break;
}
tmcount.Start();
TRACE("READPIPE:before ReadFile1\n");
bResult=Read(hFile,threadParam->pcIoBuffer,threadParam->uiLength,
&nBytes);
TRACE("READPIPE:after ReadFile1\n");
tmcount.Stop();
if (bResult != TRUE) {
}
else
{
threadParam->ulCount += nBytes;
threadParam->total_time += tmcount.OutputTime()*1000;
threadParam->cur_rate = (nBytes*1000*1000)/(tmcount.OutputTime()*1000*1000);
if(threadParam->cur_rate > threadParam->max_rate)
threadParam->max_rate = threadParam->cur_rate;
threadParam->bUpdate = TRUE;
}
}while(threadParam->bInThread == TRUE);
}// if valid hDevice
//The thread terminated by itself, delete resources
CloseHandle(hFile);
CloseHandle(hDevice);
threadParam->threadFile1=NULL;
threadParam->hThread=NULL;
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
testDlg->m_InStartBtn.EnableWindow(TRUE);
testDlg->m_InStopBtn.EnableWindow(FALSE);
if (testDlg->m_MainWrite.bInThread==FALSE) {
testDlg->m_LoopStartBtn.EnableWindow(TRUE);
}
return 0;
}
UINT WritePipe(void * pParam)
{
char temp [80],cc[80];
BOOLEAN bResult = FALSE;
ULONG nBytes = 0, ulCount = 0, index, cnt, seq;
struct CThreadParam * threadParam;
CD12TestDlg* testDlg;
unsigned char rNum;
HANDLE hFile, hDevice;
DWORD time0, time1;
IO_BLOCK ioBlock;
IO_REQUEST ioRequest;
CTIMER tmcount;
threadParam=(struct CThreadParam *)pParam;
threadParam->bInThread = FALSE;
testDlg = threadParam->testDlg;
threadParam->bUpdate = FALSE;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE)
{
strcpy(temp, "驱动程序打开失败! 不能写数据到 ");
strcat(temp, threadParam->pipe_name);
strcat(temp, " 。");
testDlg->MessageBox((LPCSTR)temp, "测试错误");
return 0;
}
hDevice = open_dev();
if(hDevice == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
strcpy(temp, "设备句柄打开失败! 不能写入 ");
strcat(temp, threadParam->pipe_name);
strcat(temp, "。");
testDlg->MessageBox((LPCSTR)temp, "Test Error");
return 0;
}
threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16);
threadParam->ulCount = 0;
threadParam->total_time = 0;
threadParam->cur_rate = 0;
threadParam->max_rate = 0;
if (hFile != INVALID_HANDLE_VALUE)
{
int buffLen;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -