-
Notifications
You must be signed in to change notification settings - Fork 60
Make compatible with pyserial, add some examples as well for computer usage #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92e1b89
Convert to only use bytestrings, and a debug flag
ladyada a53559b
add echo test (no fix needed)
ladyada 72abd3d
reasonable formatting on lat/long, convert all strings to bytes, add …
ladyada ee5c403
the feather examples is pretty involved, its easier to just make a ne…
ladyada de1888a
pyserial required on computers!
ladyada 78c06fc
encoding str works in both Cpython and circuitpython. fixed echotest
ladyada 1a38520
other encode
ladyada c62b31b
pylint fixes
ladyada 2754d0e
change encoding to "ascii"
ladyada 567f65c
that was a test to see if travis would catch it ;)
ladyada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ class GPS: | |
"""GPS parsing module. Can parse simple NMEA data sentences from serial GPS | ||
modules to read latitude, longitude, and more. | ||
""" | ||
def __init__(self, uart): | ||
def __init__(self, uart, debug=False): | ||
self._uart = uart | ||
# Initialize null starting values for GPS attributes. | ||
self.timestamp_utc = None | ||
|
@@ -90,6 +90,7 @@ def __init__(self, uart): | |
self.velocity_knots = None | ||
self.speed_knots = None | ||
self.track_angle_deg = None | ||
self.debug = debug | ||
|
||
def update(self): | ||
"""Check for updated data from the GPS module and process it | ||
|
@@ -101,11 +102,13 @@ def update(self): | |
sentence = self._parse_sentence() | ||
if sentence is None: | ||
return False | ||
if self.debug: | ||
print(sentence) | ||
data_type, args = sentence | ||
data_type = data_type.upper() | ||
if data_type == 'GPGGA': # GGA, 3d location fix | ||
data_type = bytes(data_type.upper(), "utf-8") | ||
if data_type == b'GPGGA': # GGA, 3d location fix | ||
self._parse_gpgga(args) | ||
elif data_type == 'GPRMC': # RMC, minimum location info | ||
elif data_type == b'GPRMC': # RMC, minimum location info | ||
self._parse_gprmc(args) | ||
return True | ||
|
||
|
@@ -115,15 +118,15 @@ def send_command(self, command, add_checksum=True): | |
Note you should NOT add the leading $ and trailing * to the command | ||
as they will automatically be added! | ||
""" | ||
self._uart.write('$') | ||
self._uart.write(b'$') | ||
self._uart.write(command) | ||
if add_checksum: | ||
checksum = 0 | ||
for char in command: | ||
checksum ^= ord(char) | ||
self._uart.write('*') | ||
self._uart.write('{:02x}'.format(checksum).upper()) | ||
self._uart.write('\r\n') | ||
checksum ^= char | ||
self._uart.write(b'*') | ||
self._uart.write(bytes('{:02x}'.format(checksum).upper(), "utf-8")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yah im into it. lemme do that |
||
self._uart.write(b'\r\n') | ||
|
||
@property | ||
def has_fix(self): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Simple GPS datalogging demonstration for use with a computer like Linux/desktop. | ||
# This actually doesn't even use the GPS library and instead just reads raw | ||
# NMEA sentences from the GPS unit and dumps them to a file. | ||
|
||
import serial # pyserial is required | ||
|
||
# Path to the file to log GPS data. By default this will be appended to | ||
# which means new lines are added at the end and all old data is kept. | ||
# Change this path to point at the filename desired | ||
LOG_FILE = 'gps.txt' # Example for writing to local file gps.txt | ||
|
||
# File more for opening the log file. Mode 'ab' means append or add new lines | ||
# to the end of the file rather than erasing it and starting over. If you'd | ||
# like to erase the file and start clean each time use the value 'wb' instead. | ||
LOG_MODE = 'ab' | ||
|
||
# Create a serial connection for the GPS connection using default speed and | ||
# a slightly higher timeout (GPS modules typically update once a second). | ||
# Update the serial port name to match the serial connection for the GPS! | ||
uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3000) | ||
|
||
# Main loop just reads data from the GPS module and writes it back out to | ||
# the output file while also printing to serial output. | ||
with open(LOG_FILE, LOG_MODE) as outfile: | ||
while True: | ||
sentence = uart.readline() | ||
print(str(sentence, 'ascii').strip()) | ||
outfile.write(sentence) | ||
outfile.flush() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Simple GPS module demonstration. | ||
# Will print NMEA sentences received from the GPS, great for testing connection | ||
# Uses the GPS only to send some commands, then reads directly from UART | ||
import time | ||
import board | ||
import busio | ||
|
||
import adafruit_gps | ||
|
||
|
||
# Define RX and TX pins for the board's serial port connected to the GPS. | ||
# These are the defaults you should use for the GPS FeatherWing. | ||
# For other boards set RX = GPS module TX, and TX = GPS module RX pins. | ||
RX = board.RX | ||
TX = board.TX | ||
|
||
# Create a serial connection for the GPS connection using default speed and | ||
# a slightly higher timeout (GPS modules typically update once a second). | ||
uart = busio.UART(TX, RX, baudrate=9600, timeout=3000) | ||
|
||
# for a computer, use the pyserial library for uart access | ||
#import serial | ||
#uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3000) | ||
|
||
# Create a GPS module instance. | ||
gps = adafruit_gps.GPS(uart) | ||
|
||
# Initialize the GPS module by changing what data it sends and at what rate. | ||
# These are NMEA extensions for PMTK_314_SET_NMEA_OUTPUT and | ||
# PMTK_220_SET_NMEA_UPDATERATE but you can send anything from here to adjust | ||
# the GPS module behavior: | ||
# https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf | ||
|
||
# Turn on the basic GGA and RMC info (what you typically want) | ||
gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') | ||
# Turn on just minimum info (RMC only, location): | ||
#gps.send_command(b'PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') | ||
# Turn off everything: | ||
#gps.send_command(b'PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') | ||
# Tuen on everything (not all of it is parsed!) | ||
#gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0') | ||
|
||
# Set update rate to once a second (1hz) which is what you typically want. | ||
gps.send_command(b'PMTK220,1000') | ||
# Or decrease to once every two seconds by doubling the millisecond value. | ||
# Be sure to also increase your UART timeout above! | ||
#gps.send_command(b'PMTK220,2000') | ||
# You can also speed up the rate, but don't go too fast or else you can lose | ||
# data during parsing. This would be twice a second (2hz, 500ms delay): | ||
#gps.send_command(b'PMTK220,500') | ||
|
||
# Main loop runs forever printing data as it comes in | ||
timestamp = time.monotonic() | ||
while True: | ||
data = uart.read(32) # read up to 32 bytes | ||
# print(data) # this is a bytearray type | ||
|
||
if data is not None: | ||
# convert bytearray to string | ||
data_string = ''.join([chr(b) for b in data]) | ||
print(data_string, end="") | ||
|
||
if time.monotonic() - timestamp > 5: | ||
# every 5 seconds... | ||
gps.send_command(b'PMTK605') # request firmware version | ||
timestamp = time.monotonic() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
pyserial |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using
"ascii"
here and below as the encoding? In CircuitPython this arg is ignored (you can say"foobar"
), but in regular Python it has to be an encoding name. If pyserial is reading junk with chars >128, then it will throw an error: