ladder_drive 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/Gemfile +8 -0
  4. data/Gemfile.lock +41 -0
  5. data/LICENSE +21 -0
  6. data/README.md +191 -0
  7. data/README_jp.md +193 -0
  8. data/Rakefile +9 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/exe/ladder_drive +28 -0
  12. data/ladder_drive.gemspec +33 -0
  13. data/lib/ladder_drive/asm.rb +211 -0
  14. data/lib/ladder_drive/cli.rb +55 -0
  15. data/lib/ladder_drive/config.rb +107 -0
  16. data/lib/ladder_drive/config_target.rb +93 -0
  17. data/lib/ladder_drive/console.rb +113 -0
  18. data/lib/ladder_drive/intel_hex.rb +79 -0
  19. data/lib/ladder_drive/ladder_drive.rb +36 -0
  20. data/lib/ladder_drive/plc_define.rb +35 -0
  21. data/lib/ladder_drive/plc_device.rb +171 -0
  22. data/lib/ladder_drive/protocol/emulator/emu_protocol.rb +54 -0
  23. data/lib/ladder_drive/protocol/emulator/emulator.rb +29 -0
  24. data/lib/ladder_drive/protocol/keyence/keyence.rb +31 -0
  25. data/lib/ladder_drive/protocol/keyence/kv_device.rb +51 -0
  26. data/lib/ladder_drive/protocol/keyence/kv_protocol.rb +187 -0
  27. data/lib/ladder_drive/protocol/mitsubishi/mc_protocol.rb +268 -0
  28. data/lib/ladder_drive/protocol/mitsubishi/mitsubishi.rb +30 -0
  29. data/lib/ladder_drive/protocol/mitsubishi/qdevice.rb +114 -0
  30. data/lib/ladder_drive/protocol/protocol.rb +107 -0
  31. data/lib/ladder_drive/tasks/build.rb +101 -0
  32. data/lib/ladder_drive/uploader.rb +101 -0
  33. data/lib/ladder_drive/version.rb +26 -0
  34. data/lib/ladder_drive.rb +32 -0
  35. data/lib/plc/LICENSE +21 -0
  36. data/lib/plc/emulator/emu_device.rb +121 -0
  37. data/lib/plc/emulator/emu_plc.rb +482 -0
  38. data/lib/plc/emulator/emu_plc_server.rb +71 -0
  39. data/lib/plc/emulator/emulator.rb +28 -0
  40. data/lib/plc/keyence/kv/kv-5000/DocumentWindowInfo.xml +91 -0
  41. data/lib/plc/keyence/kv/kv-5000/KvsMon.ini +0 -0
  42. data/lib/plc/keyence/kv/kv-5000/LadderDrive.mod +0 -0
  43. data/lib/plc/keyence/kv/kv-5000/LbkMdm.ini +0 -0
  44. data/lib/plc/keyence/kv/kv-5000/Main.mod +0 -0
  45. data/lib/plc/keyence/kv/kv-5000/MonEnv.kmu +1 -0
  46. data/lib/plc/keyence/kv/kv-5000/PlcSended.dky +0 -0
  47. data/lib/plc/keyence/kv/kv-5000/SensorMonitorInfo.xml +4 -0
  48. data/lib/plc/keyence/kv/kv-5000/TransInfo.tif +0 -0
  49. data/lib/plc/keyence/kv/kv-5000/UnitSet.ue2 +0 -0
  50. data/lib/plc/keyence/kv/kv-5000/UnitSet.ue2.old +0 -0
  51. data/lib/plc/keyence/kv/kv-5000/WsTreeEnv.xml +28 -0
  52. data/lib/plc/keyence/kv/kv-5000/kv-5000.cm1 +0 -0
  53. data/lib/plc/keyence/kv/kv-5000/kv-5000.cm2 +0 -0
  54. data/lib/plc/keyence/kv/kv-5000/kv-5000.ftc +0 -0
  55. data/lib/plc/keyence/kv/kv-5000/kv-5000.kpr +0 -0
  56. data/lib/plc/keyence/kv/kv-5000/kv-5000.lbl +0 -0
  57. data/lib/plc/keyence/kv/kv-5000/kv-5000.mil +0 -0
  58. data/lib/plc/keyence/kv/kv-5000/kv-5000.spl +0 -0
  59. data/lib/plc/keyence/kv/kv-5000/operand-history.txt +16 -0
  60. data/lib/plc/keyence/kv/kv-5000/sample.al2 +2 -0
  61. data/lib/plc/mitsubishi/iq-r/r08/r08.gx3 +0 -0
  62. data/lib/plc/plc.rb +27 -0
  63. data/sample/ladder_drive/sample1.esc +4 -0
  64. data/sample/ladder_drive/sample2.esc +28 -0
  65. data/sample/ladder_drive/sample2.png +0 -0
  66. data/template/escalator/config/plc.yml +27 -0
  67. data/template/ladder_drive/Rakefile +1 -0
  68. data/template/ladder_drive/asm/main.esc +70 -0
  69. data/template/ladder_drive/config/plc.yml +27 -0
  70. metadata +174 -0
@@ -0,0 +1,35 @@
1
+ #
2
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ module LadderDrive
24
+ module PlcDefine
25
+
26
+ # status flags
27
+ # SD0
28
+ ESC_STATUS_TO_PLC_STOP_PLC_FLAG = 2 # bit 1
29
+ ESC_STATUS_TO_PLC_CLEAR_PROGRAM = 4 # bit 2 require bit 1 on
30
+ # SD1
31
+ ESC_STATUS_FROM_PLC_CYCLE_RUN = 2
32
+ ESC_STATUS_FROM_PLC_ACK_CLEAR_PROGRAM = 4
33
+
34
+ end
35
+ end
@@ -0,0 +1,171 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module LadderDrive
25
+
26
+ class PlcDevice
27
+
28
+ attr_reader :suffix, :number
29
+ attr_accessor :value
30
+
31
+ NUMBER_TYPE_DEC = 0
32
+ NUMBER_TYPE_DEC_HEX = 1
33
+ NUMBER_TYPE_HEX = 2
34
+
35
+ ESC_SUFFIXES = %w(X Y M - C T L SC CC TC D - CS TS H SD)
36
+
37
+ class << self
38
+
39
+ def status_to_plc_device
40
+ @status_to_plc_device ||= new "SD0"
41
+ end
42
+
43
+ def status_from_plc_device
44
+ @status_from_plc_device ||= new "SD1"
45
+ end
46
+
47
+ def program_area_device
48
+ @program_area_device ||= new "PRG0"
49
+ end
50
+
51
+ end
52
+
53
+ def initialize a, b = nil
54
+ @suffix = nil
55
+ @value = 0
56
+ case a
57
+ when Fixnum
58
+ @suffix = ESC_SUFFIXES[a]
59
+ @number = b
60
+ when String
61
+ if b
62
+ @suffix = a.upcase
63
+ @number = b
64
+ else
65
+ /([A-Z]+)?(\d+)/i =~ a
66
+ @suffix = ($1 || "").upcase
67
+ case number_type
68
+ when NUMBER_TYPE_DEC_HEX
69
+ n = $2.to_i
70
+ @number = (n / 100) * 16 + (n % 100)
71
+ when NUMBER_TYPE_HEX
72
+ @number = $2.to_i(16)
73
+ else
74
+ @number = $2.to_i
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ def name
81
+ case number_type
82
+ when NUMBER_TYPE_DEC
83
+ "#{@suffix}#{@number}"
84
+ when NUMBER_TYPE_DEC_HEX
85
+ a = [@number / 16, @number % 16]
86
+ ns = begin
87
+ s = a.last.to_s.rjust(2, "0")
88
+ s = a.first.to_s + s unless a.first == 0
89
+ s
90
+ end
91
+ "#{@suffix}#{ns}"
92
+ when NUMBER_TYPE_HEX
93
+ ns = @number.to_s(16)
94
+ ns = "0" + ns unless /^[0-9]/ =~ ns
95
+ "#{@suffix}#{ns}"
96
+ else
97
+ nil
98
+ end
99
+ end
100
+
101
+ def next_device
102
+ d = self.class.new @suffix, @number + 1
103
+ d
104
+ end
105
+
106
+ def bit_device?
107
+ SUFFIXES_FOR_BIT.include? @suffix
108
+ end
109
+
110
+ def + value
111
+ self.class.new self.suffix, self.number + value
112
+ end
113
+
114
+ def - value
115
+ self.class.new self.suffix, [self.number - value, 0].max
116
+ end
117
+
118
+ def input?
119
+ suffixes_for_input.include? @suffix
120
+ end
121
+
122
+ def bool
123
+ case @value
124
+ when Fixnum
125
+ @value != 0
126
+ else
127
+ !!@value
128
+ end
129
+ end
130
+ def bool= v; @value = v; end
131
+ alias :word :value
132
+ alias :word= :value=
133
+
134
+ def device_code
135
+ ESC_SUFFIXES.index @suffix
136
+ end
137
+
138
+ def reset
139
+ @value = 0
140
+ end
141
+
142
+ private
143
+
144
+ SUFFIXES_FOR_DEC = %w(PRG M C T L SC CC TC D CS TS H SD)
145
+ SUFFIXES_FOR_DEC_HEX = %w()
146
+ SUFFIXES_FOR_HEX = %w(X Y)
147
+ SUFFIXES_FOR_BIT = %w(X Y M C T L SC)
148
+ SUFFIXES_FOR_INPUT = %w(X)
149
+
150
+ def suffixes_for_dec; SUFFIXES_FOR_DEC; end
151
+ def suffixes_for_dec_hex; SUFFIXES_FOR_DEC_HEX; end
152
+ def suffixes_for_hex; SUFFIXES_FOR_HEX; end
153
+ def suffixes_for_bit; SUFFIXES_FOR_BIT; end
154
+ def suffixes_for_input; SUFFIXES_FOR_INPUT; end
155
+
156
+ def suffixes
157
+ suffixes_for_dec + suffixes_for_dec_hex + suffixeds_for_hex
158
+ end
159
+
160
+ def number_type
161
+ return NUMBER_TYPE_DEC if suffixes_for_dec.include? @suffix
162
+ return NUMBER_TYPE_DEC_HEX if suffixes_for_dec_hex.include? @suffix
163
+ return NUMBER_TYPE_HEX if suffixes_for_hex.include? @suffix
164
+ nil
165
+ end
166
+
167
+ end
168
+
169
+ class EscDevice < PlcDevice; end
170
+
171
+ end
@@ -0,0 +1,54 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module LadderDrive
25
+ module Protocol
26
+ module Emulator
27
+
28
+ class EmuProtocol < LadderDrive::Protocol::Keyence::KvProtocol
29
+
30
+ def initialize options={}
31
+ options.merge host:"localhost", port:5555
32
+ super
33
+ end
34
+
35
+ def execute line
36
+ @socket.puts(line)
37
+ @socket.gets
38
+ end
39
+
40
+ private
41
+
42
+ def device_class
43
+ EscDevice
44
+ end
45
+
46
+ def local_device device
47
+ device
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,29 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ $:.unshift File.dirname(__FILE__)
25
+
26
+ require 'socket'
27
+ require 'logger'
28
+ require 'timeout'
29
+ require 'emu_protocol'
@@ -0,0 +1,31 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ dir = File.expand_path(File.dirname(__FILE__))
25
+ $:.unshift dir unless $:.include? dir
26
+
27
+ require 'socket'
28
+ require 'logger'
29
+ require 'timeout'
30
+ require 'kv_device'
31
+ require 'kv_protocol'
@@ -0,0 +1,51 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module LadderDrive
25
+ module Protocol
26
+ module Keyence
27
+
28
+ class KvDevice < PlcDevice
29
+
30
+ def initialize a, b = nil
31
+ super
32
+ @suffix = "R" if @suffix.nil? || @suffix.length == 0
33
+ end
34
+
35
+ private
36
+
37
+ SUFFIXES_FOR_DEC = %w(DM EM FM ZF TM Z T TC TS C CC CS CTH CTC AT CM VM)
38
+ SUFFIXES_FOR_DEC_HEX = %w(R MR LR CR)
39
+ SUFFIXES_FOR_HEX = %w(B VB W)
40
+ SUFFIXES_FOR_BIT = %w(R B MR LR CR VB)
41
+
42
+ def suffixes_for_dec; SUFFIXES_FOR_DEC; end
43
+ def suffixes_for_dec_hex; SUFFIXES_FOR_DEC_HEX; end
44
+ def suffixeds_for_hex; SUFFIXES_FOR_HEX; end
45
+ def suffixes_for_bit; SUFFIXES_FOR_BIT; end
46
+
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,187 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module LadderDrive
25
+ module Protocol
26
+ module Keyence
27
+
28
+ class KvProtocol < Protocol
29
+
30
+ def initialize options={}
31
+ super
32
+ @host = options[:host] || "192.168.0.10"
33
+ @port = options[:port] || 8501
34
+ prepare_device_map
35
+ end
36
+
37
+ def open
38
+ open!
39
+ rescue
40
+ nil
41
+ end
42
+
43
+ def open!
44
+ @socket ||= TCPSocket.open(@host, @port)
45
+ end
46
+
47
+ def close
48
+ @socket.close if @socket
49
+ @socket = nil
50
+ end
51
+
52
+ def get_bit_from_device device
53
+ device = device_by_name device
54
+ get_bits_from_device(1, device).first
55
+ end
56
+
57
+ def get_bits_from_device count, device
58
+ values = get_words_from_device count, device
59
+ values = values.map{|v| v == 0 ? false : true}
60
+ values.each do |v|
61
+ device.bool = v
62
+ device = device_by_name (device+1).name
63
+ end
64
+ values
65
+ end
66
+
67
+ def set_bits_to_device bits, device
68
+ device = device_by_name device
69
+ bits = [bits] unless bits.is_a? Array
70
+ @logger.debug("#{device.name}[#{bits.size}] <= #{bits}")
71
+ bits.each do |v|
72
+ cmd = "ST"
73
+ case v
74
+ when false, 0
75
+ cmd = "RS"
76
+ end
77
+ packet = "#{cmd} #{device.name}\r"
78
+ @logger.debug("> #{dump_packet packet}")
79
+ open
80
+ @socket.puts(packet)
81
+ res = receive
82
+ raise res unless /OK/i =~ res
83
+ device = device_by_name (device+1).name
84
+ end
85
+ end
86
+ alias :set_bit_to_device :set_bits_to_device
87
+
88
+
89
+ def get_word_from_device device
90
+ device = device_by_name device
91
+ get_words_from_device(1, device).first
92
+ end
93
+
94
+ def get_words_from_device(count, device)
95
+ device = local_device device
96
+ packet = "RDS #{device.name} #{count}\r"
97
+ @logger.debug("> #{dump_packet packet}")
98
+ open
99
+ @socket.puts(packet)
100
+ res = receive
101
+ values = res.split(/\s+/).map{|v| v.to_i}
102
+ @logger.debug("#{device.name}[#{count}] => #{values}")
103
+ values
104
+ end
105
+
106
+ def set_words_to_device words, device
107
+ device = local_device device
108
+ words = [words] unless words.is_a? Array
109
+ packet = "WRS #{device.name} #{words.size} #{words.map{|w| w.to_s}.join(" ")}\r"
110
+ @logger.debug("> #{dump_packet packet}")
111
+ open
112
+ @socket.puts(packet)
113
+ res = receive
114
+ @logger.debug("#{device.name}[#{words.size}] <= #{words}")
115
+ raise res unless /OK/i =~ res
116
+ end
117
+ alias :set_word_to_device :set_words_to_device
118
+
119
+
120
+ def device_by_name name
121
+ case name
122
+ when String
123
+ device_class.new name
124
+ else
125
+ # it may be already Device
126
+ name
127
+ end
128
+ end
129
+
130
+
131
+ def receive
132
+ res = ""
133
+ begin
134
+ Timeout.timeout(0.1) do
135
+ res = @socket.gets
136
+ end
137
+ rescue Timeout::Error
138
+ end
139
+ @logger.debug("< #{dump_packet res}")
140
+ res.chomp
141
+ end
142
+
143
+ def dump_packet packet
144
+ packet.dup.chomp
145
+ end
146
+
147
+ private
148
+
149
+ def device_class
150
+ KvDevice
151
+ end
152
+
153
+ def prepare_device_map
154
+ @conv_dev_dict ||= begin
155
+ h = {}
156
+ [
157
+ ["X", "R0", 1024],
158
+ ["Y", "R0", 1024],
159
+ ["M", "MR0", 1024],
160
+ ["C", "C0", 256],
161
+ ["T", "T0", 256],
162
+ ["L", "L0", 1024],
163
+ ["SC", "MR1024", 1024],
164
+ ["D", "DM0", 1024],
165
+ ["H", "DM1024", 1024],
166
+ ["SD", "DM2048", 1024],
167
+ ["PRG", "DM3072", 1024] # ..D4095
168
+ ].each do |s,d,c|
169
+ h[s] = [KvDevice.new(d), c]
170
+ end
171
+ h
172
+ end
173
+ end
174
+
175
+ def local_device device
176
+ return device if device.is_a? KvDevice
177
+ d, c = @conv_dev_dict[device.suffix]
178
+ return nil unless device.number < c
179
+ ld = KvDevice.new(d.suffix, d.number + device.number)
180
+ device_by_name ld.name
181
+ end
182
+
183
+ end
184
+
185
+ end
186
+ end
187
+ end