class Escalator::Protocol::Keyence::KvProtocol

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 30
def initialize options={}
  super
  @host = options[:host] || "192.168.0.10"
  @port = options[:port] || 8501
  prepare_device_map
end

Public Instance Methods

close() click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 47
def close
  @socket.close if @socket
  @socket = nil
end
device_by_name(name) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 120
def device_by_name name
  case name
  when String
    device_class.new name
  else
    # it may be already Device
    name
  end
end
dump_packet(packet) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 143
def dump_packet packet
  packet.dup.chomp
end
get_bit_from_device(device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 52
def get_bit_from_device device
  device = device_by_name device
  get_bits_from_device(1, device).first
end
get_bits_from_device(count, device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 57
def get_bits_from_device count, device
  values = get_words_from_device count, device
  values = values.map{|v| v == 0 ? false : true}
  values.each do |v|
    device.bool = v
    device = device_by_name (device+1).name
  end
  values
end
get_word_from_device(device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 89
def get_word_from_device device
  device = device_by_name device
  get_words_from_device(1, device).first
end
get_words_from_device(count, device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 94
def get_words_from_device(count, device)
  device = local_device device
  packet = "RDS #{device.name} #{count}\r"
  @logger.debug("> #{dump_packet packet}")
  open
  @socket.puts(packet)
  res = receive
  values = res.split(/\s+/).map{|v| v.to_i}
  @logger.debug("#{device.name}[#{count}] => #{values}")
  values
end
open() click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 37
def open
  open!
rescue
  nil
end
open!() click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 43
def open!
  @socket ||= TCPSocket.open(@host, @port)
end
receive() click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 131
def receive
  res = ""
  begin
    Timeout.timeout(0.1) do
      res = @socket.gets
    end
  rescue Timeout::Error
  end
  @logger.debug("< #{dump_packet res}")
  res.chomp
end
set_bit_to_device(bits, device)
Alias for: set_bits_to_device
set_bits_to_device(bits, device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 67
def set_bits_to_device bits, device
  device = device_by_name device
  bits = [bits] unless bits.is_a? Array
  @logger.debug("#{device.name}[#{bits.size}] <= #{bits}")
  bits.each do |v|
    cmd = "ST"
    case v
    when false, 0
      cmd = "RS"
    end
    packet = "#{cmd} #{device.name}\r"
    @logger.debug("> #{dump_packet packet}")
    open
    @socket.puts(packet)
    res = receive
    raise res unless /OK/i =~ res
    device = device_by_name (device+1).name
  end
end
Also aliased as: set_bit_to_device
set_word_to_device(words, device)
Alias for: set_words_to_device
set_words_to_device(words, device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 106
def set_words_to_device words, device
  device = local_device device
  words = [words] unless words.is_a? Array
  packet = "WRS #{device.name} #{words.size} #{words.map{|w| w.to_s}.join(" ")}\r"
  @logger.debug("> #{dump_packet packet}")
  open
  @socket.puts(packet)
  res = receive
  @logger.debug("#{device.name}[#{words.size}] <= #{words}")
  raise res unless /OK/i =~ res
end
Also aliased as: set_word_to_device

Private Instance Methods

device_class() click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 149
def device_class
  KvDevice
end
local_device(device) click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 175
def local_device device
  return device if device.is_a? KvDevice
  d, c = @conv_dev_dict[device.suffix]
  return nil unless device.number < c
  ld = KvDevice.new(d.suffix, d.number + device.number)
  device_by_name ld.name
end
prepare_device_map() click to toggle source
# File lib/escalator/protocol/keyence/kv_protocol.rb, line 153
def prepare_device_map
  @conv_dev_dict ||= begin
    h = {}
    [
      ["X", "R0", 1024],
      ["Y", "R0", 1024],
      ["M", "MR0", 1024],
      ["C", "C0", 256],
      ["T", "T0", 256],
      ["L", "L0", 1024],
      ["SC", "MR1024", 1024],
      ["D", "DM0", 1024],
      ["H", "DM1024", 1024],
      ["SD", "DM2048", 1024],
      ["PRG", "DM3072", 1024]    # ..D4095
    ].each do |s,d,c|
      h[s] = [KvDevice.new(d), c]
    end
    h
  end
end