[QGIS Commit] r12759 - in trunk/qgis/src: app app/gps core core/gps core/gps/qextserialport plugins/gps_importer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Jan 14 13:13:36 EST 2010


Author: jef
Date: 2010-01-14 13:13:35 -0500 (Thu, 14 Jan 2010)
New Revision: 12759

Added:
   trunk/qgis/src/core/gps/qextserialport/qwineventnotifier.h
   trunk/qgis/src/core/gps/qgsgpsdetector.cpp
   trunk/qgis/src/core/gps/qgsgpsdetector.h
Modified:
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/app/gps/qgsgpsinformationwidget.cpp
   trunk/qgis/src/app/gps/qgsgpsinformationwidget.h
   trunk/qgis/src/core/CMakeLists.txt
   trunk/qgis/src/core/gps/qextserialport/posix_qextserialport.cpp
   trunk/qgis/src/core/gps/qextserialport/qextserialport.h
   trunk/qgis/src/core/gps/qextserialport/win_qextserialport.cpp
   trunk/qgis/src/core/gps/qgsgpsconnection.cpp
   trunk/qgis/src/core/gps/qgsgpsconnection.h
   trunk/qgis/src/core/gps/qgsgpstrackerthread.cpp
   trunk/qgis/src/core/gps/qgsgpstrackerthread.h
   trunk/qgis/src/core/gps/qgsnmeaconnection.cpp
   trunk/qgis/src/core/gps/qgsnmeaconnection.h
   trunk/qgis/src/plugins/gps_importer/CMakeLists.txt
   trunk/qgis/src/plugins/gps_importer/qgsgpsplugingui.cpp
Log:
improve gps detection

Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/app/CMakeLists.txt	2010-01-14 18:13:35 UTC (rev 12759)
@@ -303,7 +303,7 @@
   ${GEOS_INCLUDE_DIR}
   ${GDAL_INCLUDE_DIR}
   ../core
-  ../core/gps
+  ../core/gps ../core/gps/qextserialport
   ../core/composer ../core/raster ../core/renderer ../core/symbology ../core/symbology-ng
   ../gui ../gui/symbology-ng
   ../plugins

Modified: trunk/qgis/src/app/gps/qgsgpsinformationwidget.cpp
===================================================================
--- trunk/qgis/src/app/gps/qgsgpsinformationwidget.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/app/gps/qgsgpsinformationwidget.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -18,7 +18,7 @@
 #include "qgsgpsinformationwidget.h"
 #include "qgsvectorlayer.h"
 #include "qgsnmeaconnection.h"
-#include "qgsgpstrackerthread.h"
+#include "qgsgpsdetector.h"
 #include "qgscoordinatetransform.h"
 #include <qgspoint.h>
 #include <qgsrubberband.h>
@@ -58,9 +58,7 @@
 
 QgsGPSInformationWidget::QgsGPSInformationWidget( QgsMapCanvas * thepCanvas, QWidget * parent, Qt::WindowFlags f ):
     QWidget( parent, f ),
-    mSerialPort( 0 ),
     mNmea( 0 ),
-    mThread( 0 ) ,
     mpCanvas( thepCanvas )
 {
   setupUi( this );
@@ -293,69 +291,62 @@
 {
   if ( theFlag )
   {
-    mConnectButton->setText( tr( "Connecting..." ) );
     connectGps();
-    mConnectButton->setText( tr( "Disconnect" ) );
   }
   else
   {
     disconnectGps();
-    mConnectButton->setText( tr( "Connect" ) );
   }
 }
 
 void QgsGPSInformationWidget::connectGps()
 {
+  QString port;
+
   if ( mRadUserPath->isChecked() )
   {
-    if ( !mCboDevices->itemData( mCboDevices->currentIndex() ).toString().isEmpty() )
+    port = mCboDevices->itemData( mCboDevices->currentIndex() ).toString();
+
+    if ( port.isEmpty() )
     {
-      mNmea = new QgsNMEAConnection( mCboDevices->itemData( mCboDevices->currentIndex() ).toString(), 500 );
-      QObject::connect( mNmea, SIGNAL( stateChanged( const QgsGPSInformation& ) ),
-                        this, SLOT( displayGPSInformation( const QgsGPSInformation& ) ) );
-      mThread = new QgsGPSTrackerThread( mNmea );
-      mThread->start();
-      mGPSTextEdit->append( tr( "Connecting on %1" ).arg( mCboDevices->itemData( mCboDevices->currentIndex() ).toString() ) );
-    }
-    else
-    {
       QMessageBox::information( this, tr( "/gps" ), tr( "No path to the GPS port "
                                 "is specified. Please enter a path then try again." ) );
       //toggle the button back off
       mConnectButton->setChecked( false );
-    }
-  }
-  else //autodetect
-  {
-    mNmea = QgsGPSConnection::detectGPSConnection();
-    if ( !mNmea )
-    {
-      mConnectButton->setChecked( false );
       return;
     }
-    mNmea->setPollInterval( 1000 );
-    QObject::connect( mNmea, SIGNAL( stateChanged( const QgsGPSInformation& ) ), this, SLOT( displayGPSInformation( const QgsGPSInformation& ) ) );
-
-    mThread = new QgsGPSTrackerThread( mNmea );
-    mThread->start();
-    mGPSTextEdit->append( tr( "Connected..." ) );
   }
+
+  mGPSTextEdit->append( tr( "Connecting..." ) );
+
+  QgsGPSDetector *detector = new QgsGPSDetector( port );
+  connect( detector, SIGNAL( detected( QgsGPSConnection * ) ), this, SLOT( connected( QgsGPSConnection * ) ) );
+  connect( detector, SIGNAL( detectionFailed() ), this, SLOT( timedout() ) );
 }
+
+void QgsGPSInformationWidget::timedout()
+{
+  mConnectButton->setChecked( false );
+  mNmea = NULL;
+  mGPSTextEdit->append( tr( "Timed out!" ) );
+}
+
+void QgsGPSInformationWidget::connected( QgsGPSConnection *conn )
+{
+  mNmea = conn;
+  QObject::connect( mNmea, SIGNAL( stateChanged( const QgsGPSInformation& ) ),
+                    this, SLOT( displayGPSInformation( const QgsGPSInformation& ) ) );
+  mGPSTextEdit->append( tr( "Connected!" ) );
+  mConnectButton->setText( tr( "Disconnect" ) );
+}
+
 void QgsGPSInformationWidget::disconnectGps()
 {
-  if ( mThread )
-  {
-    mThread->quit();
-    mThread->wait();
-    delete mThread;
-    mThread = 0;
-    mNmea = 0;
-    mSerialPort = 0;
-    mGPSTextEdit->append( tr( "Disconnected..." ) );
-  }
-  //mGPSTextEdit->clear();
-  //toggle the button back on
+  delete mNmea;
+
+  mGPSTextEdit->append( tr( "Disconnected..." ) );
   mConnectButton->setChecked( false );
+  mConnectButton->setText( tr( "Connect" ) );
 }
 
 
@@ -370,6 +361,7 @@
   {
     delete mMarkerList.takeFirst();
   }
+
   for ( int i = 0; i < info.satellitesInView.size(); ++i )
   {
     QgsSatelliteInfo currentInfo = info.satellitesInView.at( i );
@@ -858,7 +850,7 @@
 /* Copied from gps plugin */
 void QgsGPSInformationWidget::populateDevices()
 {
-  QList< QPair<QString, QString> > ports = QgsGPSConnection::availablePorts();
+  QList< QPair<QString, QString> > ports = QgsGPSDetector::availablePorts();
 
   mCboDevices->clear();
   for ( int i = 0; i < ports.size(); i++ )

Modified: trunk/qgis/src/app/gps/qgsgpsinformationwidget.h
===================================================================
--- trunk/qgis/src/app/gps/qgsgpsinformationwidget.h	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/app/gps/qgsgpsinformationwidget.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -58,15 +58,17 @@
     void on_mBtnCloseFeature_clicked( );
     void on_mBtnResetFeature_clicked( );
     void on_mCbxAutoAddVertices_toggled( bool theFlag );
+
+    void connected( QgsGPSConnection * );
+    void timedout();
+
   private:
     void addVertex( );
     void connectGps();
     void connectGpsSlot( );
     void disconnectGps();
     void populateDevices();
-    QextSerialPort* mSerialPort;
     QgsGPSConnection* mNmea;
-    QgsGPSTrackerThread* mThread;
     QgsMapCanvas * mpCanvas;
     QgsGpsMarker * mpMapMarker;
     QwtPlot * mpPlot;

Modified: trunk/qgis/src/core/CMakeLists.txt
===================================================================
--- trunk/qgis/src/core/CMakeLists.txt	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/CMakeLists.txt	2010-01-14 18:13:35 UTC (rev 12759)
@@ -10,8 +10,8 @@
 
   gps/qgsgpsconnection.cpp
   gps/qgsgpsconnectionregistry.cpp
-  gps/qgsgpstrackerthread.cpp
   gps/qgsnmeaconnection.cpp
+  gps/qgsgpsdetector.cpp
   gps/parse.c
   gps/sentence.c
   gps/info.c
@@ -241,6 +241,7 @@
   raster/qgsrasterlayer.h
 
   gps/qgsgpsconnection.h
+  gps/qgsgpsdetector.h
   gps/qgsnmeaconnection.h
   gps/qextserialport/qextserialport.h
   gps/qextserialport/qextserialenumerator.h

Modified: trunk/qgis/src/core/gps/qextserialport/posix_qextserialport.cpp
===================================================================
--- trunk/qgis/src/core/gps/qextserialport/posix_qextserialport.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qextserialport/posix_qextserialport.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -956,3 +956,7 @@
 
     return (qint64)retVal;
 }
+
+void QextSerialPort::onWinEvent( HANDLE h )
+{
+}

Modified: trunk/qgis/src/core/gps/qextserialport/qextserialport.h
===================================================================
--- trunk/qgis/src/core/gps/qextserialport/qextserialport.h	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qextserialport/qextserialport.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -129,46 +129,12 @@
 #include <sys/ioctl.h>
 #include <sys/select.h>
 #include <QSocketNotifier>
-#elif (defined Q_OS_WIN)
+typedef int HANDLE; // unused
+typedef 
+#elif defined (Q_OS_WIN)
 #include <windows.h>
 #include <QThread>
 #include <QReadWriteLock>
-
-// Ugly: copied private Qt header file
-QT_BEGIN_NAMESPACE
-
-class Q_CORE_EXPORT QWinEventNotifier : public QObject
-{
-    Q_OBJECT
-    Q_DECLARE_PRIVATE(QObject)
-
-public:
-    explicit QWinEventNotifier(QObject *parent = 0);
-    explicit QWinEventNotifier(HANDLE hEvent, QObject *parent = 0);
-    ~QWinEventNotifier();
-
-    void setHandle(HANDLE hEvent);
-    HANDLE handle() const;
-
-    bool isEnabled() const;
-
-public Q_SLOTS:
-    void setEnabled(bool enable);
-
-Q_SIGNALS:
-    void activated(HANDLE hEvent);
-
-protected:
-    bool event(QEvent *e);
-
-private:
-    Q_DISABLE_COPY(QWinEventNotifier)
-
-    HANDLE handleToEvent;
-    bool enabled;
-};
-
-QT_END_NAMESPACE
 #endif
 
 /*!
@@ -212,6 +178,9 @@
 
 \author Stefan Sander, Michal Policht, Brandon Fosdick, Liam Staskawicz
 */
+
+class QWinEventNotifier;
+
 class QextSerialPort: public QIODevice
 {
     Q_OBJECT
@@ -335,10 +304,8 @@
         qint64 readData(char * data, qint64 maxSize);
         qint64 writeData(const char * data, qint64 maxSize);
 
-#ifdef Q_OS_WIN
     private slots:
         void onWinEvent(HANDLE h);
-#endif
 
     private:
         Q_DISABLE_COPY(QextSerialPort)

Added: trunk/qgis/src/core/gps/qextserialport/qwineventnotifier.h
===================================================================
--- trunk/qgis/src/core/gps/qextserialport/qwineventnotifier.h	                        (rev 0)
+++ trunk/qgis/src/core/gps/qextserialport/qwineventnotifier.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -0,0 +1,43 @@
+#ifndef QWINEVENTNOTIFIER_H
+#define QWINEVENTNOTIFIER_H
+#include <QObject>
+
+#include <windows.h>
+
+// Ugly: copied private Qt header file
+QT_BEGIN_NAMESPACE
+
+class Q_CORE_EXPORT QWinEventNotifier : public QObject
+{
+    Q_OBJECT
+    Q_DECLARE_PRIVATE(QObject)
+
+public:
+    explicit QWinEventNotifier(QObject *parent = 0);
+    explicit QWinEventNotifier(HANDLE hEvent, QObject *parent = 0);
+    ~QWinEventNotifier();
+
+    void setHandle(HANDLE hEvent);
+    HANDLE handle() const;
+
+    bool isEnabled() const;
+
+public Q_SLOTS:
+    void setEnabled(bool enable);
+
+Q_SIGNALS:
+    void activated(HANDLE hEvent);
+
+protected:
+    bool event(QEvent *e);
+
+private:
+    Q_DISABLE_COPY(QWinEventNotifier)
+
+    HANDLE handleToEvent;
+    bool enabled;
+};
+
+QT_END_NAMESPACE
+
+#endif // QWINEVENTNOTIFIER_H


Property changes on: trunk/qgis/src/core/gps/qextserialport/qwineventnotifier.h
___________________________________________________________________
Added: svn:executable
   + *

Modified: trunk/qgis/src/core/gps/qextserialport/win_qextserialport.cpp
===================================================================
--- trunk/qgis/src/core/gps/qextserialport/win_qextserialport.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qextserialport/win_qextserialport.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -1,5 +1,5 @@
+#include "qwineventnotifier.h"
 
-
 #include <QMutexLocker>
 #include <QDebug>
 #include <QRegExp>

Modified: trunk/qgis/src/core/gps/qgsgpsconnection.cpp
===================================================================
--- trunk/qgis/src/core/gps/qgsgpsconnection.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qgsgpsconnection.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -27,56 +27,19 @@
 #include "qextserialenumerator.h"
 
 #include "qgsnmeaconnection.h"
+#include "qgslogger.h"
 
-QgsGPSConnection::QgsGPSConnection( QIODevice* dev, int pollInterval ): QObject( 0 ), mSource( dev ), mStatus( NotConnected )
+QgsGPSConnection::QgsGPSConnection( QIODevice* dev ): QObject( 0 ), mSource( dev ), mStatus( NotConnected )
 {
-  init( pollInterval );
-}
-
-QgsGPSConnection::QgsGPSConnection( QString port, int pollInterval ): QObject( 0 ), mStatus( NotConnected )
-{
-  QextSerialPort *s = new QextSerialPort( port );
-  s->setBaudRate( BAUD4800 );
-  s->setFlowControl( FLOW_OFF );
-  s->setParity( PAR_NONE );
-  s->setDataBits( DATA_8 );
-  s->setStopBits( STOP_2 );
-  mSource = s;
-  init( pollInterval );
-}
-
-void QgsGPSConnection::init( int pollInterval )
-{
   clearLastGPSInformation();
-  mPollTimer = new QTimer();
-  mPollTimer->setInterval( pollInterval );
-  QObject::connect( mPollTimer, SIGNAL( timeout() ), this, SLOT( parseData() ) );
+  QObject::connect( dev, SIGNAL( readyRead() ), this, SLOT( parseData() ) );
 }
 
 QgsGPSConnection::~QgsGPSConnection()
 {
   cleanupSource();
-  delete mPollTimer;
 }
 
-bool QgsGPSConnection::startPolling()
-{
-  if ( mPollTimer )
-  {
-    mPollTimer->start();
-  }
-  return true;
-}
-
-bool QgsGPSConnection::stopPolling()
-{
-  if ( mPollTimer )
-  {
-    mPollTimer->stop();
-  }
-  return true;
-}
-
 bool QgsGPSConnection::connect()
 {
   if ( !mSource )
@@ -132,121 +95,3 @@
   mLastGPSInformation.speed = 0;
   mLastGPSInformation.vdop = 0;
 }
-
-void QgsGPSConnection::setTimer( QTimer* t )
-{
-  delete mPollTimer;
-  mPollTimer = t;
-  QObject::connect( mPollTimer, SIGNAL( timeout() ), this, SLOT( parseData() ) );
-}
-
-
-
-QgsGPSConnection* QgsGPSConnection::detectGPSConnection()
-{
-  QList<BaudRateType> baudRatesToTry;
-  baudRatesToTry << BAUD4800 << BAUD9600 << BAUD38400;
-
-  QextSerialPort* port = 0;
-
-  QList<BaudRateType>::const_iterator baudIt = baudRatesToTry.constBegin();
-  for ( ; baudIt != baudRatesToTry.constEnd(); ++baudIt )
-  {
-    QList< QPair<QString, QString> > ports = availablePorts();
-
-    for ( int i = 0; i < ports.size(); i++ )
-    {
-      port = new QextSerialPort( ports[i].first );
-      port->setBaudRate( *baudIt );
-      port->setFlowControl( FLOW_OFF );
-      port->setParity( PAR_NONE );
-      port->setDataBits( DATA_8 );
-      port->setStopBits( STOP_1 );
-      if ( !port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) )
-      {
-        delete port;
-        continue;
-      }
-
-      //setup connection
-      QgsNMEAConnection* c = new QgsNMEAConnection( port, 200 );
-
-      //return connection if gps data has been received
-      c->startPolling();
-
-      QTime t = QTime::currentTime().addSecs( 4 );
-      while ( QTime::currentTime() < t )
-      {
-        QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents, 1000 );
-      }
-      c->stopPolling();
-
-      if ( c->status() != GPSDataReceived )
-      {
-        delete c;
-        continue;
-      }
-
-      return c;
-    }
-  }
-
-  return 0;
-}
-
-QList< QPair<QString, QString> > QgsGPSConnection::availablePorts()
-{
-  QList< QPair<QString, QString> > devs;
-
-#ifdef linux
-  // look for linux serial devices
-  foreach( QString linuxDev, QStringList() << "/dev/ttyS%1" << "/dev/ttyUSB%1" << "/dev/rfcomm%1" )
-  {
-    for ( int i = 0; i < 10; ++i )
-    {
-      if ( QFileInfo( linuxDev.arg( i ) ).exists() )
-      {
-        devs << QPair<QString, QString>( linuxDev.arg( i ), linuxDev.arg( i ) );
-      }
-    }
-  }
-#endif
-
-#ifdef __FreeBSD__ // freebsd
-  // and freebsd devices (untested)
-  foreach( QString freebsdDev, QStringList() << "/dev/cuaa%1" << "/dev/ucom%1" )
-  {
-    for ( int i = 0; i < 10; ++i )
-    {
-      if ( QFileInfo( freebsdDev.arg( i ) ).exists() )
-      {
-        devs << QPair<QString, QString>( freebsdDev.arg( i ), freebsdDev.arg( i ) );
-      }
-    }
-  }
-#endif
-
-#ifdef sparc
-  // and solaris devices (also untested)
-  QString solarisDev( "/dev/cua/%1" );
-  for ( char i = 'a'; i < 'k'; ++i )
-  {
-    if ( QFileInfo( solarisDev.arg( i ) ).exists() )
-    {
-      devs << QPair<QString, QString>( solarisDev.arg( i ), solarisDev.arg( i ) );
-    }
-  }
-#endif
-
-#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
-  QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
-  foreach( QextPortInfo port, ports )
-  {
-    devs << QPair<QString, QString>( port.portName, port.friendName );
-  }
-#endif
-
-  // OpenBSD, NetBSD etc? Anyone?
-
-  return devs;
-}

Modified: trunk/qgis/src/core/gps/qgsgpsconnection.h
===================================================================
--- trunk/qgis/src/core/gps/qgsgpsconnection.h	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qgsgpsconnection.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -19,8 +19,6 @@
 #define QGSGPSCONNECTION_H
 
 #include <QObject>
-#include <QTimer>
-#include <QPair>
 
 class QIODevice;
 
@@ -63,25 +61,15 @@
     /**Constructor
         @param dev input device for the connection (e.g. serial device). The class takes ownership of the object
         @param pollIntervall update intervall in milliseconds*/
-    QgsGPSConnection( QIODevice* dev, int pollInterval = 1000 );
-    QgsGPSConnection( QString port, int pollInterval = 1000 );
+    QgsGPSConnection( QIODevice* dev );
     virtual ~QgsGPSConnection();
     /**Opens connection to device*/
     bool connect();
     /**Closes connection to device*/
     bool close();
-    /**Starts polling and sending stateChanged signals*/
-    bool startPolling();
-    /**Stops polling*/
-    bool stopPolling();
-    /**Tries different interfaces and settings
-    @return true in case of success*/
-    static QgsGPSConnection* detectGPSConnection();
 
     /**Sets the GPS source. The class takes ownership of the device class*/
     void setSource( QIODevice* source );
-    void setPollInterval( int i ) { mPollTimer->setInterval( i ); }
-    int pollInterval() const { return mPollTimer->interval(); }
 
     /**Returns the status. Possible state are not connected, connected, data received*/
     Status status() const { return mStatus; }
@@ -89,20 +77,12 @@
     /**Returns the current gps information (lat, lon, etc.)*/
     QgsGPSInformation currentGPSInformation() const { return mLastGPSInformation; }
 
-    /**Sets a new timer object*/
-    const QTimer* timer() const { return mPollTimer; }
-    void setTimer( QTimer* t );
-
-    static QList< QPair<QString, QString> > availablePorts();
-
   signals:
     void stateChanged( const QgsGPSInformation& info );
 
   protected:
     /**Data source (e.g. serial device, socket, file,...)*/
     QIODevice* mSource;
-    /**Timer that triggers polling*/
-    QTimer* mPollTimer;
     /**Last state of the gps related variables (e.g. position, time, ...)*/
     QgsGPSInformation mLastGPSInformation;
     /**Connection status*/
@@ -112,7 +92,6 @@
     /**Closes and deletes mSource*/
     void cleanupSource();
     void clearLastGPSInformation();
-    void init( int pollInterval );
 
   protected slots:
     /**Parse available data source content*/

Added: trunk/qgis/src/core/gps/qgsgpsdetector.cpp
===================================================================
--- trunk/qgis/src/core/gps/qgsgpsdetector.cpp	                        (rev 0)
+++ trunk/qgis/src/core/gps/qgsgpsdetector.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -0,0 +1,179 @@
+/***************************************************************************
+                          qgsgpsdetector.cpp  -  description
+                          --------------------
+    begin                : January 13th, 2009
+    copyright            : (C) 2009 by Juergen E. Fischer
+    email                : jef at norbit dot de
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "qgsgpsdetector.h"
+#include "qextserialenumerator.h"
+#include "qgslogger.h"
+#include "qgsgpsconnection.h"
+#include "qgsnmeaconnection.h"
+
+#include <QStringList>
+#include <QFileInfo>
+#include <QTimer>
+
+QList< QPair<QString, QString> > QgsGPSDetector::availablePorts()
+{
+  QList< QPair<QString, QString> > devs;
+
+#ifdef linux
+  // look for linux serial devices
+  foreach( QString linuxDev, QStringList() << "/dev/ttyS%1" << "/dev/ttyUSB%1" << "/dev/rfcomm%1" )
+  {
+    for ( int i = 0; i < 10; ++i )
+    {
+      if ( QFileInfo( linuxDev.arg( i ) ).exists() )
+      {
+        devs << QPair<QString, QString>( linuxDev.arg( i ), linuxDev.arg( i ) );
+      }
+    }
+  }
+#endif
+
+#ifdef __FreeBSD__ // freebsd
+  // and freebsd devices (untested)
+  foreach( QString freebsdDev, QStringList() << "/dev/cuaa%1" << "/dev/ucom%1" )
+  {
+    for ( int i = 0; i < 10; ++i )
+    {
+      if ( QFileInfo( freebsdDev.arg( i ) ).exists() )
+      {
+        devs << QPair<QString, QString>( freebsdDev.arg( i ), freebsdDev.arg( i ) );
+      }
+    }
+  }
+#endif
+
+#ifdef sparc
+  // and solaris devices (also untested)
+  QString solarisDev( "/dev/cua/%1" );
+  for ( char i = 'a'; i < 'k'; ++i )
+  {
+    if ( QFileInfo( solarisDev.arg( i ) ).exists() )
+    {
+      devs << QPair<QString, QString>( solarisDev.arg( i ), solarisDev.arg( i ) );
+    }
+  }
+#endif
+
+#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
+  QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
+  foreach( QextPortInfo port, ports )
+  {
+    devs << QPair<QString, QString>( port.portName, port.friendName );
+  }
+#endif
+
+  // OpenBSD, NetBSD etc? Anyone?
+
+  return devs;
+}
+
+QgsGPSDetector::QgsGPSDetector( QString portName )
+{
+  mConn = 0;
+  mBaudList << BAUD4800 << BAUD9600 << BAUD38400;
+
+  if ( portName.isEmpty() )
+  {
+    mPortList = availablePorts();
+  }
+  else
+  {
+    mPortList << QPair<QString, QString>( portName, portName );
+  }
+
+  mPortIndex = 0;
+  mBaudIndex = -1;
+
+  advance();
+}
+
+QgsGPSDetector::~QgsGPSDetector()
+{
+  if ( mConn )
+    delete mConn;
+}
+
+void QgsGPSDetector::advance()
+{
+  if ( mConn )
+  {
+    delete mConn;
+  }
+
+  QextSerialPort *port = 0;
+
+  do
+  {
+    mBaudIndex++;
+    if ( mBaudIndex == mBaudList.size() )
+    {
+      mBaudIndex = 0;
+      mPortIndex++;
+    }
+
+    if ( mPortIndex == mPortList.size() )
+    {
+      emit detectionFailed();
+      deleteLater();
+      return;
+    }
+
+    if ( port )
+      delete port;
+
+    port = new QextSerialPort( mPortList[ mPortIndex ].first, QextSerialPort::EventDriven );
+    port->setBaudRate( mBaudList[ mBaudIndex ] );
+    port->setFlowControl( FLOW_OFF );
+    port->setParity( PAR_NONE );
+    port->setDataBits( DATA_8 );
+    port->setStopBits( STOP_1 );
+  }
+  while ( !port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) );
+
+  mConn = new QgsNMEAConnection( port );
+  connect( mConn, SIGNAL( stateChanged( const QgsGPSInformation & ) ), this, SLOT( detected( const QgsGPSInformation & ) ) );
+  connect( mConn, SIGNAL( destroyed( QObject * ) ), this, SLOT( connDestroyed( QObject * ) ) );
+
+  // leave 2s to pickup a valid string
+  QTimer::singleShot( 2000, this, SLOT( advance() ) );
+}
+
+void QgsGPSDetector::detected( const QgsGPSInformation& info )
+{
+  if ( !mConn )
+  {
+    // advance if connection was destroyed
+    advance();
+  }
+  else if ( mConn->status() == QgsGPSConnection::GPSDataReceived )
+  {
+    // signal detection
+    QgsGPSConnection *conn = mConn;
+    mConn = 0;
+    emit detected( conn );
+    deleteLater();
+  }
+}
+
+void QgsGPSDetector::connDestroyed( QObject *obj )
+{
+  if ( obj == mConn )
+  {
+    mConn = 0;
+  }
+}

Added: trunk/qgis/src/core/gps/qgsgpsdetector.h
===================================================================
--- trunk/qgis/src/core/gps/qgsgpsdetector.h	                        (rev 0)
+++ trunk/qgis/src/core/gps/qgsgpsdetector.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -0,0 +1,58 @@
+/***************************************************************************
+                          qgsgpsdetector.h  -  description
+                          -------------------
+    begin                : January 13th, 2009
+    copyright            : (C) 2009 by Juergen E. Fischer
+    email                : jef at norbit dot de
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef QGSGPSDETECTOR_H
+#define QGSGPSDETECTOR_H
+
+#include <QObject>
+#include <QList>
+#include <QPair>
+
+#include "qextserialport.h"
+
+class QgsGPSConnection;
+struct QgsGPSInformation;
+
+// Class to detect the GPS port
+class CORE_EXPORT QgsGPSDetector : public QObject
+{
+    Q_OBJECT
+  public:
+    QgsGPSDetector( QString portName );
+    ~QgsGPSDetector();
+
+    static QList< QPair<QString, QString> > availablePorts();
+
+  public slots:
+    void advance();
+    void detected( const QgsGPSInformation& );
+    void connDestroyed( QObject * );
+
+  signals:
+    void detected( QgsGPSConnection * );
+    void detectionFailed();
+
+  private:
+    int mPortIndex;
+    int mBaudIndex;
+    QList< QPair< QString, QString > > mPortList;
+    QList<BaudRateType> mBaudList;
+
+    QgsGPSConnection *mConn;
+};
+
+#endif // QGSGPSDETECTOR_H

Modified: trunk/qgis/src/core/gps/qgsgpstrackerthread.cpp
===================================================================
--- trunk/qgis/src/core/gps/qgsgpstrackerthread.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qgsgpstrackerthread.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -1,53 +0,0 @@
-#include "qgsgpstrackerthread.h"
-#include "qgsgpsconnection.h"
-
-QgsGPSTrackerThread::QgsGPSTrackerThread( QgsGPSConnection* conn ): mConnection( conn )
-{
-}
-
-QgsGPSTrackerThread::QgsGPSTrackerThread(): mConnection( 0 )
-{
-  cleanupConnection();
-}
-
-QgsGPSTrackerThread::~QgsGPSTrackerThread()
-{
-  delete mConnection;
-}
-
-void QgsGPSTrackerThread::run()
-{
-  if ( !mConnection )
-  {
-    return;
-  }
-
-  if ( !mConnection->connect() )
-  {
-    return;
-  }
-
-  //QTimer needs to be started in the same thread, so we create a new instance here
-  QTimer* t = new QTimer();
-  t->setInterval( mConnection->timer()->interval() );
-  mConnection->setTimer( t );
-
-  mConnection->startPolling();
-  exec();
-  mConnection->stopPolling();
-  mConnection->close();
-}
-
-void QgsGPSTrackerThread::cleanupConnection()
-{
-  delete mConnection;
-  mConnection = 0;
-}
-
-void QgsGPSTrackerThread::setConnection( QgsGPSConnection* c )
-{
-  cleanupConnection();
-  mConnection = c;
-}
-
-

Modified: trunk/qgis/src/core/gps/qgsgpstrackerthread.h
===================================================================
--- trunk/qgis/src/core/gps/qgsgpstrackerthread.h	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qgsgpstrackerthread.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -1,28 +0,0 @@
-#ifndef QGSGPSTRACKERTHREAD_H
-#define QGSGPSTRACKERTHREAD_H
-
-#include <QThread>
-
-class QgsGPSConnection;
-
-/**Queries GPS informations in a thread*/
-class CORE_EXPORT QgsGPSTrackerThread: public QThread
-{
-  public:
-    QgsGPSTrackerThread( QgsGPSConnection* conn );
-    ~QgsGPSTrackerThread();
-
-    void setConnection( QgsGPSConnection* c );
-    const QgsGPSConnection* connection() { return mConnection; }
-
-  protected:
-    void run();
-
-  private:
-    QgsGPSTrackerThread();
-    QgsGPSConnection* mConnection;
-
-    void cleanupConnection();
-};
-
-#endif // QGSGPSTRACKERTHREAD_H

Modified: trunk/qgis/src/core/gps/qgsnmeaconnection.cpp
===================================================================
--- trunk/qgis/src/core/gps/qgsnmeaconnection.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qgsnmeaconnection.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -30,14 +30,10 @@
 
 #define KNOTS_TO_KMH 1.852
 
-QgsNMEAConnection::QgsNMEAConnection( QIODevice* dev, int pollInterval ): QgsGPSConnection( dev, pollInterval )
+QgsNMEAConnection::QgsNMEAConnection( QIODevice* dev ): QgsGPSConnection( dev )
 {
 }
 
-QgsNMEAConnection::QgsNMEAConnection( QString port, int pollInterval ): QgsGPSConnection( port, pollInterval )
-{
-}
-
 QgsNMEAConnection::~QgsNMEAConnection()
 {
   //connection will be closed by base class
@@ -61,12 +57,9 @@
     numBytes = mSource->bytesAvailable();
   }
 
-  QgsDebugMsg( "numBytes" );
-  QgsDebugMsg( QString::number( numBytes ) );
+  QgsDebugMsg( "numBytes:" + QString::number( numBytes ) );
 
-
-
-  if ( numBytes > 0 )
+  if ( numBytes >= 6 )
   {
     if ( mStatus != GPSDataReceived )
     {
@@ -77,7 +70,6 @@
     mStringBuffer.append( mSource->read( numBytes ) );
     processStringBuffer();
     emit stateChanged( mLastGPSInformation );
-    QgsDebugMsg( mStringBuffer );
   }
 }
 

Modified: trunk/qgis/src/core/gps/qgsnmeaconnection.h
===================================================================
--- trunk/qgis/src/core/gps/qgsnmeaconnection.h	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/core/gps/qgsnmeaconnection.h	2010-01-14 18:13:35 UTC (rev 12759)
@@ -25,8 +25,7 @@
 {
     Q_OBJECT
   public:
-    QgsNMEAConnection( QIODevice* dev, int pollInterval = 1000 );
-    QgsNMEAConnection( QString port, int pollInterval = 1000 );
+    QgsNMEAConnection( QIODevice *dev );
     ~QgsNMEAConnection();
 
     //bool poll( QgsGPSInformation& info, int maxTime );

Modified: trunk/qgis/src/plugins/gps_importer/CMakeLists.txt
===================================================================
--- trunk/qgis/src/plugins/gps_importer/CMakeLists.txt	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/plugins/gps_importer/CMakeLists.txt	2010-01-14 18:13:35 UTC (rev 12759)
@@ -38,7 +38,8 @@
 INCLUDE_DIRECTORIES(
      ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_BINARY_DIR}
-     ../../core ../../core/gps
+     ../../core ../../core/gps ../../core/gps/qextserialport
+
      ../../gui
      ..
      ${EXPAT_INCLUDE_DIR}

Modified: trunk/qgis/src/plugins/gps_importer/qgsgpsplugingui.cpp
===================================================================
--- trunk/qgis/src/plugins/gps_importer/qgsgpsplugingui.cpp	2010-01-14 18:10:16 UTC (rev 12758)
+++ trunk/qgis/src/plugins/gps_importer/qgsgpsplugingui.cpp	2010-01-14 18:13:35 UTC (rev 12759)
@@ -15,7 +15,7 @@
 #include "qgsdataprovider.h"
 #include "qgscontexthelp.h"
 #include "qgslogger.h"
-#include "qgsgpsconnection.h"
+#include "qgsgpsdetector.h"
 
 //qt includes
 #include <QFileDialog>
@@ -306,7 +306,7 @@
 
 void QgsGPSPluginGui::populatePortComboBoxes()
 {
-  QList< QPair<QString, QString> > devs = QgsGPSConnection::availablePorts() << QPair<QString, QString>( "usb:", "usb:" );
+  QList< QPair<QString, QString> > devs = QgsGPSDetector::availablePorts() << QPair<QString, QString>( "usb:", "usb:" );
 
   cmbDLPort->clear();
   cmbULPort->clear();



More information about the QGIS-commit mailing list