19#include "moc_qgsnmeaconnection.cpp"
23#include <QApplication>
25#include <QRegularExpression>
36#define KNOTS_TO_KMH 1.852
52 if ( !
mSource->isSequential() )
58 numBytes =
mSource->bytesAvailable();
65 QgsDebugMsgLevel( QStringLiteral(
"Got %1 NMEA bytes" ).arg( numBytes ), 3 );
69 QgsDebugMsgLevel( QStringLiteral(
"Setting device status to DataReceived" ), 3 );
88 int endSentenceIndex = 0;
91 while ( ( endSentenceIndex =
mStringBuffer.indexOf( QLatin1String(
"\r\n" ) ) ) && endSentenceIndex != -1 )
93 endSentenceIndex =
mStringBuffer.indexOf( QLatin1String(
"\r\n" ) );
96 if ( endSentenceIndex == -1 )
101 if ( endSentenceIndex >= dollarIndex )
103 if ( dollarIndex != -1 )
105 const QString substring =
mStringBuffer.mid( dollarIndex, endSentenceIndex );
106 QByteArray ba = substring.toLocal8Bit();
107 const thread_local QRegularExpression rxSentence( QStringLiteral(
"^\\$([A-Z]{2})([A-Z]{3})" ) );
108 const QRegularExpressionMatch sentenceMatch = rxSentence.match( substring );
109 const QString sentenceId = sentenceMatch.captured( 2 );
110 if ( sentenceId == QLatin1String(
"GGA" ) )
116 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
118 else if ( sentenceId == QLatin1String(
"RMC" ) )
124 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
126 else if ( sentenceId == QLatin1String(
"GSV" ) )
132 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
134 else if ( sentenceId == QLatin1String(
"VTG" ) )
140 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
142 else if ( sentenceId == QLatin1String(
"GSA" ) )
147 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
149 else if ( sentenceId == QLatin1String(
"GST" ) )
155 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
157 else if ( sentenceId == QLatin1String(
"HDT" ) )
163 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
165 else if ( sentenceId == QLatin1String(
"HDG" ) )
171 QgsDebugMsgLevel( QStringLiteral(
"*******************GPS data received****************" ), 2 );
176 QgsDebugMsgLevel( QStringLiteral(
"unknown nmea sentence: %1" ).arg( substring ), 2 );
188 if ( nmea_parse_GPGGA( data, len, &result ) )
191 double longitude = result.lon;
192 if ( result.ew ==
'W' )
194 longitude = -longitude;
196 double latitude = result.lat;
197 if ( result.ns ==
'S' )
199 latitude = -latitude;
207 const QTime time( result.utc.hour, result.utc.min, result.utc.sec, result.utc.msec );
208 if ( time.isValid() )
221 if ( result.sig >= 0 && result.sig <= 8 )
237 if ( nmea_parse_GPGST( data, len, &result ) )
240 const double sig_lat = result.sig_lat;
241 const double sig_lon = result.sig_lon;
242 const double sig_alt = result.sig_alt;
256 if ( nmea_parse_GPHDT( data, len, &result ) )
265 if ( nmea_parse_HCHDG( data, len, &result ) )
268 if ( result.ew_variation ==
'E' )
278 if ( nmea_parse_GPRMC( data, len, &result ) )
280 double longitude = result.lon;
281 if ( result.ew ==
'W' )
283 longitude = -longitude;
285 double latitude = result.lat;
286 if ( result.ns ==
'S' )
288 latitude = -latitude;
293 if ( !std::isnan( result.direction ) )
297 const QDate date( result.utc.year + 1900, result.utc.mon + 1, result.utc.day );
298 const QTime time( result.utc.hour, result.utc.min, result.utc.sec, result.utc.msec );
299 if ( date.isValid() && time.isValid() )
313 if ( result.status ==
'A' )
315 if ( result.mode ==
'A' )
320 else if ( result.mode ==
'D' )
325 else if ( result.mode ==
'P' )
330 else if ( result.mode ==
'R' )
335 else if ( result.mode ==
'F' )
340 else if ( result.mode ==
'E' )
345 else if ( result.mode ==
'M' )
350 else if ( result.mode ==
'S' )
368 if ( result.navstatus ==
'S' )
372 else if ( result.navstatus ==
'C' )
376 else if ( result.navstatus ==
'U' )
389 if ( nmea_parse_GPGSV( data, len, &result ) )
392 for (
int i = 0; i < NMEA_SATINPACK; ++i )
394 const nmeaSATELLITE currentSatellite = result.sat_data[i];
396 satelliteInfo.
azimuth = currentSatellite.azimuth;
397 satelliteInfo.
elevation = currentSatellite.elv;
398 satelliteInfo.
id = currentSatellite.id;
399 satelliteInfo.
inUse =
false;
404 satelliteInfo.
inUse =
true;
407 satelliteInfo.
signal = currentSatellite.sig;
408 satelliteInfo.
satType = result.talkerId[1];
410 if ( result.talkerId[0] ==
'G' )
412 if ( result.talkerId[1] ==
'P' )
416 else if ( result.talkerId[1] ==
'L' )
420 else if ( result.talkerId[1] ==
'A' )
424 else if ( result.talkerId[1] ==
'B' )
428 else if ( result.talkerId[1] ==
'Q' )
434 if ( satelliteInfo.
satType ==
'P' && satelliteInfo.
id > 32 )
438 satelliteInfo.
id = currentSatellite.id + 87;
441 bool idAlreadyPresent =
false;
446 if ( existingSatInView.id == currentSatellite.id )
448 idAlreadyPresent =
true;
454 if ( !idAlreadyPresent && currentSatellite.azimuth > 0 && currentSatellite.elv > 0 )
466 if ( nmea_parse_GPVTG( data, len, &result ) )
469 if ( !std::isnan( result.dir ) )
485 if ( nmea_parse_GPGSA( data, len, &result ) )
495 bool mixedConstellation =
false;
496 for (
int i = 0; i < NMEA_MAXSAT; i++ )
498 if ( result.sat_prn[ i ] > 0 )
504 if ( ( result.talkerId[0] ==
'G' && result.talkerId[1] ==
'L' ) || result.sat_prn[i] > 64 )
506 else if ( result.sat_prn[i] >= 1 && result.sat_prn[i] <= 32 )
508 else if ( result.sat_prn[i] > 32 && result.sat_prn[i] <= 64 )
512 if ( result.sat_prn[i] > 0 )
514 if ( mixedConstellation
516 && commonConstellation != constellation ) )
518 mixedConstellation =
true;
522 commonConstellation = constellation;
527 if ( mixedConstellation )
530 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)