104 lines
2.0 KiB
Go
104 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/rzetterberg/elmobd"
|
|
)
|
|
|
|
func main() {
|
|
addr := flag.String(
|
|
"addr",
|
|
"test:///dev/ttyUSB0",
|
|
"Address of the ELM327 device to use (use either test://, tcp://ip:port or serial:///dev/ttyS0)",
|
|
)
|
|
debug := flag.Bool(
|
|
"debug",
|
|
false,
|
|
"Enable debug outputs",
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
dev, err := elmobd.NewDevice(*addr, *debug)
|
|
|
|
if err != nil {
|
|
fmt.Println("Failed to create new device", err)
|
|
return
|
|
}
|
|
|
|
supported, err := dev.CheckSupportedCommands()
|
|
|
|
if err != nil {
|
|
fmt.Println("Failed to check supported commands", err)
|
|
return
|
|
}
|
|
|
|
allCommands := elmobd.GetSensorCommands()
|
|
carCommands := supported.FilterSupported(allCommands)
|
|
|
|
version, err := dev.GetVersion()
|
|
if err != nil {
|
|
fmt.Println("Error fetching version")
|
|
return
|
|
}
|
|
fmt.Println("Working with version", version)
|
|
|
|
voltage, err := dev.GetVoltage()
|
|
if err != nil {
|
|
fmt.Println("Error getting voltage")
|
|
return
|
|
}
|
|
fmt.Printf("Voltage: %fv\n", voltage)
|
|
|
|
ignition, err := dev.GetIgnitionState()
|
|
if err != nil {
|
|
fmt.Println("Failed getting ignation state")
|
|
}
|
|
|
|
if ignition {
|
|
fmt.Println("ignition is turned on")
|
|
} else {
|
|
fmt.Println("ignition is turned off")
|
|
}
|
|
|
|
// fmt.Println("All commands:")
|
|
// fmt.Println(allCommands)
|
|
// fmt.Println("Car commands")
|
|
// fmt.Println(carCommands)
|
|
|
|
var commands []elmobd.OBDCommand
|
|
|
|
fmt.Printf("%d of %d commands supported:\n", len(carCommands), len(allCommands))
|
|
|
|
for _, cmd := range carCommands {
|
|
fmt.Printf("- %s supported\n", cmd.Key())
|
|
|
|
if cmd.Key() == "coolant_temperature" {
|
|
commands = append(commands, elmobd.NewCoolantTemperature())
|
|
}
|
|
|
|
if cmd.Key() == "short_term_fuel_trim_bank1" {
|
|
commands = append(commands, elmobd.NewShortFuelTrim1())
|
|
}
|
|
|
|
if cmd.Key() == "engine_rpm" {
|
|
commands = append(commands, elmobd.NewEngineRPM())
|
|
}
|
|
|
|
}
|
|
|
|
outputs, err := dev.RunManyOBDCommands(commands)
|
|
if err != nil {
|
|
fmt.Println("Something went very wrong! ", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
for _, value := range outputs {
|
|
fmt.Printf("%s => %s\n", value.Key(), value.ValueAsLit())
|
|
}
|
|
|
|
}
|