MCPcopy Index your code
hub / github.com/brutella/can

github.com/brutella/can @v0.0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.0.2 ↗ · + Follow
68 symbols 166 edges 21 files 27 documented · 40% 2 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

can

can provides an interface to a CAN bus to read and write frames. The library is based on the SocketCAN network stack on Linux.

Hardware

I'm using a Raspberry Pi 2 Model B and PiCAN2 CAN-Bus board for Raspberry Pi 2 to connect to a CAN bus.

Software

The Raspberry Pi runs Raspbian.

Configuration

Update /boot/config.txt with

dtparam=spi=on 
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25 
dtoverlay=spi-bcm2835-overlay

and reboot.

After phyiscally connecting to the CAN bus, you have to set up the can network interface for a specific bitrate, i.e. 50 kB

sudo ip link set can0 up type can bitrate 50000

Running ifconfig should now include the can0 interface.

Test your configuration

You should test if you actually receive data from the CAN bus. You can either use the candump tool from the can-utils or a simple reimplementation under cmd/candump.go.

Either way you will see something like this

> go run $GOSRC/github.com/brutella/can/cmd/candump.go -if can0

can0 100  [6] 20 83 0C 00 67 29        ' ...g)'
can0 701  [1] 05                       '.'

Usage

Setup the CAN bus

bus, _ := can.NewBusForInterfaceWithName("can0")
bus.ConnectAndPublish()

Send a CAN frame

frm := can.Frame{
    ID:     0x701,
    Length: 1,
    Flags:  0,
    Res0:   0,
    Res1:   0,
    Data:   [8]uint8{0x05},
}

bus.Publish(frm)

Receive a CAN frame

bus.SubscribeFunc(handleCANFrame)

func handleCANFrame(frm can.Frame) {    
    ...
}

There is more to learn from the documentation.

Contact

Matthias Hochgatterer

Github: https://github.com/brutella

Twitter: https://twitter.com/brutella

License

can is available under the MIT license. See the LICENSE file for more info.

Extension points exported contracts — how you extend this code

Handler (Interface)
The Handler interfaces defines a method to receive a frame. [4 implementers]
handler.go
Reader (Interface)
The Reader interface extends the `io.Reader` interface by method to read a frame. [1 implementers]
readwritecloser.go
HandlerFunc (FuncType)
HandlerFunc defines the function type to handle a frame.
handler.go
Writer (Interface)
The Writer interface extends the `io.Writer` interface by method to write a frame. [1 implementers]
readwritecloser.go
ReadWriteCloser (Interface)
The ReadWriteCloser interface combines the Reader and Writer and `io.Closer` interface.
readwritecloser.go

Core symbols most depended-on inside this repo

NewBus
called by 7
bus.go
Subscribe
called by 7
bus.go
read
called by 6
frame.go
write
called by 6
frame.go
NewEchoReadWriteCloser
called by 5
test_readwritecloser.go
publish
called by 3
bus.go
WriteFrame
called by 2
readwritecloser.go
Read
called by 2
readwritecloser.go

Shape

Function 26
Method 26
Struct 11
Interface 4
FuncType 1

Languages

Go100%

Modules by API surface

readwritecloser.go12 symbols
bus.go12 symbols
frame.go7 symbols
handler.go6 symbols
bus_test.go6 symbols
test_readwritecloser.go5 symbols
wait.go4 symbols
cmd/candump.go4 symbols
filter.go3 symbols
wait_test.go2 symbols
socket_windows.go1 symbols
socket_linux.go1 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

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

⬇ download graph artifact