-
Notifications
You must be signed in to change notification settings - Fork 78
/
pcdin.go
66 lines (60 loc) · 1.84 KB
/
pcdin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package nmea
import (
"encoding/hex"
"fmt"
"strconv"
)
const (
// TypePCDIN is type of PCDIN sentence for SeaSmart.Net Protocol
TypePCDIN = "CDIN"
)
// PCDIN - SeaSmart.Net Protocol transfers NMEA2000 message as NMEA0183 sentence
// http://www.seasmart.net/pdf/SeaSmart_HTTP_Protocol_RevG_043012.pdf (SeaSmart.Net Protocol Specification Version 1.7)
//
// Note: older SeaSmart.Net Protocol versions have different amount of fields
//
// Format: $PCDIN,hhhhhh,hhhhhhhh,hh,h--h*hh<CR><LF>
// Example: $PCDIN,01F112,000C72EA,09,28C36A0000B40AFD*56
type PCDIN struct {
BaseSentence
PGN uint32 // PGN of NMEA2000 packet
Timestamp uint32 // ticks since something
Source uint8 // 0-255
Data []byte // can be more than 8 bytes i.e can contain assembled fast packets
}
// newPCDIN constructor
func newPCDIN(s BaseSentence) (Sentence, error) {
p := NewParser(s)
p.AssertType(TypePCDIN)
if len(p.Fields) != 4 {
p.SetErr("fields", "invalid number of fields in sentence")
return nil, p.Err()
}
pgn, err := strconv.ParseUint(p.Fields[0], 16, 24)
if err != nil {
p.err = fmt.Errorf("nmea: %s failed to parse PGN field: %w", p.Prefix(), err)
return nil, p.Err()
}
timestamp, err := strconv.ParseUint(p.Fields[1], 16, 32)
if err != nil {
p.err = fmt.Errorf("nmea: %s failed to parse timestamp field: %w", p.Prefix(), err)
return nil, p.Err()
}
source, err := strconv.ParseUint(p.Fields[2], 16, 8)
if err != nil {
p.err = fmt.Errorf("nmea: %s failed to parse source field: %w", p.Prefix(), err)
return nil, p.Err()
}
data, err := hex.DecodeString(p.Fields[3])
if err != nil {
p.err = fmt.Errorf("nmea: %s failed to decode data: %w", p.Prefix(), err)
return nil, p.Err()
}
return PCDIN{
BaseSentence: s,
PGN: uint32(pgn),
Timestamp: uint32(timestamp),
Source: uint8(source),
Data: data,
}, p.Err()
}