Sending command to GPS device using gpsd python library

780 Views Asked by At

I use the gpsd python library in order to read and parse the NMEA string recieved from the gps device. I would like to send some command to gps in order to fine tune the measurement rate, report rate and so on.

It is possible using the gpsd library or I must send the command with in other way?

1

There are 1 best solutions below

0
O.Shevchenko On

According to 'gpsd' manual:

       To send a binary control string to a specified device, write to the
       control socket a '&', followed by the device name, followed by '=',
       followed by the control string in paired hex digits.

So if you have gpsd service running as gpsd -F /var/run/gpsd.sock you can use the following code to send commands to the gps device:


import socket
import sys
# Create a socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Connect the socket to the port where the GPSD is listening
gpsd_address = '/var/run/gpsd.sock'
sock.connect(gpsd_address)

# @BSSL 0x01<CR><LF> - Set NMEA Output Sentence (GGA only)
# cmd = '@BSSL 0x01' + '\r\n'

# @RST<CR><LF> - RST - Reset 
cmd = '@RST' + '\r\n'

message = '&/dev/ttyUSB1='
cmd_hex = cmd.encode('utf-8').hex()
print ('cmd_hex {}'.format(cmd_hex))
# cmd_hex 405253540d0a
message += cmd_hex
bin_message = message.encode('utf-8')
print ("bin message {}".format(bin_message))
# bin message b'&/dev/ttyUSB1=405253540d0a'
sock.sendall(bin_message)

data = sock.recv(16)
print ('received {}'.format(data))
# received b'OK\n'
sock.close()

In my case I am sending @RST command followed by CR, LF symbols.