can

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2025 License: MIT Imports: 0 Imported by: 1

README

go-can

github.com/jaster-prj/go-can is a canbus golang library supporting multiple transports (serial adapater, socketcan, etc).

Does not support extended frames, feel free to create a PR

Installation

go get github.com/jaster-prj/go-can

Basic usage

package main

import (
	"log"
	"time"

	"github.com/jaster-prj/go-can"
	"github.com/jaster-prj/go-can/transports"
)

const TestPort string = "/dev/tty.usbserial-14140"

func main() {
	// Configure transport
	tr := &transports.USBCanAnalyzer{
		Port:     TestPort,
		BaudRate: 2000000,
	}

	// Open bus
	bus := can.NewBus(tr)

	if err := bus.Open(); err != nil {
		log.Fatal(err)
	}

	// Write some frames

	log.Println("Write 10 frames")

	for i := 0; i < 9; i++ {
		frm := &can.Frame{
			ArbitrationID: uint32(i),
			Data:          [8]byte{0x00, 0X01, uint8(i)},
		}

		if err := bus.Write(frm); err != nil {
			log.Fatal(err)
		}

		log.Printf("Frame %v writed", frm)
	}

	// Read frames during

	log.Println("Wait a frame (10s timeout)")
	timer := time.NewTimer(10 * time.Second)

	select {
	case frm := <-bus.ReadChan():
		log.Println(frm)
	case <-timer.C:
		log.Println("Timeout")
	}

	if err := bus.Close(); err != nil {
		log.Fatal(err)
	}

	log.Println("done")
}

Documentation

Overview

Package can provide utils to connect to a can bus using hardware adapters system

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus struct {
	// Transport represent the "logical" communication layer
	// which can be socketcan on linux, a serial adapater, or your custom transport
	Transport Transport
}

Bus is the main interface to interact with the Transport

func NewBus

func NewBus(transport Transport) *Bus

NewBus create a new Bus with given transport

func (*Bus) Close

func (bus *Bus) Close() error

Close call Transport#Close

func (*Bus) Open

func (bus *Bus) Open() error

Open call Transport#Open

func (*Bus) ReadChan

func (bus *Bus) ReadChan() chan *Frame

ReadChan call Transport#ReadChan

func (*Bus) Write

func (bus *Bus) Write(frm *Frame) error

Write call Transport#Write

type Frame

type Frame struct {
	// ArbitrationID is the frame identifier
	ArbitrationID uint32

	// DLC represent the size of the data field
	DLC uint8

	// Data is the data to transmit in the frame
	Data [8]byte
}

Frame represent a can frame

func (*Frame) GetData

func (frame *Frame) GetData() []byte

GetData read frame.DLC data from frame.Data

type Transport

type Transport interface {
	// Open a connection
	Open() error

	// Close a connection
	Close() error

	// Write a frame to connection
	Write(*Frame) error

	// ReadChan return the channel for reading frames
	ReadChan() chan *Frame
}

Transport interface can be socketcan, an serial adapter, custom implementation, etc

Directories

Path Synopsis
examples
usbcananalyzer command
Package transports contain code to load a bus interface from different hardware
Package transports contain code to load a bus interface from different hardware

Jump to

Keyboard shortcuts

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