2
votes

I have used the following packages to:

  1. read from the serial port (go get go.bug.st/serial)
  2. 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.

1

1 Answers

3
votes

I was able to figure out the problem. I started by checking how my bytes were read generally from the following code:

     n, err := serPort.Read(buff)
     fmt.Printf("%d", n)

It was sequentially giving out values 1 and 73, 74. Assuming that the 1 is the new line character sent by the device itself, I figured out that this could be the reason why the code was unable to catch $GPRMC.

Hence I modified my code to check if the number of bytes read are always greater than 1 byte

    for {
        n, err := serPort.Read(buff)
        fmt.Printf("%v\n", n)
        if err != nil {
            log.Fatal(err)
            break
        }
        // do not try to parse a single read byte
        // instead parse the actual incoming string.
        if n > 1 {
            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)
            }
        }
    }

Sure enough, the code works now and the output derived is what I expect:

$GPRMC,142312.000,A,5306.6774,N,00851.3114,E,0.04,14.48,300620,,,A*5A
Raw sentence: $GPRMC,142312.000,A,5306.6774,N,00851.3114,E,0.04,14.48,300620,,,A*5A
Time: 14:23:12.0000
Validity: A
Latitude GPS: 5306.6774
Latitude DMS: 53° 6' 40.644000"
Longitude GPS: 851.3114
Longitude DMS: 8° 51' 18.684000"
Speed: 0.040000
Course: 14.480000
Date: 30/06/20
Variation: 0.000000