?? gps_commdlg.cpp
字號:
// GPS_CommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GPS_Comm.h"
#include "GPS_CommDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGPS_CommDlg dialog
CGPS_CommDlg::CGPS_CommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGPS_CommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGPS_CommDlg)
m_strFileName = _T("");
m_chkOutputFile = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strReceived=_T("");
m_fp=NULL;
}
void CGPS_CommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGPS_CommDlg)
DDX_Control(pDX, IDC_MSGLIST, m_LSTMsg);
DDX_Text(pDX, IDC_EDITFILE, m_strFileName);
DDX_Check(pDX, IDC_CHKOUTPUTFILE, m_chkOutputFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGPS_CommDlg, CDialog)
//{{AFX_MSG_MAP(CGPS_CommDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SETUP, OnSetup)
ON_BN_CLICKED(IDC_STARTRECEIVE, OnStartReceive)
ON_BN_CLICKED(IDC_STOPRECEIVE, OnStopReceive)
ON_WM_TIMER()
ON_MESSAGE(WM_COMM_RXCHAR,OnCommunication) //接收字符的消息傳遞函數,觸發主線程
ON_BN_CLICKED(IDC_CLEARLIST, OnClearList)
ON_BN_CLICKED(IDC_BROWER, OnBrower)
ON_BN_CLICKED(IDC_CHKOUTPUTFILE, OnChkOutputFile)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGPS_CommDlg message handlers
BOOL CGPS_CommDlg::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
// TODO: Add extra initialization here
m_PortEx.InitPort(this,1,9600,'N',8,1);//先初始化一下
((CButton*)GetDlgItem(IDC_STOPRECEIVE))->EnableWindow(FALSE);//停止接收按鈕不可用
((CEdit*)GetDlgItem(IDC_EDITFILE))->EnableWindow(FALSE);
((CButton*)GetDlgItem(IDC_BROWER))->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CGPS_CommDlg::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 CGPS_CommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGPS_CommDlg::OnChkOutputFile()
{
// TODO: Add your control notification handler code here
m_chkOutputFile =! m_chkOutputFile;
if(m_chkOutputFile)
{
((CEdit*)GetDlgItem(IDC_EDITFILE))->EnableWindow(TRUE);
((CButton*)GetDlgItem(IDC_BROWER))->EnableWindow(TRUE);
}
else
{
((CEdit*)GetDlgItem(IDC_EDITFILE))->EnableWindow(FALSE);
((CButton*)GetDlgItem(IDC_BROWER))->EnableWindow(FALSE);
}
}
void CGPS_CommDlg::OnBrower()
{
// TODO: Add your control notification handler code here
DWORD Flag=OFN_HIDEREADONLY|OFN_CREATEPROMPT|OFN_NONETWORKBUTTON;
CFileDialog fdlFile(TRUE,NULL,NULL,Flag,"所有文件(*.*)|*.*",NULL);
if (fdlFile.DoModal()!=IDOK)
return;
m_strFileName = fdlFile.GetPathName();
UpdateData(FALSE);
char FileName[100];
sprintf(FileName,"%s",m_strFileName);
}
//點擊串口設置按鈕
void CGPS_CommDlg::OnSetup()
{
// TODO: Add your control notification handler code here
CSetupDlg *dlg=new CSetupDlg(this,&m_PortEx);
if (dlg->DoModal() == IDOK)
{
switch(dlg->m_strComm[3])//串口串中第四個字符
{
case '1':
m_nComm=1;
break;
case '2':
m_nComm=2;
break;
case '3':
m_nComm=3;
break;
case '4':
m_nComm=4;
break;
default:
break;
}
m_PortEx.m_nPortNr=m_nComm;
m_nBandRate = m_PortEx.m_dcb.BaudRate = atoi(dlg->m_strBaudRate);
this->m_cParity = dlg->m_strParity[0];
switch(m_cParity)
{
case 'N':
m_PortEx.m_dcb.Parity=NOPARITY;
break;
case 'O':
m_PortEx.m_dcb.Parity=ODDPARITY;
break;
case 'E':
m_PortEx.m_dcb.Parity=EVENPARITY;
break;
case 'M':
m_PortEx.m_dcb.Parity=MARKPARITY;
break;
case 'S':
m_PortEx.m_dcb.Parity=SPACEPARITY;
break;
default:
break;
}
m_nDataBits= m_PortEx.m_dcb.ByteSize= atoi(dlg->m_strDataBits);
m_nStopBits= m_PortEx.m_dcb.StopBits= atoi(dlg->m_strStopBits);
if(m_PortEx.m_szWriteBuffer)
delete m_PortEx.m_szWriteBuffer;
m_PortEx.m_szWriteBuffer=new BYTE[m_PortEx.m_nWriteBufferSize];
if(!(m_PortEx.InitPort(this,m_nComm,m_nBandRate,m_cParity,
m_nDataBits,m_nStopBits)))
{
char error[100];
sprintf(error,"COM%d被占用或沒有該COM口,請改用其它COM口!",m_nComm);
AfxMessageBox(error);
}
}
delete dlg;
}
//接收字符的消息傳遞函數,輔助線程傳來,觸發主線程數據處理
LONG CGPS_CommDlg::OnCommunication(WPARAM ch, LPARAM port)
{
m_strReceived += (char)ch;//將接收到的每個字符疊加
return 0;
}
//單擊"開始接收"按鈕
void CGPS_CommDlg::OnStartReceive()
{
// TODO: Add your control notification handler code here
m_PortEx.StartMonitoring();
UINT nTimer = SetTimer(1,50,NULL);
//開始接收 按鈕不可用,停止接收 按鈕可用
((CButton*)GetDlgItem(IDC_STARTRECEIVE))->EnableWindow(FALSE);
((CButton*)GetDlgItem(IDC_STOPRECEIVE))->EnableWindow(TRUE);
UpdateData(TRUE);//將控件的內容給變量
if((m_fp=fopen(m_strFileName,"a+wt"))==NULL)
{
::MessageBox(NULL,_T("文件打開錯誤,"),_T("錯誤"),MB_ICONERROR|MB_OK);
return;
//exit(0);
}
}
//單擊"停止接收"按鈕
void CGPS_CommDlg::OnStopReceive()
{
// TODO: Add your control notification handler code here
m_PortEx.StopMonitoring();
KillTimer(1);
//開始接收 按鈕可用,停止接收 按鈕不可用
((CButton*)GetDlgItem(IDC_STARTRECEIVE))->EnableWindow(TRUE);
((CButton*)GetDlgItem(IDC_STOPRECEIVE))->EnableWindow(FALSE);
fclose(m_fp);//關閉文件
}
//定時器事件
void CGPS_CommDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent!=1) //如果不是1號定時器,則退出
return;
m_strReceived=_T("this is test!");
((CListBox*)GetDlgItem(IDC_MSGLIST))->AddString(m_strReceived);
if(m_chkOutputFile)
fprintf(m_fp,_T("%s"),m_strReceived); //變量須用str1[]的形式定義!
while((m_strReceived.Find(0x0d)!=-1)&&(m_strReceived.Find(0x0a)!=-1))
{
if(m_chkOutputFile)
{
fprintf(m_fp,_T("%s"),m_strReceived); //變量須用str1[]的形式定義!
}
//AfxMessageBox(m_strReceived);
m_LSTMsg.AddString(m_strReceived);
m_strReceived=_T("");
/*
int startLF=m_strReceived.Find(0x0a);
int endCR=m_strReceived.Find(0x0d);
if(startLF>endCR)
endCR=m_strReceived.Find(0x0d,startLF);
CString msg= m_strReceived.Mid(startLF+1,endCR-startLF-1);
m_strReceived.Delete(0,endCR+1);
// if(msg.Left(6).Compare("$GPGGA")!=0)
// continue;
//msg.Delete(0,msg.Find(',')+1);
((CListBox*)GetDlgItem(IDC_MSGLIST))->AddString(msg);*/
}
CDialog::OnTimer(nIDEvent);
}
void CGPS_CommDlg::OnClearList()
{
// TODO: Add your control notification handler code here
m_LSTMsg.ResetContent();
}
void CGPS_CommDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if(::IsWindow(m_LSTMsg.m_hWnd))
{
m_LSTMsg.SetWindowPos(NULL,0,0,cx-10,cy-50,SWP_NOZORDER|SWP_NOMOVE);
CButton bt;
bt.Attach(GetDlgItem(IDOK)->m_hWnd);
bt.SetWindowPos(NULL,cx/2-50,cy-28,0,0,SWP_NOZORDER|SWP_NOSIZE);
bt.Detach();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -