gocan

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: MIT Imports: 14 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

USB Serial

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

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

Showcase

Saab CAN flasher txlogger

Example

package main

import (
	"context"
	"log"

	"github.com/roffe/gocan"
	"github.com/roffe/gocan/adapter"
	"github.com/roffe/gocan/pkg/gmlan"
)

func main() {
	dev, err := adapter.New(
		"J2534",
		&gocan.AdapterConfig{
			Port:         `C:\Program Files (x86)\Drew Technologies, Inc\J2534\MongoosePro GM II\monpa432.dll`,
			CANRate:      33.3,
			CANFilter:    []uint32{0x64F},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	c, err := gocan.New(ctx, dev)
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	gm := gmlan.New(c, 0x24F, 0x64F)

	gm.TesterPresentNoResponseAllowed()

	if err := gm.InitiateDiagnosticOperation(ctx, 0x02); err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := gm.ReturnToNormalMode(ctx); err != nil {
			log.Println(err)
		}
	}()

	if err := gm.DisableNormalCommunication(ctx); err != nil {
		log.Fatal(err)
	}

	vin, err := gm.ReadDataByIdentifierString(ctx, 0x90)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("VIN:", vin)
}
> $env:CGO_ENABLED=1; $env:GOARCH=386; go run .\examples\gmlan\read_vin_from_uec\main.go 
VIN: YS3FB45F231027975

Documentation

Index

Constants

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

Functions

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

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
	PrintVersion           bool
	OnMessage              func(string)
	MinimumFirmwareVersion string
}

type AdapterInfo added in v1.0.7

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

func ListAdapters added in v1.0.7

func ListAdapters() []AdapterInfo

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

func (*CANFrame) ColorString added in v1.0.7

func (f *CANFrame) ColorString() string

func (*CANFrame) Length

func (f *CANFrame) Length() int

func (*CANFrame) String

func (f *CANFrame) String() string

type CANFrameType

type CANFrameType struct {
	Type      int
	Responses int
}

func (*CANFrameType) GetResponseCount

func (c *CANFrameType) GetResponseCount() int

func (*CANFrameType) SetResponseCount

func (c *CANFrameType) SetResponseCount(no int)

type Client

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

func NewClient

func NewClient(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) *Sub

Subscribe to CAN identifiers and return a message channel

func (*Client) SubscribeChan

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

Subscribe to CAN identifiers and return a message channel

func (*Client) SubscribeFunc

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

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 Handler added in v1.1.1

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

Handler takes care of faning out incoming frames to any subs

func (*Handler) Close added in v1.1.1

func (h *Handler) Close()

type Opts

type Opts func(*Client)

type Sub

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

func (*Sub) Chan

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

func (*Sub) Close

func (s *Sub) Close()

func (*Sub) Wait

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

Directories

Path Synopsis
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