⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d12testdlg.cpp

📁 该软件代码示范了如何用VC++开发环境进行windows USB 应用程序编写
💻 CPP
📖 第 1 页 / 共 3 页
字号:
						&nBytes,
						NULL);
					threadParam->bInThread = FALSE;
					wsprintf (cc, "Writing ");
					strcat( cc, threadParam->pipe_name);
					strcat( cc, " failed! Aborted.");
					testDlg->MessageBox((LPCSTR)cc, "Test Error");
					break;
				}
				else {
					ulCount ++;
					threadParam->ulCount += nBytes;
					if(time1 != time0) {
						threadParam->cur_rate = (nBytes*1000)/(time1-time0-1);
						if(threadParam->cur_rate > threadParam->max_rate)
							threadParam->max_rate = threadParam->cur_rate;
						threadParam->total_time += (time1-time0-1);
						threadParam->bUpdate = TRUE;
					}
				}
			} while(threadParam->bInThread == TRUE);

		}
    }// if valid hDevice

	//The thread terminated by itself, delete resources
	CloseHandle(hDevice);
	CloseHandle(hFile);

	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;

	testDlg->m_OutStartBtn.EnableWindow(TRUE);
	testDlg->m_OutStopBtn.EnableWindow(FALSE);
	testDlg->m_InStartBtn.EnableWindow(TRUE);
	testDlg->m_InStopBtn.EnableWindow(FALSE);
	testDlg->m_LoopStartBtn.EnableWindow(TRUE);
	testDlg->m_LoopStopBtn.EnableWindow(FALSE);

    return 0; 
} 

UINT ReadWrite(void * pParam) {  
    char temp [80],cc[80];
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0, ulCount = 0, index, pass = 0, fail = 0, repeat, i, cnt, seq;
	struct CThreadParam * threadParam;
	CD12TestDlg* testDlg;
	unsigned char rNum;
	HANDLE hRead, hWrite, hDevice;
	unsigned char *iobuf;
	IO_BLOCK ioBlock;
	IO_REQUEST ioRequest;
		
	threadParam=(struct CThreadParam *)pParam;
	threadParam->bInThread = FALSE;
	testDlg = threadParam->testDlg;
	threadParam->bUpdate = FALSE;

	hRead = open_file(threadParam->pipe_name);
	if(hRead == INVALID_HANDLE_VALUE) {
		strcpy(temp, "Open driver failed! Can't read from ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

	threadParam->pipe_name[5] += 1;
	hWrite = open_file(threadParam->pipe_name);
	if(hWrite == INVALID_HANDLE_VALUE) {
		CloseHandle(hRead);

		strcpy(temp, "Open driver failed! Can't write to ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

	hDevice = open_dev();
	if(hDevice == INVALID_HANDLE_VALUE) {
		CloseHandle(hRead);
		CloseHandle(hWrite);
		strcpy(temp, "Open device handle failed! Can't read/write to ");
		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->ulData[0] = 0;
	threadParam->ulData[1] = 0;

    iobuf = (unsigned char *)malloc (threadParam->uiLength + 16); 

    if (hRead != INVALID_HANDLE_VALUE && hWrite != INVALID_HANDLE_VALUE) {
		int buffLen;

		// this is the size of incrementing pattern
		buffLen = threadParam->uiLength - sizeof(ulCount);

		threadParam->bInThread = TRUE;
		testDlg->m_LoopStartBtn.EnableWindow(FALSE);
		testDlg->m_LoopStopBtn.EnableWindow(TRUE);
		testDlg->m_OutStartBtn.EnableWindow(FALSE);
		testDlg->m_OutStopBtn.EnableWindow(FALSE);
		testDlg->m_InStartBtn.EnableWindow(FALSE);
		testDlg->m_InStopBtn.EnableWindow(FALSE);

		repeat = threadParam->ulData[2];
		
		for(i = 0 ; i < repeat && threadParam->bInThread == TRUE ; i ++) {
			// stick unsigned long count at the beginning of buffer
			if(buffLen > 0)
			{
				rNum = 0;

				for(index = 0; index < threadParam->uiLength; index++)
					*(threadParam->pcIoBuffer+index) = rNum++;
				
				cnt = threadParam->uiLength/64;
				
				for(index = 0; index < cnt ; index ++) {
					seq = ulCount*cnt + index;
						// stick unsigned long count at the beginning of buffer
					memcpy(threadParam->pcIoBuffer + index*64, &seq, 
						min(sizeof(ulCount), threadParam->uiLength));
				}
			}

			ioRequest.uAddressL = 0;
			ioRequest.bAddressH = 0;
			ioRequest.uSize = threadParam->uiLength;
			ioRequest.bCommand = 0x80;	//start, write

			ioBlock.uOffset = 0;
			ioBlock.uLength = sizeof(IO_REQUEST);
			ioBlock.pbyData = (PUCHAR)&ioRequest;
			ioBlock.uIndex = SETUP_DMA_REQUEST;

			bResult = DeviceIoControl(hDevice,
					IOCTL_WRITE_REGISTERS,
					(PVOID)&ioBlock,
					sizeof(IO_BLOCK),
					NULL,
					0,
					&nBytes,
					NULL);

			if (bResult != TRUE) {
				threadParam->bInThread = FALSE;
				wsprintf (cc, "Requesting data transfer on ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");
				break;
			}

			bResult = WriteFile(hWrite,
	                      threadParam->pcIoBuffer,
                          threadParam->uiLength,
	                      &nBytes,
	                      NULL);
			
			if (bResult != TRUE) {
				DeviceIoControl(hWrite,
					IOCTL_D12_RESET_PIPE,
					0,
					0,
					0,
					0,
					&nBytes,
					NULL);
				wsprintf (cc, "Writing ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");

				break;
			}

			if(repeat == 1)
				CloseHandle(hWrite);

/*			ioRequest.uAddressL = 0;
			ioRequest.bAddressH = 0;
			ioRequest.uSize = threadParam->uiLength;
			ioRequest.bCommand = 0x81;	//start, read
*/
			ioRequest.uAddressL = 0;
			ioRequest.bAddressH = 0;
			ioRequest.uSize = threadParam->uiLength;
			if (DMAEnabled)
				ioRequest.bCommand = 0x81;
			else 
				ioRequest.bCommand = 0x01;

			ioBlock.uOffset = 0;
			ioBlock.uLength = sizeof(IO_REQUEST);
			ioBlock.pbyData = (PUCHAR)&ioRequest;
			ioBlock.uIndex = SETUP_DMA_REQUEST;

			bResult = DeviceIoControl(hDevice,
					IOCTL_WRITE_REGISTERS,
					(PVOID)&ioBlock,
					sizeof(IO_BLOCK),
					NULL,
					0,
					&nBytes,
					NULL);

			if (bResult != TRUE) {
				threadParam->bInThread = FALSE;
				wsprintf (cc, "Requesting data transfer on ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");
				break;
			}

			bResult = ReadFile(hRead,
	                      iobuf,
                          threadParam->uiLength,
	                      &nBytes,
	                      NULL);
			
			if (bResult != TRUE) {
				DeviceIoControl(hRead,
					IOCTL_D12_RESET_PIPE,
					0,
					0,
					0,
					0,
					&nBytes,
					NULL);
				wsprintf (cc, "Reading ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");

				break;
			}

			if(repeat == 1)
				CloseHandle(hRead);

			// verify buffer
			if(!memcmp(threadParam->pcIoBuffer, iobuf, threadParam->uiLength))
				pass++;
			else
				fail++;

			threadParam->ulData[0] = pass;
			threadParam->ulData[1] = fail;

			ulCount ++;
			threadParam->ulCount += nBytes;

			threadParam->bUpdate = TRUE;
	
		}


    }// if valid hDevice

	//The thread terminated by itself, delete resources
	if(repeat != 1) {
		CloseHandle(hRead);
		CloseHandle(hWrite);
		CloseHandle(hDevice);
	}

	threadParam->bInThread = FALSE;
	free(threadParam->pcIoBuffer);
	free(iobuf);
	threadParam->pcIoBuffer = NULL;

	testDlg->m_LoopStartBtn.EnableWindow(TRUE);
	testDlg->m_LoopStopBtn.EnableWindow(FALSE);
	testDlg->m_OutStartBtn.EnableWindow(TRUE);
	testDlg->m_OutStopBtn.EnableWindow(FALSE);
	testDlg->m_InStartBtn.EnableWindow(TRUE);
	testDlg->m_InStopBtn.EnableWindow(FALSE);

    return 0; 
} 


UINT KillDialog(void * pParam) {  
	struct CKillDialogParam * killParam;
	struct CThreadParam * threadParam;
	CD12TestDlg* testDlg;
	int i;

	killParam = (struct CKillDialogParam *)pParam;

	for(i = 0; i < 3; i ++) {
		threadParam=(struct CThreadParam *)(killParam->tp[i]);

		if(threadParam->bInThread != FALSE) {
			threadParam->bInThread = FALSE;
			WaitForSingleObject(threadParam->hThread, INFINITE);
		}
	}

	testDlg = killParam->testDlg;
	testDlg -> EndDialog(0);

	return 0;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CD12TestDlg dialog

CD12TestDlg::CD12TestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CD12TestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CD12TestDlg)
	m_OutBufferSize = _T("64");
	m_InBufferSize = _T("64");
	m_OutTotalBytes = _T("");
	m_OutCurSpeed = _T("");
	m_OutAverageSpeed = _T("");
	m_OutMaxSpeed = _T("");
	m_InTotalBytes = _T("");
	m_InCurSpeed = _T("");
	m_InAverageSpeed = _T("");
	m_InMaxSpeed = _T("");
	m_KeyStatus = _T("");
	m_PassCount = _T("");
	m_FailCount = _T("");
	m_LoopBufSize = _T("64");
	m_LED1 = FALSE;
	m_LED2 = FALSE;
	m_LED3 = FALSE;
	m_LED4 = FALSE;
	m_LoopTotalBytes = _T("");
	m_LoopRepeatTimes = _T("-1");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CD12TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CD12TestDlg)
	DDX_Control(pDX, IDC_CHECK4, m_ctrlLED4);
	DDX_Control(pDX, IDC_CHECK3, m_ctrlLED3);
	DDX_Control(pDX, IDC_BUTTON6, m_LoopStopBtn);
	DDX_Control(pDX, IDC_BUTTON5, m_LoopStartBtn);
	DDX_Control(pDX, IDC_BUTTON4, m_InStopBtn);
	DDX_Control(pDX, IDC_BUTTON3, m_InStartBtn);
	DDX_Control(pDX, IDC_BUTTON2, m_OutStopBtn);
	DDX_Control(pDX, IDC_BUTTON1, m_OutStartBtn);
	DDX_Text(pDX, IDC_EDIT1, m_OutBufferSize);
	DDX_Text(pDX, IDC_EDIT2, m_InBufferSize);
	DDX_Text(pDX, IDC_STATIC1, m_OutTotalBytes);
	DDX_Text(pDX, IDC_STATIC2, m_OutCurSpeed);
	DDX_Text(pDX, IDC_STATIC3, m_OutAverageSpeed);
	DDX_Text(pDX, IDC_STATIC4, m_OutMaxSpeed);
	DDX_Text(pDX, IDC_STATIC5, m_InTotalBytes);
	DDX_Text(pDX, IDC_STATIC6, m_InCurSpeed);
	DDX_Text(pDX, IDC_STATIC7, m_InAverageSpeed);
	DDX_Text(pDX, IDC_STATIC8, m_InMaxSpeed);
	DDX_Text(pDX, IDC_STATIC9, m_KeyStatus);
	DDX_Text(pDX, IDC_STATIC10, m_PassCount);
	DDX_Text(pDX, IDC_STATIC11, m_FailCount);
	DDX_Text(pDX, IDC_EDIT4, m_LoopBufSize);
	DDX_Check(pDX, IDC_CHECK1, m_LED1);
	DDX_Check(pDX, IDC_CHECK2, m_LED2);
	DDX_Check(pDX, IDC_CHECK3, m_LED3);
	DDX_Check(pDX, IDC_CHECK4, m_LED4);
	DDX_Text(pDX, IDC_STATIC12, m_LoopTotalBytes);
	DDX_Text(pDX, IDC_EDIT5, m_LoopRepeatTimes);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CD12TestDlg, CDialog)
	//{{AFX_MSG_MAP(CD12TestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnOutStart)
	ON_BN_CLICKED(IDC_BUTTON2, OnOutStop)
	ON_BN_CLICKED(IDC_BUTTON3, OnInStart)
	ON_BN_CLICKED(IDC_BUTTON4, OnInStop)
	ON_EN_KILLFOCUS(IDC_EDIT1, OnOutBufferSize)
	ON_EN_KILLFOCUS(IDC_EDIT2, OnInBufferSize)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON5, OnLoopStart)
	ON_BN_CLICKED(IDC_BUTTON6, OnLoopStop)
	ON_EN_KILLFOCUS(IDC_EDIT4, OnLoopBufSize)
	ON_BN_CLICKED(IDC_CHECK1, OnLED1)
	ON_BN_CLICKED(IDC_CHECK2, OnLED2)
	ON_BN_CLICKED(IDC_CHECK3, OnLED3)
	ON_BN_CLICKED(IDC_CHECK4, OnLED4)
	ON_EN_KILLFOCUS(IDC_EDIT5, OnLoopRepeatTimes)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CD12TestDlg message handlers

BOOL CD12TestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	CString strAboutMenu;
	strAboutMenu.LoadString(IDS_ABOUTBOX);
	if (!strAboutMenu.IsEmpty())
	{
		pSysMenu->AppendMenu(MF_SEPARATOR);
		pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
	}

	// 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_OutStopBtn.EnableWindow(FALSE);
	m_InStopBtn.EnableWindow(FALSE);
	m_LoopStopBtn.EnableWindow(FALSE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -