PCANUSB – Ruby Wrapper for PEAK USB-to-CAN Adapter

PCANUSB is a Ruby language wrapper for PEAK System Technik’s PCAN-USB, which is a USB CAN (Controller Area Network) bus adapter. This wrapper provides a flexible high-level interface to messaging on the CAN bus. Combined with the power of Ruby, the bits and bytes on the CAN bus can be slain into submission in no time.

Example

require 'PcanHardware'
require 'PcanMessage'

def connect
  # Create connection to PCAN-USB hardware and
  # configure for 100kbps and standard CAN message IDs
  @pcan = PcanHardware.new(100000, :msg_type_std)
  sleep 0.2
  @pcan.flush_receive_queue
end

def check_result(result)
  if !result.nil?
    flunk sprintf("Error: PCAN-USB returned 0x%04X", result[:error])
  end
end

def send_version_command
  msg = PcanMessage.new
  msg.set_id(0x170)
  msg.set_type(PcanMessage::MSGTYPE_STANDARD)
  msg.set_data([0x12, 0x34])
  result = @pcan.transmit_message(msg)
  check_result(result)
end

def verify_version_response
  result = @pcan.receive_message(5.0)
  check_result(result)
  assert_equal(PcanMessage::MSGTYPE_STANDARD, result.get_type)
  assert_equal(0x171, result.get_id)
  assert_equal([0x01, 0x21], result.get_data)
end

def disconnect
  if !@pcan.nil?
    @pcan.close
  end
end

...

# Test sequence

connect
send_version_command
verify_version_response
disconnect

Resources

Systir – System Testing in Ruby

» Download

[Ruby Gem] Download the Ruby gem and type: gem install pcan at the command prompt in the same directory you downloaded the gem to install.

Please let us know if you have any problems or questions.

Copyright © Atomic Object LLC. - Grand Rapids, MI 49506 - (616) 776-6020 - Contact Us

Edit