gocan

package module
v1.0.0 Latest Latest
Warning

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

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

Kvaser Canlib

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`,
			PortBaudrate: 0,
			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
	SystemMsgDebug
	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}
	// Used for ELM and STN adapters to signal we want the adapter to wait for a response
	ResponseRequired = CANFrameType{Type: 2, Responses: 1}
)

Functions

This section is empty.

Types

type Adapter

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

type AdapterConfig

type AdapterConfig struct {
	Debug                  bool
	Port                   string
	PortBaudrate           int
	CANRate                float64
	CANFilter              []uint32
	UseExtendedID          bool
	PrintVersion           bool
	OnMessage              func(string)
	OnError                func(error)
	MinimumFirmwareVersion string
}

type CANFrame

type CANFrame interface {
	// Return frame identifier
	Identifier() uint32
	// Return frame data length (DLC)
	Length() int
	// Return data of frame
	Data() []byte
	// Return type of frame
	Type() CANFrameType
	// Return fancy string version of frame
	String() string
	// Set response timeout
	SetTimeout(time.Duration)
	// Return response timeout
	GetTimeout() time.Duration
}

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(msg CANFrame) error

Send a CAN 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) SendFrame

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

Shortcommand to send a standard 11bit 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 Frame

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

func NewFrame

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

func (*Frame) ColorString

func (f *Frame) ColorString() string

func (*Frame) Data

func (f *Frame) Data() []byte

func (*Frame) GetTimeout

func (f *Frame) GetTimeout() time.Duration

func (*Frame) Identifier

func (f *Frame) Identifier() uint32

func (*Frame) Length

func (f *Frame) Length() int

func (*Frame) SetTimeout

func (f *Frame) SetTimeout(t time.Duration)

func (*Frame) SetType

func (f *Frame) SetType(t CANFrameType)

func (*Frame) String

func (f *Frame) String() string

func (*Frame) Type

func (f *Frame) Type() CANFrameType

type FrameHandler

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

FrameHandler takes care of faning out incoming frames to any subs

func (*FrameHandler) Close

func (h *FrameHandler) Close()

type Opts

type Opts func(*Client)

type RawCommand

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

func NewRawCommand

func NewRawCommand(data string) *RawCommand

func (*RawCommand) Data

func (r *RawCommand) Data() []byte

func (*RawCommand) GetTimeout

func (f *RawCommand) GetTimeout() time.Duration

func (*RawCommand) Identifier

func (r *RawCommand) Identifier() uint32

func (*RawCommand) Length

func (r *RawCommand) Length() int

func (*RawCommand) SetTimeout

func (f *RawCommand) SetTimeout(t time.Duration)

func (*RawCommand) String

func (r *RawCommand) String() string

func (*RawCommand) Type

func (r *RawCommand) Type() CANFrameType

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