socketcan

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

socketcan-go

socketcan-go makes use of the Linux SocketCAN abstraction for CAN communication.

Realize:

  • Up/Down
  • Set bitrate
  • Set hardware filters
  • Loopback mode

Full Demo:

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/lion187chen/socketcan-go"
	"github.com/lion187chen/socketcan-go/canframe"
)

var lookback bool = true

var wg sync.WaitGroup

func main() {
	// Create a new CAN, the can interface name is "can0".
	can := new(socketcan.Can).Init("can0")
	// Set down to set bitrate.
	can.SetDown()
	// Test if CAN is down.
	fmt.Println(can.IsUp())
	// To set bitrate, we must down the CAN interface first.
	can.SetBitrate(100000)
	// Up the CAN to set filter or transmit CAN frames.
	can.SetUp()
	// To see CAN bitrate.
	fmt.Println(can.Bitrate())
	// Test if CAN is up.
	fmt.Println(can.IsUp())
	// Dial() will open a CAN socket and bind it to the given interface in Init().
	can.Dial()

	// After Dial(), we can set CAN hardware filters.
	var filters []socketcan.Filter = []socketcan.Filter{
		socketcan.NewExtFilter(0x20),
		socketcan.NewExtFilter(0x21),
		socketcan.NewExtFilter(0x40),
	}
	can.SetFilter(filters)

	// Or set the CAN in lookback mode.
	// We will stop lookback mode in EchoTsk.
	var sendFrame canframe.Frame = canframe.Frame{
		ID:         0x20,
		Data:       []byte{0x01, 0x02, 0x03, 0x4, 0x05, 0x06, 0x07, 0x08},
		IsExtended: true,
		IsRemote:   false,
		IsError:    false,
	}
	can.SetLoopback(lookback)
	// Send a frame. We will receive it immediately because we are in the lookback mode.
	n, err := can.SendFrame(&sendFrame)
	if err != nil {
		fmt.Println(n, err)
	}
	// Start a echo goroutine.
	wg.Add(1)
	go Echoroutine(can)

	// Wait 1 minute for CAN echo test.
	time.Sleep(1 * time.Minute)
	// Close the CAN.
	can.Close()
	// After the close operation, can.RcvFrame() will return an error, so we can exit from Echoroutine().
	// Wait until Echoroutine() done.
	wg.Wait()
}

func Echoroutine(can *socketcan.Can) {
	var err error = nil
	var frame canframe.Frame
	for err == nil {
		// can.RcvFrame() will block until new datas arrived.
		frame, err = can.RcvFrame()
		if lookback {
			lookback = false
			can.SetLoopback(lookback)
		}

		fmt.Println(frame)
		n, err := can.SendFrame(&frame)
		if err != nil {
			fmt.Println(n, err)
		}
	}
	fmt.Println("Echoroutine Exit:", err)
	wg.Done()
}

Some codes were referenced:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Can

type Can struct {
	// contains filtered or unexported fields
}

Can

func (*Can) Bitrate

func (my *Can) Bitrate() (uint32, error)

Get current bitrate.

func (*Can) Close

func (my *Can) Close() error

After all, we must close the CAN.

func (*Can) Dial

func (my *Can) Dial() (err error)

Dial() will open a CAN socket and bind it to the given interface in Init().

func (*Can) Info

func (my *Can) Info() (Info, error)

Get CAN interface's info.

func (*Can) Init

func (my *Can) Init(ifName string) *Can

ifName is the CAN interface name, such as "can0", "can1"...

func (*Can) IsUp

func (my *Can) IsUp() (bool, error)

func (*Can) RcvFrame

func (my *Can) RcvFrame() (canframe.Frame, error)

RcvFrame() will block until new datas arrived or a error occured.

func (*Can) SendFrame

func (my *Can) SendFrame(f *canframe.Frame) (n int, err error)

SendFrame will block until write done or a error occured.

func (*Can) SetBitrate

func (my *Can) SetBitrate(bitrate uint32) error

To set bitrate, you must down the CAN interface first.

func (*Can) SetDown

func (my *Can) SetDown() error

Down th CAN interface.

func (*Can) SetFilter

func (my *Can) SetFilter(fs []Filter) error

You can use NewStdFilter, NewStdInvFilter(), NewExtFilter(), NewExtInvFilter() help functions to create []Filter.

func (*Can) SetListenOnlyMode

func (my *Can) SetListenOnlyMode(mode bool) error

Set the CAN in listen only mode.

func (*Can) SetLoopback

func (my *Can) SetLoopback(enable bool) error

True for lookback mode.

func (*Can) SetRecvTimeout

func (my *Can) SetRecvTimeout(timeout time.Duration) error

Set CAN receive timeout.

func (*Can) SetSendTimeout

func (my *Can) SetSendTimeout(timeout time.Duration) error

Set CAN send timeout.

func (*Can) SetUp

func (my *Can) SetUp() error

Up the CAN interface.

type CanBitTiming

type CanBitTiming unix.CANBitTiming

type CanBitTimingConst

type CanBitTimingConst unix.CANBitTimingConst

type CanBusErrCounters

type CanBusErrCounters unix.CANBusErrorCounters

type CanClock

type CanClock unix.CANClock

type CanCtrlMode

type CanCtrlMode unix.CANCtrlMode

type CanDevStats

type CanDevStats unix.CANDeviceStats

type Filter

type Filter struct {
	Id   uint32
	Mask uint32
}

func NewExtFilter

func NewExtFilter(id uint32) Filter

func NewExtInvFilter

func NewExtInvFilter(id uint32) Filter

func NewStdFilter

func NewStdFilter(id uint32) Filter

func NewStdInvFilter

func NewStdInvFilter(id uint32) Filter

type Info

type Info struct {
	DevName        string
	BitTiming      CanBitTiming
	BitTimingConst CanBitTimingConst
	Clock          CanClock
	CtrlMode       CanCtrlMode
	ErrCounters    CanBusErrCounters
	DevStats       CanDevStats
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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