Skip to content

Commit cddec59

Browse files
naveenv92Naveen Venkatesan
and
Naveen Venkatesan
authored
Updated README with example of decoding and dbc file with >8 byte message (#6)
* Updated README with example of decoding and dbc file with >8 byte message * Fixed indenting in README * Fixed indenting in README part 2 * Updated filename to example_payload.dbc * Updated dbc messages and removed unnecessary code from payload.go Co-authored-by: Naveen Venkatesan <nvenkatesan@rivian.com>
1 parent 6e5dbc8 commit cddec59

File tree

4 files changed

+272
-210
lines changed

4 files changed

+272
-210
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,45 @@ can-go makes use of the Linux SocketCAN abstraction for CAN communication.
2020

2121
## Examples
2222

23+
### Decoding CAN messages
24+
25+
Decoding CAN messages from byte arrays can be done using `can.Payload`
26+
27+
```go
28+
func main() {
29+
// Create payload from hex string
30+
byteStringHex := "8000000420061880000005200600"
31+
p, _ := can.PayloadFromHex(byteStringHex)
32+
33+
// Load example dbc file
34+
dbcFile := "./testdata/dbc/example/example_payload.dbc"
35+
input, _ := ioutil.ReadFile(dbcFile)
36+
c, _ := generate.Compile(dbcFile, input)
37+
db := *c.Database
38+
39+
// Decode message frame ID 1530
40+
message, _ := db.Message(uint32(1530))
41+
decodedSignals := message.Decode(&p)
42+
for _, signal := range decodedSignals {
43+
fmt.Printf("Signal: %s, Value: %f, Description: %s\n", signal.Signal.Name, signal.Value, signal.Description)
44+
}
45+
}
46+
```
47+
48+
```
49+
Signal: TargetSpeedRearLeft, Value: 0.000000, Description:
50+
Signal: DisconnectStateRearLeftTarget, Value: 0.000000, Description:
51+
Signal: CurrentRearLeft, Value: 4.000000, Description:
52+
Signal: LockCountRearLeft, Value: 1560.000000, Description:
53+
Signal: DisconnectStateRearLeft, Value: 2.000000, Description: Unlocked
54+
Signal: TargetSpeedRearRight, Value: 0.000000, Description:
55+
Signal: DisconnectStateRearRightTarget, Value: 0.000000, Description:
56+
Signal: CurrentRearRight, Value: 5.000000, Description:
57+
Signal: LockCountRearRight, Value: 1536.000000, Description:
58+
Signal: DisconnectStateRearRight, Value: 2.000000, Description: Unlocked
59+
```
60+
61+
2362
### Receiving CAN frames
2463

2564
Receiving CAN frames from a socketcan interface.

payload.go

Lines changed: 99 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,93 @@ import (
55
"math/big"
66
)
77

8-
// const MaxDataLength = 8
9-
10-
// // Data holds the data in a CAN frame.
11-
// //
12-
// // Layout
13-
// //
14-
// // Individual bits in the data are numbered according to the following scheme:
15-
// //
16-
// // BIT
17-
// // NUMBER
18-
// // +------+------+------+------+------+------+------+------+
19-
// // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
20-
// // BYTE +------+------+------+------+------+------+------+------+
21-
// // NUMBER
22-
// // +-----+ +------+------+------+------+------+------+------+------+
23-
// // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
24-
// // +-----+ +------+------+------+------+------+------+------+------+
25-
// // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
26-
// // +-----+ +------+------+------+------+------+------+------+------+
27-
// // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
28-
// // +-----+ +------+------+------+------+------+------+------+------+
29-
// // | 3 | | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
30-
// // +-----+ +------+------+------+------+------+------+------+------+
31-
// // | 4 | | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
32-
// // +-----+ +------+------+------+------+------+------+------+------+
33-
// // | 5 | | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
34-
// // +-----+ +------+------+------+------+------+------+------+------+
35-
// // | 6 | | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
36-
// // +-----+ +------+------+------+------+------+------+------+------+
37-
// // | 7 | | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
38-
// // +-----+ +------+------+------+------+------+------+------+------+
39-
// //
40-
// // Bit ranges can be manipulated using little-endian and big-endian bit ordering.
41-
// //
42-
// // Little-endian bit ranges
43-
// //
44-
// // Example range of length 32 starting at bit 29:
45-
// //
46-
// // BIT
47-
// // NUMBER
48-
// // +------+------+------+------+------+------+------+------+
49-
// // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
50-
// // BYTE +------+------+------+------+------+------+------+------+
51-
// // NUMBER
52-
// // +-----+ +------+------+------+------+------+------+------+------+
53-
// // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
54-
// // +-----+ +------+------+------+------+------+------+------+------+
55-
// // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
56-
// // +-----+ +------+------+------+------+------+------+------+------+
57-
// // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
58-
// // +-----+ +------+------+------+------+------+------+------+------+
59-
// // | 3 | | <-------------LSb | 28 | 27 | 26 | 25 | 24 |
60-
// // +-----+ +------+------+------+------+------+------+------+------+
61-
// // | 4 | | <-------------------------------------------------- |
62-
// // +-----+ +------+------+------+------+------+------+------+------+
63-
// // | 5 | | <-------------------------------------------------- |
64-
// // +-----+ +------+------+------+------+------+------+------+------+
65-
// // | 6 | | <-------------------------------------------------- |
66-
// // +-----+ +------+------+------+------+------+------+------+------+
67-
// // | 7 | | 63 | 62 | 61 | <-MSb--------------------------- |
68-
// // +-----+ +------+------+------+------+------+------+------+------+
69-
// //
70-
// // Big-endian bit ranges
71-
// //
72-
// // Example range of length 32 starting at bit 29:
73-
// //
74-
// // BIT
75-
// // NUMBER
76-
// // +------+------+------+------+------+------+------+------+
77-
// // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
78-
// // BYTE +------+------+------+------+------+------+------+------+
79-
// // NUMBER
80-
// // +-----+ +------+------+------+------+------+------+------+------+
81-
// // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
82-
// // +-----+ +------+------+------+------+------+------+------+------+
83-
// // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
84-
// // +-----+ +------+------+------+------+------+------+------+------+
85-
// // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
86-
// // +-----+ +------+------+------+------+------+------+------+------+
87-
// // | 3 | | 31 | 30 | <-MSb--------------------------------- |
88-
// // +-----+ +------+------+------+------+------+------+------+------+
89-
// // | 4 | | <-------------------------------------------------- |
90-
// // +-----+ +------+------+------+------+------+------+------+------+
91-
// // | 5 | | <-------------------------------------------------- |
92-
// // +-----+ +------+------+------+------+------+------+------+------+
93-
// // | 6 | | <-------------------------------------------------- |
94-
// // +-----+ +------+------+------+------+------+------+------+------+
95-
// // | 7 | | <------LSb | 61 | 60 | 59 | 58 | 57 | 56 |
96-
// // +-----+ +------+------+------+------+------+------+------+------+
97-
// type Data [MaxDataLength]byte
98-
99-
//type Packed *big.Int
8+
// Data holds the data in a CAN frame.
9+
//
10+
// Layout
11+
//
12+
// Individual bits in the data are numbered according to the following scheme:
13+
//
14+
// BIT
15+
// NUMBER
16+
// +------+------+------+------+------+------+------+------+
17+
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
18+
// BYTE +------+------+------+------+------+------+------+------+
19+
// NUMBER
20+
// +-----+ +------+------+------+------+------+------+------+------+
21+
// | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
22+
// +-----+ +------+------+------+------+------+------+------+------+
23+
// | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
24+
// +-----+ +------+------+------+------+------+------+------+------+
25+
// | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
26+
// +-----+ +------+------+------+------+------+------+------+------+
27+
// | 3 | | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
28+
// +-----+ +------+------+------+------+------+------+------+------+
29+
// | 4 | | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
30+
// +-----+ +------+------+------+------+------+------+------+------+
31+
// | 5 | | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
32+
// +-----+ +------+------+------+------+------+------+------+------+
33+
// | 6 | | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
34+
// +-----+ +------+------+------+------+------+------+------+------+
35+
// | 7 | | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
36+
// +-----+ +------+------+------+------+------+------+------+------+
37+
//
38+
// Bit ranges can be manipulated using little-endian and big-endian bit ordering.
39+
//
40+
// Little-endian bit ranges
41+
//
42+
// Example range of length 32 starting at bit 29:
43+
//
44+
// BIT
45+
// NUMBER
46+
// +------+------+------+------+------+------+------+------+
47+
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
48+
// BYTE +------+------+------+------+------+------+------+------+
49+
// NUMBER
50+
// +-----+ +------+------+------+------+------+------+------+------+
51+
// | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
52+
// +-----+ +------+------+------+------+------+------+------+------+
53+
// | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
54+
// +-----+ +------+------+------+------+------+------+------+------+
55+
// | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
56+
// +-----+ +------+------+------+------+------+------+------+------+
57+
// | 3 | | <-------------LSb | 28 | 27 | 26 | 25 | 24 |
58+
// +-----+ +------+------+------+------+------+------+------+------+
59+
// | 4 | | <-------------------------------------------------- |
60+
// +-----+ +------+------+------+------+------+------+------+------+
61+
// | 5 | | <-------------------------------------------------- |
62+
// +-----+ +------+------+------+------+------+------+------+------+
63+
// | 6 | | <-------------------------------------------------- |
64+
// +-----+ +------+------+------+------+------+------+------+------+
65+
// | 7 | | 63 | 62 | 61 | <-MSb--------------------------- |
66+
// +-----+ +------+------+------+------+------+------+------+------+
67+
//
68+
// Big-endian bit ranges
69+
//
70+
// Example range of length 32 starting at bit 29:
71+
//
72+
// BIT
73+
// NUMBER
74+
// +------+------+------+------+------+------+------+------+
75+
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
76+
// BYTE +------+------+------+------+------+------+------+------+
77+
// NUMBER
78+
// +-----+ +------+------+------+------+------+------+------+------+
79+
// | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
80+
// +-----+ +------+------+------+------+------+------+------+------+
81+
// | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
82+
// +-----+ +------+------+------+------+------+------+------+------+
83+
// | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
84+
// +-----+ +------+------+------+------+------+------+------+------+
85+
// | 3 | | 31 | 30 | <-MSb--------------------------------- |
86+
// +-----+ +------+------+------+------+------+------+------+------+
87+
// | 4 | | <-------------------------------------------------- |
88+
// +-----+ +------+------+------+------+------+------+------+------+
89+
// | 5 | | <-------------------------------------------------- |
90+
// +-----+ +------+------+------+------+------+------+------+------+
91+
// | 6 | | <-------------------------------------------------- |
92+
// +-----+ +------+------+------+------+------+------+------+------+
93+
// | 7 | | <------LSb | 61 | 60 | 59 | 58 | 57 | 56 |
94+
// +-----+ +------+------+------+------+------+------+------+------+
10095

10196
type Payload struct {
10297
// Binary data
@@ -168,13 +163,8 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
168163
return AsSigned(unsigned, length)
169164
}
170165

171-
// SignedBitsBigEndian returns little-endian bit range [start, start+length) as a signed value.
172-
// func (d *Data) SignedBitsBigEndian(start, length uint8) int64 {
173-
// unsigned := d.UnsignedBitsBigEndian(start, length)
174-
// return reinterpret.AsSigned(unsigned, length)
175-
// }
176-
177-
// // SetUnsignedBitsBigEndian sets the little-endian bit range [start, start+length) to the provided unsigned value.
166+
// TODO: Implement SetUnsignedBitsLittleEndian for Payload
167+
// SetUnsignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided unsigned value.
178168
// func (d *Data) SetUnsignedBitsLittleEndian(start, length uint8, value uint64) {
179169
// // pack bits into one continuous value
180170
// packed := d.PackLittleEndian()
@@ -190,7 +180,8 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
190180
// d.UnpackLittleEndian(newPacked)
191181
// }
192182

193-
// // SetUnsignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided unsigned value.
183+
// TODO: Implement SetUnsignedBitsBigEndian for Payload
184+
// SetUnsignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided unsigned value.
194185
// func (d *Data) SetUnsignedBitsBigEndian(start, length uint8, value uint64) {
195186
// // pack bits into one continuous value
196187
// packed := d.PackBigEndian()
@@ -208,12 +199,14 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
208199
// d.UnpackBigEndian(newPacked)
209200
// }
210201

211-
// // SetSignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided signed value.
202+
// TODO: Implement SetSignedBitsLittleEndian for Payload
203+
// SetSignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided signed value.
212204
// func (d *Data) SetSignedBitsLittleEndian(start, length uint8, value int64) {
213205
// d.SetUnsignedBitsLittleEndian(start, length, reinterpret.AsUnsigned(value, length))
214206
// }
215207

216-
// // SetSignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided signed value.
208+
// TODO: Implement SetSignedBitsBigEndian for Payload
209+
// SetSignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided signed value.
217210
// func (d *Data) SetSignedBitsBigEndian(start, length uint8, value int64) {
218211
// d.SetUnsignedBitsBigEndian(start, length, reinterpret.AsUnsigned(value, length))
219212
// }
@@ -247,20 +240,6 @@ func (p *Payload) SetBit(i uint16, value bool) {
247240
}
248241
}
249242

250-
// PackLittleEndian packs the data into a contiguous uint64 value for little-endian signals.
251-
// func (d *Data) PackLittleEndian() uint64 {
252-
// var packed uint64
253-
// packed |= uint64(d[0]) << (0 * 8)
254-
// packed |= uint64(d[1]) << (1 * 8)
255-
// packed |= uint64(d[2]) << (2 * 8)
256-
// packed |= uint64(d[3]) << (3 * 8)
257-
// packed |= uint64(d[4]) << (4 * 8)
258-
// packed |= uint64(d[5]) << (5 * 8)
259-
// packed |= uint64(d[6]) << (6 * 8)
260-
// packed |= uint64(d[7]) << (7 * 8)
261-
// return packed
262-
// }
263-
264243
// PackLittleEndian packs the byte array into a continuous little endian big.Int
265244
func (p *Payload) PackLittleEndian() *big.Int {
266245
if p.PackedLittleEndian == nil {
@@ -279,20 +258,6 @@ func reverse(data []byte) []byte {
279258
return reversedArray
280259
}
281260

282-
// // PackBigEndian packs the data into a contiguous uint64 value for big-endian signals.
283-
// func (d *Data) PackBigEndian() uint64 {
284-
// var packed uint64
285-
// packed |= uint64(d[0]) << (7 * 8)
286-
// packed |= uint64(d[1]) << (6 * 8)
287-
// packed |= uint64(d[2]) << (5 * 8)
288-
// packed |= uint64(d[3]) << (4 * 8)
289-
// packed |= uint64(d[4]) << (3 * 8)
290-
// packed |= uint64(d[5]) << (2 * 8)
291-
// packed |= uint64(d[6]) << (1 * 8)
292-
// packed |= uint64(d[7]) << (0 * 8)
293-
// return packed
294-
// }
295-
296261
// PackBigEndian packs the byte array into a continuous big endian big.Int
297262
func (p *Payload) PackBigEndian() *big.Int {
298263
if p.PackedBigEndian == nil {
@@ -302,7 +267,8 @@ func (p *Payload) PackBigEndian() *big.Int {
302267
return new(big.Int).Set(p.PackedBigEndian)
303268
}
304269

305-
// // UnpackLittleEndian sets the value of d.Bytes by unpacking the provided value as sequential little-endian bits.
270+
// TODO: Implement UnpackLittleEndian for Payload
271+
// UnpackLittleEndian sets the value of d.Bytes by unpacking the provided value as sequential little-endian bits.
306272
// func (d *Data) UnpackLittleEndian(packed uint64) {
307273
// d[0] = uint8(packed >> (0 * 8))
308274
// d[1] = uint8(packed >> (1 * 8))
@@ -314,7 +280,8 @@ func (p *Payload) PackBigEndian() *big.Int {
314280
// d[7] = uint8(packed >> (7 * 8))
315281
// }
316282

317-
// // UnpackBigEndian sets the value of d.Bytes by unpacking the provided value as sequential big-endian bits.
283+
// TODO: Implement UnpackBigEndian for Payload
284+
// UnpackBigEndian sets the value of d.Bytes by unpacking the provided value as sequential big-endian bits.
318285
// func (d *Data) UnpackBigEndian(packed uint64) {
319286
// d[0] = uint8(packed >> (7 * 8))
320287
// d[1] = uint8(packed >> (6 * 8))
@@ -335,41 +302,6 @@ func (p *Payload) invertEndian(i uint16) uint16 {
335302
return bitIndex
336303
}
337304

338-
// // CheckBitRangeLittleEndian checks that a little-endian bit range fits in the data.
339-
// func CheckBitRangeLittleEndian(frameLength, rangeStart, rangeLength uint8) error {
340-
// lsbIndex := rangeStart
341-
// msbIndex := rangeStart + rangeLength - 1
342-
// upperBound := frameLength * 8
343-
// if msbIndex >= upperBound {
344-
// return fmt.Errorf("bit range out of bounds [0, %v): [%v, %v]", upperBound, lsbIndex, msbIndex)
345-
// }
346-
// return nil
347-
// }
348-
349-
// // CheckBitRangeBigEndian checks that a big-endian bit range fits in the data.
350-
// func CheckBitRangeBigEndian(frameLength, rangeStart, rangeLength uint8) error {
351-
// upperBound := frameLength * 8
352-
// if rangeStart >= upperBound {
353-
// return fmt.Errorf("bit range starts out of bounds [0, %v): %v", upperBound, rangeStart)
354-
// }
355-
// msbIndex := invertEndian(rangeStart)
356-
// lsbIndex := msbIndex - rangeLength + 1
357-
// end := invertEndian(lsbIndex)
358-
// if end >= upperBound {
359-
// return fmt.Errorf("bit range ends out of bounds [0, %v): %v", upperBound, end)
360-
// }
361-
// return nil
362-
// }
363-
364-
// // CheckValue checks that a value fits in a number of bits.
365-
// func CheckValue(value uint64, bits uint8) error {
366-
// upperBound := uint64(1 << bits)
367-
// if value >= upperBound {
368-
// return fmt.Errorf("value out of bounds [0, %v): %v", upperBound, value)
369-
// }
370-
// return nil
371-
// }
372-
373305
// AsSigned reinterprets the provided unsigned value as a signed value.
374306
func AsSigned(unsigned uint64, bits uint16) int64 {
375307
switch bits {

0 commit comments

Comments
 (0)