📄 showgpsentrydlg.cpp
字号:
// ShowGPSEntryDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ShowGPS.h"
#include "ShowGPSEntryDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
TCHAR* tbl_Port[3] = {_T("COM2"), _T("COM3"), _T("COM4")};
BYTE buf[500];
extern CString strCom;
/////////////////////////////////////////////////////////////////////////////
// CShowGPSEntryDlg dialog
CShowGPSEntryDlg::CShowGPSEntryDlg(CWnd* pParent /*=NULL*/)
: TDialog(CShowGPSEntryDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CShowGPSEntryDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hStart = m_hStop = NULL;
strCom = _T("COM4");
}
void CShowGPSEntryDlg::DoDataExchange(CDataExchange* pDX)
{
TDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShowGPSEntryDlg)
DDX_Control(pDX, IDC_BUTTONEXE, m_btn);
DDX_Control(pDX, IDC_LISTGPS, m_listBox);
DDX_Control(pDX, IDC_COMBO, m_Port);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShowGPSEntryDlg, TDialog)
//{{AFX_MSG_MAP(CShowGPSEntryDlg)
ON_BN_CLICKED(IDC_BUTTONEXE, OnButtonexe)
ON_BN_CLICKED(IDC_BUTTON, OnButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShowGPSEntryDlg message handlers
BOOL CShowGPSEntryDlg::OnInitDialog()
{
TDialog::OnInitDialog();
// TODO: Initialize dialog contents.
for(int i=0;i<3;i++) m_Port.AddString(tbl_Port[i]);
int sel = m_Port.FindString(0, strCom);
if(sel == CB_ERR) m_Port.SetCurSel(2);
else m_Port.SetCurSel(sel);
// UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CShowGPSEntryDlg::OnButtonexe()
{
// TODO: Add your control notification handler code here
unsigned short ch[10];
m_btn.GetWindowText(ch,10);
CString str1(ch);
// m_btn.GetWindowText((LPTSTR)str1,10);
if (!str1.Compare(_T("show")))
{
m_btn.SetWindowText(_T("stop"));
//UpdateWindow();
Report.Debug(_T("ShowGPS 1"));
m_Port.GetLBText(m_Port.GetCurSel(),strCom);
Start(strCom);
}
else
{
m_btn.SetWindowText(_T("show"));
Report.Debug(_T("ShowGPS 2"));
// UpdateWindow();
Stop();
}
}
void CShowGPSEntryDlg::OnOK()
{
// TODO: Add extra validation here
m_Port.GetLBText(m_Port.GetCurSel(),strCom);
TDialog::OnOK();
}
void CShowGPSEntryDlg::OnCancel()
{
// TODO: Add extra cleanup here
TDialog::OnCancel();
}
UINT CShowGPSEntryDlg::GPSThread(LPVOID pParam)
{
Report.Debug(_T("ShowGPS: Thread started"));
// CListBox *pLstBox;
// pLstBox=(CListBox*)pParam;
// GPSControl* pMSET = (GPSControl*)pParam;
CShowGPSEntryDlg *mdlg=(CShowGPSEntryDlg*)pParam;
DWORD dwEvtMask, dwError;
COMSTAT ComStat;
SetCommMask(mdlg->Port.GetHandle(), EV_RXCHAR);
while(true)
{
PowerMgr.SetSpeed(TPowerMgr::pwr_Minute);
WaitCommEvent(mdlg->Port.GetHandle(), &dwEvtMask, NULL);
PowerMgr.SetSpeed(TPowerMgr::pwr_Fast);
if(PowerMgr.WaitForSingleObject(mdlg->m_hStop, 0) == WAIT_OBJECT_0)
{
Report.Debug(_T("ShowGPS: Receive stop event"));
break;
}
if((dwEvtMask & EV_RXCHAR))
{
ClearCommError(mdlg->Port.GetHandle(), &dwError, &ComStat);
// Report.Debug(_T("MSET: Receive active"));
if(mdlg->Port.NumberBytesInputBuffer())
{
int nRcvBytes =mdlg->Port.GetStr(buf,499);
CString s,ss;
for(int i=0;i<nRcvBytes;i++)
{
s.Format(_T("%c"), buf[i]);
ss+=s;
if(buf[i+1]=='$')
{
// mdlg->m_listBox.AddString(ss);
if (100==mdlg->m_listBox.GetCount())
{
// mdlg->m_listBox.DeleteString(mdlg->m_listBox.GetTopIndex());
}
mdlg->m_listBox.SetTabStops();
s="";
}
}
Report.Debug(_T("MSet: received bytes: %d"), nRcvBytes);
Report.Debug(_T("MSet: received str:\n\r%s"), ss);
}
}
}
Report.Debug(_T("ShowGPS: Thread terminated"));
return 0;
}
bool CShowGPSEntryDlg::Start(CString strCom)
{
Report.Debug(_T("ShowGPS Com Set!"));
strCom.Format(_T("%s:"), strCom);
Port.SetCommPort(strCom);
COMMTIMEOUTS CommTimeouts;
CommTimeouts.ReadIntervalTimeout = 10;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 200;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
Port.SetTimeouts(CommTimeouts);
Port.SetConfiguration(CBR_9600, 8, NOPARITY, ONESTOPBIT, false);
if(!Port.OpenComm())
{
Report.Debug(_T("ShowGPS: failed to open port when initializing"));
return false;
}
m_hStop = CreateEvent(NULL, true, false, NULL);
CWinThread* pThrd = AfxBeginThread(GPSThread, this);
if(pThrd)
{
m_hStart = pThrd->m_hThread;
Report.Debug(_T("ShowGPS thread start at %s"), strCom);
return true;
}
else Report.Debug(_T("ShowGPS thread failed to start"));
return false;
}
bool CShowGPSEntryDlg::Stop()
{
Report.Debug(_T("ShowGPS 3"));
if(m_hStart)
{
SetEvent(m_hStop);
PowerMgr.Sleep(500);
SetCommMask(Port.GetHandle(), NULL);
DWORD dwRet = PowerMgr.WaitForSingleObject(m_hStart, 20000);
if(dwRet != WAIT_OBJECT_0)
{
Report.Debug(_T("ShowGPS 4"));
DWORD dwExitCode;
GetExitCodeThread(m_hStart, &dwExitCode);
TerminateThread(m_hStart, dwExitCode);
}
m_hStart = NULL;
}
CloseHandle(m_hStop);
if(Port.IsOpen())
{
Port.CloseComm();
}
return true;
}
void CShowGPSEntryDlg::OnButton()
{
// TODO: Add your control notification handler code here
Stop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -