Class: PCANUSB

Inherits:
Object
  • Object
show all
Defined in:
lib/pcanusb.rb

Defined Under Namespace

Modules: Core

Constant Summary collapse

BAUD_1M =

BAUD rates used by “init”

0x0014
BAUD_500K =
0x001C
BAUD_250K =
0x011C
BAUD_125K =
0x031C
BAUD_100K =
0x432F
BAUD_50K =
0x472F
BAUD_20K =
0x532F
BAUD_10K =
0x672F
BAUD_5K =
0x7F7F
CAN_INIT_TYPE_ST =

used by “init” and “set_receive_filter”

0
CAN_INIT_TYPE_EX =

11 Bit-ID handling - Standard frame

1
MSGTYPE_STANDARD =

used by “write” and “read”

0x00
MSGTYPE_RTR =
0x01
MSGTYPE_EXTENDED =
0x02
MSGTYPE_STATUS =
0x80
CAN_OK =

return values

0x0000
CAN_XMTFULL =

No error.

0x0001
CAN_OVERRUN =

Transmission buffer of the controller is full.

0x0002
CAN_BUSLIGHT =

CAN controller has been read out too late.

0x0004
CAN_BUSHEAVY =

Bus error: An error counter has reached the ‘Light’ limit.

0x0008
CAN_BUSOFF =

Bus error: An error counter has reached the ‘Heavy’ limit.

0x0010
CAN_QRCVEMPTY =

Bus error:Actual state from the CAN controller is ‘Bus Off’.

0x0020
CAN_QOVERRUN =

Receive queue is empty.

0x0040
CAN_QXMTFULL =

Receive queue has been read out too late.

0x0080
CAN_REGTEST =

Transmission queue is full.

0x0100
CAN_NOVXD =

Register test of the 82C200/SJA1000 has failed.

0x0200
CAN_ILLHW =

Driver is not loaded.

0x1400
CAN_ILLNET =

Hardware handle is invalid.

0x1800
CAN_MASK_ILLHANDLE =

Net handle is invalid.

0x1C00
CAN_ILLCLIENT =

Mask for all handle errors.

0x1C00
CAN_RESOURCE =

Client handle is invalid.

0x2000
CAN_ILLPARAMTYPE =

Resource (FIFO, client, timeout) cannot be created.

0x4000
CAN_ILLPARAMVAL =

Parameter is not permitted/applicable here.

0x8000

Class Method Summary collapse

Class Method Details

.closeObject

Close the PCAN device connection.



66
67
68
# File 'lib/pcanusb.rb', line 66

def self.close
  return Core::cAN_Close
end

.get_usb_device_numberObject

Return the device number associated with the connected PCAN.



137
138
139
140
141
142
143
# File 'lib/pcanusb.rb', line 137

def self.get_usb_device_number
  number = Core::Device_Number.malloc
  
  err = Core::getUSBDeviceNr(number)
  
  return err, number.value
end

.init(baud_value, message_type = CAN_INIT_TYPE_EX) ⇒ Object

Initialize the PCAN device with a BAUD rate and message type. Valid message types are

  • CAN_INIT_TYPE_ST

    Standard format IDs

  • CAN_INIT_TYPE_EX

    Extended IDs



56
57
58
59
60
61
62
63
# File 'lib/pcanusb.rb', line 56

def self.init(baud_value, message_type = CAN_INIT_TYPE_EX)
  err = Core::cAN_Init(baud_value, message_type)
  
  # allow the hardware to initialize
  sleep 0.125
  
  return err
end

.readObject

Read one CAN message from the PCAN FIFO. Returns the error code, message type, ID and data array.



97
98
99
100
101
102
103
# File 'lib/pcanusb.rb', line 97

def self.read
  message = Core::TPCANMsg.malloc
  
  err = Core::cAN_Read(message)
  
  return err, message.message_type, message.id, message.data[0..message.length - 1]
end

.read_id(id, timeout = 1) ⇒ Object

Read a message with a given ID. Returns the received data array if the required ID is received within the timeout or false if not.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pcanusb.rb', line 107

def self.read_id(id, timeout=1)
  read_timeout = Time.now + timeout

  begin
    err, rx_type, rx_id, rx_data = self.read

    if err == CAN_OK && rx_id == id then
      return rx_data
    end
  end while Time.now < read_timeout
  
  return false
end

.reset_clientObject

Reset the PCAN device.



122
123
124
# File 'lib/pcanusb.rb', line 122

def self.reset_client
  return Core::cAN_ResetClient
end

.set_receive_filter(fromID, toID, message_type = MSGTYPE_EXTENDED) ⇒ Object

Set the range of message ID that will be accepted. CAN_ResetFilter is used first to close the filter range, then CAN_MsgFilter is used to open it up.



90
91
92
93
# File 'lib/pcanusb.rb', line 90

def self.set_receive_filter(fromID, toID, message_type = MSGTYPE_EXTENDED)
  Core::cAN_ResetFilter
  return Core::cAN_MsgFilter(fromID, toID, message_type)
end

.statusObject

Retrieve the current PCAN status.



71
72
73
# File 'lib/pcanusb.rb', line 71

def self.status
  return Core::cAN_Status
end

.version_infoObject

Provide information about the PCAN.



127
128
129
130
131
132
133
134
# File 'lib/pcanusb.rb', line 127

def self.version_info
  info = Core::Version_Info.malloc
  
  err = Core::cAN_VersionInfo(info)

  # info.value is an array of characters, convert it to string 
  return info.value.pack("c128")
end

.write(id, data, message_type = MSGTYPE_EXTENDED) ⇒ Object

Initiates a transmission of a CAN message with a given ID. the data parameter is expected to be less than 8 bytes.



77
78
79
80
81
82
83
84
85
# File 'lib/pcanusb.rb', line 77

def self.write(id, data, message_type = MSGTYPE_EXTENDED)
  message = Core::TPCANMsg.malloc
  message.id = id
  message.message_type = message_type
  message.length = data.length
  message.data = data.dup
  
  return Core::cAN_Write(message)
end