gocan

package module
v0.0.0-...-fe2d772 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 8 Imported by: 16

README

goCAN

A Go linux/windows/osx CAN stack running in userland with low level adapter drivers written from the ground up J2534 support in Windows / Linux

USB Serial

J2534

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 (
	CR = 0x0D
)

Variables

View Source
var (
	Incoming         = CANFrameType{Type: 0, Responses: 0}
	Outgoing         = CANFrameType{Type: 1, Responses: 0}
	ResponseRequired = CANFrameType{Type: 2, Responses: 1}
)

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	Init(context.Context) error
	Name() string
	Recv() <-chan CANFrame
	Send() chan<- CANFrame
	Close() error
	SetFilter([]uint32) error
}

type AdapterConfig

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

type CANFrame

type CANFrame interface {
	// Return frame identifier
	Identifier() uint32
	// Return frame data length
	Length() int
	// Return data of frame
	Data() []byte
	// Return type of frame
	Type() CANFrameType
	// Return fancy string version of frame
	String() string
	// Set response timeour
	SetTimeout(time.Duration)
	// Return response timeout
	Timeout() 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 New

func New(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() error

func (*Client) Poll

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

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

func (*Client) Send

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

Send a CAN Frame

func (*Client) SendAndPoll

func (c *Client) SendAndPoll(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) SetFilter

func (c *Client) SetFilter(filters []uint32) error

func (*Client) Subscribe

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

Subscribe to CAN identifiers and return a message channel

func (*Client) Subscribe2

func (c *Client) Subscribe2(ctx context.Context, identifiers ...uint32) chan CANFrame

Subscribe to CAN identifiers and return a message channel

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

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

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)

func OptOnIncoming

func OptOnIncoming(fn func(CANFrame)) Opts

func OptOnOutgoing

func OptOnOutgoing(fn func(CANFrame)) Opts

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

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

func (*RawCommand) Type

func (r *RawCommand) Type() CANFrameType

type Sub

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

func (*Sub) C

func (s *Sub) C() 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
pkg
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