22#include <QApplication>
24#include <QRegularExpression>
35#define KNOTS_TO_KMH 1.852
51 if ( !
mSource->isSequential() )
57 numBytes =
mSource->bytesAvailable();
64 QgsDebugMsgLevel( QStringLiteral(
"Got %1 NMEA bytes" ).arg( numBytes ), 3 );
68 QgsDebugMsgLevel( QStringLiteral(
"Setting device status to DataReceived" ), 3 );
87 int endSentenceIndex = 0;
90 while ( ( endSentenceIndex =
mStringBuffer.indexOf( QLatin1String(
"\r\n" ) ) ) && endSentenceIndex != -1 )
92 endSentenceIndex =
mStringBuffer.indexOf( QLatin1String(
"\r\n" ) );
95 if ( endSentenceIndex == -1 )
100 if ( endSentenceIndex >= dollarIndex )
102 if ( dollarIndex != -1 )
104 const QString substring =
mStringBuffer.mid( dollarIndex, endSentenceIndex );
105 QByteArray ba = substring.toLocal8Bit();
106 const thread_local QRegularExpression rxSentence( QStringLiteral(
"^\\$([A-Z]{2})([A-Z]{3})" ) );
107 const QRegularExpressionMatch sentenceMatch = rxSentence.match( substring );
108 const QString sentenceId = sentenceMatch.captured( 2 );
109 if ( sentenceId == QLatin1String(
"GGA" ) )
115 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
117 else if ( sentenceId == QLatin1String(
"RMC" ) )
123 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
125 else if ( sentenceId == QLatin1String(
"GSV" ) )
131 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
133 else if ( sentenceId == QLatin1String(
"VTG" ) )
139 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
141 else if ( sentenceId == QLatin1String(
"GSA" ) )
146 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
148 else if ( sentenceId == QLatin1String(
"GST" ) )
154 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
156 else if ( sentenceId == QLatin1String(
"HDT" ) )
162 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
164 else if ( sentenceId == QLatin1String(
"HDG" ) )
170 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
175 QgsDebugMsgLevel( QStringLiteral(
"unknown nmea sentence: %1" ).arg( substring ), 2 );
187 if ( nmea_parse_GPGGA( data, len, &result ) )
190 double longitude = result.lon;
191 if ( result.ew ==
'W' )
193 longitude = -longitude;
195 double latitude = result.lat;
196 if ( result.ns ==
'S' )
198 latitude = -latitude;
206 const QTime time( result.utc.hour, result.utc.min, result.utc.sec, result.utc.msec );
207 if ( time.isValid() )
220 if ( result.sig >= 0 && result.sig <= 8 )
236 if ( nmea_parse_GPGST( data, len, &result ) )
239 const double sig_lat = result.sig_lat;
240 const double sig_lon = result.sig_lon;
241 const double sig_alt = result.sig_alt;
255 if ( nmea_parse_GPHDT( data, len, &result ) )
264 if ( nmea_parse_HCHDG( data, len, &result ) )
267 if ( result.ew_variation ==
'E' )
277 if ( nmea_parse_GPRMC( data, len, &result ) )
279 double longitude = result.lon;
280 if ( result.ew ==
'W' )
282 longitude = -longitude;
284 double latitude = result.lat;
285 if ( result.ns ==
'S' )
287 latitude = -latitude;
292 if ( !std::isnan( result.direction ) )
296 const QDate date( result.utc.year + 1900, result.utc.mon + 1, result.utc.day );
297 const QTime time( result.utc.hour, result.utc.min, result.utc.sec, result.utc.msec );
298 if ( date.isValid() && time.isValid() )
312 if ( result.status ==
'A' )
314 if ( result.mode ==
'A' )
319 else if ( result.mode ==
'D' )
324 else if ( result.mode ==
'P' )
329 else if ( result.mode ==
'R' )
334 else if ( result.mode ==
'F' )
339 else if ( result.mode ==
'E' )
344 else if ( result.mode ==
'M' )
349 else if ( result.mode ==
'S' )
367 if ( result.navstatus ==
'S' )
371 else if ( result.navstatus ==
'C' )
375 else if ( result.navstatus ==
'U' )
388 if ( nmea_parse_GPGSV( data, len, &result ) )
391 for (
int i = 0; i < NMEA_SATINPACK; ++i )
393 const nmeaSATELLITE currentSatellite = result.sat_data[i];
395 satelliteInfo.
azimuth = currentSatellite.azimuth;
396 satelliteInfo.
elevation = currentSatellite.elv;
397 satelliteInfo.
id = currentSatellite.id;
398 satelliteInfo.
inUse =
false;
403 satelliteInfo.
inUse =
true;
406 satelliteInfo.
signal = currentSatellite.sig;
407 satelliteInfo.
satType = result.talkerId[1];
409 if ( result.talkerId[0] ==
'G' )
411 if ( result.talkerId[1] ==
'P' )
415 else if ( result.talkerId[1] ==
'L' )
419 else if ( result.talkerId[1] ==
'A' )
423 else if ( result.talkerId[1] ==
'B' )
427 else if ( result.talkerId[1] ==
'Q' )
433 if ( satelliteInfo.
satType ==
'P' && satelliteInfo.
id > 32 )
437 satelliteInfo.
id = currentSatellite.id + 87;
440 bool idAlreadyPresent =
false;
445 if ( existingSatInView.id == currentSatellite.id )
447 idAlreadyPresent =
true;
453 if ( !idAlreadyPresent && currentSatellite.azimuth > 0 && currentSatellite.elv > 0 )
465 if ( nmea_parse_GPVTG( data, len, &result ) )
468 if ( !std::isnan( result.dir ) )
484 if ( nmea_parse_GPGSA( data, len, &result ) )
494 bool mixedConstellation =
false;
495 for (
int i = 0; i < NMEA_MAXSAT; i++ )
497 if ( result.sat_prn[ i ] > 0 )
503 if ( ( result.talkerId[0] ==
'G' && result.talkerId[1] ==
'L' ) || result.sat_prn[i] > 64 )
505 else if ( result.sat_prn[i] >= 1 && result.sat_prn[i] <= 32 )
507 else if ( result.sat_prn[i] > 32 && result.sat_prn[i] <= 64 )
511 if ( result.sat_prn[i] > 0 )
513 if ( mixedConstellation
515 && commonConstellation != constellation ) )
517 mixedConstellation =
true;
521 commonConstellation = constellation;
526 if ( mixedConstellation )
529 switch ( result.fix_type )
GnssConstellation
GNSS constellation.
@ Gps
Global Positioning System (GPS)
@ Glonass
Global Navigation Satellite System (GLONASS)
@ Unknown
Unknown/other system.
@ Qzss
Quasi Zenith Satellite System (QZSS)
GpsQualityIndicator
GPS signal quality indicator.
@ RTK
Real-time-kynematic.
@ Simulation
Simulation mode.
@ FloatRTK
Float real-time-kynematic.
@ Manual
Manual input mode.
@ NotValid
Navigation status not valid.
Abstract base class for connection to a GPS device.
QgsGpsInformation mLastGPSInformation
Last state of the gps related variables (e.g. position, time, ...)
void nmeaSentenceReceived(const QString &substring)
Emitted whenever the GPS device receives a raw NMEA sentence.
std::unique_ptr< QIODevice > mSource
Data source (e.g. serial device, socket, file,...)
Status mStatus
Connection status.
void stateChanged(const QgsGpsInformation &info)
Emitted whenever the GPS state is changed.
void processVtgSentence(const char *data, int len)
process VTG sentence
void processRmcSentence(const char *data, int len)
process RMC sentence
void parseData() override
Parse available data source content.
void processHchdgSentence(const char *data, int len)
process HCHDG sentence
void processGgaSentence(const char *data, int len)
process GGA sentence
void processGsvSentence(const char *data, int len)
process GSV sentence
void processGstSentence(const char *data, int len)
process GST sentence
void processHdtSentence(const char *data, int len)
process HDT sentence
void processGsaSentence(const char *data, int len)
process GSA sentence
QString mStringBuffer
Store data from the device before it is processed.
QgsNmeaConnection(QIODevice *device)
Constructs a QgsNmeaConnection with given device.
void processStringBuffer()
Splits mStringBuffer into sentences and calls libnmea.
Encapsulates information relating to a GPS satellite.
double elevation
Elevation of the satellite, in degrees.
bool inUse
true if satellite was used in obtaining the position fix.
int signal
Signal strength (0-99dB), or -1 if not available.
int id
Contains the satellite identifier number.
double azimuth
The azimuth of the satellite to true north, in degrees.
QChar satType
satType value from NMEA message $GxGSV, where x: P = GPS; S = SBAS (GPSid> 32 then SBasid = GPSid + 8...
#define QgsDebugMsgLevel(str, level)