Class: Escalator::Protocol::Keyence::KvProtocol
- Inherits:
-
Protocol
- Object
- Protocol
- Escalator::Protocol::Keyence::KvProtocol
show all
- Defined in:
- lib/escalator/protocol/keyence/kv_protocol.rb
Instance Attribute Summary
Attributes inherited from Protocol
#host, #log_level, #port
Instance Method Summary
collapse
Methods inherited from Protocol
#get_from_devices, #set_to_devices
Constructor Details
#initialize(options = {}) ⇒ KvProtocol
Returns a new instance of KvProtocol.
30
31
32
33
34
35
|
# 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
|
Instance Method Details
#close ⇒ Object
47
48
49
50
|
# File 'lib/escalator/protocol/keyence/kv_protocol.rb', line 47
def close
@socket.close if @socket
@socket = nil
end
|
#device_by_name(name) ⇒ Object
120
121
122
123
124
125
126
127
128
|
# File 'lib/escalator/protocol/keyence/kv_protocol.rb', line 120
def device_by_name name
case name
when String
device_class.new name
else
name
end
end
|
#dump_packet(packet) ⇒ Object
143
144
145
|
# File 'lib/escalator/protocol/keyence/kv_protocol.rb', line 143
def dump_packet packet
packet.dup.chomp
end
|
#get_bit_from_device(device) ⇒ Object
52
53
54
55
|
# 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) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# 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) ⇒ Object
89
90
91
92
|
# 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) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
|
# 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 ⇒ Object
37
38
39
40
41
|
# File 'lib/escalator/protocol/keyence/kv_protocol.rb', line 37
def open
open!
rescue
nil
end
|
#open! ⇒ Object
43
44
45
|
# File 'lib/escalator/protocol/keyence/kv_protocol.rb', line 43
def open!
@socket ||= TCPSocket.open(@host, @port)
end
|
#receive ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
|
# 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_bits_to_device(bits, device) ⇒ Object
Also known as:
set_bit_to_device
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# 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
|
#set_words_to_device(words, device) ⇒ Object
Also known as:
set_word_to_device
106
107
108
109
110
111
112
113
114
115
116
|
# 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
|