-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobd.go
62 lines (58 loc) · 1.26 KB
/
gobd.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
// GOBD initial round
package main
import (
"io"
"log"
"os"
"os/signal"
"github.com/tarm/serial"
)
func readData(obdConn io.ReadWriteCloser, data chan byte) {
writeData("atma")
buffer := make([]byte, 1)
for {
n, err := obdConn.read(buffer)
data <- buffer[0]
}
}
func writeData(obdConn io.ReadWriteCloser, cmd string) {
cmd += "\n"
_, err := obdConn.write(cmd)
if err != nil {
log.Fatal(err)
}
}
func main() {
// Initialize serial reader
// Initialize data structures for the OBD Data
// Spin off data reading on goroutine
// Write data across channel
// Process/Print data
conf := &serial.Config{Name:"/dev/ttyUSBart0", Baud: 4800}
obdConn, err := serial.OpenPort(conf)
if err != nil {
log.Fatal(err)
}
// This is a large buffer. Hopefully its temporarily like this and can be made smaller
data := make(chan byte, 1000)
defer close(data)
////////////////////////////////////
// Init ELM Device here
////////////////////////////////////
writeData("atz")
writeData("ath1")
writeData("ate0")
writeData("atal")
////////////////////////////////////
// May also need to change baud rate
////////////////////////////////////
go readData(obdConn, data)
i := 0
for/*ever*/ {
i++
if (i%2 == 0) {
fmt.Print(" ")
}
fmt.Print(<-data)
}
}