Skip to content

Commit 3e184f7

Browse files
author
jef
committedJan 14, 2010
improve gps detection
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12759 c8812cc2-4d05-0410-92ff-de0c093fc19c

18 files changed

+345
-364
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ INCLUDE_DIRECTORIES(
303303
${GEOS_INCLUDE_DIR}
304304
${GDAL_INCLUDE_DIR}
305305
../core
306-
../core/gps
306+
../core/gps ../core/gps/qextserialport
307307
../core/composer ../core/raster ../core/renderer ../core/symbology ../core/symbology-ng
308308
../gui ../gui/symbology-ng
309309
../plugins

‎src/app/gps/qgsgpsinformationwidget.cpp

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "qgsgpsinformationwidget.h"
1919
#include "qgsvectorlayer.h"
2020
#include "qgsnmeaconnection.h"
21-
#include "qgsgpstrackerthread.h"
21+
#include "qgsgpsdetector.h"
2222
#include "qgscoordinatetransform.h"
2323
#include <qgspoint.h>
2424
#include <qgsrubberband.h>
@@ -58,9 +58,7 @@
5858

5959
QgsGPSInformationWidget::QgsGPSInformationWidget( QgsMapCanvas * thepCanvas, QWidget * parent, Qt::WindowFlags f ):
6060
QWidget( parent, f ),
61-
mSerialPort( 0 ),
6261
mNmea( 0 ),
63-
mThread( 0 ) ,
6462
mpCanvas( thepCanvas )
6563
{
6664
setupUi( this );
@@ -293,69 +291,62 @@ void QgsGPSInformationWidget::on_mConnectButton_toggled( bool theFlag )
293291
{
294292
if ( theFlag )
295293
{
296-
mConnectButton->setText( tr( "Connecting..." ) );
297294
connectGps();
298-
mConnectButton->setText( tr( "Disconnect" ) );
299295
}
300296
else
301297
{
302298
disconnectGps();
303-
mConnectButton->setText( tr( "Connect" ) );
304299
}
305300
}
306301

307302
void QgsGPSInformationWidget::connectGps()
308303
{
304+
QString port;
305+
309306
if ( mRadUserPath->isChecked() )
310307
{
311-
if ( !mCboDevices->itemData( mCboDevices->currentIndex() ).toString().isEmpty() )
312-
{
313-
mNmea = new QgsNMEAConnection( mCboDevices->itemData( mCboDevices->currentIndex() ).toString(), 500 );
314-
QObject::connect( mNmea, SIGNAL( stateChanged( const QgsGPSInformation& ) ),
315-
this, SLOT( displayGPSInformation( const QgsGPSInformation& ) ) );
316-
mThread = new QgsGPSTrackerThread( mNmea );
317-
mThread->start();
318-
mGPSTextEdit->append( tr( "Connecting on %1" ).arg( mCboDevices->itemData( mCboDevices->currentIndex() ).toString() ) );
319-
}
320-
else
308+
port = mCboDevices->itemData( mCboDevices->currentIndex() ).toString();
309+
310+
if ( port.isEmpty() )
321311
{
322312
QMessageBox::information( this, tr( "/gps" ), tr( "No path to the GPS port "
323313
"is specified. Please enter a path then try again." ) );
324314
//toggle the button back off
325315
mConnectButton->setChecked( false );
326-
}
327-
}
328-
else //autodetect
329-
{
330-
mNmea = QgsGPSConnection::detectGPSConnection();
331-
if ( !mNmea )
332-
{
333-
mConnectButton->setChecked( false );
334316
return;
335317
}
336-
mNmea->setPollInterval( 1000 );
337-
QObject::connect( mNmea, SIGNAL( stateChanged( const QgsGPSInformation& ) ), this, SLOT( displayGPSInformation( const QgsGPSInformation& ) ) );
338-
339-
mThread = new QgsGPSTrackerThread( mNmea );
340-
mThread->start();
341-
mGPSTextEdit->append( tr( "Connected..." ) );
342318
}
319+
320+
mGPSTextEdit->append( tr( "Connecting..." ) );
321+
322+
QgsGPSDetector *detector = new QgsGPSDetector( port );
323+
connect( detector, SIGNAL( detected( QgsGPSConnection * ) ), this, SLOT( connected( QgsGPSConnection * ) ) );
324+
connect( detector, SIGNAL( detectionFailed() ), this, SLOT( timedout() ) );
325+
}
326+
327+
void QgsGPSInformationWidget::timedout()
328+
{
329+
mConnectButton->setChecked( false );
330+
mNmea = NULL;
331+
mGPSTextEdit->append( tr( "Timed out!" ) );
343332
}
333+
334+
void QgsGPSInformationWidget::connected( QgsGPSConnection *conn )
335+
{
336+
mNmea = conn;
337+
QObject::connect( mNmea, SIGNAL( stateChanged( const QgsGPSInformation& ) ),
338+
this, SLOT( displayGPSInformation( const QgsGPSInformation& ) ) );
339+
mGPSTextEdit->append( tr( "Connected!" ) );
340+
mConnectButton->setText( tr( "Disconnect" ) );
341+
}
342+
344343
void QgsGPSInformationWidget::disconnectGps()
345344
{
346-
if ( mThread )
347-
{
348-
mThread->quit();
349-
mThread->wait();
350-
delete mThread;
351-
mThread = 0;
352-
mNmea = 0;
353-
mSerialPort = 0;
354-
mGPSTextEdit->append( tr( "Disconnected..." ) );
355-
}
356-
//mGPSTextEdit->clear();
357-
//toggle the button back on
345+
delete mNmea;
346+
347+
mGPSTextEdit->append( tr( "Disconnected..." ) );
358348
mConnectButton->setChecked( false );
349+
mConnectButton->setText( tr( "Connect" ) );
359350
}
360351

361352

@@ -370,6 +361,7 @@ void QgsGPSInformationWidget::displayGPSInformation( const QgsGPSInformation& in
370361
{
371362
delete mMarkerList.takeFirst();
372363
}
364+
373365
for ( int i = 0; i < info.satellitesInView.size(); ++i )
374366
{
375367
QgsSatelliteInfo currentInfo = info.satellitesInView.at( i );
@@ -858,7 +850,7 @@ void QgsGPSInformationWidget::on_mBtnRefreshDevices_clicked( )
858850
/* Copied from gps plugin */
859851
void QgsGPSInformationWidget::populateDevices()
860852
{
861-
QList< QPair<QString, QString> > ports = QgsGPSConnection::availablePorts();
853+
QList< QPair<QString, QString> > ports = QgsGPSDetector::availablePorts();
862854

863855
mCboDevices->clear();
864856
for ( int i = 0; i < ports.size(); i++ )

‎src/app/gps/qgsgpsinformationwidget.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ class QgsGPSInformationWidget: public QWidget, private Ui::QgsGPSInformationWidg
5858
void on_mBtnCloseFeature_clicked( );
5959
void on_mBtnResetFeature_clicked( );
6060
void on_mCbxAutoAddVertices_toggled( bool theFlag );
61+
62+
void connected( QgsGPSConnection * );
63+
void timedout();
64+
6165
private:
6266
void addVertex( );
6367
void connectGps();
6468
void connectGpsSlot( );
6569
void disconnectGps();
6670
void populateDevices();
67-
QextSerialPort* mSerialPort;
6871
QgsGPSConnection* mNmea;
69-
QgsGPSTrackerThread* mThread;
7072
QgsMapCanvas * mpCanvas;
7173
QgsGpsMarker * mpMapMarker;
7274
QwtPlot * mpPlot;

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ SET(QGIS_CORE_SRCS
1010

1111
gps/qgsgpsconnection.cpp
1212
gps/qgsgpsconnectionregistry.cpp
13-
gps/qgsgpstrackerthread.cpp
1413
gps/qgsnmeaconnection.cpp
14+
gps/qgsgpsdetector.cpp
1515
gps/parse.c
1616
gps/sentence.c
1717
gps/info.c
@@ -241,6 +241,7 @@ SET(QGIS_CORE_MOC_HDRS
241241
raster/qgsrasterlayer.h
242242

243243
gps/qgsgpsconnection.h
244+
gps/qgsgpsdetector.h
244245
gps/qgsnmeaconnection.h
245246
gps/qextserialport/qextserialport.h
246247
gps/qextserialport/qextserialenumerator.h

‎src/core/gps/qextserialport/posix_qextserialport.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,3 +956,7 @@ qint64 QextSerialPort::writeData(const char * data, qint64 maxSize)
956956

957957
return (qint64)retVal;
958958
}
959+
960+
void QextSerialPort::onWinEvent( HANDLE h )
961+
{
962+
}

‎src/core/gps/qextserialport/qextserialport.h

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -129,46 +129,12 @@ struct PortSettings
129129
#include <sys/ioctl.h>
130130
#include <sys/select.h>
131131
#include <QSocketNotifier>
132-
#elif (defined Q_OS_WIN)
132+
typedef int HANDLE; // unused
133+
typedef
134+
#elif defined (Q_OS_WIN)
133135
#include <windows.h>
134136
#include <QThread>
135137
#include <QReadWriteLock>
136-
137-
// Ugly: copied private Qt header file
138-
QT_BEGIN_NAMESPACE
139-
140-
class Q_CORE_EXPORT QWinEventNotifier : public QObject
141-
{
142-
Q_OBJECT
143-
Q_DECLARE_PRIVATE(QObject)
144-
145-
public:
146-
explicit QWinEventNotifier(QObject *parent = 0);
147-
explicit QWinEventNotifier(HANDLE hEvent, QObject *parent = 0);
148-
~QWinEventNotifier();
149-
150-
void setHandle(HANDLE hEvent);
151-
HANDLE handle() const;
152-
153-
bool isEnabled() const;
154-
155-
public Q_SLOTS:
156-
void setEnabled(bool enable);
157-
158-
Q_SIGNALS:
159-
void activated(HANDLE hEvent);
160-
161-
protected:
162-
bool event(QEvent *e);
163-
164-
private:
165-
Q_DISABLE_COPY(QWinEventNotifier)
166-
167-
HANDLE handleToEvent;
168-
bool enabled;
169-
};
170-
171-
QT_END_NAMESPACE
172138
#endif
173139

174140
/*!
@@ -212,6 +178,9 @@ No guarantees are made as to the quality of POSIX support under NT/2000 however.
212178
213179
\author Stefan Sander, Michal Policht, Brandon Fosdick, Liam Staskawicz
214180
*/
181+
182+
class QWinEventNotifier;
183+
215184
class QextSerialPort: public QIODevice
216185
{
217186
Q_OBJECT
@@ -335,10 +304,8 @@ class QextSerialPort: public QIODevice
335304
qint64 readData(char * data, qint64 maxSize);
336305
qint64 writeData(const char * data, qint64 maxSize);
337306

338-
#ifdef Q_OS_WIN
339307
private slots:
340308
void onWinEvent(HANDLE h);
341-
#endif
342309

343310
private:
344311
Q_DISABLE_COPY(QextSerialPort)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef QWINEVENTNOTIFIER_H
2+
#define QWINEVENTNOTIFIER_H
3+
#include <QObject>
4+
5+
#include <windows.h>
6+
7+
// Ugly: copied private Qt header file
8+
QT_BEGIN_NAMESPACE
9+
10+
class Q_CORE_EXPORT QWinEventNotifier : public QObject
11+
{
12+
Q_OBJECT
13+
Q_DECLARE_PRIVATE(QObject)
14+
15+
public:
16+
explicit QWinEventNotifier(QObject *parent = 0);
17+
explicit QWinEventNotifier(HANDLE hEvent, QObject *parent = 0);
18+
~QWinEventNotifier();
19+
20+
void setHandle(HANDLE hEvent);
21+
HANDLE handle() const;
22+
23+
bool isEnabled() const;
24+
25+
public Q_SLOTS:
26+
void setEnabled(bool enable);
27+
28+
Q_SIGNALS:
29+
void activated(HANDLE hEvent);
30+
31+
protected:
32+
bool event(QEvent *e);
33+
34+
private:
35+
Q_DISABLE_COPY(QWinEventNotifier)
36+
37+
HANDLE handleToEvent;
38+
bool enabled;
39+
};
40+
41+
QT_END_NAMESPACE
42+
43+
#endif // QWINEVENTNOTIFIER_H

‎src/core/gps/qextserialport/win_qextserialport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
#include "qwineventnotifier.h"
22

33
#include <QMutexLocker>
44
#include <QDebug>

‎src/core/gps/qgsgpsconnection.cpp

Lines changed: 3 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -27,54 +27,17 @@
2727
#include "qextserialenumerator.h"
2828

2929
#include "qgsnmeaconnection.h"
30+
#include "qgslogger.h"
3031

31-
QgsGPSConnection::QgsGPSConnection( QIODevice* dev, int pollInterval ): QObject( 0 ), mSource( dev ), mStatus( NotConnected )
32-
{
33-
init( pollInterval );
34-
}
35-
36-
QgsGPSConnection::QgsGPSConnection( QString port, int pollInterval ): QObject( 0 ), mStatus( NotConnected )
37-
{
38-
QextSerialPort *s = new QextSerialPort( port );
39-
s->setBaudRate( BAUD4800 );
40-
s->setFlowControl( FLOW_OFF );
41-
s->setParity( PAR_NONE );
42-
s->setDataBits( DATA_8 );
43-
s->setStopBits( STOP_2 );
44-
mSource = s;
45-
init( pollInterval );
46-
}
47-
48-
void QgsGPSConnection::init( int pollInterval )
32+
QgsGPSConnection::QgsGPSConnection( QIODevice* dev ): QObject( 0 ), mSource( dev ), mStatus( NotConnected )
4933
{
5034
clearLastGPSInformation();
51-
mPollTimer = new QTimer();
52-
mPollTimer->setInterval( pollInterval );
53-
QObject::connect( mPollTimer, SIGNAL( timeout() ), this, SLOT( parseData() ) );
35+
QObject::connect( dev, SIGNAL( readyRead() ), this, SLOT( parseData() ) );
5436
}
5537

5638
QgsGPSConnection::~QgsGPSConnection()
5739
{
5840
cleanupSource();
59-
delete mPollTimer;
60-
}
61-
62-
bool QgsGPSConnection::startPolling()
63-
{
64-
if ( mPollTimer )
65-
{
66-
mPollTimer->start();
67-
}
68-
return true;
69-
}
70-
71-
bool QgsGPSConnection::stopPolling()
72-
{
73-
if ( mPollTimer )
74-
{
75-
mPollTimer->stop();
76-
}
77-
return true;
7841
}
7942

8043
bool QgsGPSConnection::connect()
@@ -132,121 +95,3 @@ void QgsGPSConnection::clearLastGPSInformation()
13295
mLastGPSInformation.speed = 0;
13396
mLastGPSInformation.vdop = 0;
13497
}
135-
136-
void QgsGPSConnection::setTimer( QTimer* t )
137-
{
138-
delete mPollTimer;
139-
mPollTimer = t;
140-
QObject::connect( mPollTimer, SIGNAL( timeout() ), this, SLOT( parseData() ) );
141-
}
142-
143-
144-
145-
QgsGPSConnection* QgsGPSConnection::detectGPSConnection()
146-
{
147-
QList<BaudRateType> baudRatesToTry;
148-
baudRatesToTry << BAUD4800 << BAUD9600 << BAUD38400;
149-
150-
QextSerialPort* port = 0;
151-
152-
QList<BaudRateType>::const_iterator baudIt = baudRatesToTry.constBegin();
153-
for ( ; baudIt != baudRatesToTry.constEnd(); ++baudIt )
154-
{
155-
QList< QPair<QString, QString> > ports = availablePorts();
156-
157-
for ( int i = 0; i < ports.size(); i++ )
158-
{
159-
port = new QextSerialPort( ports[i].first );
160-
port->setBaudRate( *baudIt );
161-
port->setFlowControl( FLOW_OFF );
162-
port->setParity( PAR_NONE );
163-
port->setDataBits( DATA_8 );
164-
port->setStopBits( STOP_1 );
165-
if ( !port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) )
166-
{
167-
delete port;
168-
continue;
169-
}
170-
171-
//setup connection
172-
QgsNMEAConnection* c = new QgsNMEAConnection( port, 200 );
173-
174-
//return connection if gps data has been received
175-
c->startPolling();
176-
177-
QTime t = QTime::currentTime().addSecs( 4 );
178-
while ( QTime::currentTime() < t )
179-
{
180-
QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents, 1000 );
181-
}
182-
c->stopPolling();
183-
184-
if ( c->status() != GPSDataReceived )
185-
{
186-
delete c;
187-
continue;
188-
}
189-
190-
return c;
191-
}
192-
}
193-
194-
return 0;
195-
}
196-
197-
QList< QPair<QString, QString> > QgsGPSConnection::availablePorts()
198-
{
199-
QList< QPair<QString, QString> > devs;
200-
201-
#ifdef linux
202-
// look for linux serial devices
203-
foreach( QString linuxDev, QStringList() << "/dev/ttyS%1" << "/dev/ttyUSB%1" << "/dev/rfcomm%1" )
204-
{
205-
for ( int i = 0; i < 10; ++i )
206-
{
207-
if ( QFileInfo( linuxDev.arg( i ) ).exists() )
208-
{
209-
devs << QPair<QString, QString>( linuxDev.arg( i ), linuxDev.arg( i ) );
210-
}
211-
}
212-
}
213-
#endif
214-
215-
#ifdef __FreeBSD__ // freebsd
216-
// and freebsd devices (untested)
217-
foreach( QString freebsdDev, QStringList() << "/dev/cuaa%1" << "/dev/ucom%1" )
218-
{
219-
for ( int i = 0; i < 10; ++i )
220-
{
221-
if ( QFileInfo( freebsdDev.arg( i ) ).exists() )
222-
{
223-
devs << QPair<QString, QString>( freebsdDev.arg( i ), freebsdDev.arg( i ) );
224-
}
225-
}
226-
}
227-
#endif
228-
229-
#ifdef sparc
230-
// and solaris devices (also untested)
231-
QString solarisDev( "/dev/cua/%1" );
232-
for ( char i = 'a'; i < 'k'; ++i )
233-
{
234-
if ( QFileInfo( solarisDev.arg( i ) ).exists() )
235-
{
236-
devs << QPair<QString, QString>( solarisDev.arg( i ), solarisDev.arg( i ) );
237-
}
238-
}
239-
#endif
240-
241-
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
242-
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
243-
foreach( QextPortInfo port, ports )
244-
{
245-
devs << QPair<QString, QString>( port.portName, port.friendName );
246-
}
247-
#endif
248-
249-
// OpenBSD, NetBSD etc? Anyone?
250-
251-
return devs;
252-
}

‎src/core/gps/qgsgpsconnection.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#define QGSGPSCONNECTION_H
2020

2121
#include <QObject>
22-
#include <QTimer>
23-
#include <QPair>
2422

2523
class QIODevice;
2624

@@ -63,46 +61,28 @@ class CORE_EXPORT QgsGPSConnection : public QObject
6361
/**Constructor
6462
@param dev input device for the connection (e.g. serial device). The class takes ownership of the object
6563
@param pollIntervall update intervall in milliseconds*/
66-
QgsGPSConnection( QIODevice* dev, int pollInterval = 1000 );
67-
QgsGPSConnection( QString port, int pollInterval = 1000 );
64+
QgsGPSConnection( QIODevice* dev );
6865
virtual ~QgsGPSConnection();
6966
/**Opens connection to device*/
7067
bool connect();
7168
/**Closes connection to device*/
7269
bool close();
73-
/**Starts polling and sending stateChanged signals*/
74-
bool startPolling();
75-
/**Stops polling*/
76-
bool stopPolling();
77-
/**Tries different interfaces and settings
78-
@return true in case of success*/
79-
static QgsGPSConnection* detectGPSConnection();
8070

8171
/**Sets the GPS source. The class takes ownership of the device class*/
8272
void setSource( QIODevice* source );
83-
void setPollInterval( int i ) { mPollTimer->setInterval( i ); }
84-
int pollInterval() const { return mPollTimer->interval(); }
8573

8674
/**Returns the status. Possible state are not connected, connected, data received*/
8775
Status status() const { return mStatus; }
8876

8977
/**Returns the current gps information (lat, lon, etc.)*/
9078
QgsGPSInformation currentGPSInformation() const { return mLastGPSInformation; }
9179

92-
/**Sets a new timer object*/
93-
const QTimer* timer() const { return mPollTimer; }
94-
void setTimer( QTimer* t );
95-
96-
static QList< QPair<QString, QString> > availablePorts();
97-
9880
signals:
9981
void stateChanged( const QgsGPSInformation& info );
10082

10183
protected:
10284
/**Data source (e.g. serial device, socket, file,...)*/
10385
QIODevice* mSource;
104-
/**Timer that triggers polling*/
105-
QTimer* mPollTimer;
10686
/**Last state of the gps related variables (e.g. position, time, ...)*/
10787
QgsGPSInformation mLastGPSInformation;
10888
/**Connection status*/
@@ -112,7 +92,6 @@ class CORE_EXPORT QgsGPSConnection : public QObject
11292
/**Closes and deletes mSource*/
11393
void cleanupSource();
11494
void clearLastGPSInformation();
115-
void init( int pollInterval );
11695

11796
protected slots:
11897
/**Parse available data source content*/

‎src/core/gps/qgsgpsdetector.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/***************************************************************************
2+
qgsgpsdetector.cpp - description
3+
--------------------
4+
begin : January 13th, 2009
5+
copyright : (C) 2009 by Juergen E. Fischer
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 "qgsgpsdetector.h"
19+
#include "qextserialenumerator.h"
20+
#include "qgslogger.h"
21+
#include "qgsgpsconnection.h"
22+
#include "qgsnmeaconnection.h"
23+
24+
#include <QStringList>
25+
#include <QFileInfo>
26+
#include <QTimer>
27+
28+
QList< QPair<QString, QString> > QgsGPSDetector::availablePorts()
29+
{
30+
QList< QPair<QString, QString> > devs;
31+
32+
#ifdef linux
33+
// look for linux serial devices
34+
foreach( QString linuxDev, QStringList() << "/dev/ttyS%1" << "/dev/ttyUSB%1" << "/dev/rfcomm%1" )
35+
{
36+
for ( int i = 0; i < 10; ++i )
37+
{
38+
if ( QFileInfo( linuxDev.arg( i ) ).exists() )
39+
{
40+
devs << QPair<QString, QString>( linuxDev.arg( i ), linuxDev.arg( i ) );
41+
}
42+
}
43+
}
44+
#endif
45+
46+
#ifdef __FreeBSD__ // freebsd
47+
// and freebsd devices (untested)
48+
foreach( QString freebsdDev, QStringList() << "/dev/cuaa%1" << "/dev/ucom%1" )
49+
{
50+
for ( int i = 0; i < 10; ++i )
51+
{
52+
if ( QFileInfo( freebsdDev.arg( i ) ).exists() )
53+
{
54+
devs << QPair<QString, QString>( freebsdDev.arg( i ), freebsdDev.arg( i ) );
55+
}
56+
}
57+
}
58+
#endif
59+
60+
#ifdef sparc
61+
// and solaris devices (also untested)
62+
QString solarisDev( "/dev/cua/%1" );
63+
for ( char i = 'a'; i < 'k'; ++i )
64+
{
65+
if ( QFileInfo( solarisDev.arg( i ) ).exists() )
66+
{
67+
devs << QPair<QString, QString>( solarisDev.arg( i ), solarisDev.arg( i ) );
68+
}
69+
}
70+
#endif
71+
72+
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
73+
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
74+
foreach( QextPortInfo port, ports )
75+
{
76+
devs << QPair<QString, QString>( port.portName, port.friendName );
77+
}
78+
#endif
79+
80+
// OpenBSD, NetBSD etc? Anyone?
81+
82+
return devs;
83+
}
84+
85+
QgsGPSDetector::QgsGPSDetector( QString portName )
86+
{
87+
mConn = 0;
88+
mBaudList << BAUD4800 << BAUD9600 << BAUD38400;
89+
90+
if ( portName.isEmpty() )
91+
{
92+
mPortList = availablePorts();
93+
}
94+
else
95+
{
96+
mPortList << QPair<QString, QString>( portName, portName );
97+
}
98+
99+
mPortIndex = 0;
100+
mBaudIndex = -1;
101+
102+
advance();
103+
}
104+
105+
QgsGPSDetector::~QgsGPSDetector()
106+
{
107+
if ( mConn )
108+
delete mConn;
109+
}
110+
111+
void QgsGPSDetector::advance()
112+
{
113+
if ( mConn )
114+
{
115+
delete mConn;
116+
}
117+
118+
QextSerialPort *port = 0;
119+
120+
do
121+
{
122+
mBaudIndex++;
123+
if ( mBaudIndex == mBaudList.size() )
124+
{
125+
mBaudIndex = 0;
126+
mPortIndex++;
127+
}
128+
129+
if ( mPortIndex == mPortList.size() )
130+
{
131+
emit detectionFailed();
132+
deleteLater();
133+
return;
134+
}
135+
136+
if ( port )
137+
delete port;
138+
139+
port = new QextSerialPort( mPortList[ mPortIndex ].first, QextSerialPort::EventDriven );
140+
port->setBaudRate( mBaudList[ mBaudIndex ] );
141+
port->setFlowControl( FLOW_OFF );
142+
port->setParity( PAR_NONE );
143+
port->setDataBits( DATA_8 );
144+
port->setStopBits( STOP_1 );
145+
}
146+
while ( !port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) );
147+
148+
mConn = new QgsNMEAConnection( port );
149+
connect( mConn, SIGNAL( stateChanged( const QgsGPSInformation & ) ), this, SLOT( detected( const QgsGPSInformation & ) ) );
150+
connect( mConn, SIGNAL( destroyed( QObject * ) ), this, SLOT( connDestroyed( QObject * ) ) );
151+
152+
// leave 2s to pickup a valid string
153+
QTimer::singleShot( 2000, this, SLOT( advance() ) );
154+
}
155+
156+
void QgsGPSDetector::detected( const QgsGPSInformation& info )
157+
{
158+
if ( !mConn )
159+
{
160+
// advance if connection was destroyed
161+
advance();
162+
}
163+
else if ( mConn->status() == QgsGPSConnection::GPSDataReceived )
164+
{
165+
// signal detection
166+
QgsGPSConnection *conn = mConn;
167+
mConn = 0;
168+
emit detected( conn );
169+
deleteLater();
170+
}
171+
}
172+
173+
void QgsGPSDetector::connDestroyed( QObject *obj )
174+
{
175+
if ( obj == mConn )
176+
{
177+
mConn = 0;
178+
}
179+
}

‎src/core/gps/qgsgpsdetector.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/***************************************************************************
2+
qgsgpsdetector.h - description
3+
-------------------
4+
begin : January 13th, 2009
5+
copyright : (C) 2009 by Juergen E. Fischer
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+
#ifndef QGSGPSDETECTOR_H
19+
#define QGSGPSDETECTOR_H
20+
21+
#include <QObject>
22+
#include <QList>
23+
#include <QPair>
24+
25+
#include "qextserialport.h"
26+
27+
class QgsGPSConnection;
28+
struct QgsGPSInformation;
29+
30+
// Class to detect the GPS port
31+
class CORE_EXPORT QgsGPSDetector : public QObject
32+
{
33+
Q_OBJECT
34+
public:
35+
QgsGPSDetector( QString portName );
36+
~QgsGPSDetector();
37+
38+
static QList< QPair<QString, QString> > availablePorts();
39+
40+
public slots:
41+
void advance();
42+
void detected( const QgsGPSInformation& );
43+
void connDestroyed( QObject * );
44+
45+
signals:
46+
void detected( QgsGPSConnection * );
47+
void detectionFailed();
48+
49+
private:
50+
int mPortIndex;
51+
int mBaudIndex;
52+
QList< QPair< QString, QString > > mPortList;
53+
QList<BaudRateType> mBaudList;
54+
55+
QgsGPSConnection *mConn;
56+
};
57+
58+
#endif // QGSGPSDETECTOR_H

‎src/core/gps/qgsgpstrackerthread.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +0,0 @@
1-
#include "qgsgpstrackerthread.h"
2-
#include "qgsgpsconnection.h"
3-
4-
QgsGPSTrackerThread::QgsGPSTrackerThread( QgsGPSConnection* conn ): mConnection( conn )
5-
{
6-
}
7-
8-
QgsGPSTrackerThread::QgsGPSTrackerThread(): mConnection( 0 )
9-
{
10-
cleanupConnection();
11-
}
12-
13-
QgsGPSTrackerThread::~QgsGPSTrackerThread()
14-
{
15-
delete mConnection;
16-
}
17-
18-
void QgsGPSTrackerThread::run()
19-
{
20-
if ( !mConnection )
21-
{
22-
return;
23-
}
24-
25-
if ( !mConnection->connect() )
26-
{
27-
return;
28-
}
29-
30-
//QTimer needs to be started in the same thread, so we create a new instance here
31-
QTimer* t = new QTimer();
32-
t->setInterval( mConnection->timer()->interval() );
33-
mConnection->setTimer( t );
34-
35-
mConnection->startPolling();
36-
exec();
37-
mConnection->stopPolling();
38-
mConnection->close();
39-
}
40-
41-
void QgsGPSTrackerThread::cleanupConnection()
42-
{
43-
delete mConnection;
44-
mConnection = 0;
45-
}
46-
47-
void QgsGPSTrackerThread::setConnection( QgsGPSConnection* c )
48-
{
49-
cleanupConnection();
50-
mConnection = c;
51-
}
52-
53-

‎src/core/gps/qgsgpstrackerthread.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +0,0 @@
1-
#ifndef QGSGPSTRACKERTHREAD_H
2-
#define QGSGPSTRACKERTHREAD_H
3-
4-
#include <QThread>
5-
6-
class QgsGPSConnection;
7-
8-
/**Queries GPS informations in a thread*/
9-
class CORE_EXPORT QgsGPSTrackerThread: public QThread
10-
{
11-
public:
12-
QgsGPSTrackerThread( QgsGPSConnection* conn );
13-
~QgsGPSTrackerThread();
14-
15-
void setConnection( QgsGPSConnection* c );
16-
const QgsGPSConnection* connection() { return mConnection; }
17-
18-
protected:
19-
void run();
20-
21-
private:
22-
QgsGPSTrackerThread();
23-
QgsGPSConnection* mConnection;
24-
25-
void cleanupConnection();
26-
};
27-
28-
#endif // QGSGPSTRACKERTHREAD_H

‎src/core/gps/qgsnmeaconnection.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030

3131
#define KNOTS_TO_KMH 1.852
3232

33-
QgsNMEAConnection::QgsNMEAConnection( QIODevice* dev, int pollInterval ): QgsGPSConnection( dev, pollInterval )
34-
{
35-
}
36-
37-
QgsNMEAConnection::QgsNMEAConnection( QString port, int pollInterval ): QgsGPSConnection( port, pollInterval )
33+
QgsNMEAConnection::QgsNMEAConnection( QIODevice* dev ): QgsGPSConnection( dev )
3834
{
3935
}
4036

@@ -61,12 +57,9 @@ void QgsNMEAConnection::parseData()
6157
numBytes = mSource->bytesAvailable();
6258
}
6359

64-
QgsDebugMsg( "numBytes" );
65-
QgsDebugMsg( QString::number( numBytes ) );
66-
67-
60+
QgsDebugMsg( "numBytes:" + QString::number( numBytes ) );
6861

69-
if ( numBytes > 0 )
62+
if ( numBytes >= 6 )
7063
{
7164
if ( mStatus != GPSDataReceived )
7265
{
@@ -77,7 +70,6 @@ void QgsNMEAConnection::parseData()
7770
mStringBuffer.append( mSource->read( numBytes ) );
7871
processStringBuffer();
7972
emit stateChanged( mLastGPSInformation );
80-
QgsDebugMsg( mStringBuffer );
8173
}
8274
}
8375

‎src/core/gps/qgsnmeaconnection.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class CORE_EXPORT QgsNMEAConnection: public QgsGPSConnection
2525
{
2626
Q_OBJECT
2727
public:
28-
QgsNMEAConnection( QIODevice* dev, int pollInterval = 1000 );
29-
QgsNMEAConnection( QString port, int pollInterval = 1000 );
28+
QgsNMEAConnection( QIODevice *dev );
3029
~QgsNMEAConnection();
3130

3231
//bool poll( QgsGPSInformation& info, int maxTime );

‎src/plugins/gps_importer/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ ADD_LIBRARY (gpsimporterplugin MODULE ${GPS_SRCS} ${GPS_MOC_SRCS} ${GPS_RCC_SRCS
3838
INCLUDE_DIRECTORIES(
3939
${CMAKE_CURRENT_SOURCE_DIR}
4040
${CMAKE_CURRENT_BINARY_DIR}
41-
../../core ../../core/gps
41+
../../core ../../core/gps ../../core/gps/qextserialport
42+
4243
../../gui
4344
..
4445
${EXPAT_INCLUDE_DIR}

‎src/plugins/gps_importer/qgsgpsplugingui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "qgsdataprovider.h"
1616
#include "qgscontexthelp.h"
1717
#include "qgslogger.h"
18-
#include "qgsgpsconnection.h"
18+
#include "qgsgpsdetector.h"
1919

2020
//qt includes
2121
#include <QFileDialog>
@@ -306,7 +306,7 @@ void QgsGPSPluginGui::on_pbnRefresh_clicked()
306306

307307
void QgsGPSPluginGui::populatePortComboBoxes()
308308
{
309-
QList< QPair<QString, QString> > devs = QgsGPSConnection::availablePorts() << QPair<QString, QString>( "usb:", "usb:" );
309+
QList< QPair<QString, QString> > devs = QgsGPSDetector::availablePorts() << QPair<QString, QString>( "usb:", "usb:" );
310310

311311
cmbDLPort->clear();
312312
cmbULPort->clear();

0 commit comments

Comments
 (0)
Please sign in to comment.