gocan

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 36 Imported by: 6 modules, 25 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goCAN

A Go Linux/Windows/OSX CAN library

Build tags

  • combi - requires libusb-1.0.dll
  • canlib - requires client32.dll
  • canusb - requires canusbdrv(64).dll
  • j2538 - requires vendor DLL to be installed

Usage

Get goCAN

go get github.com/roffe/gocan@latest

Import the package in your imports

import (
	...
	"github.com/roffe/gocan"
)

Showcase

Supported Adapters

USB Serial
libusb
  • CombiAdapter
Canusb DLL

Supported via goCANUSB

Lawicel canusbdrv.dll for both 32 and 64bit is supported

Kvaser Canlib

Supported via goCANlib, Tested with the following adapters

J2534

Support for both 32 & 64bit DLL's. Your GOARCH will controll which it will look for.

Do note that not all vendors provide 64bit DLL's so you migh need to build your software with GOARCH=386 to be able to use the j2534 DLL

Most adapters that comes with a J2534 DLL will work. The list given is just ones verified to work.

Windows
Linux
Other
  • SocketCAN
  • OBDX Pro GT BLE & Wifi
  • Combiadapter via libusb

Documentation

Index

Constants

View Source
const (
	OBDLinkSX = "OBDLink SX"
	OBDLinkEX = "OBDLink EX"
	STN1170   = "STN1170"
	STN2120   = "STN2120"
)
View Source
const (
	SystemMsg uint32 = 0x80000000 + iota
	SystemMsgError
	SystemMsgUnrecoverableError
	SystemMsgWBLReading
	SystemMsgDataResponse
	SystemMsgDataRequest
	SystemMsgWriteResponse
	SystemMsgUnknown
)

We have 3 bits allowing 8 different system messages hidden in a 29bit can id stored in a uint32

Variables

View Source
var (
	Incoming         = CANFrameType{Type: 0, Responses: 0}
	Outgoing         = CANFrameType{Type: 1, Responses: 0}
	ResponseRequired = CANFrameType{Type: 2, Responses: 1} // Used for ELM and STN adapters to signal we want the adapter to wait for a response
)
View Source
var (
	ErrDroppedFrame           = errors.New("adapter incoming channel full")
	ErrFramhandlerRegisterSub = errors.New("failed to register subscription, framehandler is full")
)

Functions

func FindDevices added in v1.1.3

func FindDevices() (dev []string)

func GetAdapterMap added in v1.0.8

func GetAdapterMap() map[string]*AdapterInfo

func IsRecoverable added in v1.0.1

func IsRecoverable(err error) bool

IsRecoverable checks if error is an instance of `unrecoverableError`

func List added in v1.0.8

func List() []string

func NewGRPCClient added in v1.0.7

func NewGRPCClient() (*grpc.ClientConn, proto.GocanClient, error)

func NewSTN added in v1.1.3

func NewSTN(name string) func(cfg *AdapterConfig) (Adapter, error)

func NewSocketCAN added in v1.1.3

func NewSocketCAN(cfg *gocan.AdapterConfig) (gocan.Adapter, error)

func NewSocketCANFromDevName added in v1.1.3

func NewSocketCANFromDevName(dev string) func(cfg *gocan.AdapterConfig) (gocan.Adapter, error)

func NewTxbridge added in v1.0.7

func NewTxbridge(name string) func(cfg *AdapterConfig) (Adapter, error)

func RegisterAdapter added in v1.0.7

func RegisterAdapter(adapter *AdapterInfo) error

func Unrecoverable added in v1.0.1

func Unrecoverable(err error) error

Unrecoverable wraps an error in `unrecoverableError` struct

Types

type Adapter

type Adapter interface {
	Name() string
	Open(context.Context) error
	Close() error
	Send() chan<- *CANFrame
	Recv() <-chan *CANFrame
	Err() <-chan error
}

func NewAdapter added in v1.0.7

func NewAdapter(adapterName string, cfg *AdapterConfig) (Adapter, error)

func NewCanusbVCP added in v1.0.7

func NewCanusbVCP(cfg *AdapterConfig) (Adapter, error)

func NewJust4Trionic added in v1.0.7

func NewJust4Trionic(cfg *AdapterConfig) (Adapter, error)

func NewMock added in v1.0.7

func NewMock(name string, cfg *AdapterConfig) (Adapter, error)

Create a new Mock adapter used for testing

func NewOBDXProWifi added in v1.0.7

func NewOBDXProWifi(cfg *AdapterConfig) (Adapter, error)

func NewSLCan added in v1.0.7

func NewSLCan(cfg *AdapterConfig) (Adapter, error)

func NewTemplate added in v1.0.7

func NewTemplate(name string, cfg *AdapterConfig) (Adapter, error)

func NewYACA added in v1.0.7

func NewYACA(cfg *AdapterConfig) (Adapter, error)

type AdapterCapabilities added in v1.0.7

type AdapterCapabilities struct {
	HSCAN bool
	SWCAN bool
	KLine bool
}

type AdapterConfig

type AdapterConfig struct {
	Debug                  bool
	Port                   string
	PortBaudrate           int
	CANRate                float64
	CANFilter              []uint32
	UseExtendedID          bool // only used for j2534 when setting upp frame filters
	PrintVersion           bool
	OnMessage              func(string)
	MinimumFirmwareVersion string
}

type AdapterInfo added in v1.0.7

type AdapterInfo struct {
	Name               string
	Description        string
	RequiresSerialPort bool
	Capabilities       AdapterCapabilities
	New                func(*AdapterConfig) (Adapter, error)
}

func ListAdapters added in v1.0.7

func ListAdapters() []AdapterInfo

type BaseAdapter added in v1.0.7

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

func NewBaseAdapter added in v1.0.7

func NewBaseAdapter(name string, cfg *AdapterConfig) BaseAdapter

func (*BaseAdapter) Close added in v1.0.7

func (base *BaseAdapter) Close()

func (*BaseAdapter) Err added in v1.0.7

func (base *BaseAdapter) Err() <-chan error

func (*BaseAdapter) Name added in v1.0.7

func (base *BaseAdapter) Name() string

func (*BaseAdapter) Recv added in v1.0.7

func (base *BaseAdapter) Recv() <-chan *CANFrame

func (*BaseAdapter) Send added in v1.0.7

func (base *BaseAdapter) Send() chan<- *CANFrame

func (*BaseAdapter) SetError added in v1.0.8

func (base *BaseAdapter) SetError(err error)

type CANFrame

type CANFrame struct {
	Identifier uint32
	Extended   bool
	RTR        bool
	Data       []byte
	FrameType  CANFrameType
	Timeout    time.Duration
}

func NewExtendedFrame added in v1.0.7

func NewExtendedFrame(identifier uint32, data []byte, frameType CANFrameType) *CANFrame

NewExtendedFrame creates a new CANFrame and copies the data slice

func NewFrame

func NewFrame(identifier uint32, data []byte, frameType CANFrameType) *CANFrame

NewFrame creates a new CANFrame and copies the data slice

func (*CANFrame) Bytes added in v1.0.7

func (f *CANFrame) Bytes() []byte

returns the frame as a byte slice, 4 bytes for the identifier, 1 byte for the length and the data if holding more than 8 bytes of data, it will be truncated

func (*CANFrame) ColorString added in v1.0.7

func (f *CANFrame) ColorString() string

func (*CANFrame) Length

func (f *CANFrame) Length() int

Returns the length of the data (DLC)

func (*CANFrame) String

func (f *CANFrame) String() string

type CANFrameType

type CANFrameType struct {
	Type      int
	Responses int
}

type CanusbVCP added in v1.0.7

type CanusbVCP struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*CanusbVCP) Close added in v1.0.7

func (cu *CanusbVCP) Close() error

func (*CanusbVCP) Open added in v1.0.7

func (cu *CanusbVCP) Open(ctx context.Context) error

func (*CanusbVCP) SetFilter added in v1.0.7

func (cu *CanusbVCP) SetFilter(filters []uint32) error

type Client

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

func New added in v1.0.7

func New(ctx context.Context, adapterName string, cfg *AdapterConfig) (*Client, error)

func NewWithAdapter added in v1.0.8

func NewWithAdapter(ctx context.Context, adapter Adapter) (*Client, error)

func NewWithOpts

func NewWithOpts(ctx context.Context, adapter Adapter, opts ...Opts) (*Client, error)

func (*Client) Adapter

func (c *Client) Adapter() Adapter

func (*Client) Close

func (c *Client) Close() (err error)

func (*Client) Err

func (c *Client) Err() <-chan error

func (*Client) Send

func (c *Client) Send(identifier uint32, data []byte, f CANFrameType) error

Shortcommand to send a standard 11bit frame

func (*Client) SendAndWait

func (c *Client) SendAndWait(ctx context.Context, frame *CANFrame, timeout time.Duration, identifiers ...uint32) (*CANFrame, error)

Send and wait up to <timeout> for a answer on given identifiers

func (*Client) SendExtended added in v1.0.7

func (c *Client) SendExtended(identifier uint32, data []byte, f CANFrameType) error

Shortcommand to send a extended 29bit frame

func (*Client) SendFrame

func (c *Client) SendFrame(msg *CANFrame) error

Send a CAN Frame

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, identifiers ...uint32) *Subscriber

Subscribe to CAN identifiers and return a message channel

func (*Client) SubscribeChan

func (c *Client) SubscribeChan(ctx context.Context, channel chan *CANFrame, identifiers ...uint32) *Subscriber

Subscribe to CAN identifiers and return a message channel

func (*Client) SubscribeFunc

func (c *Client) SubscribeFunc(ctx context.Context, f func(*CANFrame), identifiers ...uint32) *Subscriber

func (*Client) Wait

func (c *Client) Wait(ctx context.Context, timeout time.Duration, identifiers ...uint32) (*CANFrame, error)

Wait for a certain CAN identifier for up to <timeout>

type GWClient added in v1.0.7

type GWClient struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func NewGWClient added in v1.0.7

func NewGWClient(adapterName string, cfg *AdapterConfig) (*GWClient, error)

func (*GWClient) Close added in v1.0.7

func (c *GWClient) Close() (err error)

func (*GWClient) Open added in v1.0.7

func (c *GWClient) Open(gctx context.Context) error

func (*GWClient) SetFilter added in v1.0.7

func (c *GWClient) SetFilter([]uint32) error

type Just4Trionic added in v1.0.7

type Just4Trionic struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*Just4Trionic) Close added in v1.0.7

func (a *Just4Trionic) Close() error

func (*Just4Trionic) Open added in v1.0.7

func (a *Just4Trionic) Open(ctx context.Context) error

func (*Just4Trionic) SetFilter added in v1.0.7

func (a *Just4Trionic) SetFilter(filters []uint32) error

type Mock added in v1.0.7

type Mock struct {
	BaseAdapter
}

func (*Mock) Close added in v1.0.7

func (v *Mock) Close() error

func (*Mock) Open added in v1.0.7

func (v *Mock) Open(ctx context.Context) error

type OBDXProWifi added in v1.0.7

type OBDXProWifi struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*OBDXProWifi) Close added in v1.0.7

func (a *OBDXProWifi) Close() error

func (*OBDXProWifi) Open added in v1.0.7

func (a *OBDXProWifi) Open(ctx context.Context) error

func (*OBDXProWifi) SetFilter added in v1.0.7

func (a *OBDXProWifi) SetFilter(filters []uint32) error

type Opts

type Opts func(*Client)

type SLCan added in v1.0.7

type SLCan struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*SLCan) Close added in v1.0.7

func (sl *SLCan) Close() error

func (*SLCan) Open added in v1.0.7

func (sl *SLCan) Open(ctx context.Context) error

func (*SLCan) SetFilter added in v1.0.7

func (sl *SLCan) SetFilter(filters []uint32) error

type STN added in v1.1.3

type STN struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*STN) Close added in v1.1.3

func (stn *STN) Close() error

func (*STN) Open added in v1.1.3

func (stn *STN) Open(ctx context.Context) error

func (*STN) SetFilter added in v1.1.3

func (stn *STN) SetFilter(filters []uint32) error

type SocketCAN added in v1.1.3

type SocketCAN struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*SocketCAN) Close added in v1.1.3

func (a *SocketCAN) Close() error

func (*SocketCAN) Open added in v1.1.3

func (a *SocketCAN) Open(ctx context.Context) error

func (*SocketCAN) SetFilter added in v1.1.3

func (a *SocketCAN) SetFilter(uint32s []uint32) error

type Subscriber added in v1.0.7

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

func (*Subscriber) Chan added in v1.0.7

func (s *Subscriber) Chan() <-chan *CANFrame

func (*Subscriber) Close added in v1.0.7

func (s *Subscriber) Close()

func (*Subscriber) Wait added in v1.0.8

func (s *Subscriber) Wait(ctx context.Context, timeout time.Duration) (*CANFrame, error)

type Template added in v1.0.7

type Template struct {
	BaseAdapter
}

func (*Template) Close added in v1.0.7

func (a *Template) Close() error

func (*Template) Open added in v1.0.7

func (a *Template) Open(ctx context.Context) error

func (*Template) SetFilter added in v1.0.7

func (a *Template) SetFilter(filters []uint32) error

type Txbridge added in v1.0.7

type Txbridge struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*Txbridge) Close added in v1.0.7

func (tx *Txbridge) Close() error

func (*Txbridge) Open added in v1.0.7

func (tx *Txbridge) Open(ctx context.Context) error

func (*Txbridge) SetFilter added in v1.0.7

func (tx *Txbridge) SetFilter(filters []uint32) error

type YACA added in v1.0.7

type YACA struct {
	BaseAdapter
	// contains filtered or unexported fields
}

func (*YACA) Close added in v1.0.7

func (ya *YACA) Close() error

func (*YACA) Open added in v1.0.7

func (ya *YACA) Open(ctx context.Context) error

func (*YACA) SetFilter added in v1.0.7

func (ya *YACA) SetFilter(filters []uint32) error

Directories

Path Synopsis
cmd
cangateway command
testsuite command
examples
pingpong/client command
pingpong/server command
pkg
dvi
gmlan
see GMW3110 gmw3110-2010.pdf
see GMW3110 gmw3110-2010.pdf

Jump to

Keyboard shortcuts

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