1 回答
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
我能夠找出問題所在。我首先檢查我的字節(jié)是如何從以下代碼中讀取的:
n, err := serPort.Read(buff)
fmt.Printf("%d", n)
它依次給出值1和73, 74。假設(shè)1是設(shè)備本身發(fā)送的換行符,我發(fā)現(xiàn)這可能是代碼無法捕獲的原因$GPRMC。
因此我修改了我的代碼以檢查讀取的字節(jié)數(shù)是否總是大于 1 字節(jié)
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)
}
}
}
果然,代碼現(xiàn)在可以工作了,得到的輸出是我所期望的:
$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 關(guān)注
- 113 瀏覽
添加回答
舉報(bào)
