1 回答

TA贡献1804条经验 获得超2个赞
我能够找出问题所在。我首先检查我的字节是如何从以下代码中读取的:
n, err := serPort.Read(buff)
fmt.Printf("%d", n)
它依次给出值1和73, 74。假设1是设备本身发送的换行符,我发现这可能是代码无法捕获的原因$GPRMC。
因此我修改了我的代码以检查读取的字节数是否总是大于 1 字节
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)
}
}
}
果然,代码现在可以工作了,得到的输出是我所期望的:
$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
- 1 回答
- 0 关注
- 87 浏览
添加回答
举报