-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsend.go
More file actions
49 lines (38 loc) · 1.23 KB
/
Copy pathsend.go
File metadata and controls
49 lines (38 loc) · 1.23 KB
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
package canlib
import (
"encoding/binary"
"errors"
"golang.org/x/sys/unix"
)
// SendCan will send the provided CAN message on the given CAN interface
func SendCan(canInterface string, message RawCanFrame) error {
if (message.Dlc > 8) || (len(message.Data) != int(message.Dlc)) {
return errors.New("CAN message to send is invalid")
}
canFD, err := SetupCanInterface(canInterface)
if err != nil {
return errors.New("error setting up CAN interface: " + err.Error())
}
frame := make([]byte, 16)
binary.LittleEndian.PutUint32(frame[0:4], message.OID)
frame[4] = byte(message.Dlc)
copy(frame[8:], message.Data)
unix.Write(canFD, frame)
return nil
}
// SendCanConcurrent will utilize a channel to send CAN messages on the given CAN interface
func SendCanConcurrent(canInterface string, canChannel <-chan RawCanFrame, errorChannel chan<- error) {
canFD, err := SetupCanInterface(canInterface)
if err != nil {
errorChannel <- errors.New("error setting up CAN interface: " + err.Error())
return
}
for message := range canChannel {
frame := make([]byte, 16)
binary.LittleEndian.PutUint32(frame[0:4], message.OID)
frame[4] = byte(message.Dlc)
copy(frame[8:], message.Data)
unix.Write(canFD, frame)
}
errorChannel <- nil
}