GPSDlg.cpp
资源名称:gps-232.rar [点击查看]
上传用户:yyd112
上传日期:2009-12-24
资源大小:43k
文件大小:8k
源码类别:
GPS编程
开发平台:
Visual C++
- // 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
- extern CGPSApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // 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)
- DDX_Control(pDX, IDC_COMBCMD, m_CombCmd);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CGPSDlg, CDialog)
- //{{AFX_MSG_MAP(CGPSDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CMD, OnCmd)
- ON_BN_CLICKED(IDC_BIN, OnBin)
- ON_MESSAGE(WM_USER,OnMyMessage)
- ON_BN_CLICKED(IDC_NMEA, OnNmea)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CGPSDlg message handlers
- int OpenComm();
- UINT ComReceiveTheadProc(LPVOID pParam);
- BOOL CGPSDlg::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
- OpenComm();
- AfxBeginThread(ComReceiveTheadProc, NULL,THREAD_PRIORITY_LOWEST);
- 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 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;
- }
- char gBuf[100];
- LRESULT CGPSDlg::OnMyMessage(WPARAM wParam,LPARAM lParam)
- {
- SetDlgItemText(IDC_GPSINFO,gBuf);
- int i,k;
- char buf[100],str[20],sumhex[3];
- char UTC[20],STA[3],Latitude[40],Longitude[40];
- unsigned char sum;
- CString str1;
- strcpy(buf,gBuf);
- sum=0;
- for(i=1;i<100;i++) {if(buf[i]=='*') break; sum^=buf[i];}
- sumhex[0]=(sum/16)>9 ? (sum/16)-10+0x41:sum/16+0x30;
- sumhex[1]=(sum&15)>9 ? (sum&15)-10+0x41:(sum&15)+0x30;
- sumhex[2]=0;
- str1.Format("%2X",sum);
- SetDlgItemText(IDC_SUM,sumhex);
- for(i=0;i<6;i++) str[i]=buf[i];
- str[6]=0;
- if((strcmp(str,"$GPRMC"))==0)
- {
- k=0;
- for(i=0;i<10;i++){if(buf[k++] == ',') break;}
- for(i=0;i<10;i++){ UTC[i]=buf[k]; if(buf[k++] == ',') break;}
- UTC[i]=0;
- STA[0]=buf[k];
- STA[1]=0;
- // for(i=0;i<10;i++){if(buf[k++] == ',') break;} k++;
- for(i=0;i<10;i++){if(buf[k++] == ',') break;}
- for(i=0;i<20;i++){ Latitude[i]=buf[k]; if(buf[k++] == ',') break;}
- Latitude[i]=0;
- for(i=0;i<10;i++){if(buf[k++] == ',') break;}
- for(i=0;i<20;i++){ Longitude[i]=buf[k]; if(buf[k++] == ',') break;}
- Longitude[i]=0;
- SetDlgItemText(IDC_UTC,UTC);
- SetDlgItemText(IDC_STA,STA);
- SetDlgItemText(IDC_LATITUDE,Latitude);
- SetDlgItemText(IDC_LONGITUDE,Longitude);
- }
- return 0;
- }
- HANDLE hCom;
- DCB Dcb;
- void CGPSDlg::OnCmd()
- {
- DWORD Num;
- char buf[50];
- char cmd0[]="$PRWIILOG,???,V,,,rn";
- char cmd[][40]={"$PRWIILOG,RMC,A,T,1,0rn",
- "$PRWIILOG,GGA,A,T,2,0rn",
- "$PRWIILOG,GSV,A,T,2,0rn",
- "$PRWIILOG,GSA,A,T,2,0rn",
- "$PRWIILOG,ZCH,A,T,1,0rn",
- "$PRWIINIT,A, , , , , , , , , , , , ,rn",
- "$PRWIINIT,A, , , , , , , , , , , , ,rn"};
- char cr[]="rn";
- strcpy(buf,cmd0);
- WriteFile(hCom,buf,strlen(buf),&Num,NULL);
- strcpy(buf,cmd[m_CombCmd.GetCurSel()]);
- // strcpy(buf,"$PRWIILOG,GGA,A,T,1,0");
- WriteFile(hCom,buf,strlen(buf),&Num,NULL);
- if(m_CombCmd.GetCurSel()==6)
- {
- int i;
- for(i=0;i<5;i++)
- {
- strcpy(buf,cmd[i]);
- WriteFile(hCom,buf,strlen(buf),&Num,NULL);
- }
- }
- }
- UINT ComReceiveTheadProc(LPVOID pParam)
- {
- unsigned char buf[100],buf1[2];
- UINT Msg=WM_USER;
- WPARAM wParam=NULL;
- LPARAM lParam=0;
- int i,num;
- bool rev$,LF;
- CString strm;
- DWORD Num1;
- HWND hWnd=theApp.m_pMainWnd->m_hWnd;
- while(1)
- {
- num=0;
- rev$=false;
- LF=false;
- while(!rev$)
- {
- Sleep(10);
- ReadFile(hCom,buf1,1,&Num1,NULL);
- if(Num1==1 && buf1[0]=='$')rev$=true;
- };
- buf[num++]=buf1[0];
- while(!LF && num<80 )
- {
- ReadFile(hCom,buf1,1,&Num1,NULL);
- if(Num1==1)
- {
- buf[num++]=buf1[0];
- if(buf1[0]==0x0A) LF=true;
- }
- };
- if(LF)
- {
- for(i=0;i<num;i++) gBuf[i]=buf[i];
- // pView->PostMessage(Msg,wParam, lParam );
- PostMessage(hWnd,Msg,wParam, lParam );
- }
- }
- // HWND hWnd=(HWND)theApp.GetMainWnd();
- return 1;
- }
- int OpenComm()
- {
- hCom=CreateFile(
- "COM1",
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- if(hCom==INVALID_HANDLE_VALUE)
- {
- char szMsg[40]="Can not open ";
- MessageBox(NULL,"COM1",szMsg,MB_OK );
- return 0;
- }
- GetCommState(hCom,&Dcb);
- Dcb.BaudRate=CBR_9600;
- Dcb.BaudRate=CBR_4800;
- Dcb.ByteSize=8;
- Dcb.Parity=NOPARITY;
- Dcb.StopBits=ONESTOPBIT;
- Dcb.fOutxCtsFlow=FALSE;
- Dcb.fOutxDsrFlow=FALSE;
- Dcb.fDtrControl=DTR_CONTROL_ENABLE;
- // Dcb.fDtrControl=DTR_CONTROL_DISABLE;
- Dcb.fDsrSensitivity=FALSE;
- Dcb.fRtsControl=RTS_CONTROL_ENABLE;
- Dcb.fNull=FALSE;
- SetCommState(hCom,&Dcb);
- COMMTIMEOUTS Timeouts;
- GetCommTimeouts(hCom,&Timeouts);
- Timeouts.ReadIntervalTimeout =100;
- // Timeouts.ReadTotalTimeoutMultiplier=2;
- Timeouts.ReadTotalTimeoutMultiplier=20;
- Timeouts.ReadTotalTimeoutConstant=20;
- Timeouts.WriteTotalTimeoutMultiplier=20;
- Timeouts.WriteTotalTimeoutConstant =200;
- SetCommTimeouts(hCom,&Timeouts);
- return 1;
- }
- void CGPSDlg::OnBin()
- {
- DWORD Num;
- int i;
- char buf[50];
- char cr[]="rn";
- strcpy(buf,"$PRWIIPRO,,RBIN");
- strcat(buf,cr);
- WriteFile(hCom,buf,strlen(buf),&Num,NULL);
- }
- void CGPSDlg::OnNmea()
- {
- DWORD Num;
- int i;
- char buf[18],cmd[18];
- char cr[]="rn";
- union {
- unsigned short Word[5];
- unsigned char byte[10];
- }Head;
- union{
- unsigned short Word[4];
- unsigned char byte[8];
- }Data;
- char cmdx[]={0xFF,0x81, 0x33,5, 3,0, 0,7, 0xCB,0x71, 0,0, 0,0, 1,0, 0xFF,0xFF};
- unsigned short x,x1;
- x=0xFF81^0x533^3^0x700;
- x1=0x81FF+0x533+3+0x700;
- x=-x1;
- unsigned short w[4],sum;
- w[0]=0xFF81;
- w[1]=0x533;
- w[2]=3;
- w[3]=0x700;
- sum=0;
- for(i=0;i<4;i++) sum+=w[i];
- Head.Word[0]=0x81FF;
- Head.Word[1]=0x533;//1331; /* ID proctocal=1331=0x533 */
- Head.Word[2]=3; /* count for the data */
- Head.Word[3]=0x700;
- Head.Word[4]=Head.Word[0]+Head.Word[1]+Head.Word[2]+Head.Word[3];
- if(Head.Word[4] != 32768) Head.Word[4]=-Head.Word[4];
- Data.Word[0]=0;
- Data.Word[1]=0;
- Data.Word[2]=1;
- Data.Word[3]=Data.Word[0]+Data.Word[1]+Data.Word[2];
- Data.Word[3]=-Data.Word[3];
- for(i=0;i<10;i++) buf[i]=Head.byte[i];
- for(i=0;i<8;i++) buf[10+i]=Data.byte[i];
- for(i=0;i<9;i++)
- {
- cmd[2*i]=buf[2*i+1];
- cmd[2*i+1]=buf[2*i];
- }
- WriteFile(hCom,cmdx,18,&Num,NULL);
- }