cantact

package module
v0.0.0-...-194f749 Latest Latest
Warning

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

Go to latest
Published: May 26, 2016 License: BSD-3-Clause Imports: 2 Imported by: 0

README

GoDoc

go-cantact

A package for communicating on Controller Area Network buses using the CANtact hardawre.

Details

This package makes use of the serial library for providing access to CANtact's serial port. It provides functions for sending and receiving frames in the correct format for the device.

Usage

package main

import (
	"log"
	"os"
	"github.com/linklayer/go-cantact"
)

func main() {
	d, err := cantact.NewDevice(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	
	// set bitrate mode to 6, 500 kbps
	d.SetBitrate(6)

	// open connection to CAN bus
	d.Open()

	// send a frame
	data := []byte{0xDE, 0xAD, 0xBE, 0xEF}
	txFrame := cantact.Frame{ID: 0x123, Dlc: len(data), Data: data}
	d.WriteFrame(txFrame)

	// read a frame (blocking call)
	rxFrame, err := d.ReadFrame()
	if err != nil {
		log.Panic(err)
	}
	log.Println(rxFrame)
}

Documentation

Overview

Package cantact implements an interface to the CANtact device, to provide to Controller Area Network (CAN) buses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Device

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

Device is a reference to a hardware CANtact device connected to this computer.

func NewDevice

func NewDevice(portName string) (Device, error)

NewDevice creates a new device.

func (*Device) Close

func (d *Device) Close() error

Close closes the connection to the CAN bus, disabling reception and transmission of CAN frames.

func (*Device) Open

func (d *Device) Open() error

Open opens the connection to the CAN bus, enabling reception and transmission of CAN frames.

func (*Device) ReadFrame

func (d *Device) ReadFrame() (Frame, error)

ReadFrame reads a single Frame from the CAN bus, blocks until a frame is received.

func (*Device) SetBitrate

func (d *Device) SetBitrate(rate int) error

SetBitrate sets the bitrate of the device.

func (*Device) WriteFrame

func (d *Device) WriteFrame(f Frame) error

WriteFrame writes a single Frame to the CAN bus.

type Frame

type Frame struct {
	ID   int
	Dlc  int
	Data []byte
}

Frame is a single CAN frame.

Jump to

Keyboard shortcuts

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