QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsgpsdconnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgpsdconnection.cpp - description
3  ---------------------
4  begin : October 4th, 2010
5  copyright : (C) 2010 by Jürgen E. Fischer, norBIT GmbH
6  email : jef at norbit dot de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsgpsdconnection.h"
19 #include "qgslogger.h"
20 
21 #include <QTcpSocket>
22 
23 QgsGpsdConnection::QgsGpsdConnection( const QString& host, qint16 port, const QString& device )
24  : QgsNMEAConnection( new QTcpSocket() )
25  , mDevice( device )
26 {
27  QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
28 
29  QObject::connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
30  QObject::connect( socket, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( error( QAbstractSocket::SocketError ) ) );
31  socket->connectToHost( host, port );
32 }
33 
35 {
36  //connection will be closed by base class
37 }
38 
39 void QgsGpsdConnection::connected()
40 {
41  QgsDebugMsg( "connected!" );
42  QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
43  socket->write( QString( "?WATCH={\"enable\":true,\"nmea\":true,\"raw\":true%1};" ).arg( mDevice.isEmpty() ? mDevice : QString( ",\"device\":%1" ).arg( mDevice ) ).toUtf8() );
44 }
45 
46 void QgsGpsdConnection::error( QAbstractSocket::SocketError socketError )
47 {
48 #ifdef QGISDEBUG
49  QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
50  QgsDebugMsg( QString( "error: %1 %2" ).arg( socketError ).arg( socket->errorString() ) );
51 #else
52  Q_UNUSED( socketError );
53 #endif
54 }
QString errorString() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void connectToHost(const QString &hostName, quint16 port, QFlags< QIODevice::OpenModeFlag > openMode)
bool isEmpty() const
Evaluates NMEA sentences coming from a GPS device.
QgsGpsdConnection(const QString &host, qint16 port, const QString &device)
QIODevice * mSource
Data source (e.g.
qint64 write(const char *data, qint64 maxSize)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const