📄 gpsdlg.cpp
字号:
// GpsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Gps.h"
#include "GpsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define XON 0x11
#define XOFF 0x13
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
UINT ComProc(LPVOID pParam);
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()
/////////////////////////////////////////////////////////////////////////////
// CGpsDlg dialog
CGpsDlg::CGpsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGpsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGpsDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGpsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGpsDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGpsDlg, CDialog)
//{{AFX_MSG_MAP(CGpsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_MESSAGE ( WM_HIDEGPSDIALOG, OnHide )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGpsDlg message handlers
UINT CommProc(LPVOID pParam)
{
OVERLAPPED os;
DWORD dwMask, dwTrans;
COMSTAT ComStat;
DWORD dwErrorFlags;
CGpsDlg *p = ( CGpsDlg * ) pParam;
BYTE a[50];
BYTE *buf = a;
char checkSum;
BYTE messageFrame[500];
int tempYear;
SYSTEMTIME time;
LPSYSTEMTIME lpSystemTime = &time ;
int position = 0;
memset(&os, 0, sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if ( os.hEvent==NULL )
{
AfxMessageBox( "Can't create event object!" );
return (UINT)-1;
}
if ( p->m_hCom )
{
PurgeComm( p->m_hCom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT );
}
while( p->m_bConnected )
{
//Sleep(1);
ClearCommError( p->m_hCom,&dwErrorFlags,&ComStat );
if ( ComStat.cbInQue )
{
for ( DWORD j=0; j< ComStat.cbInQue; j++ )
{
p->ReadComm( buf,1 );
if (*buf == 0x24){
memset( messageFrame, 0, sizeof( messageFrame ) );
messageFrame[0] = *buf;
position = 1;
}
if ( ( *buf != 0x24 ) && ( messageFrame[0] == 0x24 ) )
{
messageFrame[position] = *buf;
position++;
}
Sleep(1);
}
TRACE("%d,",position);
if ( position == 36 )
{
if (messageFrame[0] == 0x24 && messageFrame[1] == 0x47 && messageFrame[2] == 0x50 &&
messageFrame[3] == 0x5a && messageFrame[4] == 0x44 && messageFrame[5] == 0x41){
for (int i = 0; i < 36; i++){
TRACE("%x,",messageFrame[i]);
}
TRACE( "\n" );
GetLocalTime( lpSystemTime );
lpSystemTime->wHour = ( messageFrame[7]-0x30 )*10 + ( messageFrame[8]-0x30 );
lpSystemTime->wMinute = ( messageFrame[9]-0x30 )*10 + ( messageFrame[10]-0x30 );
lpSystemTime->wSecond = ( messageFrame[11]-0x30 )*10 + ( messageFrame[12]-0x30 );
lpSystemTime->wDay = ( messageFrame[14]-0x30 )*10 + ( messageFrame[15]-0x30 );
lpSystemTime->wMonth = ( messageFrame[17]-0x30 )*10 + ( messageFrame[18]-0x30 );
CTime* t = new CTime(*lpSystemTime);
CTimeSpan* span = new CTimeSpan(0,8,0,0);
CTime time = *t + *span;
lpSystemTime->wHour = time.GetHour();
lpSystemTime->wMinute = time.GetMinute();
lpSystemTime->wSecond = time.GetSecond();
lpSystemTime->wDay = time.GetDay();
lpSystemTime->wMonth = time.GetMonth();
lpSystemTime->wYear = time.GetYear();
TRACE(" %d年%d月%d日%d时%d分%d秒\n",lpSystemTime->wYear,lpSystemTime->wMonth,lpSystemTime->wDay,lpSystemTime->wHour,lpSystemTime->wMinute,lpSystemTime->wSecond);
SetLocalTime( lpSystemTime );
}
}
continue;
}
dwMask=0;
if ( !WaitCommEvent(p->m_hCom, &dwMask, &os ) ) // 重叠操作
{
if ( GetLastError()==ERROR_IO_PENDING )// 无限等待重叠操作结果
{
GetOverlappedResult( p->m_hCom, &os, &dwTrans, TRUE );
}
else
{
CloseHandle( os.hEvent );
return (UINT)-1;
}
}
}
CloseHandle( os.hEvent );
return 0;
}
BOOL CGpsDlg::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
ConfigComm( "COM1", 4800 );
memset( &m_osRead, 0, sizeof(OVERLAPPED ) );
if ( !( m_osRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ) ) )
{
return FALSE;
}
m_pThread = AfxBeginThread( CommProc, this, THREAD_PRIORITY_NORMAL,
0, CREATE_SUSPENDED, NULL ); // 创建并挂起线程
if ( m_pThread == NULL )
{
CloseHandle( m_hCom );
return FALSE;
}
else
{
m_bConnected = TRUE;
m_pThread->ResumeThread(); // 恢复线程运行
}
char str[50] = " GPS程序正在加载.....";
CEdit *p = ( CEdit * )GetDlgItem( IDC_EDIT1 );
p->SetWindowText( str );
PostMessage( WM_HIDEGPSDIALOG, 0, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CGpsDlg::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 CGpsDlg::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 CGpsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CGpsDlg::ConfigComm( CString port, int baud )
{
COMMTIMEOUTS TimeOuts;
DCB dcb;
m_hCom = CreateFile( port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL ); // 重叠方式
if ( m_hCom == INVALID_HANDLE_VALUE )
{
return FALSE;
}
SetupComm( m_hCom,1000,1000 );
SetCommMask( m_hCom, EV_RXCHAR );
// 把间隔超时设为最大,把总超时设为0将导致ReadFile立即返回并完成操作
TimeOuts.ReadIntervalTimeout = 0xffffffff ;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
/* 设置写超时以指定WriteComm成员函数中的GetOverlappedResult函数的等待时间*/
TimeOuts.WriteTotalTimeoutMultiplier = 50;
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts( m_hCom, &TimeOuts );
if ( !GetCommState( m_hCom, &dcb ) )
{
return FALSE;
}
dcb.fBinary = TRUE;
dcb.BaudRate = baud; // 数据传输速率
dcb.ByteSize = 8; // 每字节位数
// dcb.fParity = TRUE;
dcb.Parity = NOPARITY;
// 硬件流控制设置
dcb.fOutxCtsFlow = FALSE; //如果仅使用TX、RX、GND三根线连接设置为FALSE,否则为TRUE
dcb.fRtsControl = RTS_CONTROL_ENABLE;
// XON/XOFF流控制设置
dcb.fInX=dcb.fOutX = FALSE; //如果仅使用TX、RX、GND三根线连接设置为FALSE,否则为TRUE
dcb.XonChar = XON;
dcb.XoffChar = XOFF;
dcb.XonLim = 50;
dcb.XoffLim = 50;
return SetCommState( m_hCom, &dcb );
// return TRUE;
}
DWORD CGpsDlg::ReadComm( BYTE *buf, DWORD dwLength)
{
DWORD length = 0;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
length = min( dwLength, ComStat.cbInQue );
ReadFile( m_hCom, buf, length, &length, &m_osRead);
return length;
}
void CGpsDlg::OnHide(WPARAM wParam, LPARAM lParam)
{
Sleep( 2000 );
ShowWindow( SW_HIDE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -