Permalink
Browse files

Just CLI stuff yet

  • Loading branch information...
0 parents commit 334ccb8f3b3e56b75079a07e589fef104ce835c5 @tallakt committed Nov 15, 2011
Showing with 89 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 Gemfile
  3. +1 −0 Rakefile
  4. +7 −0 bin/modbus
  5. +42 −0 lib/modbus-cli.rb
  6. +5 −0 lib/modbus-cli/version.rb
  7. +25 −0 modbus-cli.gemspec
5 .gitignore
@@ -0,0 +1,5 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+.*swp
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in modbus-cli.gemspec
+gemspec
1 Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
7 bin/modbus
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+
+require 'modbus-cli'
+
+Modbus::Cli::CommandLineRunner.run
+
42 lib/modbus-cli.rb
@@ -0,0 +1,42 @@
+require "modbus-cli/version"
+require 'clamp'
+
+module Modbus
+ module Cli
+
+ class ReadCommand < Clamp::Command
+ parameter 'HOST', 'IP address or hostname to the Modbus server', :attribute_name => :host
+ parameter 'ADDRESS', 'Start address, in Schneider format (eg %M100, %MW100)', :attribute_name => :address do |a|
+ m = a.match /%M(W)(\d+)/
+ raise Clamp::UsageError "Illegal address #{a}" unless m
+ {:offset => m[2].to_i, :datatype => (if m[1] then :word else :bit end)}
+ end
+ parameter 'COUNT', 'number of data to read', :attribute_name => :count
+
+ def execute
+ puts "reading from host: #{host} #{count} values starting from address #{address}"
+ end
+ end
+
+ class WriteCommand < Clamp::Command
+ parameter 'HOST', 'IP address or hostname to the Modbus server', :attribute_name => :host
+ parameter 'ADDRESS', 'Start address, in Schneider format (eg %M100, %MW100)', :attribute_name => :address do |a|
+ m = a.match /%M(W)(\d+)/
+ raise Clamp::UsageError "Illegal address #{a}" unless m
+ {:offset => m[2].to_i, :datatype => (if m[1] then :word else :bit end)}
+ end
+ parameter 'VALUES ...', 'values to write, nonzero counts as true for discrete values', :attribute_name => :values
+
+ def execute
+ puts "wrriting to host: #{host} #{values.join ", "} values starting from address #{address}"
+ end
+ end
+
+ class CommandLineRunner < Clamp::Command
+ self.default_subcommand = "read"
+
+ subcommand 'read', 'read from the device', ReadCommand
+ subcommand 'write', 'write to the device', WriteCommand
+ end
+ end
+end
5 lib/modbus-cli/version.rb
@@ -0,0 +1,5 @@
+module Modbus
+ module Cli
+ VERSION = "0.0.1"
+ end
+end
25 modbus-cli.gemspec
@@ -0,0 +1,25 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "modbus-cli/version"
+
+Gem::Specification.new do |s|
+ s.name = "modbus-cli"
+ s.version = Modbus::Cli::VERSION
+ s.authors = ["Tallak Tveide"]
+ s.email = ["tallak@tveide.net"]
+ s.homepage = ""
+ s.summary = %q{Modbus command line}
+ s.description = %q{Command line interface to communicate over Modbus TCP}
+
+ s.rubyforge_project = "modbus-cli"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ s.add_runtime_dependency "rmodbus", '>= 1.1.0'
+ s.add_runtime_dependency "clamp", '~ 0.3.0'
+end

0 comments on commit 334ccb8

Please sign in to comment.