📄 serialdlg.cpp
字号:
// SerialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Serial.h"
#include "SerialDlg.h"
#include "ConfigComm.h"
#include "Serial1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSerialDlg dialog
CSerialDlg::CSerialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialDlg)
m_strData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialDlg)
DDX_Control(pDX, IDC_BUTTON_RECEIVE, m_Receive);
DDX_Control(pDX, IDC_BUTTON_SEND, m_Send);
DDX_Control(pDX, IDC_EDIT_DATA, m_Data);
DDX_Text(pDX, IDC_EDIT_DATA, m_strData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
//{{AFX_MSG_MAP(CSerialDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONFIGCOMM, OnButtonConfigcomm)
ON_BN_CLICKED(IDC_BUTTON_CLEARDATA, OnButtonCleardata)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_RECEIVE, OnButtonReceive)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_COMM_MESSAGE,OnCommEvent)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialDlg message handlers
BOOL CSerialDlg::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);
if (pSysMenu != NULL)
{
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_Receive_Send=0;
m_Port="COM1";
m_hCom=0;
m_bIsConfigFlag=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSerialDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSerialDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////
////////////////////////
//define the "serial"
CSerial serial;
UINT CommWatchProc(LPVOID lpParam);
DWORD dwEvtMask=0;
//////////////
UINT CommWatchProc(LPVOID lpParam)
{
DWORD dwErrorFlag=0;
CSerialDlg* serialDlg=(CSerialDlg*)lpParam;
SetCommMask(serial.hCom,EV_RXCHAR);
while(1)
{
dwEvtMask=0;
WaitCommEvent(serial.hCom,&dwEvtMask,&serial.m_ReadOverlapped);
if(dwEvtMask&EV_RXCHAR)
{
ResetEvent(serial.m_ReadOverlapped.hEvent);
if(!serialDlg->Read())
{
ClearCommError(serial.hCom,&dwErrorFlag,&serial.ComState);
SetEvent(serial.m_ReadOverlapped.hEvent);
// SetCommMask(serial.hCom,EV_RXCHAR);
serialDlg->PostMessage(WM_COMM_MESSAGE,0,0);
PurgeComm(serial.hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR
| PURGE_RXCLEAR ); //清干净输入、输出缓冲区
}
else
{
PurgeComm(serial.hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR
| PURGE_RXCLEAR ); //清干净输入、输出缓冲区
SetEvent(serial.m_ReadOverlapped.hEvent);
}// break;
}
else
continue;
}
return 0;
}
void CSerialDlg::OnButtonConfigcomm()
{
// TODO: Add your control notification handler code here
CConfigComm config;
//close the commPort before SetConfig
if(m_hCom)
{
serial.CloseCommPort();
m_hCom=0;
}
//set dcb
if(config.DoModal()==IDOK)
{
m_bIsConfigFlag=1;
m_Receive_Send=config.m_bSend;
if(m_Receive_Send==0)
{
m_Data.SetReadOnly(FALSE);
m_Data.SetFocus();
}
else
{
m_Data.SetReadOnly(TRUE);
}
switch(config.m_nPort)
{
case 0:
m_Port=_T("COM1");
break;
case 1:
m_Port=_T("COM2");
break;
}return;
switch(config.m_nBaudRate)
{
case 0:
dcb.BaudRate=CBR_110;
break;
case 1:
dcb.BaudRate=CBR_1200;
break;
case 2:
dcb.BaudRate=CBR_2400;
break;
case 3:
dcb.BaudRate=CBR_4800;
break;
case 4:
dcb.BaudRate=CBR_9600;
break;
case 5:
dcb.BaudRate=CBR_19200;
break;
case 6:
dcb.BaudRate=CBR_56000;
break;
}
switch(config.m_nDataBits)
{
case 0:
dcb.ByteSize='5';
break;
case 1:
dcb.ByteSize='6';
break;
case 2:
dcb.ByteSize='7';
break;
case 3:
dcb.ByteSize='8';
break;
}
switch(config.m_nStopBit)
{
case 0:
dcb.StopBits=ONESTOPBIT;
break;
case 1:
dcb.StopBits=ONE5STOPBITS;
break;
case 2:
dcb.StopBits=TWOSTOPBITS;
break;
}
switch(config.m_nParity)
{
case 0:
dcb.Parity=NOPARITY;
break;
case 1:
dcb.Parity=ODDPARITY;
break;
case 2:
dcb.Parity=EVENPARITY;
break;
case 3:
dcb.Parity=MARKPARITY;
break;
case 4:
dcb.Parity=SPACEPARITY;
break;
}
CSerial serial;
serial.SetCommDCB(&dcb);
}
}
void CSerialDlg::OnButtonCleardata()
{
// TODO: Add your control notification handler code here
m_strData=_T("");
UpdateData(FALSE);
}
void CSerialDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(m_hCom)
{
if(AfxMessageBox("确实要中断吗?",NULL,MB_OK)==MB_OK)
{
m_pThread->SuspendThread();
serial.CloseCommPort();
delete m_pThread;
}
}
CDialog::OnCancel();
}
void CSerialDlg::OnCommEvent()
{
int t;
UpdateData(TRUE);
t=m_strData.GetLength();
m_strData.Insert(t,m_strBuffer);
t=m_strData.GetLength();
for(int i=0;i<5;i++)
{
m_strData.Delete(t-i,1);
}
UpdateData(FALSE);
// delete m_buffer;
}
BOOL CSerialDlg::Read()
{
BOOL bRead=true;
BOOL bResult;
DWORD dwLength=0;
DWORD dwErrorFlag=0;
WaitForSingleObject(serial.m_ReadOverlapped.hEvent,1000);
ResetEvent(serial.m_ReadOverlapped.hEvent);
ClearCommError(serial.hCom,&dwErrorFlag,&serial.ComState);
if(!serial.ComState.cbInQue){dwLength=0;return 1;}
else
{
dwLength=min(2048,serial.ComState.cbInQue);
}
m_buffer=new char [dwLength];
if(bRead)
{
bResult=serial.ReadComm(m_buffer,dwLength);
if(!bResult)
{
if(GetLastError()==ERROR_IO_PENDING)
bRead=false;
else
bRead=true;
}
if(!bRead)
{
bRead=true;
GetOverlappedResult(serial.hCom,
&serial.m_ReadOverlapped,
&dwLength,
TRUE);
}
}
else
{
dwLength=0;
ClearCommError(serial.hCom,&dwErrorFlag,&serial.ComState);
}
m_strBuffer=m_buffer;
delete m_buffer;
return 0;
}
void CSerialDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
if(!m_bIsConfigFlag)
{
MessageBox("请先对串口进行设置!",NULL,MB_OK);
return;
}
////open the commport before writing the commPort
UpdateData(TRUE);
if(m_strData==_T(""))
{
MessageBox("请先输入要发送的内容!",NULL,MB_OK);
return;
}
else
{
/////////////////////////////使接收按钮无效
m_Receive.EnableWindow(false);
/////////////////////////////////////////
//////打开串口并初始化串口
if(!m_hCom)
{
m_hCom=serial.OpenCommPort(m_Port);
serial.InitialCommPort();
}
//// write the commPort/////////////////
// DWORD dwErrorFlags;
// COMSTAT ComState;
PurgeComm(serial.hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR
| PURGE_RXCLEAR ); //清干净输入、输出缓冲区
length=(DWORD)m_strData.GetLength();
m_buffer=new char[length+1];
strcpy(m_buffer,m_strData);
if(length>0)
{
if(!serial.WriteComm(m_buffer,length))
{
// WaitForSingleObject(serial.m_WriteOverlapped.hEvent,1000);
// if(!ClearCommError(serial.hCom,&dwErrorFlag,&serial.ComState))
if(GetLastError()==ERROR_IO_PENDING)
{
GetOverlappedResult(serial.hCom,
&serial.m_WriteOverlapped,&length,TRUE);
}
else
{
MessageBox("发送过程出错",NULL,MB_OK);
return;
}
}
}
delete m_buffer;
return;
}
}
void CSerialDlg::OnButtonReceive()
{
// TODO: Add your control notification handler code here
if(!m_bIsConfigFlag)
{
MessageBox("请先对串口进行设置!",NULL,MB_OK);
return;
}
///////////////////////////使发送按钮无效
m_Send.EnableWindow(false);
////open the commport before reading the commPort
if(!m_hCom)
{
m_hCom=serial.OpenCommPort(m_Port);//m_hCom==serial.hCom
serial.InitialCommPort();
}
else
{
MessageBox("串口已经打开",NULL,MB_OK);
}
////read the commPort//////////////////
m_pThread=
AfxBeginThread( CommWatchProc,
this,//
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);
if(m_pThread==NULL)
{
CloseHandle(serial.hCom);
}
else
{
m_pThread->ResumeThread();//恢复线程运行
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -