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

📄 comtestdlg.cpp

📁 基于WinCE和ARMV4I的串口调试程序
💻 CPP
字号:
// COMTestDlg.cpp : implementation file
//
/*
ati
at+cmut=0
at+cmut=1
at%tsim
ate0
ate1

  */
#include "stdafx.h"
#include "COMTest.h"
#include "COMTestDlg.h"

//#include <winbase.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static	HANDLE hPort;
static  CString strInChar;
static  DWORD 	m_nRx=0;

DWORD WINAPI ReadPortThread(LPVOID lpvoid);
/////////////////////////////////////////////////////////////////////////////
// CCOMTestDlg dialog

CCOMTestDlg::CCOMTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCOMTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCOMTestDlg)
	m_strReceive = _T("");
	m_strSend = _T("");
	m_strExample = _T("");
	m_strTx = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCOMTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCOMTestDlg)
	DDX_Control(pDX, IDC_EDIT_RX, m_wndRx);
	DDX_Control(pDX, IDC_COMBO_PORT, m_wndPort);
	DDX_Control(pDX, IDC_COMBO_BAUD, m_wndBaud);
	DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strReceive);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	DDX_CBString(pDX, IDC_COMBO_EXAMPLE, m_strExample);
	DDX_Text(pDX, IDC_EDIT_TX, m_strTx);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCOMTestDlg, CDialog)
	//{{AFX_MSG_MAP(CCOMTestDlg)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_CBN_SELCHANGE(IDC_COMBO_PORT, OnSelchangeComboPort)
	ON_CBN_SELCHANGE(IDC_COMBO_BAUD, OnSelchangeComboBaud)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_WM_DESTROY()
	ON_WM_LBUTTONDBLCLK()
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_CBN_EDITCHANGE(IDC_COMBO_EXAMPLE, OnEditchangeComboExample)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCOMTestDlg message handlers

BOOL CCOMTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	strCom="COM1:";
	nBaud=115200;
	m_strSend="AT";
	m_strExample="AT";
	m_nTx=0;
	UpdateData(FALSE);
	hReadThread=NULL;
	m_wndPort.SetCurSel(0);
	m_wndBaud.SetCurSel(1);
	// 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
	strCom="COM1:";
	m_strTx.Format(TEXT("Tx: 0"));
	UpdateData(FALSE);
	CString strRx="Rx: 0";
	m_wndRx.SetWindowText(strRx);
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CCOMTestDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	if((hPort == INVALID_HANDLE_VALUE)||(hPort==0))	
	{
		OpenPort((LPTSTR)(LPCTSTR)strCom);
	}
	else
	{
		if(strComm==strCom)
		{
			if(AboveSeven())
				m_strReceive="";
			m_strReceive+=strCom;
			m_strReceive+=" has been Opened\r\n";
			UpdateData(FALSE);
		}
		else
		{
			ClosePort(hPort);
			OpenPort((LPTSTR)(LPCTSTR)strCom);
		}
	}
	
}

void CCOMTestDlg::OnSelchangeComboPort() 
{
	// TODO: Add your control notification handler code here
//	UpdateData();
//	strCom=m_strPort;
	switch(m_wndPort.GetCurSel())
	{
		case 0:
			strCom="COM1:";
			break;
		case 1:
			strCom="COM2:";
			break;
		case 2:
			strCom="COM3:";
			break;
	}
}

void CCOMTestDlg::OnSelchangeComboBaud() 
{
	// TODO: Add your control notification handler code here
	switch(m_wndBaud.GetCurSel())
	{
		case 0:
			nBaud=9600;;
			break;
		case 1:
			nBaud=115200;;
			break;
	}
		
}
 
BOOL CCOMTestDlg::OpenPort(LPTSTR lpszPortName)
{
	DWORD dwError;
	DWORD dwThreadID;
//	if(hPort)
//	{
//		return FALSE;
//	}
//	else
	{
		hPort=CreateFile(lpszPortName,
						GENERIC_READ|GENERIC_WRITE,
						0,
						NULL,
						OPEN_EXISTING,
						0,
						NULL);
		if(hPort==INVALID_HANDLE_VALUE)
		{
			CString strError;
			strError.Format(_T("Unable to open %s, Error No.=%d"),
							lpszPortName,
							GetLastError());
			MessageBox(strError,TEXT("Error"),MB_OK);
			if(AboveSeven())
				m_strReceive="";
			m_strReceive+=strCom;
			m_strReceive+=" is not Opened\r\n";
			UpdateData(FALSE);
			return FALSE;
		}
		SetCommMask(hPort,EV_RXCHAR);
		SetupComm(hPort,512,512);
		// FlushFileBuffers()
		PurgeComm(hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
		if(!InitDCB())
		{
			if(AboveSeven())
				m_strReceive="";
			m_strReceive+="InitDCB() failed\r\n";
			UpdateData(FALSE);
			return FALSE;
		}
		if(!InitCommTimeouts())
		{
			if(AboveSeven())
				m_strReceive="";
			m_strReceive+="InitCommTimeouts() failed\r\n";
			UpdateData(FALSE);
			return FALSE;
		}
		
//		EscapeCommFunction(hPort, SETDTR);
//		EscapeCommFunction(hPort, SETRTS);
		if(hReadThread=CreateThread(NULL,
									0,
									ReadPortThread,
									0,
									0,
									&dwThreadID))
		{
//			m_strReceive+="CreateThread() succeeded\r\n";
//			UpdateData(FALSE);
		}
		else
		{
			MessageBox(TEXT("Unable to create the read thread"),
						TEXT("Error"),
						MB_OK);
			dwError=GetLastError();
			return FALSE;
		}
		strComm=strCom;
		if(AboveSeven())
				m_strReceive="";
		m_strReceive+=strCom;
		m_strReceive+=" is Opened\r\n";
		UpdateData(FALSE);
/*		if(strCom=="COM1:")
			m_wndPort.SetCurSel(0);
		if(strCom=="COM2:")
			m_wndPort.SetCurSel(1);
		if(strCom=="COM3:")
			m_wndPort.SetCurSel(2);
*/
		m_bConnected=TRUE;
		return TRUE;

	}

}

DWORD CCOMTestDlg::WritePort(BYTE *buf, DWORD dwByteToWrite)
{
	BOOL fWriteState;
	DWORD dwBytesWritten;
	CString tmp;

	fWriteState=WriteFile(hPort,
						buf,
						dwByteToWrite,
						&dwBytesWritten,
						NULL);
	if(!fWriteState)
	{
		MessageBox(TEXT("Can't write string to COMM"),TEXT("Error"),MB_OK);
		dwBytesWritten=0;
	}
//	tmp.Format("Sent data: %s\r\n",buf);
	return dwBytesWritten;
}
//

BOOL CCOMTestDlg::ClosePort(HANDLE hCommPort)
{
	if(hCommPort!=INVALID_HANDLE_VALUE)
	{
		m_bConnected=FALSE;
		SetCommMask(hPort,0);
		if(hReadThread)
		{
			TerminateThread(hReadThread,0);
			CloseHandle(hReadThread);
		}
//		EscapeCommFunction(hPort,CLRDTR);
//		EscapeCommFunction(hPort,CLRRTS);
		PurgeComm(hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
		CloseHandle (hCommPort);
		hCommPort = INVALID_HANDLE_VALUE;
	}
	if(AboveSeven())
				m_strReceive="";
	m_strReceive+=strComm;
	m_strReceive+=" is Closed\r\n";
	UpdateData(FALSE);
	return TRUE;
}

BOOL CCOMTestDlg::InitDCB()
{
	DCB PortDCB;
	DWORD dwError;
	PortDCB.DCBlength = sizeof (DCB); 

	GetCommState(hPort, &PortDCB);

	PortDCB.BaudRate =nBaud;// 115200; //波特率
	PortDCB.fBinary = TRUE;//Win32不支持非二进制串行传输模式,必须为TRUE 
	PortDCB.fParity = FALSE;//TRUE; //启用奇偶校验
	PortDCB.fOutxCtsFlow =FALSE;// TRUE; //串行端口的输出由CTS线控制
	PortDCB.fOutxDsrFlow = FALSE;//关闭串行端口的DSR流控制
	PortDCB.fDtrControl = DTR_CONTROL_DISABLE;//DTR_CONTROL_ENABLE; //启用DTR线
	PortDCB.fDsrSensitivity = FALSE; //如果设为TRUE将忽略任何输入的字节,除非DSR线被启用
	
	//PortDCB.fTXContinueOnXoff = TRUE;//当为TRUE时,如果接收缓冲区已满且驱动程序已传送XOFF字符,将使驱动程序停止传输字符
	PortDCB.fTXContinueOnXoff = FALSE;
	PortDCB.fOutX = FALSE;//设为TRUE指定XON/XOFF控制被用于控制串行输出
	PortDCB.fInX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输入
	PortDCB.fErrorChar = FALSE;//WINCE串行驱动程序的默认执行将忽略这个字段
	PortDCB.fNull = FALSE;//设为TRUE将使串行驱动程序忽略收到的空字节
	PortDCB.fRtsControl = RTS_CONTROL_DISABLE;//RTS_CONTROL_ENABLE;//启用RTS线 
	PortDCB.fAbortOnError = FALSE; //WINCE串行驱动程序的默认执行将忽略这个字段
	PortDCB.ByteSize = 8; //每字节的位数
	PortDCB.Parity = NOPARITY;	//无奇偶校验
	PortDCB.StopBits = ONESTOPBIT; //每字节一位停止位
	
	if (!SetCommState (hPort, &PortDCB))
	{	
		MessageBox (TEXT("Unable to configure the serial port"), 
		TEXT("Error"), MB_OK);
		dwError = GetLastError ();
		return FALSE;
	}
	return TRUE;
}

BOOL CCOMTestDlg::InitCommTimeouts()
{
	COMMTIMEOUTS CommTimeouts;
	DWORD dwError;	
	GetCommTimeouts (hPort, &CommTimeouts);
	CommTimeouts.ReadIntervalTimeout = MAXDWORD;
	CommTimeouts.ReadTotalTimeoutMultiplier = 0;
	CommTimeouts.ReadTotalTimeoutConstant = 0;
	CommTimeouts.WriteTotalTimeoutMultiplier = 10;
	CommTimeouts.WriteTotalTimeoutConstant = 1000;

	if (!SetCommTimeouts (hPort, &CommTimeouts))
	{
		MessageBox (TEXT("Unable to set the time-out parameters"), 
		TEXT("Error"), MB_OK);
		dwError = GetLastError ();
		return FALSE;
	}
	return TRUE;
}

DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
	BOOL fReadState;
	DWORD dwCommModemStatus;
	DWORD dwLength;
	COMSTAT ComStat;
	DWORD dwErrorFlags;
	CEdit *pEdit;
	CEdit *pRx;
	CString strTmp;
//	HWND *pEdit;
	CCOMTestApp* pApp=(CCOMTestApp*)AfxGetApp();
	CCOMTestDlg* pDlg=(CCOMTestDlg*)pApp->m_pMainWnd;
	
	pEdit=(CEdit*)(pDlg->GetDlgItem(IDC_EDIT_RECEIVE));
	pRx=(CEdit*)(pDlg->GetDlgItem(IDC_EDIT_RX));
//	GetDlgItem(IDC_EDIT_RECEIVE,&pEdit);
//	pDlg->GetDlgItem(IDC_EDIT_RECEIVE,pEdit);
//	(CEdit*)pEdit->
	while(hPort!=INVALID_HANDLE_VALUE)
	{
		
		WaitCommEvent (hPort, &dwCommModemStatus, 0);
		if (dwCommModemStatus & EV_RXCHAR) 
		{
			ClearCommError(hPort,&dwErrorFlags,&ComStat);
			
			dwLength=ComStat.cbInQue;
			if(dwLength>0)
			{
				
				BYTE* buf=new BYTE[256];
				fReadState=ReadFile(hPort,buf,dwLength,&dwLength,NULL);
				if(!fReadState)
				{	
					MessageBox(NULL,TEXT("Error in read from serial port"),TEXT("Read Error"),MB_OK);
				}
				else
				{
					pEdit->GetWindowText(strTmp);
					strInChar=buf;
					strTmp+=strInChar;
					pEdit->SetWindowText(strTmp);
					m_nRx+=dwLength;
					strTmp.Format(TEXT("Rx: %d"),m_nRx);
					pRx->SetWindowText(strTmp);
			//		pDlg->m_strReceive+=strInChar;
			//		pDlg->UpdateData(FALSE);//导致异常发生
				}
				delete[] buf;
			} 
		}
		GetCommModemStatus (hPort, &dwCommModemStatus);
	}
	return 0;

}


void CCOMTestDlg::OnButtonSend() 
{
	if((hPort == INVALID_HANDLE_VALUE)||(hPort==0))
	{
		if(AboveSeven())
				m_strReceive="";
		m_strReceive+="No Port is Open\r\n";
		UpdateData(FALSE);
		return;
	}
	UpdateData();
//	char qq[]="liuaibin";
	BYTE tmp[256];       //LAB
//	p=&m_strSend[0];

//	BYTE* p=(BYTE *)qq;
//	int i=m_strSend.GetLength();
	int i=m_strExample.GetLength();
	for(int j=0;j<i;j++)
	{ 
//		tmp[j]=(BYTE)m_strSend.GetAt(j);
//		tmp[j]=(BYTE)m_strSend[j];
		tmp[j]=(BYTE)m_strExample[j];
	}
	tmp[j]='\r';
	j++;
	tmp[j]='\n';
//	int i=8;
//	WritePort(p,8);	
	DWORD nTx;
	nTx=WritePort(tmp,i+2);
	m_nTx+=nTx;
	m_strTx.Format(TEXT("Tx: %d"),m_nTx);
	if(nTx>0)
		m_strReceive="";
	UpdateData(FALSE);
    
	// TODO: Add your control notification handler code here
	
}

void CCOMTestDlg::OnDestroy() 
{
	if(!((hPort == INVALID_HANDLE_VALUE)||(hPort==0)))	
		ClosePort(hPort);	
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CCOMTestDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnLButtonDblClk(nFlags, point);
}

void CCOMTestDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_strReceive="";
	UpdateData(FALSE);
}

void CCOMTestDlg::OnEditchangeComboExample() 
{
	// TODO: Add your control notification handler code here
//	UpdateData();
}

BOOL CCOMTestDlg::AboveSeven()
{
	int i,n,k;
	BYTE tmp[512];
	return FALSE;
	k=0;
	n=m_strReceive.GetLength();
	if(n==0)
		return TRUE;
	for(i=0;i<n;i++);
	{
		tmp[i]=(BYTE)m_strReceive[i];//Erroer
		if(tmp[i]=='\n')
			k++;
		if(k>7)
			return TRUE;
	}
	return FALSE;
}

⌨️ 快捷键说明

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