gmarkView.cpp
上传用户:pumpssky
上传日期:2007-12-07
资源大小:110k
文件大小:27k
- // gmarkView.cpp : implementation of the CGmarkView class
- //
- #include "stdafx.h"
- #include "gmark.h"
- #include "gmarkDoc.h"
- #include "gmarkView.h"
- #include "serial.h"
- #include "define.h"
- #include "ras.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #ifndef _WIN32_WCE_CEPC
- HRASCONN hRasConn = NULL;
- const UINT WM_RASEVENT = WM_RASDIALEVENT;
- #else
- HANDLE hRasConn = NULL;
- const UINT WM_RASEVENT = WM_USER;
- #endif
- HANDLE hReadSocketThread;
- #ifdef _SAVE_RTCM
- FILE *fpRtcm;
- #endif
- #ifdef _SAVE_GPSDATA
- FILE *fpGps; //save the GPS result data
- #endif
- CONF_DATA gtConfData;
- /////////////////////////////////////////////////////////////////////////////
- // CGmarkView
- IMPLEMENT_DYNCREATE(CGmarkView, CFormView)
- BEGIN_MESSAGE_MAP(CGmarkView, CFormView)
- //{{AFX_MSG_MAP(CGmarkView)
- ON_BN_CLICKED(IDC_BUTTON_DIAL, OnButtonDial)
- ON_BN_CLICKED(IDC_BUTTON_HANGUP, OnButtonHangup)
- ON_REGISTERED_MESSAGE(WM_RASEVENT, OnRasDialEvent)
- ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
- ON_BN_CLICKED(IDC_BUTTON_INIT, OnButtonInit)
- ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_BUTTON_GGAOFF, OnButtonGgaoff)
- ON_BN_CLICKED(IDC_BUTTON_GGA2S, OnButtonGga2s)
- ON_WM_CLOSE()
- ON_BN_CLICKED(IDC_BUTTON_TESTMAP, OnButtonTestmap)
- ON_BN_CLICKED(IDC_BUTTON_LOGON, OnButtonLogon)
- ON_BN_CLICKED(IDC_BUTTON_LOGOFF, OnButtonLogoff)
- ON_BN_CLICKED(IDC_BUTTON_RTCMON, OnButtonRtcmon)
- ON_BN_CLICKED(IDC_BUTTON_RTCMOFF, OnButtonRtcmoff)
- ON_BN_CLICKED(IDC_BUTTON_PBNON2, OnButtonPbnon2)
- ON_MESSAGE(WM_GPSRECEIVED, OnGpsReceived)
- ON_MESSAGE(WM_GPSUPDATE, OnGpsUpdate)
- ON_MESSAGE(WM_AUTOPROCESS, AutoProcess)
- ON_BN_CLICKED(IDC_BUTTON_PBNOFF, OnButtonPbnoff)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CGmarkView construction/destruction
- CGmarkView::CGmarkView()
- : CFormView(CGmarkView::IDD)
- {
- //{{AFX_DATA_INIT(CGmarkView)
- //}}AFX_DATA_INIT
-
- // TODO: add construction code here
- memset( gtConfData.szBaseIP, 0, sizeof(gtConfData.szBaseIP) );
- memset( gtConfData.szRoverID, 0, sizeof(gtConfData.szRoverID) );
- memset( gtConfData.szRoverDesc, 0, sizeof(gtConfData.szRoverDesc) );
- memset( m_RTCMBuf, 0, sizeof(m_RTCMBuf) );
- m_bCloseSocketFlg = FALSE;
- m_Socket = INVALID_SOCKET;
- m_bNeedActivate = FALSE;
- m_hDialCompleteEvent = CreateEvent(0, FALSE, FALSE, 0);
- }
- CGmarkView::~CGmarkView()
- {
- }
- void CGmarkView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CGmarkView)
- DDX_Control(pDX, IDC_TAB_MAIN, m_tabMain);
- //}}AFX_DATA_MAP
- }
- BOOL CGmarkView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CGmarkView diagnostics
- #ifdef _DEBUG
- void CGmarkView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CGmarkView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CGmarkDoc* CGmarkView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGmarkDoc)));
- return (CGmarkDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CGmarkView message handlers
- void CGmarkView::OnButtonDial()
- {
- // TODO: Add your control notification handler code here
- #ifndef _WIN32_WCE_CEPC
- RASDIALPARAMS RasDialParams;
- DWORD dwNotifierType = 0xFFFFFFFF;
- HWND hwnd;
- int iRet=0;
- RasDialParams.dwSize = sizeof(RASDIALPARAMS);
- wcscpy( RasDialParams.szEntryName, _T("GPRS57600") );
- wcscpy( RasDialParams.szPhoneNumber, _T("") );
- wcscpy( RasDialParams.szCallbackNumber, _T("") );
- wcscpy( RasDialParams.szUserName, _T("") );
- wcscpy( RasDialParams.szPassword, _T("") );
- wcscpy( RasDialParams.szDomain, _T("") );
- hwnd = this->m_hWnd;
- iRet = RasDial( NULL,
- NULL,
- &RasDialParams,
- dwNotifierType,
- hwnd,
- &hRasConn
- );
- if(iRet != 0)
- {
- MessageBox(_T("Failed to connect to CMNET"));
- }
- #endif
- }
- void CGmarkView::OnButtonHangup()
- {
- // TODO: Add your control notification handler code here
- #ifndef _WIN32_WCE_CEPC
- if (hRasConn != NULL)
- {
- RasHangUp(hRasConn);
- hRasConn = NULL;
- // ::Sleep(2000);
- RASCONNSTATUS rStatus;
- while( RasGetConnectStatus(hRasConn, &rStatus) != ERROR_INVALID_HANDLE )
- {
- ::Sleep(0);
- }
- }
- #endif
- }
- LRESULT CGmarkView::OnRasDialEvent(WPARAM wp, LPARAM lp)
- {
- #ifndef _WIN32_WCE_CEPC
- RASCONNSTATE rasstate= (RASCONNSTATE)wp;
- CEdit *edtInfo = (CEdit *)GetDlgItem(IDC_EDIT_STATUS_DIAL);
- switch(rasstate)
- {
- case RASCS_OpenPort:
- edtInfo->SetWindowText(_T("To open COM port乧乧"));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("To open COM port乧乧"));
- /////////////////////
- break;
- case RASCS_PortOpened:
- edtInfo->SetWindowText(_T("Port opened."));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("Port opened."));
- /////////////////////
- break;
- case RASCS_ConnectDevice:
- edtInfo->SetWindowText(_T("To connect device乧乧"));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("To connect device乧乧"));
- /////////////////////
- break;
- case RASCS_DeviceConnected:
- edtInfo->SetWindowText(_T("Device connected."));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("Device connected."));
- /////////////////////
- break;
- case RASCS_Authenticate:
- edtInfo->SetWindowText(_T("To verify user and password乧乧"));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("To verify user and password乧乧"));
- /////////////////////
- break;
- case RASCS_Authenticated:
- edtInfo->SetWindowText(_T("Verification passed"));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("Verification passed"));
- /////////////////////
- break;
- case RASCS_Connected:
- edtInfo->SetWindowText(_T("Connected"));
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("Connected"));
- /////////////////////
- SetEvent(m_hDialCompleteEvent);
- break;
- case RASCS_Disconnected:
- edtInfo->SetWindowText(_T("Disconnected"));
- hRasConn=NULL;
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_DIAL, _T("Disconnected"));
- /////////////////////
- break;
- default:
- return (LRESULT)0;
- }
- #endif
- return (LRESULT)0;
- }
- void CGmarkView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
-
- // TODO: Add your specialized code here and/or call the base class
- // read the configuration
- char filename[MAX_PATH+1];
- // read the configuration file
- if( !ReadConfig() )
- {
- AfxGetMainWnd()->PostMessage(WM_CLOSE);
- return;
- }
- // Initialize gps module
- m_Gps.SetOrigPosition( gtConfData.dOrigLon, gtConfData.dOrigLat );
- //set view handle to gps
- m_Gps.SetViewHandle( m_hWnd );
- //set view window object
- m_Gps.SetViewWnd(this);
- //Initialize the share memory
- m_hMap = CreateFileMapping( INVALID_HANDLE_VALUE,
- NULL,
- PAGE_READWRITE | SEC_COMMIT,
- 0,
- sizeof(GPS_DATA),
- MAP_FILENAME );
- if (m_hMap != NULL)
- {
- m_lpMapBuf = (LPBYTE)MapViewOfFile(m_hMap, FILE_MAP_WRITE, 0, 0, 0);
- if (m_lpMapBuf == NULL)
- {
- CloseHandle(m_hMap);
- m_hMap = NULL;
- }
- }
- // Open the log file
- memset( filename, 0, sizeof(filename) );
- CreateFilename(filename, FN_LOG);
- m_Log.Open(filename);
- m_Gps.m_pLog = &m_Log; //for test
- // open a file to save rtcm data
- #ifdef _SAVE_RTCM
- fpRtcm = fopen("\Storage Card\rtcm.dat", "wb");
- #endif
- // Open a file to save gps data
- #ifdef _SAVE_GPSDATA
- memset( filename, 0, sizeof(filename) );
- CreateFilename(filename, FN_GPSDATA);
- fpGps = fopen(filename, "wtc");
- #endif
- // complet the main tab control
- RECT rect;
- m_tabMain.InitDialogs();
- m_tabMain.InsertItem(0, _T("Monitor"));
- m_tabMain.InsertItem(1, _T("Operate"));
- m_tabMain.InsertItem(2, _T("Setup"));
- m_tabMain.InsertItem(3, _T("Advance"));
- GetWindowRect(&rect);
- m_tabMain.SetWindowPos(&CWnd::wndTopMost, 0, 0,
- rect.right-rect.left, rect.bottom-rect.top, 0);
-
- m_tabMain.ActivateTabDialogs();
- m_dlgMonitor = m_tabMain.GetMonitorDlg();
- m_dlgOperate = m_tabMain.GetOperateDlg();
- m_dlgSetup = m_tabMain.GetSetupDlg ();
- m_dlgAdvance = m_tabMain.GetAdvanceDlg();
- if(gtConfData.bAutoStart)
- {
- PostMessage(WM_AUTOPROCESS, 0, 0);
- }
- //set gps to dialogs
- m_dlgAdvance->SetGps(&m_Gps);
- }
- void CGmarkView::OnGpsReceived(WPARAM w, LPARAM l)
- {
- //set busy flag
- m_Gps.m_bGpsMsgBusy = TRUE;
- unsigned char * buf = (unsigned char *)l;
- int nBytes = (int)w;
- /*TCHAR wBuff[GPS_MAX_NEMASIZE+1];
- // Convert from MultiByte to UNICODE
- mbstowcs(wBuff, (const char *)buf, GPS_MAX_NEMASIZE);
- // Display the received data
- AddText2Edit( IDC_EDIT_GPSMSG, wBuff );*/
- ////////////////////
- m_dlgMonitor->UpdateGpsMsg((const char *)buf);
- ////////////////////
- #if 1
- // compose gps message into a complete record
- m_Gps.CompleteMsg(buf, nBytes);
- #else
- ProcessMsg(buf, nBytes);
- #endif
- // write received message to file
- #ifdef _SAVE_GPSDATA
- #ifdef _SAVE_GPSMSG
- fprintf(fpGps, "%s", buf);
- #endif
- #endif
- if(buf)
- {
- delete buf;
- buf = NULL;
- }
- //Create gps data
- m_Gps.CreateGPSData();
- //reset busy flag
- m_Gps.m_bGpsMsgBusy = FALSE;
- }
- void CGmarkView::OnGpsUpdate(WPARAM w, LPARAM l)
- {
- /* char szTemp[100];
- UpdateData(false);
- m_nStatus = m_Gps.m_tGpsData.Status;
- m_nSatNum = m_Gps.m_tGpsData.SatCount;
- memset( szTemp, 0, sizeof(szTemp) );
- sprintf(szTemp, "%f", m_Gps.m_tGpsData.Longitude);
- m_szLon = szTemp;
-
- memset( szTemp, 0, sizeof(szTemp) );
- sprintf(szTemp, "%f", m_Gps.m_tGpsData.Latitude);
- m_szLat = szTemp;
- memset( szTemp, 0, sizeof(szTemp) );
- sprintf(szTemp, "%f", m_Gps.m_tGpsData.X);
- m_szX = szTemp;
- memset( szTemp, 0, sizeof(szTemp) );
- sprintf(szTemp, "%f", m_Gps.m_tGpsData.Y);
- m_szY = szTemp;
- //refresh the gps status message
- if( m_nStatus > 0 )
- {
- m_szGpsStatus = _T("Normal");
- UpdateData(false);
- }
- UpdateData(false);
- */
- // write gps result data to file
- #ifdef _SAVE_GPSDATA
- if( m_Gps.m_tGpsData.Status > 0 )
- {
- fprintf( fpGps,
- ":%f,%f,%d,%dn=%f,%fn",
- m_Gps.m_tGpsData.Longitude,
- m_Gps.m_tGpsData.Latitude,
- m_Gps.m_tGpsData.SatCount,
- m_Gps.m_tGpsData.Status,
- m_Gps.m_tGpsData.X,
- m_Gps.m_tGpsData.Y);
- fflush(fpGps);
- }
- #endif
- // write gps data into the mapfile in shareable memory
- MapGpsData();
- /////////////////////
- m_dlgMonitor->UpdateGpsData( &m_Gps.m_tGpsData );
- /////////////////////
- }
- void CGmarkView::OnButtonClose()
- {
- // TODO: Add your control notification handler code here
- if(m_Gps.Close() == RET_NG)
- {
- MessageBox(_T("Close GPS port A or B failed!"));
- return;
- }
- // kill the timer
- KillTimer( m_nMonitorTimer );
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_COMM, _T("Gps port closed"));
- m_dlgOperate->GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(TRUE);
- m_dlgOperate->GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE);
- /////////////////////
- }
- void CGmarkView::OnButtonInit()
- {
- // TODO: Add your control notification handler code here
- m_Gps.Initialize( 4800 );
- }
- void CGmarkView::OnButtonConnect()
- {
- // TODO: Add your control notification handler code here
- // connect gps
- if(m_Gps.Connect() == RET_NG) return;
- // start timer to monitor if gps receiving is normal
- m_nMonitorTimer = SetTimer( 1, GPS_CON_MONITOR_INTERVAL, 0 );
- /////////////////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_COMM, _T("Gps port opened"));
- m_dlgOperate->GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(FALSE);
- m_dlgOperate->GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(TRUE);
- /////////////////////
- }
- void CGmarkView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- static BOOL bGpsWarn = FALSE;
- switch( nIDEvent )
- {
- case 1: //m_nMonitorTimer
- if( m_Gps.m_tGpsData.Status == 0 )
- {
- if( bGpsWarn )
- {
- // m_szGpsStatus = _T("No Position Data");
- // UpdateData(false);
- }
- else
- {
- bGpsWarn = TRUE;
- }
- }
- else
- {
- bGpsWarn = FALSE;
- }
- break;
- case 2: //m_nActivate -- to activate the GPRS connection
- // m_bNeedActivate = TRUE;
- break;
- }
-
- CFormView::OnTimer(nIDEvent);
- }
- void CGmarkView::OnButtonGgaoff()
- {
- // TODO: Add your control notification handler code here
- m_Gps.SendCmd("$PASHS,NME,GGA,A,OFF");
- }
- void CGmarkView::OnButtonGga2s()
- {
- // TODO: Add your control notification handler code here
- m_Gps.SendCmd("$PASHS,NME,GGA,A,ON,2");
- }
- void CGmarkView::MapGpsData()
- {
- memcpy(m_lpMapBuf, &(m_Gps.m_tGpsData), sizeof(m_Gps.m_tGpsData));
- }
- void CGmarkView::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- if (m_lpMapBuf != NULL)
- {
- UnmapViewOfFile(m_lpMapBuf);
- m_lpMapBuf = NULL;
- }
- if (m_hMap != NULL)
- {
- CloseHandle(m_hMap);
- m_hMap = NULL;
- }
- // Close the log file
- m_Log.Close();
-
- #ifdef _SAVE_RTCM
- fclose(fpRtcm);
- #endif
- #ifdef _SAVE_GPSDATA
- fclose(fpGps);
- #endif
-
- CFormView::OnClose();
- }
- /*for test mapping file*/
- void CGmarkView::OnButtonTestmap()
- {
- // TODO: Add your control notification handler code here
- m_Gps.m_tGpsData.Latitude = 12;
- m_Gps.m_tGpsData.Longitude = 34;
- m_Gps.m_tGpsData.SatCount = 8;
- m_Gps.m_tGpsData.Status = 1;
- OnGpsUpdate(0,0);
- }
- void CGmarkView::OnButtonLogon()
- {
- // TODO: Add your control notification handler code here
- WORD wVersionRequested;
- WSADATA wsaData;
- int nRet;
- if(m_Socket != INVALID_SOCKET) return;
- wVersionRequested = MAKEWORD(2,2);
- WSAStartup( wVersionRequested, &wsaData);
- #ifndef _USE_UDP
- m_Socket = socket(AF_INET, SOCK_STREAM, 0);
- #else
- m_Socket = socket(AF_INET, SOCK_DGRAM, 0); // use UDP
- #endif
- if( m_Socket == INVALID_SOCKET )
- {
- MessageBox(_T("INVALID_SOCKET is returned by socket()"));
- return;
- }
- SOCKADDR_IN BaseSockAddr;
- memset( &BaseSockAddr, 0, sizeof(BaseSockAddr) );
- BaseSockAddr.sin_port = htons(PORT);
- BaseSockAddr.sin_family = AF_INET;
- BaseSockAddr.sin_addr.S_un.S_addr = inet_addr(gtConfData.szBaseIP);
- // connect to server
- #ifndef _USE_UDP
- nRet = connect( m_Socket, (const struct sockaddr*)&BaseSockAddr, sizeof(BaseSockAddr) );
- if( nRet == SOCKET_ERROR )
- {
- MessageBox(_T("SOCKET_ERROR is returned by connect()"));
- return;
- }
- #endif
- //to logon
- char send_data[MAX_ROVERID+MAX_ROVERDESC+2];
-
- memset( send_data, 0, sizeof(send_data) );
- #ifndef _USE_UDP
- strcpy( send_data, "LOG,");
- strcat( send_data, gtConfData.szRoverID );
- strcat( send_data, "," );
- strcat( send_data, gtConfData.szRoverDesc );
- #else
- strcpy( send_data, "LOG,");
- strcat( send_data, gtConfData.szRoverID );
- strcat( send_data, "," );
- strcat( send_data, gtConfData.szRoverDesc );
- #endif
- // send logon message to base
- #ifndef _USE_UDP
- nRet = send( m_Socket, send_data, strlen(send_data), 0 );
- #else
- nRet = sendto( m_Socket, send_data, strlen(send_data), 0,
- (struct sockaddr*)&BaseSockAddr, sizeof(BaseSockAddr) );
- #endif
- if( nRet == SOCKET_ERROR )
- {
- MessageBox(_T("SOCKET_ERROR is returned by send()"));
- return;
- }
- // specify a read socket thread
- DWORD dwThreadID;
- m_bCloseSocketFlg = FALSE;
- if (hReadSocketThread = CreateThread (NULL, 0, ReadSocketThread, this , 0, &dwThreadID))
- {
- ResumeThread( hReadSocketThread );
- // SetThreadPriority( hReadSocketThread, THREAD_PRIORITY_TIME_CRITICAL );
- CeSetThreadPriority(hReadSocketThread, 0);
- }
- else
- {
- MessageBox(TEXT("Unable to create the read socket thread"), TEXT("Error"), MB_OK);
- return;
- }
- SetDlgItemText(IDC_EDIT_STATUS_SOCK, _T("Connected to Center"));
- // enable to send activation package
- m_nActivateTimer = SetTimer( 2, SOCK_CON_ACTIVATE_INTERVAL, 0 );
- GetDlgItem(IDC_BUTTON_LOGON)->EnableWindow(FALSE);
- GetDlgItem(IDC_BUTTON_LOGOFF)->EnableWindow(TRUE);
- /////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_SOCK, _T("Connected to Center"));
- m_dlgOperate->GetDlgItem(IDC_BUTTON_LOGON)->EnableWindow(FALSE);
- m_dlgOperate->GetDlgItem(IDC_BUTTON_LOGOFF)->EnableWindow(TRUE);
- /////////
- }
- void CGmarkView::OnButtonLogoff()
- {
- // TODO: Add your control notification handler code here
- // send logoff message to base
- SOCKADDR_IN BaseSockAddr;
- char send_data[MAX_ROVERID+MAX_ROVERDESC+2];
-
- memset( send_data, 0, sizeof(send_data) );
- memset( &BaseSockAddr, 0, sizeof(BaseSockAddr) );
- BaseSockAddr.sin_port = htons(PORT);
- BaseSockAddr.sin_family = AF_INET;
- BaseSockAddr.sin_addr.S_un.S_addr = inet_addr(gtConfData.szBaseIP);
- strcpy( send_data, "OFF,");
- strcat( send_data, gtConfData.szRoverID );
- strcat( send_data, "," );
- strcat( send_data, gtConfData.szRoverDesc );
- sendto( m_Socket, send_data, strlen(send_data), 0,
- (struct sockaddr*)&BaseSockAddr, sizeof(BaseSockAddr) );
- //
- KillTimer( m_nActivateTimer ); // disable to send activate package.
- m_bCloseSocketFlg = TRUE;
- TerminateThread(hReadSocketThread,0);
- CloseHandle(hReadSocketThread);
- closesocket( m_Socket );
- WSACleanup();
- m_Socket = INVALID_SOCKET;
- SetDlgItemText(IDC_EDIT_STATUS_SOCK, _T("Disconnected from Center"));
- GetDlgItem(IDC_BUTTON_LOGON)->EnableWindow(TRUE);
- GetDlgItem(IDC_BUTTON_LOGOFF)->EnableWindow(FALSE);
- ///////////
- m_dlgOperate->SetDlgItemText(IDC_EDIT_STATUS_SOCK, _T("Disconnected from Center"));
- m_dlgOperate->GetDlgItem(IDC_BUTTON_LOGON)->EnableWindow(TRUE);
- m_dlgOperate->GetDlgItem(IDC_BUTTON_LOGOFF)->EnableWindow(FALSE);
- ///////////
- }
- BOOL CGmarkView::ReadConfig()
- {
- FILE * fp;
- char sTemp[CONFIG_MAX_LINE+1];
- int n;
- BOOL nRet = FALSE;
- memset( gtConfData.szRoverID, 0, sizeof(gtConfData.szRoverID));
- memset( gtConfData.szRoverDesc, 0, sizeof(gtConfData.szRoverDesc));
- memset( sTemp, 0, sizeof(sTemp));
- #ifndef _WIN32_WCE_CEPC
- fp = fopen("\ResidentFlash\config.txt","rt");
- #else
- fp = fopen("\config.txt","rt");
- #endif
- if(fp == NULL)
- {
- // MessageBox(_T("Open configuration file failed"));
- // return FALSE;
- }
-
- while(1)
- {
- n = getline(fp, gtConfData.szRoverID);
- if( n<= 0 )
- {
- //MessageBox(_T("Read configuration file failed"));
- //break;
- }
- n = getline(fp, gtConfData.szRoverDesc);
- if( n<= 0 )
- {
- //MessageBox(_T("Read configuration file failed"));
- //break;
- }
- n = getline(fp, sTemp); //read ip address of base station
- if( n<= 0 || strlen(sTemp)>15)
- {
- //MessageBox(_T("Read configuration file failed"));
- //break;
- }
- strcpy( gtConfData.szBaseIP, sTemp );
- n = getline(fp, sTemp); //read original longitude
- if( n<= 0 )
- {
- // MessageBox(_T("Read configuration file failed"));
- // break;
- }
- gtConfData.dOrigLon = atof(sTemp);
- n = getline(fp, sTemp); //read original latitude
- if( n<= 0 )
- {
- //MessageBox(_T("Read configuration file failed"));
- // break;
- }
- gtConfData.dOrigLat = atof(sTemp);
- n = getline(fp, sTemp); //read start mode
- if( n<= 0 )
- {
- // MessageBox(_T("Read configuration file failed"));
- // break;
- }
- gtConfData.bAutoStart = atoi(sTemp);
- n = getline(fp, sTemp); //read if display NEMA message
- if( n<= 0 )
- {
- // MessageBox(_T("Read configuration file failed"));
- // break;
- }
- gtConfData.bDispNEMA = atoi(sTemp);
- n = getline(fp, sTemp); //read if display RTCM data
- if( n<= 0 )
- {
- // MessageBox(_T("Read configuration file failed"));
- // break;
- }
- gtConfData.bDispRTCM = atoi(sTemp);
- nRet = TRUE;
- break;
- }
- fclose(fp);
- return nRet;
- }
- void CGmarkView::GmarkMessageBox(const char * buf)
- {
- int nLength=strlen((const char *)buf);
- TCHAR *wszBuffer = new TCHAR[nLength+1];
- memset(wszBuffer, 0, (nLength+1)*sizeof(TCHAR));
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (const char *)buf, -1, wszBuffer, nLength);
- MessageBox(wszBuffer);
- delete wszBuffer;
- }
- int CGmarkView::getline(FILE *fp, char *szLine)
- {
- int len;
- char *hp, *tp;
- char buffer[CONFIG_MAX_LINE+1];
- memset( buffer, 0, sizeof(buffer) );
- if( fgets(buffer, CONFIG_MAX_LINE, fp) == NULL ) return 0;
- hp = buffer;
- while(*hp == ' ' || *hp == 't') hp++;
- tp = hp + strlen(hp) - 1;
- while (tp >= hp && (*tp == ' ' || *tp == 't' || *tp == 'n')) tp--;
- *(tp+1) = ' ';
- for(tp = buffer; *tp++ = *hp++;);
- len = strlen(buffer);
- strcpy( szLine, buffer );
- return len;
- }
- void CGmarkView::WriteRTCM(char * RTCMBuf, int nBytes)
- {
- //save rtcm data
- #ifdef _SAVE_RTCM
- fwrite(RTCMBuf, 1, nBytes, fpRtcm);
- #endif
- /*****************check rtcm sum****************************/
- /*
- unsigned char sum;
- int len = strlen(RTCMBuf);
- char * recvmsg = new char[len];
- memset( recvmsg, 0, sizeof(recvmsg));
- if((unsigned char)RTCMBuf[0] != 0x8F)
- {
- m_dlgMonitor->UpdateRtcmData("0x8F head error ");
- delete recvmsg;
- return;
- }
- sum =0;
- for(int i=1; i<len-1; i++)
- {
- sum = sum^(unsigned char)RTCMBuf[i];
- recvmsg[i-1] = RTCMBuf[i];
- }
- if(sum != RTCMBuf[len-1])
- {
- m_dlgMonitor->UpdateRtcmData("check sum error ");
- delete recvmsg;
- return;
- }
- memset( RTCMBuf, 0, sizeof(RTCMBuf));
- strcpy(RTCMBuf, recvmsg);
-
- delete recvmsg;
- */
- /******************end check*******************************/
- /******************** //for test********************************/
- /* TCHAR wBuff[GPS_MAX_NEMASIZE+1];
- // Convert from MultiByte to UNICODE
- mbstowcs(wBuff, (const char *)RTCMBuf, GPS_MAX_NEMASIZE);
- SetDlgItemText( IDC_EDIT_RECV, wBuff );*/
- //////////////////////
- m_dlgMonitor->UpdateRtcmData((const char *)RTCMBuf);
- //////////////////////
- /******************************************************************/
- m_Gps.WriteRTCM( RTCMBuf );
- }
- DWORD WINAPI ReadSocketThread(LPVOID lpvoid) //Read Socket Thread
- {
- int nBytes=0;
- char sLog[MAX_LOGMSG_SIZE+1];
- CGmarkView *pView = (CGmarkView *)lpvoid;
- try
- {
- while(1)
- {
- if(pView->m_bCloseSocketFlg)
- {
- break;
- }
- // receive from socket
- memset( pView->m_RTCMBuf, 0, sizeof(pView->m_RTCMBuf) );
- pView->m_Log.Write("Just before recv()");
- #ifndef _USE_UDP
- nBytes = recv( pView->m_Socket, pView->m_RTCMBuf, sizeof(pView->m_RTCMBuf)-1, 0 );
- #else
- SOCKADDR_IN SockAddr;
- int buflen;
- memset( &SockAddr, 0, sizeof(SockAddr) );
- SockAddr.sin_port = htons(PORT);
- SockAddr.sin_family = AF_INET;
- SockAddr.sin_addr.S_un.S_addr = inet_addr(gtConfData.szBaseIP);
- buflen = sizeof(SockAddr);
- nBytes = recvfrom( pView->m_Socket, pView->m_RTCMBuf, sizeof(pView->m_RTCMBuf)-1, 0,
- (struct sockaddr*)&SockAddr, &buflen );
- // strcpy(pView->m_RTCMBuf, "12345678901234567890123456789012345678901234567890
- // 123456789012345678901234567890");//for test
- // nBytes = strlen(pView->m_RTCMBuf);
- #endif
- memset( sLog, 0, sizeof(sLog) );
- _snprintf( sLog, sizeof(sLog)-1, "%d bytes rtcm data received.", nBytes );
- pView->m_Log.Write( sLog );
- // check the result of recv()
- if(nBytes>0)
- {
- //reply an ACK to server
- #ifndef _USE_UDP
- send( pView->m_Socket, "ACK", 3, 0 );
- #else
- sendto( pView->m_Socket, "ACK", 3, 0,
- (const struct sockaddr*)&SockAddr, buflen);
- #endif
- pView->WriteRTCM( pView->m_RTCMBuf, nBytes );
- memset( sLog, 0, sizeof(sLog) );
- _snprintf( sLog, sizeof(sLog)-1, "%d bytes rtcm data writed.", nBytes );
- pView->m_Log.Write( sLog );
- }
- else if(nBytes==0)
- {
- pView->m_Log.Write( "return 0 by recv()" );
- break;
- }
- else if(nBytes==SOCKET_ERROR)
- {
- DWORD error = GetLastError();
- memset( sLog, 0, sizeof(sLog) );
- _snprintf( sLog, sizeof(sLog)-1, "%d returned by recv() and error code = %d.", nBytes, error );
- pView->m_Log.Write( sLog );
- break;
- }
- else
- {
- pView->m_Log.Write( "return else value by recv()" );
- break;
- }
- // send activate package if need
- if( pView->m_bNeedActivate )
- {
- send( pView->m_Socket, SOCK_CON_ACTIVEPACK, 1, 0 );
- pView->m_Log.Write( "Activate package is sent" );
- pView->m_bNeedActivate = FALSE;
- }
- }
- }catch(...)
- {
- pView->m_Log.Write( "Exception is caught" );
- }
-
- pView->m_Log.Write( "ReadSocketThread will exit soon." );
- //MessageBox(pView->m_hWnd,_T("ReadSocketThread will exit soon."),_T("Info"),MB_OK);
- ExitThread(WM_QUIT);
- return 0;
- }
- void CGmarkView::AddText2Edit(int idc, LPCTSTR text)
- {
- CString szText;
- CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT_GPSMSG);
- GetDlgItemText(IDC_EDIT_GPSMSG, szText);
- szText += text;
- SetDlgItemText(IDC_EDIT_GPSMSG, szText);
- int nCount = pEdit->GetLineCount();
- if(szText.GetLength()>1000)
- {
- SetDlgItemText(IDC_EDIT_GPSMSG, text);
- }
- else if(nCount>5)
- {
- pEdit->LineScroll( (nCount - 6) );
- }
- }
- void CGmarkView::OnButtonRtcmon()
- {
- // TODO: Add your control notification handler code here
- m_Gps.SendCmd("$PASHS,RTC,REM,B");
- m_Gps.SendCmd("$PASHQ,RTC,A");
- }
- void CGmarkView::OnButtonRtcmoff()
- {
- // TODO: Add your control notification handler code here
- m_Gps.SendCmd("$PASHS,RTC,OFF");
- m_Gps.SendCmd("$PASHQ,RTC,A");
- }
- void CGmarkView::CreateFilename(LPSTR filename, int type)
- {
- SYSTEMTIME st;
- char sTemp[MAX_PATH+1];
- memset( sTemp, 0, sizeof(sTemp) );
-
- GetLocalTime(&st);
- sprintf( sTemp, "%4d%2d%2d-%2d%2d",
- st.wYear, st.wMonth, st.wDay,
- st.wHour, st.wMinute );
- switch(type)
- {
- case FN_GPSDATA:
- strcpy( filename, "\Storage Card\Gps" );
- strcat( filename, gtConfData.szRoverID );
- strcat( filename, "-");
- strcat( filename, sTemp );
- strcat( filename, ".txt");
- break;
- case FN_LOG:
- strcpy( filename, "\Storage Card\Log" );
- strcat( filename, gtConfData.szRoverID );
- strcat( filename, "-");
- strcat( filename, sTemp );
- strcat( filename, ".txt");
- break;
- case FN_RAW:
- strcpy( filename, "\Storage Card\Raw" );
- strcat( filename, gtConfData.szRoverID );
- strcat( filename, "-");
- strcat( filename, sTemp );
- strcat( filename, ".dat");
- break;
- }
- }
- LRESULT CGmarkView::AutoProcess(WPARAM w, LPARAM l)
- {
- BOOL bRet;
- DWORD dwFlag = INTERNET_CONNECTION_PROXY;
- int nCount = 0;
- bRet = InternetGetConnectedState(&dwFlag,0);
- if(!bRet)
- {
- do
- {
- OnButtonDial();
- DWORD nWaitRet = WaitForSingleObject(m_hDialCompleteEvent, 20000);
- if(nWaitRet == WAIT_OBJECT_0) break;
- nCount++;
- }while(nCount <= 2);
- }
- if(nCount > 2)
- {
- MessageBox(_T("Dial up to internet failed. Program will exit."));
- AfxGetMainWnd()->PostMessage(WM_CLOSE);
- return -1;
- }
- OnButtonConnect();
- Sleep(1000);
- OnButtonLogon();
- return 0;
- }
- void CGmarkView::OnButtonPbnon2()
- {
- m_Gps.SendCmd("$PASHS,NME,PBN,A,ON,1");
- }
- void CGmarkView::OnButtonPbnoff()
- {
- m_Gps.SendCmd("$PASHS,NME,PBN,A,OFF");
- }