Documentation
¶
Index ¶
- Constants
- Variables
- func GetAdapters() (adapters []string, err error)
- func GetFirstAdapter() (int, string, error)
- func GetNextAdapter() (string, error)
- func Init() error
- func IsNoMessage(err error) bool
- func NewError[T ~int | ~int32 | int64](code T) error
- type CANHANDLE
- func (ch *CANHANDLE) Close() error
- func (ch *CANHANDLE) Flush(flags FlushFlag) error
- func (ch *CANHANDLE) GetStatistics() (*CANUSBStatistics, error)
- func (ch *CANHANDLE) Read() (msg *CANMsg, err error)
- func (ch *CANHANDLE) ReadEx() (msg *CANMsgEx, data []byte, err error)
- func (ch *CANHANDLE) ReadFirst(id uint32, flags MessageFlag) (msg *CANMsg, err error)
- func (ch *CANHANDLE) SetAsyncReceiveCallback(fn CallbackFunc) error
- func (ch *CANHANDLE) SetReceiveCallback(fn CallbackFunc) error
- func (ch *CANHANDLE) SetTimeouts(receiveTimeout, sendTimeout uint32) error
- func (ch *CANHANDLE) Status() error
- func (ch *CANHANDLE) VersionInfo() (string, error)
- func (ch *CANHANDLE) Write(msg *CANMsg) error
- func (ch *CANHANDLE) WriteEx(msg *CANMsgEx, data []byte) error
- type CANMsg
- type CANMsgEx
- type CANUSBStatistics
- type CallbackFunc
- type Error
- type ErrorCode
- type FlushFlag
- type MessageFlag
- type OpenFlag
Constants ¶
const ( // Filter mask settings ACCEPTANCE_CODE_ALL uint32 = 0x00000000 ACCEPTANCE_MASK_ALL uint32 = 0xFFFFFFFF )
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 )
const MAX_DLC = 8
Variables ¶
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)"} )
var InitErr error
Functions ¶
func GetAdapters ¶
Get all found adapters that is connected to this machine.
func GetFirstAdapter ¶
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 ¶
Get the found adapter(s) in turn that is connected to this machine.
Returns the serial number of the next adapter.
func IsNoMessage ¶
Types ¶
type CANHANDLE ¶
type CANHANDLE struct {
// contains filtered or unexported fields
}
CANHANDLE is a handle to a CANUSB device
func Open ¶
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) Flush ¶
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) ReadEx ¶
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 ¶
Set timeouts used for blocking calls for channel.
func (*CANHANDLE) VersionInfo ¶
Get hardware/firmware and driver version for channel
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
type CANMsgEx ¶
type CANMsgEx struct {
ID uint32 // Message id
Timestamp uint32 // timestamp in milliseconds
Flags MessageFlag // Message flags
Len uint8 // Frame size (0.8)
}
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 ¶
Callback function used with SetReceiveCallback
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 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 )