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

📄 d12testdlg.cpp

📁 可测试D12做的usb板子和PC是否通讯成功含有源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
//*************************************************************************
//
//                  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"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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 = 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 = 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
				{
					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 = WriteFile(hFile,
			threadParam->pcIoBuffer,
			threadParam->uiLength,
			&nBytes,
			NULL);
		
		if (bResult != TRUE) {
			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;
	
	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;
		
			bResult = DeviceIoControl(hDevice,
				IOCTL_WRITE_REGISTERS,
				(PVOID)&ioBlock,
				sizeof(IO_BLOCK),
				NULL,
				0,
				&nBytes,
				NULL);
			
			if (bResult != TRUE) {
				threadParam->bInThread = FALSE;
				wsprintf (cc, "在 ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " 上请求数据传送失败! 已放弃。");
				testDlg->MessageBox((LPCSTR)cc, "测试错误");
				break;
			}
			
			time0 = timeGetTime();
			bResult = ReadFile(hFile,
				threadParam->pcIoBuffer,
				threadParam->uiLength,
				&nBytes,
				NULL);
			time1 = timeGetTime();

			if (bResult != TRUE) {
				DeviceIoControl(hFile,
					IOCTL_D12_RESET_PIPE,
					0,
					0,
					0,
					0,
					&nBytes,
					NULL);
				threadParam->bInThread = FALSE;
				wsprintf (cc, "Reading ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");
				break;
			} 
			else
			{
				threadParam->ulCount += nBytes;
				threadParam->total_time += (time1-(threadParam->Last_time));
				threadParam->Last_time=time1;
				if(time1 != time0 && (time1-time0-1)!=0)
				{
					threadParam->cur_rate = (nBytes*1000)/(time1-time0);
					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;
	
	
	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;
		
		// this is the size of incrementing pattern
		buffLen = threadParam->uiLength - sizeof(ulCount);
		
		threadParam->bInThread = TRUE;
		threadParam->threadFile1=hFile;
		testDlg->m_OutStartBtn.EnableWindow(FALSE);
		testDlg->m_OutStopBtn.EnableWindow(TRUE);
		testDlg->m_LoopStartBtn.EnableWindow(FALSE);
		testDlg->m_LoopStopBtn.EnableWindow(FALSE);
		
		if(threadParam->uiLength == 0)
		{
		}
		else
		{
			threadParam->Last_time=timeGetTime();
			do 
			{
				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);
					wsprintf (cc, threadParam->pipe_name);
					strcat( cc, " 请求数据传送失败! 已放弃。");
					testDlg->MessageBox((LPCSTR)cc, "测试错误");
					break;
				}
				
				time0 = timeGetTime();
				bResult = WriteFile(hFile,
					threadParam->pcIoBuffer,
					threadParam->uiLength,
					&nBytes,
					NULL);
				
				time1 = timeGetTime();
				if (bResult != TRUE)
				{
					DeviceIoControl(hFile,
						IOCTL_D12_RESET_PIPE,
						0,
						0,
						0,
						0,
						&nBytes,
						NULL);
					threadParam->bInThread = FALSE;  //结束线程
					wsprintf (cc, "写 ");
					strcat( cc, threadParam->pipe_name);
					strcat( cc, " 失败! 已放弃。");
					testDlg->MessageBox((LPCSTR)cc, "测试错误");

⌨️ 快捷键说明

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