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

📄 txrxdlg.cpp

📁 一个非常好的串口程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	case 3:
		hCom=CreateFile("\\.\\COM4:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	default:
		hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	}
	if (hCom==INVALID_HANDLE_VALUE)
	{
		MessageBox("Invalid Handle Value For Communication Port.Program Fails!");
		return ;
	}

	lpDcb=new(DCB);
	if (!GetCommState(hCom,lpDcb))
	{
		MessageBox("Program Fails To Get Communication Port State.");
		return ;
	}
	
	CString strTemp=_T("baud= parity= data= stop=");
	strTemp.Insert(strTemp.Find(_T("baud="))+5,m_Baud);
	
	if (m_RadioData81.GetCheck()) 
	{
		strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
		strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("1"));
	}
	else
	{
		strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
		strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("2"));
	}

	if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityN,IDC_RadioParityN))
		strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
	else 
	{
		if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityE,IDC_RadioParityE))
			strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("E"));
		else
			if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityO,IDC_RadioParityO))
				strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("O"));
			else
				strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
	}

	if (!BuildCommDCB(strTemp,lpDcb))
	{
		MessageBox("Program Fails To Build Communication Port State.");
		return ;
	}

	if (!SetCommState(hCom,lpDcb))
	{
		MessageBox("Program Fails To Set Communication Port State.");
		return ;
	}

	
	BYTE* Buffer;
	DWORD dwTimeout=1000;
	DWORD dwRead;
	DWORD dwResult;
	OVERLAPPED OL={0};
	long BufferIndex=0;
	BOOL blContinue=TRUE;

	m_EditTimeout.GetWindowText(strTemp);
	dwTimeout=atol(strTemp);
	Buffer=(BYTE*) malloc(m_BytesOfMemory);
	if(Buffer==NULL)
	{
		MessageBox(_T("Program Failed!Can Not Allocate Memory"));
		return;
	}

	OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
	if (OL.hEvent ==NULL )
	{
		MessageBox(_T("Program Failed!Can Not Create Event"));
		return ;
	}

	CFile bf;
	if (!bf.Open(m_FileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
	{
		MessageBox(_T("Program Failed!Can Create File"));
		return;
	}
	
	m_ProgressRx.SetRange(0,100);
	do
	{
		m_ProgressRx.SetPos(0);
		m_LEDReceive.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconReceive)));
		dwRead=ReadFile(hCom,Buffer,1, &dwRead, &OL);//m_BytesOfMemory
		if (dwRead==0)
		{
			if (GetLastError() != ERROR_IO_PENDING) 
				dwRead=0 ;
			else
			{	// Write is pending.
				dwResult = WaitForSingleObject(OL.hEvent, dwTimeout);
				switch(dwResult)
				{
				case WAIT_OBJECT_0:
					if (!GetOverlappedResult(hCom, &OL, &dwRead, FALSE))
						dwRead=0 ;
					break;
				case WAIT_ABANDONED:
					dwRead=0 ;
					break;
				case WAIT_TIMEOUT:
					dwRead=0 ;
					break;
				default:
					dwRead=0 ;
					break;
				}
			}
			if (dwRead==0)
				blContinue=FALSE;
			else
			{
				BufferIndex+=dwRead;
				bf.Write(Buffer,dwRead);
			}
			m_LEDReceive.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
		}
		else
		{
			BufferIndex+=dwRead;
			m_LEDReceive.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
			bf.Write(Buffer,dwRead);
		}
		m_ProgressRx.SetPos((int)((dwRead*100)/m_BytesOfMemory));
	}while(blContinue);

	Sleep(700);
	m_ProgressRx.SetPos(0);
	bf.Close();
	CloseHandle(OL.hEvent);
	CloseHandle(hCom);
}

void CTxRxDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg* pAboutDlg;
	pAboutDlg=new CAboutDlg;
	CTxRxDlg::EndDialog(1);	
	pAboutDlg->DoModal();
}

void CTxRxDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	
	if (m_FileName.IsEmpty())
	{
		MessageBox(_T("File has been not specified.Cancel Operation!"));
		return;
	}
	HANDLE hCom;
	LPDCB lpDcb;
	int i=m_ComboCom.GetCurSel();
	switch (i)
	{
	case 0:
		hCom=CreateFile("\\.\\COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	case 1:
		hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	case 2:
		hCom=CreateFile("\\.\\COM3:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	case 3:
		hCom=CreateFile("\\.\\COM4:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	default:
		hCom=CreateFile("\\.\\COM2:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
		break;
	}
	if (hCom==INVALID_HANDLE_VALUE)
	{
		MessageBox("Invalid Handle Value For Communication Port.Program Fails!");
		return ;
	}

	lpDcb=new(DCB);
	if (!GetCommState(hCom,lpDcb))
	{
		MessageBox("Program Fails To Get Communication Port State.");
		return ;
	}
	
	CString strTemp=_T("baud= parity= data= stop=");
	strTemp.Insert(strTemp.Find(_T("baud="))+5,m_Baud);
	
	if (m_RadioData81.GetCheck()) 
	{
		strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
		strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("1"));
	}
	else
	{
		strTemp.Insert(strTemp.Find(_T("data="))+5,_T("8"));
		strTemp.Insert(strTemp.Find(_T("stop="))+5,_T("2"));
	}

	if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityN,IDC_RadioParityN))
		strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
	else 
	{
		if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityE,IDC_RadioParityE))
			strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("E"));
		else
			if (CTxRxDlg::GetCheckedRadioButton(IDC_RadioParityO,IDC_RadioParityO))
				strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("O"));
			else
				strTemp.Insert(strTemp.Find(_T("parity="))+7,_T("N"));
	}
	if (!BuildCommDCB(strTemp,lpDcb))
	{
		MessageBox("Program Fails To Build Communication Port State.");
		return ;
	}

	if (!SetCommState(hCom,lpDcb))
	{
		MessageBox("Program Fails To Set Communication Port State.");
		return ;
	}
	
	BYTE* Buffer;
	DWORD dwTimeout=1000;
	DWORD dwWritten,dwRead;
	DWORD dwFileLen;
	DWORD dwResult;
	OVERLAPPED OL={0};
	int Perc=0;
	long BufferIndex=0;
	BOOL blContinue=TRUE;

	m_EditTimeout.GetWindowText(strTemp);
	dwTimeout=atol(strTemp);
	Buffer=(BYTE*) malloc(m_BytesOfMemory);
	if(Buffer==NULL)
	{
		MessageBox(_T("Program Failed!Can Not Allocate Memory"));
		return;
	}

	OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
	if (OL.hEvent ==NULL )
	{
		MessageBox(_T("Program Failed!Can Not Create Event"));
		return ;
	}

	CFile bf;
	if (!bf.Open(m_FileName,CFile::modeRead|CFile::typeBinary)) 
	{
		MessageBox(_T("Program Failed!Can't Open Source File"));
		return;
	}

	dwFileLen=bf.GetLength();
	m_ProgressTx.SetRange(0,100);
	do
	{

		dwRead=bf.ReadHuge(Buffer,m_BytesOfMemory);
		if (dwRead>0)
		{
			m_LEDSend.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconSend)));
			WriteFile(hCom,Buffer,dwRead,&dwWritten,&OL);
			if (dwWritten==0)
			{
				if (GetLastError() != ERROR_IO_PENDING) 
					dwWritten=0 ;
				else
				{	// Write is pending.
					dwResult = WaitForSingleObject(OL.hEvent, INFINITE);
					switch(dwResult)
					{
					case WAIT_OBJECT_0:
						if (!GetOverlappedResult(hCom, &OL, &dwWritten, FALSE))
							dwWritten=0 ;
						break;
					case WAIT_ABANDONED:
						dwWritten=0 ;
						break;
					case WAIT_TIMEOUT:
						dwWritten=0 ;
						break;
					default:
						dwWritten=0 ;
						break;
					}
				}
				BufferIndex+=dwWritten;
				if (dwWritten==0)
					blContinue=FALSE;
				m_LEDSend.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
			}
			else
			{
				BufferIndex+=dwWritten;
				m_LEDSend.SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_IconOff)));
				bf.Write(Buffer,dwRead);
			}
			Perc=(int)((BufferIndex*100)/dwFileLen);
			m_ProgressTx.SetPos(Perc);
		}
		else
			blContinue=FALSE;
	}while(blContinue);
	Sleep(700);
	m_ProgressTx.SetPos(0);
	bf.Close();
	CloseHandle(OL.hEvent);
	CloseHandle(hCom);
}

void CTxRxDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnClose();
	CAboutDlg* pAboutDlg;
	pAboutDlg=new CAboutDlg;
	pAboutDlg->DoModal();
}

⌨️ 快捷键说明

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