canusb

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 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.

Documentation

Index

Constants

View Source
const (
	// Filter mask settings
	ACCEPTANCE_CODE_ALL uint32 = 0x00000000
	ACCEPTANCE_MASK_ALL uint32 = 0xFFFFFFFF
)
View Source
const (
	// Status bits
	CANSTATUS_RECEIVE_FIFO_FULL  = 0x01
	CANSTATUS_TRANSMIT_FIFO_FULL = 0x02
	CANSTATUS_ERROR_WARNING      = 0x04
	CANSTATUS_DATA_OVERRUN       = 0x08
	CANSTATUS_ERROR_PASSIVE      = 0x20
	CANSTATUS_ARBITRATION_LOST   = 0x40
	CANSTATUS_BUS_ERROR          = 0x80
)
View Source
const MAX_DLC = 8

Variables

View Source
var (
	ErrOK               = &Error{ERROR_CANUSB_OK, "OK"}
	ErrGeneral          = &Error{ERROR_CANUSB_GENERAL, "general error"}
	ErrOpenSubsystem    = &Error{ERROR_CANUSB_OPEN_SUBSYSTEM, "open subsystem error"}
	ErrCommandSubsystem = &Error{ERROR_CANUSB_COMMAND_SUBSYSTEM, "command subsystem error"}
	ErrNotOpen          = &Error{ERROR_CANUSB_NOT_OPEN, "not open error"}
	ErrTxFifoFull       = &Error{ERROR_CANUSB_TX_FIFO_FULL, "transmit FIFO full"}
	ErrInvalidParam     = &Error{ERROR_CANUSB_INVALID_PARAM, "invalid parameter"}
	ErrNoMessage        = &Error{ERROR_CANUSB_NO_MESSAGE, "no message"}
	ErrMemoryError      = &Error{ERROR_CANUSB_MEMORY_ERROR, "memory error"}
	ErrNoDevice         = &Error{ERROR_CANUSB_NO_DEVICE, "no device"}
	ErrTimeout          = &Error{ERROR_CANUSB_TIMEOUT, "timeout"}
	ErrInvalidHardware  = &Error{ERROR_CANUSB_INVALID_HARDWARE, "invalid hardware"}

	ErrNoDeviceAvailable  = &Error{-100, "no device available"}
	ErrMessageDataToLarge = &Error{-101, "message data to large"}
	ErrMessageDataSize    = &Error{-102, "message data size missmatch"}
	ErrMessageDataToSmall = &Error{-103, "message data to small"}

	ErrReceiveFifoFull  = &Error{CANSTATUS_RECEIVE_FIFO_FULL, "receive FIFO full"}
	ErrTransmitFifoFull = &Error{CANSTATUS_TRANSMIT_FIFO_FULL, "transmit FIFO full"}
	ErrWarning          = &Error{CANSTATUS_ERROR_WARNING, "error warning (EI)"}
	ErrDataOverrun      = &Error{CANSTATUS_DATA_OVERRUN, "data overrun (DOI)"}
	ErrErrorPassive     = &Error{CANSTATUS_ERROR_PASSIVE, "error passive (EPI)"}
	ErrArbitrationLost  = &Error{CANSTATUS_ARBITRATION_LOST, "arbitration lost (ALI)"}
	ErrBussError        = &Error{CANSTATUS_BUS_ERROR, "bus error (BEI)"}
)
View Source
var InitErr error

Functions

func GetAdapters

func GetAdapters() (adapters []string, err error)

Get all found adapters that is connected to this machine.

func GetFirstAdapter

func GetFirstAdapter() (int, string, error)

Get the first found adapter that is connected to this machine.

Returns the number of adapters found and the serial number of the first adapter.

func GetNextAdapter

func GetNextAdapter() (string, error)

Get the found adapter(s) in turn that is connected to this machine.

Returns the serial number of the next adapter.

func Init

func Init() error

func IsNoMessage

func IsNoMessage(err error) bool

func NewError

func NewError[T ~int | ~int32 | int64](code T) error

Types

type CANHANDLE

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

CANHANDLE is a handle to a CANUSB device

func Open

func Open(szID, szBitrate string, code, mask uint32, flags OpenFlag) (*CANHANDLE, error)

Open CAN interface to device

Returs handle to device if open was successfull or zero or negative error code on falure.

szID

Serial number for adapter or emptry string to open the first found.

szBitrate

"10" for 10kbps
"20" for 20kbps
"50" for 50kbps
"100" for 100kbps
"250" for 250kbps
"500" for 500kbps
"800" for 800kbps
"1000" for 1Mbps

or

btr0:btr1 pair  ex. "0x03:0x1c" or 3:28

acceptance_code

Set to ACCEPTANCE_CODE_ALL to get all messages.

acceptance_mask

Set to ACCEPTANCE_MASK_ALL to get all messages.

flags

FLAG_TIMESTAMP - Timestamp will be set by adapter.
FLAG_QUEUE_REPLACE - If input queue is full remove oldest message and insert new message.
FLAG_BLOCK - Block receive/transmit
FLAG_SLOW - Check ACK/NACK's
FLAG_NO_LOCAL_SEND - Don't send transmited frames on other local channels for the same interface

func (*CANHANDLE) Close

func (ch *CANHANDLE) Close() error

Close channel

func (*CANHANDLE) Flush

func (ch *CANHANDLE) Flush(flags FlushFlag) error

Flush output buffer on channel

If flushflags is set to FLUSH_DONTWAIT the queue is just emptied and there will be no wait for any frames in it to be sent

func (*CANHANDLE) GetStatistics

func (ch *CANHANDLE) GetStatistics() (*CANUSBStatistics, error)

Get statistics for channel

func (*CANHANDLE) Read

func (ch *CANHANDLE) Read() (msg *CANMsg, err error)

Read message from channel

func (*CANHANDLE) ReadEx

func (ch *CANHANDLE) ReadEx() (msg *CANMsgEx, data []byte, err error)

Read message from channel

This is a version without a data-array in the structure

func (*CANHANDLE) ReadFirst

func (ch *CANHANDLE) ReadFirst(id uint32, flags MessageFlag) (msg *CANMsg, err error)

Read message with id which satisfy flags.

func (*CANHANDLE) SetAsyncReceiveCallback

func (ch *CANHANDLE) SetAsyncReceiveCallback(fn CallbackFunc) error

Set a receive callback function. Set the callback to nil to reset it. the callback will be called in a separate go routine to prevent blocking the receive loop if your callback takes long to execute. this will allow the receive loop to continue receiving messages while the callback is processing the message.

func (*CANHANDLE) SetReceiveCallback

func (ch *CANHANDLE) SetReceiveCallback(fn CallbackFunc) error

Set a receive callback function. Set the callback to nil to reset it.

The callback will be called in a separate goroutine using a buffered channel to prevent blocking the device.

func (*CANHANDLE) SetTimeouts

func (ch *CANHANDLE) SetTimeouts(receiveTimeout, sendTimeout uint32) error

Set timeouts used for blocking calls for channel.

func (*CANHANDLE) Status

func (ch *CANHANDLE) Status() error

Get Adaper status for channel

func (*CANHANDLE) VersionInfo

func (ch *CANHANDLE) VersionInfo() (string, error)

Get hardware/firmware and driver version for channel

func (*CANHANDLE) Write

func (ch *CANHANDLE) Write(msg *CANMsg) error

Write message to channel

func (*CANHANDLE) WriteEx

func (ch *CANHANDLE) WriteEx(msg *CANMsgEx, data []byte) error

Write message to channel with handle h.

This is a version without a data-array in the structure

type CANMsg

type CANMsg struct {
	ID        uint32        // Message id
	Timestamp uint32        // timestamp in milliseconds
	Flags     MessageFlag   // Message flags
	Len       uint8         // Frame size (0.8)
	Data      [MAX_DLC]byte // Databytes 0..7
}

CAN Frame

func (*CANMsg) Bytes

func (msg *CANMsg) Bytes() []byte

Returns the data bytes of the CANMsg

func (*CANMsg) String

func (msg *CANMsg) String() string

Returns a string representation of the CANMsg

type CANMsgEx

type CANMsgEx struct {
	ID        uint32      // Message id
	Timestamp uint32      // timestamp in milliseconds
	Flags     MessageFlag // Message flags
	Len       uint8       // Frame size (0.8)
}

func (*CANMsgEx) String

func (msg *CANMsgEx) String() string

Returns a string representation of the CANMsgEx

type CANUSBStatistics

type CANUSBStatistics struct {
	ReceiveFrames  uint32 // # of receive frames
	TransmitFrames uint32 // # of transmitted frames
	ReceiveData    uint32 // # of received data bytes
	TransmitData   uint32 // # of transmitted data bytes
	Overruns       uint32 // # of overruns
	BusWarnings    uint32 // # of bus warnings
	BusOff         uint32 // # of bus off's
}

func (*CANUSBStatistics) String

func (c *CANUSBStatistics) String() string

Returns a string representation of the CANUsbStatistics

  • RF - # of Received frames
  • TF - # of Transmitted frames
  • RB - # of Received data bytes
  • TB - # of Transmitted data bytes
  • Overruns - # of overruns
  • BusWarnings - # of bus warnings
  • BusOff - # of bus off's

type CallbackFunc

type CallbackFunc func(msg *CANMsg) uintptr

Callback function used with SetReceiveCallback

type Error

type Error struct {
	Code        ErrorCode
	Description string
}

func (*Error) Error

func (ke *Error) Error() string

type ErrorCode

type ErrorCode int32
const (
	ERROR_CANUSB_OK                ErrorCode = 1
	ERROR_CANUSB_GENERAL           ErrorCode = -1
	ERROR_CANUSB_OPEN_SUBSYSTEM    ErrorCode = -2
	ERROR_CANUSB_COMMAND_SUBSYSTEM ErrorCode = -3
	ERROR_CANUSB_NOT_OPEN          ErrorCode = -4
	ERROR_CANUSB_TX_FIFO_FULL      ErrorCode = -5
	ERROR_CANUSB_INVALID_PARAM     ErrorCode = -6
	ERROR_CANUSB_NO_MESSAGE        ErrorCode = -7
	ERROR_CANUSB_MEMORY_ERROR      ErrorCode = -8
	ERROR_CANUSB_NO_DEVICE         ErrorCode = -9
	ERROR_CANUSB_TIMEOUT           ErrorCode = -10
	ERROR_CANUSB_INVALID_HARDWARE  ErrorCode = -11
)

type FlushFlag

type FlushFlag uint8
const (
	FLUSH_WAIT          FlushFlag = 0x00
	FLUSH_DONTWAIT      FlushFlag = 0x01
	FLUSH_EMPTY_INQUEUE FlushFlag = 0x02
)

Flush flags

type MessageFlag

type MessageFlag uint8
const (
	CANMSG_STANDARD MessageFlag = 0x00 // Standard 11-bit CAN id
	CANMSG_EXTENDED MessageFlag = 0x80 // Extended 29-bit CAN id
	CANMSG_RTR      MessageFlag = 0x40 // Remote frame
)

Message flags

type OpenFlag

type OpenFlag uint32
const (
	FLAG_TIMESTAMP     OpenFlag = 0x0001 // Timestamp messages
	FLAG_QUEUE_REPLACE OpenFlag = 0x0002 // If input queue is full remove oldest message and insert new message.
	FLAG_BLOCK         OpenFlag = 0x0004 // Block receive/transmit
	FLAG_SLOW          OpenFlag = 0x0008 // Check ACK/NACK's
	FLAG_NO_LOCAL_SEND OpenFlag = 0x0010 // Don't send transmited frames on other local channels for the same interface
)

Jump to

Keyboard shortcuts

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