forked from NOVACProject/MobileDOAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPS.cpp
213 lines (179 loc) · 4.55 KB
/
GPS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// GPS.cpp: implementation of the CGPS class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <iostream>
#include "GPS.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CString g_exePath; // <-- This is the path to the executable. This is a global variable and should only be changed in DMSpecView.cpp
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGPS::CGPS()
: m_gpsInfo(), fRun(false)
{
}
CGPS::CGPS(const char* pCOMPort, long pBaudrate)
: m_gpsInfo(), fRun(false)
{
serial.SetBaudrate(pBaudrate);
serial.SetPort(pCOMPort);
if (!Connect())
{
MessageBox(nullptr, "Could not communicate with GPS. No GPS-data can be retrieved!", "Error", MB_OK | MB_SYSTEMMODAL);
}
}
CGPS::CGPS(CSerialConnection&& serial)
: m_gpsInfo(), fRun(false)
{
// Take ownership of the serial connection.
this->serial = std::move(serial);
}
CGPS::CGPS(CGPS&& other)
{
this->serial = std::move(other.serial);
this->fRun = other.fRun;
this->m_gotContact = other.m_gotContact;
this->m_gpsInfo = other.m_gpsInfo;
this->m_logFile = other.m_logFile;
}
CGPS& CGPS::operator=(CGPS&& other)
{
this->serial = std::move(other.serial);
this->fRun = other.fRun;
this->m_gotContact = other.m_gotContact;
this->m_gpsInfo = other.m_gpsInfo;
this->m_logFile = other.m_logFile;
return *this;
}
CGPS::~CGPS()
{
serial.Close();
}
bool CGPS::Connect()
{
if (!serial.Init())
{
return false;
}
return true;
}
void CGPS::Get(gpsData& dst)
{
std::lock_guard<std::mutex> guard(this->m_gpsInfoMutex); // lock the access to the 'm_gpsInfo' while we're copying out the data
dst = this->m_gpsInfo;
}
bool CGPS::ReadGPS()
{
const long bytesToRead = 512;
char gpstxt[512];
gpstxt[0] = 0;
// copy the old data into the temp structure (not all sentences provide all data...)
gpsData localGpsInfo = this->m_gpsInfo;
do{
long cnt = 0;
// serial.FlushSerialPort(10);
if(serial.Check(2000))
{
while(serial.Check(100) && cnt < bytesToRead)
{
// Read GPRMC and GPGGA
serial.Read(gpstxt + cnt, 1);
if (gpstxt[cnt] == '\n')
{
break; // each sentence ends with newline
}
cnt++;
}
m_gotContact = true;
}
else
{
std::cerr << "timeout in getting gps." << std::endl;
serial.FlushSerialPort(1);
m_gotContact = false;
this->m_gpsInfo.status = "NA";
return false;
}
}while(!Parse(gpstxt, localGpsInfo));
// Copy the parsed data to our member structure in a controlled fashion
{
std::lock_guard<std::mutex> guard(this->m_gpsInfoMutex); // lock the access to the 'm_gpsInfo' while we're copying out the data
this->m_gpsInfo = localGpsInfo;
}
#ifdef _DEBUG
m_logFile.Format("gps.log"); // for testing only
if(strlen(m_logFile) > 0){
FILE *f = fopen(g_exePath + m_logFile, "a+");
fprintf(f, "%1d\t%ld\t", localGpsInfo.date, localGpsInfo.time);
fprintf(f, "%lf\t%lf\t%lf\t", localGpsInfo.latitude, localGpsInfo.longitude, localGpsInfo.altitude);
fprintf(f, "%ld\t", localGpsInfo.nSatellitesTracked);
fprintf(f, "%ld\n", localGpsInfo.nSatellitesSeen);
fclose(f);
}
#endif
return true;
}
void CGPS::CloseSerial()
{
this->serial.Close();
}
/** The async GPS data collection thread. This will take ownership of the CGPS which is passed in as reference
and delete it when we're done. */
UINT CollectGPSData(LPVOID pParam)
{
CGPS *gps = (CGPS *)pParam;
gps->fRun = true;
while(gps->fRun)
{
if (gps->ReadGPS())
{
/* make a small pause */
Sleep(10);
}
else
{
// Error reading GPS. Sleep longer and try again.
gps->CloseSerial();
Sleep(1000);
gps->Connect();
Sleep(1000);
}
}
gps->fRun = false;
delete gps;
return 0;
}
GpsAsyncReader::GpsAsyncReader(const char* pCOMPort, long baudrate)
{
m_gps = new CGPS(pCOMPort, baudrate);
m_gpsThread = AfxBeginThread(CollectGPSData, (LPVOID)m_gps, THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
}
GpsAsyncReader::GpsAsyncReader(CGPS&& gps)
{
m_gps = new CGPS(std::move(gps));
m_gpsThread = AfxBeginThread(CollectGPSData, (LPVOID)m_gps, THREAD_PRIORITY_NORMAL, 0, 0, nullptr);
}
GpsAsyncReader::~GpsAsyncReader()
{
m_gps = nullptr; // we don't get to delete the m_gps, its up to the background thread to do so.
}
void GpsAsyncReader::Stop()
{
if(nullptr != m_gps && m_gps->fRun == true)
{
m_gps->fRun = false;
}
}
void GpsAsyncReader::Get(gpsData& data)
{
m_gps->Get(data);
}
bool GpsAsyncReader::GotContact() const
{
return m_gps->m_gotContact;
}