gps: improve parsing and add tests to verify

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-01-16 22:07:08 +01:00
committed by Ron Evans
parent ecb343b3b3
commit f2637a87d2
3 changed files with 130 additions and 22 deletions
+14 -3
View File
@@ -42,6 +42,12 @@ func (ge GPSError) Unwrap() error {
return ge.Err
}
const (
minimumNMEALength = 7
startingDelimiter = '$'
checksumDelimiter = '*'
)
// Device wraps a connection to a GPS device.
type Device struct {
buffer []byte
@@ -87,11 +93,11 @@ func (gps *Device) readNextSentence() (sentence string) {
gps.sentence.Reset()
var b byte = ' '
for b != '$' {
for b != startingDelimiter {
b = gps.readNextByte()
}
for b != '*' {
for b != checksumDelimiter {
gps.sentence.WriteByte(b)
b = gps.readNextByte()
}
@@ -153,8 +159,13 @@ func (gps *Device) WriteBytes(bytes []byte) {
}
// validSentence checks if a sentence has been received uncorrupted
// For example, a valid NMEA sentence such as this:
// $GPGLL,3751.65,S,14507.36,E*77
// It has to start with a '$' character.
// It has to have a 5 character long sentence identifier.
// It has to end with a '*' character following by a checksum.
func validSentence(sentence string) error {
if len(sentence) < 7 || sentence[0] != '$' || sentence[len(sentence)-3] != '*' {
if len(sentence) < minimumNMEALength || sentence[0] != startingDelimiter || sentence[len(sentence)-3] != checksumDelimiter {
return errInvalidNMEASentenceLength
}
var cs byte = 0