Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replicate candump in GO #224

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions datalogging/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# Data Logging & Processing

## Setup
1. Ensure your Linux or WSL environment has SocketCAN enabled (Documentation WIP).

2. Run `sudo ./vcan_setup.sh` to setup a virtual CAN interface.

3. Run `go run can_receive.go <INTERFACE NAME>`. where `<INTERFACE NAME>` is the virtual CAN interface name. If `./vcan_setup.sh` was executed, `<INTERFACE NAME>` will be `vcan0`.

4. Within another window, test the connection by executing `cansend vcan0 01a#11223344AABBCCDD` or `cangen vcan0 -v` to send continuous CAN frames.
58 changes: 58 additions & 0 deletions datalogging/can_receive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"net"
"os"
"os/signal"
"syscall"

"github.com/brutella/can"
)

func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run can_receive.go <INTERFACE NAME>")
os.Exit(2)
}
var interfaceName = os.Args[1]

canInterface, err := net.InterfaceByName(interfaceName)

if err != nil {
fmt.Printf("Could not connect to %s. Check that you have added it properly.\n", interfaceName)
os.Exit(1)
}

// Establish connection to CAN bus
connection, err := can.NewReadWriteCloserForInterface(canInterface)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

can_bus := can.NewBus(connection)
can_bus.SubscribeFunc(func(frm can.Frame) {
printCANFrame(interfaceName, frm)
})
can_bus.ConnectAndPublish()

// Intercepts interrupt to perform cleanup actions before exiting program
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
signal.Notify(signalChan, syscall.SIGTERM)

go func() {
<-signalChan
fmt.Println("\nExiting...")
can_bus.Disconnect()
os.Exit(1)
}()
}

// Formats and outputs CAN frames to match output of candump
func printCANFrame(interfaceName string, frm can.Frame) {
dataToPrint := frm.Data[:frm.Length]
fmt.Printf("%s %-4X [%d] % -42X\n", interfaceName, frm.ID, frm.Length, dataToPrint)
}
8 changes: 8 additions & 0 deletions datalogging/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module datalogging

go 1.23.2

require (
github.com/brutella/can v0.0.2 // direct
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect
)
4 changes: 4 additions & 0 deletions datalogging/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/brutella/can v0.0.2 h1:8TyjZrBZSwQwSr5x3U9KtKzGW8HNE/NpUgsNcYDAVIM=
github.com/brutella/can v0.0.2/go.mod h1:NYDxbQito3w4+4DcjWs/fpQ3xyaFdpXw/KYqtZFU98k=
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 h1:0oC8rFnE+74kEmuHZ46F6KHsMr5Gx2gUQPuNz28iQZM=
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6 changes: 6 additions & 0 deletions datalogging/vcan_setup.sh
BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

modprobe -a can can_raw vcan
ip link add dev vcan0 type vcan
ip link set up vcan0
ip link show vcan0
Loading