Documentation
¶
Overview ¶
Package canbus is built around the Channel structure, which represents a single canbus channel for sending/receiving CAN frames.
CAN bus (Controller Area Network) is a communication protocol widely used in marine NMEA 2000 networks and automotive systems. This package provides two implementations:
- usbCANChannel: for USB-to-CAN adapter dongles that communicate via serial port
- socketCANChannel: for Linux SocketCAN interfaces (e.g., MCP2515 SPI-based CAN controllers)
Both implementations satisfy the Interface type and handle the low-level framing and frame parsing required to send and receive raw CAN frames.
Index ¶
- func NewSocketCAN(log *slog.Logger, iface string) *socketCANChannel
- func NewUSBCAN(log *slog.Logger, port string) *usbCANChannel
- func RunSocketCAN(ctx context.Context, log *slog.Logger, iface string, handler func(can.Frame)) error
- func RunUSBCAN(ctx context.Context, log *slog.Logger, port string, handler func(can.Frame)) error
- type Interface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewSocketCAN ¶
NewSocketCAN creates a SocketCAN channel for the given Linux CAN interface name. The channel is not opened, and no handler is registered, until Run() is called.
func NewUSBCAN ¶
NewUSBCAN creates a USB-CAN channel for the given serial port. The channel is not opened, and no handler is registered, until Run() is called.
func RunSocketCAN ¶
func RunSocketCAN(ctx context.Context, log *slog.Logger, iface string, handler func(can.Frame)) error
RunSocketCAN creates a SocketCAN channel for the given interface and runs it, calling handler for each received CAN frame. The interface must already be configured and up. Blocks until error or context done.
Types ¶
type Interface ¶
type Interface interface {
// Run opens the CAN bus channel and starts listening for incoming frames,
// delivering each one to handler. This method blocks until the context is
// cancelled or an unrecoverable error occurs. It is the caller's
// responsibility to invoke Close() after Run() returns.
Run(ctx context.Context, handler func(can.Frame)) error
// Close shuts down the CAN bus channel, releasing any underlying resources
// (serial ports, sockets, etc.). It is safe to call Close() even if Run() was never called.
Close() error
// WriteFrame sends a single CAN frame out on the bus. The frame's ID and data payload
// are encoded according to the transport's wire format (USB-CAN protocol or SocketCAN).
WriteFrame(frame can.Frame) error
}
Interface is a basic interface for a CANbus implementation. Any CAN bus transport (USB-CAN dongle, SocketCAN, etc.) must implement these three methods to be usable as a CAN channel in the system.
Implementations are responsible for:
- Opening and configuring the underlying hardware/OS interface
- Continuously reading incoming CAN frames and dispatching them to a handler callback
- Providing the ability to send outgoing CAN frames
- Cleanly shutting down when requested