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

📄 d12testdlg.cpp

📁 可测试D12做的usb板子和PC是否通讯成功含有源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
					break;
				}
				else
				{
					ulCount ++;
					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(hDevice);
	CloseHandle(hFile);
	threadParam->threadFile1=NULL;
	threadParam->hThread=NULL;
	
	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;
	
	testDlg->m_OutStartBtn.EnableWindow(TRUE);
	testDlg->m_OutStopBtn.EnableWindow(FALSE);

	if (testDlg->m_MainRead.bInThread==FALSE) {
		testDlg->m_LoopStartBtn.EnableWindow(TRUE);
	}
	
    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, "驱动程序打开失败! 不能从 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, " 中读出数据。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		return 0;
		
	}
	
	threadParam->pipe_name[5] += 1;
	hWrite = open_file(threadParam->pipe_name);
	if(hWrite == INVALID_HANDLE_VALUE) {
		CloseHandle(hRead);
		
		strcpy(temp, "驱动程序打开失败! 不能写数据到 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, " 。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		return 0;
	}
	
	hDevice = open_dev();
	if(hDevice == INVALID_HANDLE_VALUE) {
		CloseHandle(hRead);
		CloseHandle(hWrite);
		strcpy(temp, "设备句柄打开失败!不能读写 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, "。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		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); 
	threadParam->testDlg->ReadWriteBuff=iobuf;
	
    if (hRead != INVALID_HANDLE_VALUE && hWrite != INVALID_HANDLE_VALUE) {
		int buffLen;
		
		// this is the size of incrementing pattern
		buffLen = threadParam->uiLength - sizeof(ulCount);
		threadParam->threadFile1=hRead;
		threadParam->threadFile2=hWrite;
		
		threadParam->bInThread = TRUE;
		testDlg->m_InStartBtn.EnableWindow(FALSE);
		testDlg->m_OutStartBtn.EnableWindow(FALSE);
		testDlg->m_LoopStartBtn.EnableWindow(FALSE);
		testDlg->m_LoopStopBtn.EnableWindow(TRUE);
		
		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);
				threadParam->threadFile2=NULL;
			}
			
			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, "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);
				threadParam->threadFile1=NULL;
			}

			// 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->threadFile1=NULL;
		threadParam->threadFile2=NULL;
	}
	
	threadParam->bInThread = FALSE;
	threadParam->hThread=NULL;
	free(threadParam->pcIoBuffer);
	free(iobuf);
	threadParam->pcIoBuffer = NULL;
	
	testDlg->m_LoopStartBtn.EnableWindow(TRUE);
	testDlg->m_LoopStopBtn.EnableWindow(FALSE);
	testDlg->m_InStartBtn.EnableWindow(TRUE);
	testDlg->m_OutStartBtn.EnableWindow(TRUE);
	
    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_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_LED1 = FALSE;
	m_LED2 = FALSE;
	m_LED3 = FALSE;
	m_LED4 = FALSE;
	m_LoopTotalBytes = _T("");
	m_LoopRepeatTimes = _T("-1");
	m_OutSize = 64;
	m_InSize = 64;
	m_LoopSize = 64;
	//}}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_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_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);
	DDX_Text(pDX, IDC_EDIT1, m_OutSize);
	DDV_MinMaxInt(pDX, m_OutSize, 1, 64);
	DDX_Text(pDX, IDC_EDIT2, m_InSize);
	DDV_MinMaxInt(pDX, m_InSize, 1, 64);
	DDX_Text(pDX, IDC_EDIT4, m_LoopSize);
	DDV_MinMaxInt(pDX, m_LoopSize, 1, 64);
	//}}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);
	
	m_MainRead.bInThread = FALSE;
	m_MainRead.hThread=NULL;
	m_MainWrite.bInThread = FALSE;
	m_MainWrite.hThread=NULL;
	m_ReadWrite.bInThread = FALSE;
	m_ReadWrite.hThread=NULL;
	m_MainRead.ulCount = 0;
	m_MainWrite.ulCount = 0;
	m_ReadWrite.ulCount = 0;
	
	SetTimer(1, 500, 0);
	
	m_InterruptIn.testDlg = this;
	m_InterruptIn.uiLength = 16;
	strcpy(m_InterruptIn.pipe_name, (LPCSTR)"PIPE00");
	strcpy(m_InterruptIn.driver_name, (LPCSTR)"D12TEST-0");
	m_InterruptIn.bInThread=true;
	m_InterruptIn.threadFile1=NULL;
	m_InterruptIn.threadFile2=NULL;

	CWinThread * wt = AfxBeginThread( 
		InterruptIn, // thread function 
		&m_InterruptIn); // argument to thread function 

	m_InterruptIn.hThread = wt;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CD12TestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else

⌨️ 快捷键说明

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