I have used the following packages to:
- read from the serial port (
go get go.bug.st/serial
) - parse the incoming message from the serial port (
go get adrianmo/go-nmea
)
Host machine: Windows 10
Go Version: go version go1.14.4 windows/amd64
Based on the documentation I wrote a simple code that opens the dedicated serial port (COM4
) and reads the NMEA data from the port and tries to parse the data according to the go-nmea
package
Data
Incoming Data from GPS sensor:
$GPRMC,135533.000,A,5306.6644,N,00851.3177,E,0.11,214.59,300620,,,A*6E
$GPRMC,135534.000,A,5306.6643,N,00851.3177,E,0.06,187.72,300620,,,A*68
$GPRMC,135535.000,A,5306.6643,N,00851.3177,E,0.22,341.68,300620,,,A*6C
$GPRMC,135536.000,A,5306.6644,N,00851.3176,E,0.20,324.35,300620,,,A*60
$GPRMC,135537.000,A,5306.6645,N,00851.3176,E,0.12,348.37,300620,,,A*69
Code Snippet
package main
import (
"fmt"
"log"
"github.com/adrianmo/go-nmea"
"go.bug.st/serial"
)
func main() {
mode := &serial.Mode{
BaudRate: 9600,
Parity: serial.NoParity,
DataBits: 8,
StopBits: serial.OneStopBit,
}
serPort, err := serial.Open("COM4", mode)
if err != nil {
log.Fatal(err)
}
defer serPort.Close()
buff := make([]byte, 1024)
for {
n, err := serPort.Read(buff)
if err != nil {
log.Fatal(err)
break
}
if n == 0 {
fmt.Println("\nEOF")
break
}
rawSentence := string(buff[:n])
fmt.Print(rawSentence)
s, err := nmea.Parse(rawSentence)
if err != nil {
log.Fatal(err)
}
if s.DataType() == nmea.TypeRMC {
m := s.(nmea.RMC)
fmt.Printf("Raw sentence: %v\n", m)
fmt.Printf("Time: %s\n", m.Time)
fmt.Printf("Validity: %s\n", m.Validity)
fmt.Printf("Latitude GPS: %s\n", nmea.FormatGPS(m.Latitude))
fmt.Printf("Latitude DMS: %s\n", nmea.FormatDMS(m.Latitude))
fmt.Printf("Longitude GPS: %s\n", nmea.FormatGPS(m.Longitude))
fmt.Printf("Longitude DMS: %s\n", nmea.FormatDMS(m.Longitude))
fmt.Printf("Speed: %f\n", m.Speed)
fmt.Printf("Course: %f\n", m.Course)
fmt.Printf("Date: %s\n", m.Date)
fmt.Printf("Variation: %f\n", m.Variation)
}
}
}
Problem
If I run the code I get the following error:
2020/06/30 16:02:16 nmea: sentence does not start with a '$' or '!'
exit status 1
which is strange because if I comment out the code parsing code:
// s, err := nmea.Parse(rawSentence)
// if err != nil {
// log.Fatal(err)
// }
// if s.DataType() == nmea.TypeRMC {
// m := s.(nmea.RMC)
// fmt.Printf("Raw sentence: %v\n", m)
// fmt.Printf("Time: %s\n", m.Time)
// fmt.Printf("Validity: %s\n", m.Validity)
// fmt.Printf("Latitude GPS: %s\n", nmea.FormatGPS(m.Latitude))
// fmt.Printf("Latitude DMS: %s\n", nmea.FormatDMS(m.Latitude))
// fmt.Printf("Longitude GPS: %s\n", nmea.FormatGPS(m.Longitude))
// fmt.Printf("Longitude DMS: %s\n", nmea.FormatDMS(m.Longitude))
// fmt.Printf("Speed: %f\n", m.Speed)
// fmt.Printf("Course: %f\n", m.Course)
// fmt.Printf("Date: %s\n", m.Date)
// fmt.Printf("Variation: %f\n", m.Variation)
// }
The serial port prints the GPS co-ordinates as mentioned above.
Where am I going wrong here? I tried removing the new-line and carriage-return by doing the following in the code:
rawSentence := string(buff[:n])
rawSentence = string.ReplaceAll(rawSentence, "\r\n", "")
fmt.Print(rawSentence)
But I still get the same error.