plc_access 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2019 ITO SOFT DESIGN Inc.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ # Supported models : CP1E
27
+
28
+ module PlcAccess
29
+ module Protocol
30
+ module Omron
31
+ class OmronDevice < PlcDevice
32
+ attr_reader :suffix, :channel, :bit
33
+
34
+ SUFFIXES = %w[M H D T C A].freeze
35
+
36
+ def initialize(a, b = nil, c = nil)
37
+ case a
38
+ when Array
39
+ # case a.size
40
+ # when 4
41
+ # @suffix = suffix_for_code(a[3])
42
+ # @channel = ((a[2] << 8 | a[1]) << 8) | a[0]
43
+ # end
44
+ else
45
+ if b
46
+ @suffix = a.upcase if a
47
+ @channel = b.to_i
48
+ @bit = c.to_i if c
49
+ elsif /^(M|H|D|T|C|A)?([0-9]+)(\.([0-9]{1,2}))?$/i =~ a
50
+ @suffix = ::Regexp.last_match(1).upcase if ::Regexp.last_match(1)
51
+ @channel = ::Regexp.last_match(2).to_i
52
+ @bit = ::Regexp.last_match(4).to_i if ::Regexp.last_match(4)
53
+ end
54
+ end
55
+ case @suffix
56
+ when 'T', 'C'
57
+ raise "#{name} is not allowed as a bit device." if @bit
58
+ end
59
+ end
60
+
61
+ def channel_device
62
+ return self unless bit_device?
63
+
64
+ self.class.new suffix, channel
65
+ end
66
+
67
+ def valid?
68
+ !!channel
69
+ end
70
+
71
+ def name
72
+ if bit
73
+ "#{suffix}#{channel}.#{bit.to_s.rjust(2, '0')}"
74
+ else
75
+ "#{suffix}#{channel}"
76
+ end
77
+ end
78
+
79
+ def next_device
80
+ self + 1
81
+ end
82
+
83
+ def bit_device?
84
+ !!bit
85
+ end
86
+
87
+ def suffix_for_code(code)
88
+ index = SUFFIX_CODES.index code
89
+ index ? SUFFIXES[index] : nil
90
+ end
91
+
92
+ def suffix_code
93
+ index = SUFFIXES.index suffix
94
+ index ? SUFFIX_CODES[index] : 0
95
+ end
96
+
97
+ def +(other)
98
+ if bit
99
+ v = channel * 16 + bit + other
100
+ c = v / 16
101
+ b = v % 16
102
+ self.class.new suffix, c, b
103
+ else
104
+ self.class.new suffix, channel + other
105
+ end
106
+ end
107
+
108
+ def -(other)
109
+ case other
110
+ when OmronDevice
111
+ d = other
112
+ raise "Can't subtract between different device type." if bit_device? ^ d.bit_device?
113
+
114
+ if bit
115
+ (channel * 16 + bit) - (d.channel * 16 + d.bit)
116
+ else
117
+ channel - d.channel
118
+ end
119
+ else
120
+ other = other.to_i
121
+ if bit
122
+ v = [channel * 16 + bit - other, 0].max
123
+ c = v / 16
124
+ b = v % 16
125
+ self.class.new suffix, c, b
126
+ else
127
+ self.class.new suffix, [channel - other, 0].max
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,225 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ module PlcAccess
27
+ class PlcDevice
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].freeze
36
+
37
+ class << self
38
+ def status_to_plc_device
39
+ @status_to_plc_device ||= new 'SD0'
40
+ end
41
+
42
+ def status_from_plc_device
43
+ @status_from_plc_device ||= new 'SD1'
44
+ end
45
+
46
+ def program_area_device
47
+ @program_area_device ||= new 'PRG0'
48
+ end
49
+ end
50
+
51
+ def initialize(a, b = nil)
52
+ @suffix = nil
53
+ @value = 0
54
+ case a
55
+ when Integer
56
+ @suffix = ESC_SUFFIXES[a]
57
+ @number = b
58
+ when String, Symbol
59
+ a = a.to_s # convert to string if it's a symbol
60
+ if b
61
+ @suffix = a.upcase
62
+ @number = b
63
+ else
64
+ /([A-Z]+)?([0-9A-F]+)/i =~ a
65
+ @suffix = (::Regexp.last_match(1) || '').upcase
66
+ case number_type
67
+ when NUMBER_TYPE_DEC_HEX
68
+ n = ::Regexp.last_match(2).to_i
69
+ @number = (n / 100) * 16 + (n % 100)
70
+ when NUMBER_TYPE_HEX
71
+ @number = ::Regexp.last_match(2).to_i(16)
72
+ else
73
+ @number = ::Regexp.last_match(2).to_i
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def name
80
+ case number_type
81
+ when NUMBER_TYPE_DEC
82
+ "#{@suffix}#{@number}"
83
+ when NUMBER_TYPE_DEC_HEX
84
+ a = [@number / 16, @number % 16]
85
+ ns = begin
86
+ s = a.last.to_s.rjust(2, '0')
87
+ s = a.first.to_s + s unless a.first.zero?
88
+ s
89
+ end
90
+ "#{@suffix}#{ns}"
91
+ when NUMBER_TYPE_HEX
92
+ ns = @number.to_s(16).upcase
93
+ ns = "0#{ns}" unless /^[0-9]/ =~ ns
94
+ "#{@suffix}#{ns}"
95
+ end
96
+ end
97
+
98
+ # NOTE: override at subclass
99
+ # It should get from plc
100
+ def device_by_suffix_number(suffix, number)
101
+ self.class.new suffix, number
102
+ end
103
+
104
+ def next_device
105
+ device_by_suffix_number @suffix, @number + 1
106
+ end
107
+
108
+ def bit_device?
109
+ suffixes_for_bit.include? @suffix
110
+ end
111
+
112
+ def +(other)
113
+ device_by_suffix_number suffix, number + other
114
+ end
115
+
116
+ def -(other)
117
+ device_by_suffix_number suffix, [number - other, 0].max
118
+ end
119
+
120
+ def input?
121
+ suffixes_for_input.include? @suffix
122
+ end
123
+
124
+ def value
125
+ case @value
126
+ when true
127
+ 1
128
+ when false, nil
129
+ 0
130
+ else
131
+ @value
132
+ end
133
+ end
134
+
135
+ def bool
136
+ case @value
137
+ when Integer
138
+ @value != 0
139
+ else
140
+ !!@value
141
+ end
142
+ end
143
+
144
+ def bool=(v)
145
+ @value = v
146
+ end
147
+ alias word value
148
+ alias word= value=
149
+
150
+ def text(len = 8)
151
+ n = (len + 1) / 2
152
+ d = self
153
+ a = []
154
+ n.times do
155
+ a << d.value
156
+ d = d.next_device
157
+ end
158
+ s = a.pack('n*').split("\x00").first
159
+ s ? s[0, len] : ''
160
+ end
161
+
162
+ def set_text(value, len = 8)
163
+ value = value[0, len]
164
+ value << "\00" unless value.length.even?
165
+ a = value.unpack('n*')
166
+ d = self
167
+ a.each do |v|
168
+ d.value = v
169
+ d = d.next_device
170
+ end
171
+ end
172
+
173
+ def text=(value)
174
+ set_text value
175
+ end
176
+
177
+ def device_code
178
+ ESC_SUFFIXES.index @suffix
179
+ end
180
+
181
+ def reset
182
+ @value = 0
183
+ end
184
+
185
+ private
186
+
187
+ SUFFIXES_FOR_DEC = %w[PRG M C T L SC CC TC D CS TS H SD].freeze
188
+ SUFFIXES_FOR_DEC_HEX = %w[].freeze
189
+ SUFFIXES_FOR_HEX = %w[X Y].freeze
190
+ SUFFIXES_FOR_BIT = %w[X Y M C T L SC].freeze
191
+ SUFFIXES_FOR_INPUT = %w[X].freeze
192
+
193
+ def suffixes_for_dec
194
+ SUFFIXES_FOR_DEC
195
+ end
196
+
197
+ def suffixes_for_dec_hex
198
+ SUFFIXES_FOR_DEC_HEX
199
+ end
200
+
201
+ def suffixes_for_hex
202
+ SUFFIXES_FOR_HEX
203
+ end
204
+
205
+ def suffixes_for_bit
206
+ SUFFIXES_FOR_BIT
207
+ end
208
+
209
+ def suffixes_for_input
210
+ SUFFIXES_FOR_INPUT
211
+ end
212
+
213
+ def suffixes
214
+ suffixes_for_dec + suffixes_for_dec_hex + suffixeds_for_hex
215
+ end
216
+
217
+ def number_type
218
+ return NUMBER_TYPE_DEC if suffixes_for_dec.include? @suffix
219
+ return NUMBER_TYPE_DEC_HEX if suffixes_for_dec_hex.include? @suffix
220
+ return NUMBER_TYPE_HEX if suffixes_for_hex.include? @suffix
221
+
222
+ nil
223
+ end
224
+ end
225
+ end
@@ -0,0 +1,219 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ dir = __dir__
27
+ $LOAD_PATH.unshift dir unless $LOAD_PATH.include? dir
28
+
29
+ module PlcAccess
30
+ module Protocol
31
+ class Protocol
32
+ attr_accessor :host, :port
33
+
34
+ def initialize(options = {})
35
+ @logger = Logger.new($stdout)
36
+ self.log_level = options[:log_level] || :info
37
+ end
38
+
39
+ attr_reader :log_level
40
+
41
+ def log_level=(level)
42
+ @log_level = level.is_a?(String) ? level.to_sym : level
43
+ case @log_level
44
+ when :debug
45
+ @logger.level = Logger::DEBUG
46
+ when :error
47
+ @logger.level = Logger::ERROR
48
+ when :fatal
49
+ @logger.level = Logger::FATAL
50
+ when :info
51
+ @logger.level = Logger::INFO
52
+ when :unknown
53
+ @logger.level = Logger::UNKNOWN
54
+ when :warn
55
+ @logger.level = Logger::WARN
56
+ end
57
+ end
58
+
59
+ TIMEOUT = 1.0
60
+
61
+ # abstract methods
62
+
63
+ def open; end
64
+ def close; end
65
+
66
+ def get_bit_from_device(device)
67
+ get_bits_from_device(1, device_by_name(device)).first
68
+ end
69
+
70
+ def get_bits_from_device(count, device); end
71
+
72
+ def set_bit_to_device(bit, device)
73
+ set_bits_to_device [bit], device
74
+ end
75
+
76
+ def set_bits_to_device(bits, device); end
77
+
78
+ def get_word_from_device(device)
79
+ get_words_from_device(1, device_by_name(device)).first
80
+ end
81
+
82
+ def get_words_from_device(count, device); end
83
+
84
+ def set_word_to_device(word, device)
85
+ set_words_to_device [word], device
86
+ end
87
+
88
+ def set_words_to_device(words, device); end
89
+
90
+ def device_by_name(_name)
91
+ nil
92
+ end
93
+
94
+ def get_from_devices(device, count = 1)
95
+ d = device_by_name device
96
+ if d.bit_device?
97
+ get_bits_from_device count, d
98
+ else
99
+ get_words_from_device count, d
100
+ end
101
+ end
102
+
103
+ def set_to_devices(device, values)
104
+ values = [values] unless values.is_a? Array
105
+ d = device_by_name device
106
+ if d.bit_device?
107
+ values = values.map { |v| case v; when 1 then true; when 0 then false; else; v; end }
108
+ set_bits_to_device values, d
109
+ else
110
+ set_words_to_device values, d
111
+ end
112
+ end
113
+
114
+ def available_bits_range(_device = nil)
115
+ -Float::INFINITY..Float::INFINITY
116
+ end
117
+
118
+ def available_words_range(_device = nil)
119
+ -Float::INFINITY..Float::INFINITY
120
+ end
121
+
122
+ def [](*args)
123
+ case args.size
124
+ when 1
125
+ # protocol["DM0"]
126
+ # protocol["DM0".."DM9"]
127
+ case args[0]
128
+ when String
129
+ self[args[0], 1].first
130
+ when Range
131
+ self[args[0].first, args[0].count]
132
+ else
133
+ raise ArgumentError, "#{args[0]} must be String or Range."
134
+ end
135
+ when 2
136
+ # protocol["DM0", 10]
137
+ d = device_by_name args[0]
138
+ c = args[1]
139
+ a = []
140
+ if d.bit_device?
141
+ b = available_bits_range(d).last
142
+ until c.zero?
143
+ n_c = [b, c].min
144
+ a += get_bits_from_device(n_c, d)
145
+ d += n_c
146
+ c -= n_c
147
+ end
148
+ else
149
+ b = available_words_range(d).last
150
+ until c.zero?
151
+ n_c = [b, c].min
152
+ a += get_words_from_device(n_c, d)
153
+ d += n_c
154
+ c -= n_c
155
+ end
156
+ end
157
+ a
158
+ else
159
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1 or 2)"
160
+ end
161
+ end
162
+
163
+ def []=(*args)
164
+ case args.size
165
+ when 2
166
+ # protocol["DM0"] = 0
167
+ # protocol["DM0".."DM9"] = [0, 1, .., 9]
168
+ v = args[1]
169
+ v = [v] unless v.is_a? Array
170
+ case args[0]
171
+ when String
172
+ self[args[0], 1] = v
173
+ when Range
174
+ self[args[0].first, args[0].count] = v
175
+ else
176
+ raise ArgumentError, "#{args[1]} must be String or Array."
177
+ end
178
+ when 3
179
+ # protocol["DM0", 10] = [0, 1, .., 9]
180
+ d = device_by_name args[0]
181
+ c = args[1]
182
+ values = args[2]
183
+ values = [values] unless values.is_a? Array
184
+ raise ArgumentError, "Count #{c} is not match #{args[2].size}." unless c == values.size
185
+
186
+ a = []
187
+ if d.bit_device?
188
+ values.each_slice(available_bits_range(d).last) do |sv|
189
+ set_bits_to_device(sv, d)
190
+ d += sv.size
191
+ end
192
+ else
193
+ values.each_slice(available_words_range(d).last) do |sv|
194
+ set_words_to_device(sv, d)
195
+ d += sv.size
196
+ end
197
+ end
198
+ a
199
+ else
200
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 2 or 3)"
201
+ end
202
+ end
203
+
204
+ def destination_ipv4
205
+ Socket.gethostbyname(host)[3].unpack('C4').join('.')
206
+ end
207
+
208
+ def self_ipv4
209
+ Socket.getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET)[0][3]
210
+ end
211
+ end
212
+ end
213
+ end
214
+
215
+ require 'serialport'
216
+ require 'plc_device'
217
+ require 'keyence/keyence'
218
+ require 'mitsubishi/mitsubishi'
219
+ require 'omron/omron'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PlcAccess
4
+ VERSION = '0.1.0'
5
+ end
data/lib/plc_access.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2016 ITO SOFT DESIGN Inc.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining
8
+ # a copy of this software and associated documentation files (the
9
+ # "Software"), to deal in the Software without restriction, including
10
+ # without limitation the rights to use, copy, modify, merge, publish,
11
+ # distribute, sublicense, and/or sell copies of the Software, and to
12
+ # permit persons to whom the Software is furnished to do so, subject to
13
+ # the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ dir = __dir__
27
+ $LOAD_PATH.unshift dir unless $LOAD_PATH.include? dir
28
+
29
+ require 'plc_access/protocol/protocol'
30
+ require 'plc_access/version'
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/plc_access/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'plc_access'
7
+ spec.version = PlcAccess::VERSION
8
+ spec.authors = ['Katsuyoshi Ito']
9
+ spec.email = ['kito@itosoft.com']
10
+
11
+ spec.summary = 'The PlcAccess communicates with PLCs.'
12
+ spec.description = 'The PlcAccess communicates with PLCs. You can get values or states of PLC devices.'
13
+ spec.homepage = 'https://github.com/ito-soft-design/plc_access'
14
+ spec.license = 'MIT'
15
+
16
+ spec.add_runtime_dependency 'serialport', '~> 1.3', '>= 1.3.1'
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/ito-soft-design/plc_access'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/ito-soft-design/plc_access/CHANGES.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+ end