-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (128 loc) · 3.52 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
// ---------------------------------------------------------------------------------------
// imports
// ---------------------------------------------------------------------------------------
import (
"bytes"
"errors"
"flag"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"canlisten/can"
"canlisten/filter"
"github.com/tarm/serial"
)
// ---------------------------------------------------------------------------------------
// constants
// ---------------------------------------------------------------------------------------
const (
CmdCanTransmit = 't'
)
// ---------------------------------------------------------------------------------------
// global variables
// ---------------------------------------------------------------------------------------
var (
Device string
FilterExpr string
Running = true
)
// ---------------------------------------------------------------------------------------
// helper functions
// ---------------------------------------------------------------------------------------
func ReadNextCommand(s *serial.Port) (string, error) {
buf := make([]byte, 1)
str := bytes.NewBufferString("")
for Running {
n, err := s.Read(buf)
if err == io.EOF {
continue
} else if err != nil {
return "", err
}
if n < 1 {
continue
}
err = str.WriteByte(buf[0])
if err != nil {
return "", err
}
if buf[0] == '\r' || buf[0] == '\a' {
return str.String(), nil
}
}
return "", errors.New("empty response")
}
func GetPrintableCmd(cmd string) string {
cmd = strings.Replace(cmd, "\r", "!", -1)
cmd = strings.Replace(cmd, "\a", "?", -1)
return cmd
}
// ---------------------------------------------------------------------------------------
// application entry
// ---------------------------------------------------------------------------------------
func main() {
flag.StringVar(&Device, "dev", "/dev/ttyACM0", "")
flag.StringVar(&FilterExpr, "filter", "", "")
flag.Parse()
c := &serial.Config{Name: Device, Baud: 115200, ReadTimeout: 50 * time.Millisecond}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
defer s.Close()
// Open CAN interface with 1MBit/s
_, err = s.Write([]byte("C\rS8\rO\r"))
if err != nil {
log.Println("failed to open serial port")
}
// gracefull shutdown on sigint/sigterm
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
Running = false
log.Println("closing application")
}()
log.Printf("Using filter: \"%s\"", FilterExpr)
filterVm := filter.NewFilter(FilterExpr)
// print all can messages to stdout
stdout := log.New(os.Stdout, "", log.LstdFlags|log.Lmicroseconds)
canCounter := 0
for Running {
cmd, err := ReadNextCommand(s)
if err != nil {
log.Println("ReadNextCommand failed:", err.Error())
continue
}
// ignore all non can messages
if cmd[0] != CmdCanTransmit {
log.Printf("ignoring command \"%s\": unexpected opcode", GetPrintableCmd(cmd))
continue
}
frame, err := can.ParseFrame(cmd)
if err != nil {
log.Printf("failed to parse message \"%s\": %s", GetPrintableCmd(cmd), err.Error())
continue
}
frame.Timestamp = time.Now()
// apply the filterVm
show, err := filterVm.Eval(frame)
if err != nil {
log.Println("failed to evaluate filterVm:", err.Error())
continue
}
if !show {
continue
}
// count message and print to stdout
canCounter++
stdout.Printf("%s %s", filepath.Base(Device), frame.String())
}
log.Printf("statistics: rx=%d", canCounter)
}