📄 serialporttestdlg.cpp
字号:
// SerialPortTestpDlg->cpp : implementation file
//
#include "stdafx.h"
#include "SerialPortTest.h"
#include "SerialPortTestDlg.h"
#include "pegdser.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSerialPortTestDlg dialog
CSerialPortTestDlg::CSerialPortTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialPortTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialPortTestDlg)
m_strSendEdit = _T("");
m_strRecDisp = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialPortTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialPortTestDlg)
DDX_Control(pDX, IDC_SEND, m_ButSend);
DDX_Control(pDX, IDC_AUTO_SEND, m_ButAutoSend);
DDX_Control(pDX, IDC_STOP_AUTO_SEND, m_ButStopAutoSend);
DDX_Control(pDX, IDC_TEST_CONTROLS, m_ButTestControls);
DDX_Control(pDX, IDC_STATE, m_State);
DDX_Control(pDX, IDC_CLOSE_COM, m_ButClose);
DDX_Control(pDX, IDC_OPEN_PORT, m_ButOpen);
DDX_Control(pDX, IDC_PORT, m_ComboPort);
DDX_Control(pDX, IDC_BAUD, m_ComboBaud);
DDX_Text(pDX, IDC_SEND_EDIT, m_strSendEdit);
DDX_Text(pDX, IDC_REC_DISP, m_strRecDisp);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialPortTestDlg, CDialog)
//{{AFX_MSG_MAP(CSerialPortTestDlg)
ON_WM_DESTROY()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_OPEN_PORT, OnOpenPort)
ON_BN_CLICKED(IDC_CLOSE_COM, OnCloseCom)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_CLEAR_SEND, OnClearSend)
ON_BN_CLICKED(IDC_CLEAR_REC, OnClearRec)
ON_BN_CLICKED(IDC_STOP_AUTO_SEND, OnStopAutoSend)
ON_BN_CLICKED(IDC_TEST_CONTROLS, OnTestControls)
ON_BN_CLICKED(IDC_AUTO_SEND, OnAutoSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortTestDlg message handlers
BOOL CSerialPortTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
m_ComboPort.SetCurSel(0);
m_ComboBaud.SetCurSel(4);
m_ButClose.EnableWindow(FALSE);
m_ButSend.EnableWindow(FALSE);
m_ButAutoSend.EnableWindow(FALSE);
m_ButStopAutoSend.EnableWindow(FALSE);
m_ButTestControls.EnableWindow(FALSE);
m_hComm=INVALID_HANDLE_VALUE;
m_strRecDisp=_T("");
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
BOOL CSerialPortTestDlg::OpenPort(LPCTSTR Port, int BaudRate, int DataBits,int StopBits,int Parity)
{
COMMTIMEOUTS CommTimeOuts;
m_hComm=CreateFile(Port,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
if(m_hComm==INVALID_HANDLE_VALUE)
{
MessageBox(_T("Can't open the port or the port has been opened!"));
return FALSE;
}
GetCommState(m_hComm,&dcb);
dcb.BaudRate=BaudRate;
dcb.ByteSize=DataBits;
dcb.Parity=Parity;
dcb.StopBits=StopBits;
dcb.fParity=FALSE;
dcb.fBinary=TRUE;
dcb.fDtrControl=0;
dcb.fRtsControl=0;
dcb.fOutX=0;
dcb.fInX=0;
dcb.fTXContinueOnXoff=0;
//SetCommMask(m_hComm,EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
SetupComm(m_hComm,16384,16384);
if(!SetCommState(m_hComm,&dcb))
{
MessageBox(_T("Can't set the configuration ,please check the parameters!"));
ClosePort();
return FALSE;
}
GetCommTimeouts(m_hComm,&CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout=100;
CommTimeOuts.ReadTotalTimeoutMultiplier=1;
CommTimeOuts.ReadTotalTimeoutConstant=100;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutConstant=0;
if(!SetCommTimeouts(m_hComm,&CommTimeOuts))
{
MessageBox(_T("Can't set the time out parameters"));
ClosePort();
return FALSE;
}
PurgeComm(m_hComm,PURGE_TXCLEAR | PURGE_RXCLEAR);
return TRUE;
}
const CString PortTbl[8]={_T("COM1:"),_T("COM2:"),_T("COM3:"),_T("COM4:"),_T("COM5:"),_T("COM6:"),_T("COM7:"),_T("COM8:")};
const DWORD BaudTbl[5]={9600,19200,38400,57600,115200};
//const DWORD DataBitTbl[2]={7,8};
//const BYTE StopBitTbl[3]={ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS};
//const BYTE ParityTbl[4]={NOPARITY,ODDPARITY,EVENPARITY,MARKPARITY};
DWORD CSerialPortTestDlg::CommRecvTread(LPVOID lparam)
{
DWORD dwLength;
char * recvBuf=new char[1024];
CSerialPortTestDlg * pDlg= (CSerialPortTestDlg * )lparam;
while(TRUE)
{
if(WaitForSingleObject(pDlg->m_ExitThreadEvent,0)==WAIT_OBJECT_0)
break;
if(pDlg->m_hComm !=INVALID_HANDLE_VALUE)
{
BOOL fReadState =ReadFile(pDlg->m_hComm,recvBuf,1024,&dwLength,NULL);
if (!fReadState)
{
//MessageBox(_T("Can't read data from the port!"));
}
else
{
if (dwLength !=0)
OnCommRecv(pDlg,recvBuf,dwLength);
}
}
}
delete[] recvBuf;
return 0;
}
void CALLBACK CSerialPortTestDlg::OnCommRecv(CWnd * pWnd,char * buf,int buflen)
{
CString tmp;
CSerialPortTestDlg * pDlg=(CSerialPortTestDlg *)pWnd;
CEdit * pRecvStrEdit=(CEdit * )pDlg->GetDlgItem(IDC_REC_DISP);
for(int i=0;i<buflen;i++,buf++)
{
tmp.Format(_T("%c"),* buf);
pDlg->m_strRecDisp +=tmp;
}
pRecvStrEdit->SetWindowText(pDlg->m_strRecDisp);
}
DWORD CSerialPortTestDlg::CommTestControlsTread(LPVOID lparam)
{
DWORD dwCommModemStatus;
CSerialPortTestDlg * pDlg= (CSerialPortTestDlg * )lparam;
SetCommMask (pDlg->m_hComm, EV_CTS | EV_DSR | EV_RLSD | EV_RING);
while(pDlg->m_hComm !=INVALID_HANDLE_VALUE)
{
WaitCommEvent (pDlg->m_hComm, &dwCommModemStatus, NULL);
SetCommMask (pDlg->m_hComm, EV_CTS | EV_DSR | EV_RLSD | EV_RING);
if (dwCommModemStatus & EV_CTS)
{
pDlg->m_State.InsertString(-1,_T("(3)The CTS is OK !"));
}
else
{
pDlg->m_State.InsertString(-1,_T("(3)The CTS is Bad!"));
}
if (dwCommModemStatus & EV_RLSD)
{
pDlg->m_State.InsertString(-1,_T("(4)The DCD is OK !"));
}
else
{
pDlg->m_State.InsertString(-1,_T("(4)The DCD is Bad !"));
}
if (dwCommModemStatus & EV_DSR)
{
pDlg->m_State.InsertString(-1,_T("(5)The DSR is OK !"));
}
else
{
pDlg->m_State.InsertString(-1,_T("(5)The DSR is Bad !"));
}
if (dwCommModemStatus & EV_RING)
{
pDlg->m_State.InsertString(-1,_T("(6)The RING is OK !"));
}
else
{
pDlg->m_State.InsertString(-1,_T("(6)The RING is Bad !"));
}
//dwCommModemStatus=0;
}
return 0;
}
BOOL CSerialPortTestDlg::ClosePort(void)
{
if (m_hComm != INVALID_HANDLE_VALUE)
{
SetCommMask(m_hComm,0);
PurgeComm(m_hComm,PURGE_TXCLEAR | PURGE_RXCLEAR);
//CloseHandle(hTestControlsThread);
CloseHandle(m_hComm);
m_ButOpen.EnableWindow(TRUE);
m_ButClose.EnableWindow(FALSE);
m_ButSend.EnableWindow(FALSE);
m_ButTestControls.EnableWindow(FALSE);
m_ButAutoSend.EnableWindow(FALSE);
m_ButStopAutoSend.EnableWindow(FALSE);
m_hComm=INVALID_HANDLE_VALUE;
return TRUE;
}
return FALSE;
}
void CSerialPortTestDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if(m_ExitThreadEvent != NULL)
{
SetEvent(m_ExitThreadEvent);
Sleep(1000);
CloseHandle(m_ExitThreadEvent);
m_ExitThreadEvent=NULL;
}
ClosePort();
}
void CSerialPortTestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_strSendEdit.MakeReverse();
UpdateData(FALSE);
UpdateData(TRUE);
OnSend();
CDialog::OnTimer(nIDEvent);
}
void CSerialPortTestDlg::OnOpenPort()
{
// TODO: Add your control notification handler code here
DWORD IDThreadCommRecv;
DWORD IDThreadCommTestControls;
HANDLE hRecvThread;
HANDLE hTestControlsThread;
UpdateData(TRUE);
CString strPort=PortTbl[m_ComboPort.GetCurSel()];
DWORD baud=BaudTbl[m_ComboBaud.GetCurSel()];
//DWORD databit=DataBitTbl[m_ComboData.GetCurSel()];
//BYTE stopbit=StopBitTbl[m_ComboStop.GetCurSel()];
//BYTE parity=ParityTbl[m_ComboParity.GetCurSel()];
DWORD databit=8;
BYTE stopbit=ONESTOPBIT;
BYTE parity=NOPARITY;
BOOL ret=OpenPort(strPort,baud,databit,stopbit,parity);
if(ret==FALSE)
return;
m_ExitThreadEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hRecvThread = CreateThread(0,0,CommRecvTread,this,0,&IDThreadCommRecv);
hTestControlsThread= CreateThread(0,0,CommTestControlsTread,this,0,&IDThreadCommTestControls);
if (hRecvThread==NULL)
{
MessageBox(_T("Can't create a receive thread !"));
return;
}
CloseHandle(hRecvThread);
CloseHandle(hTestControlsThread);
m_ButOpen.EnableWindow(FALSE);
m_ButClose.EnableWindow(TRUE);
m_ButSend.EnableWindow(TRUE);
m_ButAutoSend.EnableWindow(TRUE);
m_ButStopAutoSend.EnableWindow(TRUE);
m_ButTestControls.EnableWindow(TRUE);
//MessageBox(_T("Open the ")+ strPort + _T("port successfully !"));
}
void CSerialPortTestDlg::OnCloseCom()
{
// TODO: Add your control notification handler code here
ClosePort();
}
void CSerialPortTestDlg::OnSend()
{
// TODO: Add your control notification handler code here
DWORD dwactlen;
if(m_hComm==INVALID_HANDLE_VALUE)
{
MessageBox(_T("The port hasn't been opened ! "));
return;
}
UpdateData(TRUE);
int len =m_strSendEdit.GetLength();
char * psendbuf=new char[len];
for (int i=0;i<len;i++)
psendbuf[i]=(char)m_strSendEdit.GetAt(i);
WriteFile(m_hComm,psendbuf,len,&dwactlen,NULL);
delete[] psendbuf;
}
void CSerialPortTestDlg::OnClearSend()
{
// TODO: Add your control notification handler code here
m_strSendEdit=_T("");
UpdateData(FALSE);
}
void CSerialPortTestDlg::OnClearRec()
{
// TODO: Add your control notification handler code here
m_strRecDisp=_T("");
SetDlgItemText(IDC_REC_DISP,m_strRecDisp);
}
void CSerialPortTestDlg::OnStopAutoSend()
{
// TODO: Add your control notification handler code here
m_ButSend.EnableWindow(TRUE);
m_ButClose.EnableWindow(TRUE);
m_ButAutoSend.EnableWindow(TRUE);
m_ButStopAutoSend.EnableWindow(FALSE);
KillTimer(TIMER_EVENT);
}
void CSerialPortTestDlg::OnTestControls()
{
// TODO: Add your control notification handler code here
//CSerialPortTestDlg * pDlg;
//CSerialPortTestDlg Dlg;
BOOL bFlagSetRTS=FALSE;
BOOL bFlagClrRTS=FALSE;
BOOL bFlagSetDTR=FALSE;
BOOL bFlagClrDTR=FALSE;
bFlagSetDTR= DeviceIoControl(m_hComm,IOCTL_SERIAL_SET_DTR,NULL,0,NULL,0, NULL,NULL);
bFlagClrDTR= DeviceIoControl(m_hComm,IOCTL_SERIAL_CLR_DTR,NULL,0,NULL,0, NULL,NULL);
if(bFlagSetDTR & bFlagClrDTR)
{
m_State.InsertString(-1,_T("(1)The DTR is OK !"));
}
else
{
m_State.InsertString(-1,_T("(1)The DTR is Bad !"));
}
bFlagSetRTS= DeviceIoControl(m_hComm,IOCTL_SERIAL_SET_RTS,NULL,0,NULL,0, NULL,NULL);
bFlagClrRTS= DeviceIoControl(m_hComm,IOCTL_SERIAL_CLR_RTS,NULL,0,NULL,0, NULL,NULL);
if(bFlagSetRTS & bFlagClrRTS)
{
m_State.InsertString(-1,_T("(2)The RTS is OK !"));
}
else
{
m_State.InsertString(-1,_T("(2)The RTS is Bad !"));
}
}
void CSerialPortTestDlg::OnAutoSend()
{
// TODO: Add your control notification handler code here
m_ButSend.EnableWindow(FALSE);
m_ButClose.EnableWindow(FALSE);
m_ButAutoSend.EnableWindow(FALSE);
m_ButStopAutoSend.EnableWindow(TRUE);
m_strSendEdit=_T("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
UpdateData(FALSE);
OnSend();
SetTimer(TIMER_EVENT,1000,NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -