Skip to content

Commit d0c0080

Browse files
authored
Merge pull request #69 from kvartborg/add-vhw
Add VHW water speed and heading sentence
2 parents 6384a69 + d05ce7c commit d0c0080

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ At this moment, this library supports the following sentence types:
3131
- [VDM/VDO](http://catb.org/gpsd/AIVDM.html) - Encapsulated binary payload
3232
- [WPL](http://aprs.gids.nl/nmea/#wpl) - Waypoint location
3333
- [RTE](http://aprs.gids.nl/nmea/#rte) - Route
34+
- [VHW](https://www.tronico.fi/OH6NT/docs/NMEA0183.pdf) - Water Speed and Heading
3435

3536
## Example
3637

sentence.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ func Parse(raw string) (Sentence, error) {
150150
return newWPL(s)
151151
case TypeRTE:
152152
return newRTE(s)
153+
case TypeVHW:
154+
return newVHW(s)
153155
}
154156
}
155157
if strings.HasPrefix(s.Raw, SentenceStartEncapsulated) {

vhw.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package nmea
2+
3+
const (
4+
// TypeVHW type for VHW sentences
5+
TypeVHW = "VHW"
6+
)
7+
8+
// VHW contains information about water speed and heading
9+
type VHW struct {
10+
BaseSentence
11+
TrueHeading float64
12+
MagneticHeading float64
13+
SpeedThroughWaterKnots float64
14+
SpeedThroughWaterKPH float64
15+
}
16+
17+
// newVHW constructor
18+
func newVHW(s BaseSentence) (VHW, error) {
19+
p := newParser(s)
20+
p.AssertType(TypeVHW)
21+
return VHW{
22+
BaseSentence: s,
23+
TrueHeading: p.Float64(0, "true heading"),
24+
MagneticHeading: p.Float64(2, "magnetic heading"),
25+
SpeedThroughWaterKnots: p.Float64(4, "speed through water in knots"),
26+
SpeedThroughWaterKPH: p.Float64(6, "speed through water in kilometers per hour"),
27+
}, p.Err()
28+
}

vhw_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package nmea
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var vhw = []struct {
10+
name string
11+
raw string
12+
err string
13+
msg VHW
14+
}{
15+
{
16+
name: "good sentence",
17+
raw: "$VWVHW,45.0,T,43.0,M,3.5,N,6.4,K*56",
18+
msg: VHW{
19+
TrueHeading: 45.0,
20+
MagneticHeading: 43.0,
21+
SpeedThroughWaterKnots: 3.5,
22+
SpeedThroughWaterKPH: 6.4,
23+
},
24+
},
25+
{
26+
name: "bad sentence",
27+
raw: "$VWVHW,T,45.0,43.0,M,3.5,N,6.4,K*56",
28+
err: "nmea: VWVHW invalid true heading: T",
29+
},
30+
}
31+
32+
func TestVHW(t *testing.T) {
33+
for _, tt := range vhw {
34+
t.Run(tt.name, func(t *testing.T) {
35+
m, err := Parse(tt.raw)
36+
if tt.err != "" {
37+
assert.Error(t, err)
38+
assert.EqualError(t, err, tt.err)
39+
} else {
40+
assert.NoError(t, err)
41+
vhw := m.(VHW)
42+
vhw.BaseSentence = BaseSentence{}
43+
assert.Equal(t, tt.msg, vhw)
44+
}
45+
})
46+
}
47+
}

0 commit comments

Comments
 (0)