Class: LadderDrive::Protocol::Keyence::KvProtocol
- Inherits:
-
Protocol
- Object
- Protocol
- LadderDrive::Protocol::Keyence::KvProtocol
show all
- Defined in:
- lib/ladder_drive/protocol/keyence/kv_protocol.rb
Constant Summary
Constants inherited
from Protocol
Protocol::TIMEOUT
Instance Attribute Summary
Attributes inherited from Protocol
#host, #port
Instance Method Summary
collapse
Methods inherited from Protocol
#[], #[]=, #destination_ipv4, #get_bit_from_device, #get_from_devices, #get_word_from_device, #log_level, #log_level=, #self_ipv4, #set_to_devices
Constructor Details
#initialize(options = {}) ⇒ KvProtocol
Returns a new instance of KvProtocol.
30
31
32
33
34
35
36
|
# 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
|
Instance Method Details
#available_bits_range(suffix = nil) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/ladder_drive/protocol/keyence/kv_protocol.rb', line 140
def available_bits_range suffix=nil
case suffix
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) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/ladder_drive/protocol/keyence/kv_protocol.rb', line 160
def available_words_range suffix=nil
case suffix
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 ⇒ Object
48
49
50
51
|
# File 'lib/ladder_drive/protocol/keyence/kv_protocol.rb', line 48
def close
@socket.close if @socket
@socket = nil
end
|
#device_by_name(name) ⇒ Object
113
114
115
116
117
118
119
120
121
|
# 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
name
end
end
|
#dump_packet(packet) ⇒ Object
136
137
138
|
# 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) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# 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) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
|
# 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 ⇒ Object
38
39
40
41
42
|
# File 'lib/ladder_drive/protocol/keyence/kv_protocol.rb', line 38
def open
open!
rescue
nil
end
|
#open! ⇒ Object
44
45
46
|
# File 'lib/ladder_drive/protocol/keyence/kv_protocol.rb', line 44
def open!
@socket ||= TCPSocket.open(@host, @port)
end
|
#receive ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
|
# 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_bits_to_device(bits, device) ⇒ Object
Also known as:
set_bit_to_device
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# 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
|
#set_words_to_device(words, device) ⇒ Object
Also known as:
set_word_to_device
99
100
101
102
103
104
105
106
107
108
109
|
# 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
|