Skip to content

Instantly share code, notes, and snippets.

@yuasatakayuki
Last active February 15, 2016 01:38
Show Gist options
  • Save yuasatakayuki/de61b6796847fe68f215 to your computer and use it in GitHub Desktop.
Save yuasatakayuki/de61b6796847fe68f215 to your computer and use it in GitHub Desktop.
User Ruby wrapper of SpaceWire/RMAP Library

Now users can use basic SpaceWire and RMAP functionalities of the library in the Ruby scripting language. Scripting capability allows developers to quickly iterate try-and-error loops on, for example, new SpaceWire device without compiling a test program.

The SWIG wrapper is already included in the Github repository of SpaceWire RMAP Library, and existing users can pull it by executing:

git pull

in your SpaceWireRMAPLibrary/ folder. A new folder “swig/” contains all necessary files to generate a Ruby module. See the SpaceWire RMAP Library page for detailed an install procedure.

Example usage

Below shows snippets of Ruby SpaceWire/RMAP programs.

SpaceWire: sending a packet via SpaceWire-to-GigabitEther

This file can be found as SpaceWireRMAPLibrary/swig/testrun/test_spacewireif_client.rb.

#load SpaceWireRMAPLibrary module
require "SpaceWireRMAP"
include SpaceWireRMAP

#Connect to SpaceWire-to-GigabitEther device via TCP/IP
serverURL="localhost"
tcpPort=10030
spwif=SpaceWireIFOverTCP.new(serverURL,tcpPort)

#Open a SpaceWire link
spwif.open()
print "Connected\n"

#Create a packet, and then send it via the SpaceWire I/F
packet=VectorUInt8.new([1,2,3,4,5,6,7])
spwif.send(packet)
print "Sent\n"

#Close the SpaceWire I/F
spwif.close()

RMAP: Perform RMAP Read to a dummy RMAP Target device

The original code assumes a virtual RMAP Target node running on localhost (using a virtual SpaceWire I/F). Change “localhost” to an appropriate IP address when execute this with real SpaceWire-to-GigabitEther and RMAP Target devices.

This file can be found as SpaceWireRMAPLibrary/swig/testrun/rmap_test.rb.

#!/usr/bin/env ruby

#
# 2013-03-22 Hirokazu Odaka
# RMAP demo of the Ruby binding of SpaceWireRMAPLibrary
# - based on main_RMAP_readWireRMAPTargetNode.cc by Takayuki Yuasa
#

require 'spaceWire'
include SpaceWire

### initialize parameters
ConfigurationFile = "Tutorial_RMAPTarget.xml"
IPAddress = "localhost"
Port = 10030
TargetNodeID = "SampleRMAPTargetNode"
MemoryObjectID = "SampleRegister"

### load XML file
puts "Loading " + ConfigurationFile
db = RMAPTargetNodeDB.new
begin
  db.loadRMAPTargetNodesFromXMLFile(ConfigurationFile)
rescue => e
  puts "Error in loading " + ConfigurationFile
  puts e
  exit(-1)
end

### find RMAPTargetNode and MemoryObject
target_node = nil
begin
  target_node = db.getRMAPTargetNode(TargetNodeID)
rescue => e
  puts "It seems RMAPTargetNode named " + TargetNodeID + " is not defined in " + ConfigurationFile
  puts e
    exit(-1)
else
  puts "RMAPTargetNode named " << TargetNodeID << " was found." end memory_object = nil begin   memory_object = target_node.getMemoryObject(MemoryObjectID) rescue   puts "It seems Mmeory Object named " + MemoryObjectID + " of " + TargetNodeID + " is not defined in " + ConfigurationFile   puts e   exit(-1) else   puts "Memory Object named " + MemoryObjectID + " was found." end ### SpaceWire part puts "Opening SpaceWireIF..." spwif = SpaceWireIFOverTCP.new(IPAddress, Port) begin   spwif.open() rescue   puts "Connection timed out."   puts e   exit(-1) end ### RMAPEngine/RMAPInitiator part rmap_engine = RMAPEngine.new(spwif) rmap_engine.start() rmap_initiator = RMAPInitiator.new(rmap_engine) rmap_initiator.setInitiatorLogicalAddress(0xFE) rmap_initiator.setRMAPTargetNodeDB(db) ### Read puts "Performing RMAP Read..." buffer = nil # VectorUInt8.new begin   ### read   buffer = rmap_initiator.read(TargetNodeID, MemoryObjectID, 300000.0) rescue => ex
  puts "Exception => "
  puts ex
  exit(-1)
end

puts "Read successfully done."
puts "Data: "
buffer.each_with_index {|x, i| puts "%4d %4d" % [x, i]}

puts ""
puts "object => "
p buffer

puts "to_a => "
p buffer.to_a

### finalize
puts "RMAP enging stopping..."
rmap_engine.stop()
puts "SpaceWireIF closing..."
spwif.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment