class LadderDrive::Protocol::Keyence::KvProtocol

Public Class Methods

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

Public Instance Methods

available_bits_range(suffix=nil) click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 140
def available_bits_range suffix=nil
  case suffix
  # FIXME: duplicated
  when "TM"
    1..512
  when "TM"
    1..12
  when "T", "TC", "TS", "C", "CC", "CS"
    1..120
  when "CTH"
    1..2
  when "CTC"
    1..4
  when "AT"
    1..8
  else
    1..1000
  end
end
available_words_range(suffix=nil) click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 160
def available_words_range suffix=nil
  case suffix
  # FIXME: duplicated
  when "TM"
    1..256
  when "TM"
    1..12
  when "T", "TC", "TS", "C", "CC", "CS"
    1..120
  when "CTH"
    1..2
  when "CTC"
    1..4
  when "AT"
    1..8
  else
    1..500
  end
end
close() click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 48
def close
  @socket.close if @socket
  @socket = nil
end
device_by_name(name) click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 113
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/ladder_drive/protocol/keyence/kv_protocol.rb, line 136
def dump_packet packet
  packet.dup.chomp
end
get_bits_from_device(count, device) click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 53
def get_bits_from_device count, device
  c = (count + 15) / 16
  words = get_words_from_device c, device
  values = []
  count.times do |i|
    index = i / 16
    bit = i % 16
    values << ((words[index] & (1 << bit)) != 0)
  end
  values
end
get_words_from_device(count, device) click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 87
def get_words_from_device(count, device)
  device = local_device device
  packet = "RDS #{device.name}.H #{count}\r\n"
  @logger.debug("> #{dump_packet packet}")
  open
  @socket.puts(packet)
  res = receive
  values = res.split(/\s+/).map{|v| v.to_i(16)}
  @logger.debug("#{device.name}[#{count}] => #{values}")
  values
end
open() click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 38
def open
  open!
rescue
  nil
end
open!() click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 44
def open!
  @socket ||= TCPSocket.open(@host, @port)
end
receive() click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 124
def receive
  res = ""
  begin
    Timeout.timeout(TIMEOUT) 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/ladder_drive/protocol/keyence/kv_protocol.rb, line 65
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\n"
    @logger.debug("> #{dump_packet packet}")
    open
    @socket.puts(packet)
    res = receive
    raise res unless /OK/i =~ res
    device += 1
  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/ladder_drive/protocol/keyence/kv_protocol.rb, line 99
def set_words_to_device words, device
  device = local_device device
  words = [words] unless words.is_a? Array
  packet = "WRS #{device.name}.H #{words.size} #{words.map{|w| w.to_s(16)}.join(" ")}\r\n"
  @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/ladder_drive/protocol/keyence/kv_protocol.rb, line 183
def device_class
  KvDevice
end
local_device(device) click to toggle source
# File lib/ladder_drive/protocol/keyence/kv_protocol.rb, line 209
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/ladder_drive/protocol/keyence/kv_protocol.rb, line 187
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