GPS 10Hz with serial connection without using gpsd. The code is as follows, and write the code to process the received data in the main part at the bottom.
mtk3339_set_10Hz.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from time import sleep
import serial
#Serial connection baudrate initial value:9600
ser=serial.Serial('/dev/ttyS0', baudrate=9600, timeout=30)
class GPS:
def __init__(self):
UPDATE_100msec= r"$PMTK220,100*2F\r\n" #Update interval 100msec(10Hz)
UPDATE_200msec= r"$PMTK220,200*2C\r\n" #Update interval 200msec(5Hz)
UPDATE_250msec= r"$PMTK220,250*29\r\n" #Update interval 250msec(4Hz)
UPDATE_500msec= r"$PMTK220,500*2B\r\n" #Update interval 500msec(2Hz)
UPDATE_1000msec= r"$PMTK220,1000*1F\r\n" #Update interval 1sec(1Hz)
MEAS_100msec= r"$PMTK300,100,0,0,0,0*2C\r\n" #Measurement interval 100msec(10Hz)
MEAS_200msec= r"$PMTK300,200,0,0,0,0*2F\r\n" #Measurement interval 200msec(5Hz)
MEAS_250msec= r"$PMTK300,250,0,0,0,0*2A\r\n" #Measurement interval 250 msec(4Hz)
MEAS_500msec= r"$PMTK300,500,0,0,0,0*28\r\n" #Measurement interval 500msec(2Hz)
MEAS_1000msec= r"$PMTK300,1000,0,0,0,0*1C\r\n" #Measurement interval 1 sec(1Hz)
BAUDRATE_9600 = r"$PMTK251,9600*17\r\n" #tty baud rate 9600bps
BAUDRATE_19200 = r"$PMTK251,19200*22\r\n" #tty baud rate 19200bps
BAUDRATE_38400 = r"$PMTK251,38400*27\r\n" #tty baud rate 38400bps
BAUDRATE_57600 = r"$PMTK251,57600*2C\r\n" #tty baud rate 57600bps
BAUDRATE_115200 = r"$PMTK251,115200*1F\r\n" #tty baud rate 115200bps
#Commands for which NMEA Sentences are sent
GPRMC_ONLY= "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" # Send only the GPRMC Sentence
GPRMC_GPGGA="$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" # Send GPRMC AND GPGGA Sentences
SEND_ALL ="$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" # Send All Sentences
SEND_NOTHING="$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" # Send Nothing
ser.write(BAUDRATE_115200.encode('utf-8')) # Set Baudrate to 115200
sleep(1)
ser.baudrate=115200
ser.write(UPDATE_100msec.encode('utf-8')) # Set update interval 100ms
sleep(1)
ser.write(MEAS_100msec.encode('utf-8')) # Set measurement interval 100ms
sleep(1)
ser.write(GPRMC_ONLY.encode('utf-8')) # Set report GPRMC message only
sleep(1)
ser.flushInput()
ser.flushOutput()
print("GPS is Initialized\n")
gps=GPS() #GPS is set when the instance is initialized
while(1):
while ser.inWaiting()==0:
pass
data=ser.readline()
#Write the processing of the acquired data here
print(data)
Why are the update and measurement settings separate? I think it's meaningless if it doesn't work together.
Recommended Posts