can

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 5 Imported by: 0

README

🔌 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

Use DBC file to parse OBC CanFd data
func main() {
	dbcFile = "/path/xxxx.dbc"
	obcCanFd, err := obc.NewObcCanFD(dbcFile)

	canID = uint32(298)
	canDataHexStr = "1A02A300C23290802"
	params := obcCanFd.ParseCanHexStr(canID, canDataHexStr)
	fmt.Printf("canID(%d), canData(%v)", canID, params)
}
Setting up a CAN interface
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.

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/cleey/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)

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

Documentation

Overview

Package can provides primitives for working with CAN.

See: https://en.wikipedia.org/wiki/CAN_bus

Index

Constants

View Source
const (
	MaxID         = 0x7ff
	MaxExtendedID = 0x1fffffff
)

CAN format constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Frame

type Frame struct {
	// ID is the CAN ID
	ID uint32
	// Length is the number of bytes of data in the frame.
	Length uint8
	// Data is the frame data.
	Data Data
	// IsRemote is true for remote frames.
	IsRemote bool
	// IsExtended is true for extended frames, i.e. frames with 29-bit IDs.
	IsExtended bool
}

Frame represents a CAN frame.

A Frame is intentionally designed to fit into 16 bytes on common architectures and is therefore amenable to pass-by-value and judicious copying.

func (Frame) JSON

func (f Frame) JSON() string

JSON returns the JSON-encoding of f, using hex-encoding for the data.

Examples:

{"id":32,"data":"0102030405060708"}
{"id":32,"extended":true,"remote":true,"length":4}

func (Frame) MarshalJSON

func (f Frame) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON-encoding of f, using hex-encoding for the data.

See JSON for an example of the JSON schema.

func (Frame) String

func (f Frame) String() string

String returns an ASCII representation the CAN frame.

Format:

([0-9A-F]{3}|[0-9A-F]{3})#(R[0-8]?|[0-9A-F]{0,16})

The format is compatible with the candump(1) log file format.

func (*Frame) UnmarshalJSON

func (f *Frame) UnmarshalJSON(jsonData []byte) error

UnmarshalJSON sets *f using the provided JSON-encoded values.

See MarshalJSON for an example of the expected JSON schema.

The result should be checked with Validate to guard against invalid JSON data.

func (*Frame) UnmarshalString

func (f *Frame) UnmarshalString(s string) error

UnmarshalString sets *f using the provided ASCII representation of a Frame.

func (*Frame) Validate

func (f *Frame) Validate() error

Validate returns an error if the Frame is not a valid CAN frame.

type FrameMarshaler

type FrameMarshaler interface {
	MarshalFrame() (Frame, error)
}

FrameMarshaler can marshal itself to a CAN frame.

type FrameUnmarshaler

type FrameUnmarshaler interface {
	UnmarshalFrame(Frame) error
}

FrameUnmarshaler can unmarshal itself from a CAN frame.

type Message

type Message interface {
	FrameMarshaler
	FrameUnmarshaler
}

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

Directories

Path Synopsis
cmd
pkg
clock
Package clock provides primitives for mocking time.
Package clock provides primitives for mocking time.
dbc
Package dbc provides primitives for parsing, formatting and linting DBC files.
Package dbc provides primitives for parsing, formatting and linting DBC files.
generated
Package generated provides primitives for working with code-generated CAN messages.
Package generated provides primitives for working with code-generated CAN messages.
mocks/gen/mockcanrunner
Package mockcanrunner is a generated GoMock package.
Package mockcanrunner is a generated GoMock package.
mocks/gen/mockclock
Package mockclock is a generated GoMock package.
Package mockclock is a generated GoMock package.
mocks/gen/mocksocketcan
Package mocksocketcan is a generated GoMock package.
Package mocksocketcan is a generated GoMock package.
obc
reinterpret
Package reinterpret provides primitives for reinterpreting arbitrary-length values as signed or unsigned.
Package reinterpret provides primitives for reinterpreting arbitrary-length values as signed or unsigned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL