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

📄 serialtestdlg.cpp

📁 EVC下的串口测试工具
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}

	if(!SetCommMask( m_hComm, EV_RXCHAR | EV_TXEMPTY))
		fResult = FALSE;
	if(!SetupComm( m_hComm, 20480, 20480000 ))
		fResult = FALSE;
	if(!PurgeComm( m_hComm, PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR))
		fResult = FALSE;

	// timeout 汲沥.
	GetCommTimeouts(m_hComm, &timeouts);
	timeouts.ReadIntervalTimeout = 100;//0x1000;//0xFFFFFFFF;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.ReadTotalTimeoutConstant = 0;
	//timeouts.ReadTotalTimeoutConstant = 0x0000FFFF;
	timeouts.WriteTotalTimeoutMultiplier = 100;
	timeouts.WriteTotalTimeoutConstant = 0;
	//timeouts.WriteTotalTimeoutConstant = 0x0000FFFF;
	SetCommTimeouts( m_hComm, &timeouts);

	// dcb 汲沥
	dcb.DCBlength = sizeof(DCB);
	GetCommState( m_hComm, &dcb);	// 抗傈 蔼阑 佬澜.
	dcb.BaudRate = baud;
	dcb.ByteSize = 8;
	dcb.Parity = 0;
	dcb.StopBits = 0;
	dcb.fInX = dcb.fOutX = 1;		// Xon, Xoff 荤侩.
	dcb.XonChar = ASCII_XON;
	dcb.XoffChar = ASCII_XOFF;
	dcb.XonLim = 100;
	dcb.XoffLim = 100;
	dcb.fAbortOnError = FALSE;
	// Auto flow Controll 汲沥
#ifdef UNDER_CE
	if(m_Afc == TRUE)
	{
		dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
		dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
		dcb.fOutxCtsFlow = TRUE;		
		dcb.fOutxDsrFlow = TRUE;
	}
	else
	{
		dcb.fRtsControl = RTS_CONTROL_DISABLE;
		dcb.fDtrControl = DTR_CONTROL_DISABLE;
		dcb.fOutxCtsFlow = FALSE;		
		dcb.fOutxDsrFlow = FALSE;
	}
#endif
	if (!SetCommState( m_hComm, &dcb))	
		fResult = FALSE;

/* 吝汗登瘤 臼档废 技付器绢 积己 */		
#ifdef UNDER_CE
	srand(GetTickCount());
#else
	srand((unsigned)time(NULL));
#endif
	CString name;
	int res;
	do
	{
		name.Format(TEXT("Comm_Sem_%d"), rand()%10000);
		hCommSem = CreateSemaphore(NULL, 1, 1, name);
		if(hCommSem == NULL)
			fResult = FALSE;
		else
			res = GetLastError();
	}while(res == ERROR_ALREADY_EXISTS);

	do
	{
		name.Format(TEXT("Read_Sem_%d"), rand()%10000);
		hReadSem = CreateSemaphore(NULL, 1, 1, name);
		if(hReadSem == NULL)
			fResult = FALSE;
		else
			res = GetLastError();
	}while(res == ERROR_ALREADY_EXISTS);

	do
	{
		name.Format(TEXT("Write_Sem_%d"), rand()%10000);
		hWriteSem = CreateSemaphore(NULL, 1, 1, name);
		if(hWriteSem == NULL)
			fResult = FALSE;
		else
			res = GetLastError();
	}while(res == ERROR_ALREADY_EXISTS);

	if(fResult == FALSE)
	{
		CString msg;
		msg.Format(TEXT("ERROR : Setting %s port failed.\r\n"), port);
		LogMessage(&msg, TRUE);
		ClosePort();
		return;
	}


	m_fConnect = TRUE;

	pRead->Clear();
	pWrite->Clear();

	pRead->SetReady();
	pWrite->SetReady();

	AfxBeginThread(WhatchThread, this);

	CButton *pBtn = (CButton *)GetDlgItem(IDC_BTNCONNECT);
	pBtn->SetWindowTextW(TEXT("DISCONNECT"));
	
	( (CComboBox *)GetDlgItem(IDC_CMBPORT) )->EnableWindow(FALSE);
	( (CComboBox *)GetDlgItem(IDC_CMBBAUD) )->EnableWindow(FALSE);
//	CButton *pAfc = (CButton *)GetDlgItem(IDC_CHKAFC);
//	pAfc->EnableWindow(!m_fConnect);

	((CButton *)GetDlgItem(IDC_BTNSEND))->EnableWindow(TRUE);
	((CButton *)GetDlgItem(IDC_BTNREAD))->EnableWindow(TRUE);
	((CButton *)GetDlgItem(IDC_BTNREAD))->SetWindowTextW(TEXT("READ ON"));

	((CButton *)GetDlgItem(IDC_CHKSYNC))->EnableWindow(TRUE);
	((CButton *)GetDlgItem(IDC_CHKLOOPBACK))->EnableWindow(TRUE);

	msg.Format(TEXT("Open %s port successed.\r\n"), port);
	LogMessage(&msg);

}

void CSerialTestDlg::ClosePort()
{
	CString msg;
	msg.Format(TEXT("Wait for Finish porcessing...\r\n"));
	LogMessage(&msg);

	m_fConnect = FALSE;
	fRead = FALSE;

	while(fReading || fWrite)
		Sleep(10);
	SetCommMask( m_hComm, 0);

	EscapeCommFunction( m_hComm, CLRDTR ) ;
	PurgeComm( m_hComm,					
		PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR);

	pRead->Clear();
	pWrite->Clear();

	if(m_hComm != INVALID_HANDLE_VALUE)
		CloseHandle( m_hComm);
	m_hComm = INVALID_HANDLE_VALUE;

	if(hCommSem != NULL)
		CloseHandle(hCommSem);
	if(hReadSem != NULL)
		CloseHandle(hReadSem);
	if(hWriteSem != NULL)
		CloseHandle(hWriteSem);

	CButton *pBtn = (CButton *)GetDlgItem(IDC_BTNCONNECT);
	pBtn->SetWindowTextW(TEXT("CONNECT"));

	( (CComboBox *)GetDlgItem(IDC_CMBPORT) )->EnableWindow(TRUE);
	( (CComboBox *)GetDlgItem(IDC_CMBBAUD) )->EnableWindow(TRUE);
//	CButton *pAfc = (CButton *)GetDlgItem(IDC_CHKAFC);
//	pAfc->EnableWindow(!m_fConnect);

	((CButton *)GetDlgItem(IDC_BTNSEND))->EnableWindow(FALSE);
	((CButton *)GetDlgItem(IDC_BTNREAD))->EnableWindow(FALSE);
	((CButton *)GetDlgItem(IDC_BTNREAD))->SetWindowTextW(TEXT("READ ON"));

	((CButton *)GetDlgItem(IDC_CHKSYNC))->EnableWindow(TRUE);
	((CButton *)GetDlgItem(IDC_CHKLOOPBACK))->EnableWindow(TRUE);

	msg.Format(TEXT("Close port successed.\r\n"));
	LogMessage(&msg);
}

void CSerialTestDlg::OnBnClickedBtnsend()
{
	UpdateData(TRUE);
	
	if(m_fConnect == TRUE)
	{
		if( m_DataSize > MAX_BUFF_SIZE)
		{
			CString msg;
			msg.Format( TEXT("ERROR : Data Size must be less than %d byte.\r\n"), MAX_BUFF_SIZE);
			LogMessage(&msg, TRUE);
			return;
		}
		if( m_DataSize < BUFF_HEAD_SIZE)
		{
			CString msg;
			msg.Format( TEXT("ERROR : Data Size must be mort than %d byte.\r\n"), MAX_BUFF_SIZE);
			LogMessage(&msg, TRUE);
			return;
		}

		if(m_Repeat == REPEAT_TIME)
			SetTimer(ID_TIMER, m_RepeatTime * 60 * 1000, NULL);

		/* 鸥捞赣 犬牢 函荐 */
		m_fRepeat = TRUE;
		AfxBeginThread(WriteThread, this);
	}
}

void CSerialTestDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 咯扁俊 皋矫瘤 贸府扁 内靛甫 眠啊 棺/肚绰 扁夯蔼阑 龋免钦聪促.

	m_fRepeat = FALSE;
	KillTimer(ID_TIMER);
	CDialog::OnTimer(nIDEvent);
}

void CSerialTestDlg::OnBnClickedChksync()
{
	// TODO: 咯扁俊 牧飘费 舅覆 贸府扁 内靛甫 眠啊钦聪促.
	UpdateData(TRUE);
	if(m_fSync == TRUE)
	{
		m_fLoopBack = FALSE;
		GetDlgItem(IDC_EDTTIMEOUT)->EnableWindow(TRUE);
	}
	else
		GetDlgItem(IDC_EDTTIMEOUT)->EnableWindow(FALSE);
	UpdateData(FALSE);
}

void CSerialTestDlg::OnBnClickedChkloopback()
{
	// TODO: 咯扁俊 牧飘费 舅覆 贸府扁 内靛甫 眠啊钦聪促.
	UpdateData(TRUE);
	if(m_fLoopBack == TRUE)
	{
		m_fSync = FALSE;
		GetDlgItem(IDC_EDTTIMEOUT)->EnableWindow(TRUE);
	}
	else
		GetDlgItem(IDC_EDTTIMEOUT)->EnableWindow(FALSE);
	UpdateData(FALSE);
}

UINT StressThread(LPVOID lParam)
{
	int cnt = 0;
	while(fStress)
	{
		for(int i=0; i<10000; i++)
			cnt = cnt + i;
	}
	return 0;
}

void CSerialTestDlg::OnBnClickedBtnstress()
{
	// TODO: 咯扁俊 牧飘费 舅覆 贸府扁 内靛甫 眠啊钦聪促.
	CButton *pBtn = (CButton *)GetDlgItem(IDC_BTNSTRESS);

	fStress = !fStress;
	
	if(fStress == TRUE)
	{
		pBtn->SetWindowTextW(TEXT("Stress Off"));
		AfxBeginThread(StressThread, NULL);
	}
	else
		pBtn->SetWindowTextW(TEXT("Stress On"));
}

UINT WriteThread(LPVOID lParam)
{
	CSerialTestDlg *pDlg = (CSerialTestDlg *)lParam;
	
	DWORD totalWrite = 0;
	DWORD dwWritten = 0;
	DWORD buffSize = pDlg->m_DataSize;
	DWORD blockSize = pDlg->m_BlockSize;
	DWORD replyTimeout = pDlg->m_timeout;
	PBYTE pBuff = NULL;
	CString msg;
	unsigned int offset;

	int baud;
	CString sbaud;
	pDlg->m_CmbBaud.GetWindowTextW(sbaud);
	baud = _wtoi(sbaud);

	pBuff = (PBYTE)LocalAlloc(LPTR, buffSize);
	if(pBuff == NULL)
	{
		msg.Format(TEXT("ERROR : Fail to memory allocate.\r\n"));
		pDlg->LogMessage(&msg, TRUE);
		pDlg->ClosePort();
		return 0;
	}

	*(DWORD *)(pBuff) = buffSize;
	for(offset=0; offset<BUFF_HEAD_SIZE; offset++)
	{
		if( (*(BYTE *)(pBuff+offset) == ASCII_XON) || (*(BYTE *)(pBuff+offset) == ASCII_XOFF) )
		{
			msg.Format(TEXT("ERROR : Cannot Send %d byte.\r\n"), buffSize);
			pDlg->LogMessage(&msg, TRUE);
			pDlg->m_fRepeat = FALSE;
			break;
		}
	}
	for(offset=BUFF_HEAD_SIZE; offset<buffSize; offset++)
	{
		*(BYTE *)(pBuff+offset) = offset%26 + 'A';
	}
	*(BYTE *)(pBuff+offset-1) = '\n';
	
	int repeatCount;
	if(pDlg->m_Repeat == REPEAT_TIME)
		repeatCount = MAX_INT;
	else
		repeatCount = pDlg->m_RepeatCount;

	/* 肯丰 登瘤 臼篮 write 檬扁拳. */
	PurgeComm(pDlg->m_hComm, PURGE_TXABORT | PURGE_TXCLEAR);
	pWrite->SetReady();

	/* 览翠阑 罐扁 困秦 肯丰登瘤 臼篮 Read何盒 檬扁拳. */
	if( (pDlg->m_fSync == TRUE) || (pDlg->m_fLoopBack == TRUE) )
	{
		( (CButton *)(pDlg->GetDlgItem(IDC_BTNREAD)) )->EnableWindow(FALSE);
		PurgeComm(pDlg->m_hComm, PURGE_RXABORT | PURGE_RXCLEAR);
		pRead->SetReady();
		fRead = TRUE;
	}

	((CButton *)pDlg->GetDlgItem(IDC_BTNSEND))->EnableWindow(FALSE);
	((CButton *)pDlg->GetDlgItem(IDC_CHKSYNC))->EnableWindow(FALSE);
	((CButton *)pDlg->GetDlgItem(IDC_CHKLOOPBACK))->EnableWindow(FALSE);

	fWrite = TRUE;
	for(int i=0; i<repeatCount; i++)
	{
		if (pDlg->m_fConnect == FALSE || pDlg->m_fRepeat == FALSE)
		{
			break;
		}

		CString time;
		GetTimeText(&time);
		msg.Format(TEXT("Send Data %4d : %s\r\n"), i, time);
		pDlg->LogMessage(&msg);

		DWORD sum = 0;
		blockSize = pDlg->m_BlockSize;
		do
		{
			if(pDlg->m_fConnect == FALSE)
				break;

			if(sum + blockSize >= buffSize)
				blockSize = buffSize - sum;

			QNode buff;
			buff.data = (PBYTE)LocalAlloc(LPTR, blockSize);
			ByteCpy(pBuff+sum, buff.data, blockSize);
			buff.size =blockSize;

//			msg.Format(TEXT("Push %dbyte To Write Queue..\r\n"), blockSize);
//			pDlg->LogMessage(&msg);

			if( pWrite->WaitForSpace(INFINITE) == WAIT_OBJECT_0)
				break;
			WaitForSingleObject(hWriteSem, INFINITE);
			pWrite->Push(&buff);
			ReleaseSemaphore(hWriteSem, 1, NULL);

			sum += blockSize;
		}while(sum < buffSize);
		totalWrite += sum;

		msg.Format(TEXT("%d byte Pushed\r\n"), sum);
		pDlg->LogMessage(&msg);
		
		if(pDlg->m_fSync == TRUE)
		{
			BYTE buff = 0;;

			msg.Format(TEXT("Wating Reply\r\n"));
			pDlg->LogMessage(&msg);

			pRead->QueueReader(&buff, 1,replyTimeout);

			if(pDlg->m_fConnect == FALSE)
				break;
			if(buff == 0)
			{
				msg.Format(TEXT("ERROR : No Reply.\r\n"), buff);
				pDlg->LogMessage(&msg, TRUE);
			}
			else
			{
				msg.Format(TEXT("Reply : %c\r\n"), buff);
				pDlg->LogMessage(&msg);
			}

			if(buff == '0')
			{
				msg.Format(TEXT("Send Data was Complete\r\n"));
				pDlg->LogMessage(&msg);
			}
			else
			{
				msg.Format(TEXT("Send Data was not Complete\r\n"));
				pDlg->LogMessage(&msg, TRUE);
			}			
		}
		if(pDlg->m_fLoopBack == TRUE)
		{
			DWORD dwRead;
			PBYTE rBuff;
			rBuff = (PBYTE)LocalAlloc(LPTR, buffSize);

			msg.Format(TEXT("Wating Reply\r\n"));
			pDlg->LogMessage(&msg);

			dwRead = pRead->QueueReader(rBuff, sum, replyTimeout);

			if(pDlg->m_fConnect == FALSE)
				break;
			msg.Format(TEXT("Reply : %d byte\r\n"), dwRead);
			pDlg->LogMessage(&msg);

			if(CheckBuffer(rBuff, sum))
			{
				msg.Format(TEXT("Send Data was Complete\r\n"));
				pDlg->LogMessage(&msg);
			}
			else
			{
				msg.Format(TEXT("Send Data was not Complete\r\n"));
				pDlg->LogMessage(&msg, TRUE);
//#ifdef _DEBUG
				FILE *fout;
				CString name;
				name.Format(TEXT("ReplyMemory.txt"));
				fout = _wfopen(name, TEXT("a"));
				fprintf(fout, "\r\n\r\n==============================================\r\n");
				DumpMemory(rBuff, fout, dwRead, 26);
//#endif
			}
			LocalFree(rBuff);
		}

		while(pWrite->IsEmpty() == FALSE)
			Sleep(10);

		Sleep(pDlg->m_RepeatTerm);
	}
	fWrite = FALSE;
	if( (pDlg->m_fSync == TRUE) || (pDlg->m_fLoopBack == TRUE) )
		fRead = FALSE;

	if( pDlg->m_fConnect == TRUE )
	{
		( (CButton *)(pDlg->GetDlgItem(IDC_BTNREAD)) )->EnableWindow(TRUE);
		( (CButton *)pDlg->GetDlgItem(IDC_BTNSEND) )->EnableWindow(TRUE);
	}
	if(fRead == FALSE)
	{
		((CButton *)pDlg->GetDlgItem(IDC_CHKSYNC))->EnableWindow(TRUE);
		((CButton *)pDlg->GetDlgItem(IDC_CHKLOOPBACK))->EnableWindow(TRUE);
	}


	if(pBuff != NULL)
		LocalFree(pBuff);

	return dwWritten;
}

UINT ReadThread(LPVOID lParam)
{

	int ecnt = 0;
	CSerialTestDlg *pDlg = (CSerialTestDlg *)lParam;

	BOOL chkBuffer = FALSE;
	DWORD dwRead;
	DWORD totalRead = 0;
	DWORD buffSize = MAX_BUFF_SIZE;
	PBYTE pBuff = NULL;
	CString msg;

⌨️ 快捷键说明

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