Class: PlcAccess::Protocol::Keyence::KvProtocol

Inherits:
Protocol
  • Object
show all
Defined in:
lib/plc_access/protocol/keyence/kv_protocol.rb

Direct Known Subclasses

PlcShare::PlcShareProtocol

Constant Summary

Constants inherited from Protocol

Protocol::TIMEOUT

Instance Attribute Summary

Attributes inherited from Protocol

#host, #log_level, #port

Instance Method Summary collapse

Methods inherited from Protocol

#[], #[]=, #destination_ipv4, #get_bit_from_device, #get_from_devices, #get_word_from_device, #self_ipv4, #set_to_devices

Constructor Details

#initialize(options = {}) ⇒ KvProtocol

Returns a new instance of KvProtocol.



30
31
32
33
34
35
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 30

def initialize(options = {})
  super
  @socket = nil
  @host = options[:host] || '192.168.0.10'
  @port = options[:port] || 8501
end

Instance Method Details

#available_bits_range(suffix = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 135

def available_bits_range(suffix = nil)
  case suffix
  when 'TM'
    1..512
  when 'Z'
    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) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 154

def available_words_range(suffix = nil)
  case suffix
  when 'TM'
    1..256
  when 'Z'
    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

#closeObject



47
48
49
50
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 47

def close
  @socket&.close
  @socket = nil
end

#device_by_name(name) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 109

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) ⇒ Object



131
132
133
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 131

def dump_packet(packet)
  packet.dup.chomp
end

#get_bits_from_device(count, device) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 52

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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 86

def get_words_from_device(count, 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

#openObject



37
38
39
40
41
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 37

def open
  open!
rescue StandardError
  nil
end

#open!Object



43
44
45
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 43

def open!
  @socket ||= TCPSocket.open(@host, @port)
end

#receiveObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 119

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_bits_to_device(bits, device) ⇒ Object Also known as: set_bit_to_device



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 64

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

#set_words_to_device(words, device) ⇒ Object Also known as: set_word_to_device



97
98
99
100
101
102
103
104
105
106
# File 'lib/plc_access/protocol/keyence/kv_protocol.rb', line 97

def set_words_to_device(words, 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