class LadderDrive::Console

Public Instance Methods

run() click to toggle source
# File lib/ladder_drive/console.rb, line 32
def run
  trap(:INT) { puts "\n> " }

  display_title

  loop do
    begin
      print "> "
      line = STDIN.gets
      case line.chomp
      when /^\s*exit\s*$/, /^\s*quit\s*$/, /^\s*q\s*$/
        break
      when /^r\s+(\w+)(\s+(\d+))?/
        d = protocol.device_by_name EscDevice.new($1)
        c = $2 ? $2.to_i : 1
        values = protocol.get_from_devices d, c
        values = values.map{|v| case v;  when true; 1; when false; 0; else; v; end}
        puts values.join(" ")
      when /^p\s+(\w+)(\s+(\d+))?/
        d = protocol.device_by_name EscDevice.new($1)
        t = $2 ? $2.to_f : 0.1
        protocol.set_to_devices d, true
        sleep t
        protocol.set_to_devices d, false
      when /^w\s+(\w+)/
        d = protocol.device_by_name EscDevice.new($1)
        v = $'.scan(/\d+/).map{|e| e.to_i}
        protocol.set_to_devices d, v
      when /^E\s+/
        puts protocol.execute(line)
      when /^help/, /^h/
        display_help
      end
    rescue => e
      puts "*** ERROR: #{e} ***"
    end
  end
end

Private Instance Methods

display_help() click to toggle source
# File lib/ladder_drive/console.rb, line 86
      def display_help
        puts <<EOB
Commands:

  r: Read values from device.
     r device [count]
       e.g.) it reads values from M0 to M7.
             > r m0 8
  w: Write values to device.
     w device value1 value2 ...
       e.g.) it write values from D0 to D7.
             > w d0 1 2 3 4 5 6 7 8
       e.g.) it write values from M0 to M7.
             > w m0 0 0 0 1 1 0 1 1
  p: Pulse out on device. Default duration is 0.1 sec
     p device [duration]
       e.g.) it write 1 to m0, then write 0 after 0.5 sec.
             > w m0 0.5
  q: Quit this. You can use quit and exit instead of q.
  h: Show help. You can use help instead of h.

EOB
      end
display_title() click to toggle source
# File lib/ladder_drive/console.rb, line 77
      def display_title
        puts <<EOB

  LadderDrive is an abstract PLC.
  This is a console to communicate with PLC.

EOB
      end
protocol() click to toggle source
# File lib/ladder_drive/console.rb, line 73
def protocol
  target.protocol
end