7

In QGIS I would like to establish a connection to my GPS device programmatically (not via button in the GPS control panel)

I found out how to get available ports with QgsGPSDetector i.e.

QgsGPSDetector.availablePorts()
[(u'localhost:2947:', u'Lokaler GPSD'), (u'\\\\.\\COM4', 'COM4:')]

and how to get information via QgsGPSConnection to a connected device (cp. Accessing GPS from QGIS 2.14.1/Python 2.7 /Windows10).

Via

connectionRegistry = QgsGPSConnectionRegistry().instance()
connectionList = connectionRegistry.connectionList()

i can do

connectionList[0].close() rp. .connect()

But if no device is initially connected via the GPS control panel, the .connectionList() return nothing, so I'm not shure how to connect / disconnect to a GPS device.

ADD/EDIT

tried

con = QgsGpsdConnection('localhost',2947,'local gpsd')
# or con = QgsGpsdConnection('localhost',2947,''), rsp.
con.connect()
>>> True
con.status()
>>> 1
cr = QgsGPSConnectionRegistry().instance()
cr.connectionList()
>>> []

I'd like to try this 'COM4', but the port parameter is an integer.

Connecting via GPS Information panel does not work with gpsd, only Autodetect and serial (COM4:):

enter image description here

Do I have to construct a QgsGPSConnection that i can

.connect()

and

.close()

?

Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117
  • Thanks for your post a few hours after mine. Your post and it's links solved my problems. I'm sorry I can't help with your question. – Phil Apr 05 '16 at 07:30
  • Please take the [Tour] to learn about our focussed Q&A format. As it currently reads your answer does not provide a standalone solution to the question asked. The idea is not to have future readers following links to try and synthesize an answer. – PolyGeo Apr 05 '16 at 07:48

1 Answers1

4

As indicated in qgis api documentation (https://qgis.org/api/classQgsNMEAConnection.html) the third parameter of the constructor is the device. It's the string you put in the "Device" field of the GPS pane. In the case you have only one device on your gpsd, you can keep it empty.

So, you should try:

d = QgsGPSDetector("COM4")
def _connected(c):
  global con
  con = c
d.detected.connect(_connected)
d.advance()

(sorry for the ugly signal interception, there is no method exposed to get the connection object)

If you are using GPSD, update the first line to:

d = QgsGPSDetector("localhost:2947:")

But when you call con.status() you should get 3.

Here is the values table:

  • Connected = 1
  • DataReceived = 2
  • GPSDataReceived = 3
  • NotConnected = 0

If the value is 3, you can then query values!

Here is a sample:

>>> d = QgsGPSDetector("COM4")
>>> def _connected(c):
...   global con
...   con = c
>>> d.detected.connect(_connected)
>>> d.advance()
>>> con.status()
3
>>> con.currentGPSInformation().latitude
55.67122833333333
>>> con.currentGPSInformation().longitude
12.521531666666666

Here is the field list: https://qgis.org/api/structQgsGPSInformation.html

Elektordi
  • 401
  • 2
  • 6
  • This seems not to work with my device (cp. my answer to http://gis.stackexchange.com/questions/187050/qgis-live-gps-tracking-recommended-hardware-gps-usb-stick). I only get con.status() >>> 1 when connecting before with con.connect(), I never get con.status() >>> 3. In GPS Information panel gpsd does not connect to my device, only 'Autodetect' or 'serial'. I edit my question, rsp. – Jochen Schwarze Jan 25 '17 at 10:47
  • You don't need to use con.connect(), it's automatic when you create the object. But, could you indicate the parameters you use in the GPS pane to connect to your device? – Elektordi Jan 26 '17 at 18:40
  • Oops, just saw your screen capture now, GPSD is only for remote GPS devices or for some Linux GPS! In your case, could you try the following code? con = QgsNMEAConnection(QSerialPort('COM4')) – Elektordi Jan 26 '17 at 18:48
  • just updated your answer with this hint, i think the bounty is yours :-) +1 – Jochen Schwarze Jan 28 '17 at 09:29
  • But before I try them all, in what module is QSerialPort? Is it in Qt 4.8, I was not able to import it... – Jochen Schwarze Jan 28 '17 at 17:00
  • I'm on Linux and it was working out of the box when I tested it (with "/dev/ttyS0" instead of "COM4"), but dependencies can be a bit harder on Windows... :(

    QSerialPort is in module "serialport" in qt4, inside qt core in qt5.

    I dug a bit further in qgis source, they are using "QextSerialPort" but I cannot find any acceptable documentation for it.

    Take a look here: https://qgis.org/api/qgsgpsdetector_8cpp_source.html line 167

    But this object is not exported to Python :(

    So I found a way to "intercept" the internal qgis detector, I'll update the answer in few minutes!

    – Elektordi Jan 28 '17 at 23:44
  • Answer updated, it's working on my Linux box.

    But it seems Windows sometimes calls serial ports with strange prefixes. If my code does not work on your side, try to replace "COM4" with "\\.\COM4".

    – Elektordi Jan 28 '17 at 23:56