Categorygithub.com/toitware/can-go
modulepackage
0.2.3
Repository: https://github.com/toitware/can-go.git
Documentation: pkg.go.dev

# README

:electric_plug: CAN Go

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).

Examples

Receiving CAN frames

Receiving CAN frames from a socketcan interface.

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.

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 github.com/toitware/can-go/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)

# Packages

No description provided by the author
No description provided by the author

# Functions

CheckBitRangeBigEndian checks that a big-endian bit range fits in the data.
CheckBitRangeLittleEndian checks that a little-endian bit range fits in the data.
CheckValue checks that a value fits in a number of bits.

# Constants

No description provided by the author
CAN format constants.
CAN format constants.

# Structs

Frame represents a CAN frame.

# Interfaces

FrameMarshaler can marshal itself to a CAN frame.
FrameUnmarshaler can unmarshal itself from a CAN frame.
Message is anything that can marshal and unmarshal itself to/from a CAN frame.

# Type aliases

Data holds the data in a CAN frame.