Initial magic

This commit is contained in:
Sacha Ligthert 2025-03-04 09:20:51 +01:00
commit d491fa5def
3 changed files with 118 additions and 0 deletions

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module gitea.ligthert.net/golang/cartracker
go 1.24.0
require (
github.com/rzetterberg/elmobd v0.0.0-20240426091703-01e7bbc11e6c // indirect
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 // indirect
golang.org/x/sys v0.30.0 // indirect
)

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
github.com/rzetterberg/elmobd v0.0.0-20240426091703-01e7bbc11e6c h1:+KTTRRXwNEZaby+mTA5o8vbeLFtffJ8rDCw/E4zEE0A=
github.com/rzetterberg/elmobd v0.0.0-20240426091703-01e7bbc11e6c/go.mod h1:pjUsxTi7MfZhog8b0rwIxtddeBI35SxEi8mNOQgzSO8=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

103
main.go Normal file
View File

@ -0,0 +1,103 @@
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())
}
}