MCPcopy Index your code
hub / github.com/einride/can-go

github.com/einride/can-go @v0.17.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.17.0 ↗ · + Follow
1,421 symbols 4,024 edges 146 files 479 documented · 34%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

:electric_plug: go.einride.tech/can

PkgGoDev GoReportCard Codecov

CAN toolkit for Go programmers.

can-go makes use of the Linux SocketCAN abstraction for CAN communication. (See the SocketCAN documentation for more details).

Installation

go get -u go.einride.tech/can

Examples

Setting up a CAN interface


import "go.einride.tech/can/pkg/candevice"

func main() {
    // Error handling omitted to keep example simple
    d, _ := candevice.New("can0")
    _ := d.SetBitrate(250000)
    _ := d.SetUp()
    defer d.SetDown()
}

Receiving CAN frames

Receiving CAN frames from a socketcan interface.

import "go.einride.tech/can/pkg/socketcan"

func main() {
    // Error handling omitted to keep example simple
    conn, _ := socketcan.DialContext(context.Background(), "can", "can0")

    recv := socketcan.NewReceiver(conn)
    for recv.Receive() {
        frame := recv.Frame()
        fmt.Println(frame.String())
    }
}

Sending CAN frames/messages

Sending CAN frames to a socketcan interface.

import "go.einride.tech/can/pkg/socketcan"

func main() {
    // Error handling omitted to keep example simple

    conn, _ := socketcan.DialContext(context.Background(), "can", "can0")

    frame := can.Frame{}
    tx := socketcan.NewTransmitter(conn)
    _ = tx.TransmitFrame(context.Background(), frame)
}

Generating Go code from a DBC file

It is possible to generate Go code from a .dbc file.

$ go run go.einride.tech/can/cmd/cantool generate <dbc file root folder> <output folder>

In order to generate Go code that makes sense, we currently perform some validations when parsing the DBC file so there may need to be some changes on the DBC file to make it work

After generating Go code we can marshal a message to a frame:

// import etruckcan "github.com/myproject/myrepo/gen"

auxMsg := etruckcan.NewAuxiliary().SetHeadLights(etruckcan.Auxiliary_HeadLights_LowBeam)
frame := auxMsg.Frame()

Or unmarshal a frame to a message:

// import etruckcan "github.com/myproject/myrepo/gen"

// Error handling omitted for simplicity
_ := recv.Receive()
frame := recv.Frame()

var auxMsg *etruckcan.Auxiliary
_ = auxMsg.UnmarshalFrame(frame)

Running integration tests

Building the tests:

$ make build-integration-tests

Built tests are placed in build/tests.

The candevice test requires access to physical HW, so run it using sudo. Example:

$ sudo ./build/tests/candevice.test
> PASS

Extension points exported contracts — how you extend this code

Message (Interface)
Message is anything that can marshal and unmarshal itself to/from a CAN frame. [14 implementers]
message.go
EmptyMessageWriter (Interface)
EmptyMessageWriter provides write access to a EmptyMessage message. [8 implementers]
testdata/gen/go/example/example.dbc.go
Def (Interface)
Def represents a single definition within a DBC file. [18 implementers]
pkg/dbc/def.go
Node (Interface)
Node is an interface for a CAN node to be run by the runner. [7 implementers]
pkg/canrunner/run.go
Clock (Interface)
Clock provides capabilities from the time standard library package. [3 implementers]
internal/clock/clock.go
EmulatorOption (FuncType)
EmulatorOption represents a way to configure an Emulator prior to creating it.
pkg/socketcan/emulator.go
CanDevice (Interface)
(no doc) [1 implementers]
pkg/candevice/device_linux.go
FrameMarshaler (Interface)
FrameMarshaler can marshal itself to a CAN frame. [15 implementers]
message.go

Core symbols most depended-on inside this repo

P
called by 471
internal/generate/file.go
Messages
called by 74
testdata/gen/go/example/example.dbc.go
Run
called by 73
testdata/gen/go/example/example.dbc.go
Close
called by 41
pkg/socketcan/fileconn.go
token
called by 38
pkg/dbc/parser.go
peekToken
called by 37
pkg/dbc/parser.go
failf
called by 34
pkg/dbc/parser.go
String
called by 32
testdata/gen/go/example/example.dbc.go

Shape

Method 856
Function 341
Struct 137
Interface 62
TypeAlias 20
FuncType 5

Languages

Go100%

Modules by API surface

testdata/gen/go/example/example.dbc.go579 symbols
internal/mocks/gen/mockcanrunner/mocks.go78 symbols
pkg/dbc/def.go57 symbols
pkg/candevice/device_linux.go44 symbols
internal/generate/file.go44 symbols
pkg/dbc/parser.go33 symbols
pkg/canrunner/run.go23 symbols
pkg/descriptor/signal.go22 symbols
data.go19 symbols
pkg/socketcan/frame.go18 symbols
internal/mocks/gen/mockclock/mocks.go18 symbols
pkg/socketcan/fileconn.go17 symbols

For agents

$ claude mcp add can-go \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact