MFC help for building GUI in microsoft visual studio 2008
hi,
i am developing a GUI for my project through MFC using Microsoft visual studio 2008. down below is a part my code that i have written. the purpose of this code is to get GPS data (using marshallsoft GPS component) from serial port, display, and keep on updating the data. i am facing problem using While (1) loop to update GPS data continuously. implementing while loop (as shown in my code) causes the dialog box disappear, although mydialog.exe is shown running at background in the task manager.
any kind of help is appreciated.
thanks
//Variables CTextFile
int Code;
int LinesRX;
char Temp[256];
int decimal, precision = 12, sign;
//CString sDataBuffer(DataBuffer);
//LPCTSTR lpszDataBuffer = sDataBuffer;
CString str, Lat, Long, Alt, Azi, Dist;
CStringArray sensorval;
double Latgcs, Longcs, Altgcs, Compgcs;
double Distance, Azimuth, Angle;
double Latitude, Longitude, Altitude;
CTextFile Sensor;
BOOL res;
str=_T("E:\\sensorfile.txt");
Code = mgcAttach(MGC_KEY_CODE); // attach MGC component
if (mgcOpen(MGC_COM2)== 0)
SetDlgItemText(IDC_GPSPORT, (LPCTSTR) L"OK");
else
SetDlgItemText(IDC_GPSPORT, (LPCTSTR) L"Port Error");
res = Sensor.ReadTextFile(str, sensorval);
Latgcs = wcstod(sensorval[0], NULL);
SetDlgItemText(IDC_LATITUDE, sensorval[0]);
Longcs = wcstod(sensorval[1], NULL);
SetDlgItemText(IDC_LONGITUDE, sensorval[1]);
Altgcs = wcstod(sensorval[2], NULL);
SetDlgItemText(IDC_ALTITUDE, sensorval[2]);
Compgcs = wcstod(sensorval[3], NULL);
SetDlgItemText(IDC_NORTH, sensorval[3]);
while (1)
{
Code = mgcSetInteger(MGC_SET_SENTENCE_TYPE, MGC_SENTENCE_GPGGA);
mgcLockData(1);
Latitude = mgcLatitude();
Lat = _ecvt( Latitude, precision, &decimal, &sign );
SetDlgItemText(IDC_ACLAT, Lat);
Code = mgcGetData(GPGGA_LONGITUDE,(LPSTR)DataBuffer);
Longitude = atof (DataBuffer);
Long = _ecvt( Longitude, precision, &decimal, &sign );
SetDlgItemText(IDC_ACLONG, Long);
Code = mgcGetData(GPGGA_ALTITUDE,(LPSTR)DataBuffer);
Altitude = atof(DataBuffer);
Alt = _ecvt( Altitude, precision, &decimal, &sign );
SetDlgItemText(IDC_ACALT, Alt);
Distance = mgcGreatCircle(IDC_LATITUDE, IDC_LONGITUDE, IDC_ACLAT, IDC_ACLONG);
Dist = _ecvt( Distance, precision, &decimal, &sign );
SetDlgItemText(IDC_ACDIST, Dist);
Angle = mgcBearing (IDC_LATITUDE, IDC_LONGITUDE, IDC_ACLAT, IDC_ACLONG);
Azimuth = Angle / 60000.0;
Azi = _ecvt( Azimuth, precision, &decimal, &sign );
SetDlgItemText(IDC_ACBEAR, Azi);
mgcLockData(0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
Re: MFC help for building GUI in microsoft visual studio 2008
Mike is very good about including samples into his software...look in the "X:\mgc4c\APPS" for sample programs...I'd start with looking into "MFC_PGM.CPP"
Re: MFC help for building GUI in microsoft visual studio 2008
You should create a worker thread and move your code to communicate with the serial port (GPS) to this thread. Note: only code tocommunicate with the serial port, not the statements to update GUI!
Then your worker thread has to periodically notify the main GUI thread about new received data (the easiest way to notify is to PostMessage some user defined message(s); then the main thread will update its GUI from the message handler(s) ofthis (these) user defined message(s)).
See Joe Newcomer's essay Using Worker Threads
Re: MFC help for building GUI in microsoft visual studio 2008
thank you so much VictorN. that really helped. :-)
Re: MFC help for building GUI in microsoft visual studio 2008