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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2f35c23c610072572e035350b54c7040e43a63a7fff03e6545975c479ee5a230
4
+ data.tar.gz: f9077c4ded62f71b8531102099a826bb93866a08d6035bd8e96626e986f867a7
5
+ SHA512:
6
+ metadata.gz: 5934dfc5bf9a828ec264336a93bcd15289d7144032b6b054d3635c1dd218d718d442472673a2fc6b52c823786d16bea09771096d31a74bacaa18f53679276b67
7
+ data.tar.gz: 2eb98b439a85c974a27aaf619fff06b0303bbcdf66e24c4fc870d2e37af1c0b995f18a1a67936079710bdbdba41a0ece4188acb0a3b1daf294195c6fd6fc0aae
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.5
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kito@itosoft.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in plc_access.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem 'test-unit'
10
+ gem 'test-unit-rr'
11
+ end
12
+
13
+ group :development do
14
+ gem 'rubocop', require: false
15
+ end
16
+
17
+ gem 'rake', '~> 12.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ plc_access (0.1.0)
5
+ serialport (~> 1.3, >= 1.3.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ json (2.6.2)
12
+ parallel (1.22.1)
13
+ parser (3.1.2.1)
14
+ ast (~> 2.4.1)
15
+ power_assert (2.0.1)
16
+ rainbow (3.1.1)
17
+ rake (12.3.3)
18
+ regexp_parser (2.5.0)
19
+ rexml (3.2.5)
20
+ rr (3.1.0)
21
+ rubocop (1.36.0)
22
+ json (~> 2.3)
23
+ parallel (~> 1.10)
24
+ parser (>= 3.1.2.1)
25
+ rainbow (>= 2.2.2, < 4.0)
26
+ regexp_parser (>= 1.8, < 3.0)
27
+ rexml (>= 3.2.5, < 4.0)
28
+ rubocop-ast (>= 1.20.1, < 2.0)
29
+ ruby-progressbar (~> 1.7)
30
+ unicode-display_width (>= 1.4.0, < 3.0)
31
+ rubocop-ast (1.21.0)
32
+ parser (>= 3.1.1.0)
33
+ ruby-progressbar (1.11.0)
34
+ serialport (1.3.2)
35
+ test-unit (3.5.3)
36
+ power_assert
37
+ test-unit-rr (1.0.5)
38
+ rr (>= 1.1.1)
39
+ test-unit (>= 2.5.2)
40
+ unicode-display_width (2.3.0)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ plc_access!
47
+ rake (~> 12.0)
48
+ rubocop
49
+ test-unit
50
+ test-unit-rr
51
+
52
+ BUNDLED WITH
53
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Katsuyoshi Ito
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # PlcAccess
2
+
3
+ PlcAccess is library to make connection with PLCs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'plc_access'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install plc_access
20
+
21
+ ## Usage
22
+
23
+
24
+ Mitsubishi MC Protocol:
25
+
26
+ ```
27
+ require 'plc_access'
28
+
29
+ plc = PlcAccess::Protocol::Mitsubishi::McProtocol.new host:"192.168.0.10"
30
+
31
+ plc["M0"] = true
32
+ plc["M0"] # => true
33
+ plc["M0", 10] # => [true, false, ..., false]
34
+
35
+ plc["D0"] = 123
36
+ plc["D0"] # => 123
37
+ plc["D0", 10] = [0, 1, 2, ..., 9]
38
+ plc["D0".."D9"] => [0, 1, 2, ..., 9]
39
+ ```
40
+
41
+ Keyence PLCs:
42
+
43
+ ```
44
+ require 'plc_access'
45
+
46
+ plc = PlcAccess::Protocol::Keyence::KvProtocol.new host:"192.168.0.10"
47
+
48
+ plc["MR0"] = true
49
+ plc["MR0"] # => true
50
+ plc["MR0", 10] # => [true, false, ..., false]
51
+
52
+ plc["DM0"] = 123
53
+ plc["DM0"] # => 123
54
+ plc["DM0", 10] = [0, 1, 2, ..., 9]
55
+ plc["DM0".."DM9"] => [0, 1, 2, ..., 9]
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ito-soft-design/plc_access. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ito-soft-design/plc_access/blob/master/CODE_OF_CONDUCT.md).
61
+
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the PlcAccess project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ito-soft-design/plc_access/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ task default: [:test]
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.test_files = FileList['test/**/test*.rb']
10
+ t.verbose = true
11
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'plc_access'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
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 'socket'
30
+ require 'logger'
31
+ require 'timeout'
32
+ require 'kv_device'
33
+ require 'kv_protocol'
@@ -0,0 +1,68 @@
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
+ module Protocol
28
+ module Keyence
29
+ class KvDevice < PlcDevice
30
+ def initialize(a, b = nil)
31
+ super
32
+ @suffix = 'R' if @suffix.nil? || @suffix.length.zero?
33
+ end
34
+
35
+ def +(other)
36
+ self.class.new suffix, number + other
37
+ end
38
+
39
+ def -(other)
40
+ self.class.new suffix, [number - other, 0].max
41
+ end
42
+
43
+ private
44
+
45
+ SUFFIXES_FOR_DEC = %w[DM EM FM ZF TM Z T TC TS C CC CS CTH CTC AT CM VM].freeze
46
+ SUFFIXES_FOR_DEC_HEX = %w[R MR LR CR].freeze
47
+ SUFFIXES_FOR_HEX = %w[B VB W].freeze
48
+ SUFFIXES_FOR_BIT = %w[R B MR LR CR VB].freeze
49
+
50
+ def suffixes_for_dec
51
+ SUFFIXES_FOR_DEC
52
+ end
53
+
54
+ def suffixes_for_dec_hex
55
+ SUFFIXES_FOR_DEC_HEX
56
+ end
57
+
58
+ def suffixes_for_hex
59
+ SUFFIXES_FOR_HEX
60
+ end
61
+
62
+ def suffixes_for_bit
63
+ SUFFIXES_FOR_BIT
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,181 @@
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
+ module Protocol
28
+ module Keyence
29
+ class KvProtocol < Protocol
30
+ def initialize(options = {})
31
+ super
32
+ @socket = nil
33
+ @host = options[:host] || '192.168.0.10'
34
+ @port = options[:port] || 8501
35
+ end
36
+
37
+ def open
38
+ open!
39
+ rescue StandardError
40
+ nil
41
+ end
42
+
43
+ def open!
44
+ @socket ||= TCPSocket.open(@host, @port)
45
+ end
46
+
47
+ def close
48
+ @socket&.close
49
+ @socket = nil
50
+ end
51
+
52
+ def get_bits_from_device(count, device)
53
+ c = (count + 15) / 16
54
+ words = get_words_from_device c, device
55
+ values = []
56
+ count.times do |i|
57
+ index = i / 16
58
+ bit = i % 16
59
+ values << ((words[index] & (1 << bit)) != 0)
60
+ end
61
+ values
62
+ end
63
+
64
+ def set_bits_to_device(bits, device)
65
+ device = device_by_name device
66
+ bits = [bits] unless bits.is_a? Array
67
+ @logger.debug("#{device.name}[#{bits.size}] <= #{bits}")
68
+ bits.each do |v|
69
+ cmd = 'ST'
70
+ case v
71
+ when false, 0
72
+ cmd = 'RS'
73
+ end
74
+ packet = "#{cmd} #{device.name}\r\n"
75
+ @logger.debug("> #{dump_packet packet}")
76
+ open
77
+ @socket.puts(packet)
78
+ res = receive
79
+ raise res unless /OK/i =~ res
80
+
81
+ device += 1
82
+ end
83
+ end
84
+ alias set_bit_to_device set_bits_to_device
85
+
86
+ def get_words_from_device(count, device)
87
+ packet = "RDS #{device.name}.H #{count}\r\n"
88
+ @logger.debug("> #{dump_packet packet}")
89
+ open
90
+ @socket.puts(packet)
91
+ res = receive
92
+ values = res.split(/\s+/).map { |v| v.to_i(16) }
93
+ @logger.debug("#{device.name}[#{count}] => #{values}")
94
+ values
95
+ end
96
+
97
+ def set_words_to_device(words, device)
98
+ words = [words] unless words.is_a? Array
99
+ packet = "WRS #{device.name}.H #{words.size} #{words.map { |w| w.to_s(16) }.join(' ')}\r\n"
100
+ @logger.debug("> #{dump_packet packet}")
101
+ open
102
+ @socket.puts(packet)
103
+ res = receive
104
+ @logger.debug("#{device.name}[#{words.size}] <= #{words}")
105
+ raise res unless /OK/i =~ res
106
+ end
107
+ alias set_word_to_device set_words_to_device
108
+
109
+ def device_by_name(name)
110
+ case name
111
+ when String
112
+ device_class.new name
113
+ else
114
+ # it may be already Device
115
+ name
116
+ end
117
+ end
118
+
119
+ def receive
120
+ res = ''
121
+ begin
122
+ Timeout.timeout(TIMEOUT) do
123
+ res = @socket.gets
124
+ end
125
+ rescue Timeout::Error
126
+ end
127
+ @logger.debug("< #{dump_packet res}")
128
+ res.chomp
129
+ end
130
+
131
+ def dump_packet(packet)
132
+ packet.dup.chomp
133
+ end
134
+
135
+ def available_bits_range(suffix = nil)
136
+ case suffix
137
+ when 'TM'
138
+ 1..512
139
+ when 'Z'
140
+ 1..12
141
+ when 'T', 'TC', 'TS', 'C', 'CC', 'CS'
142
+ 1..120
143
+ when 'CTH'
144
+ 1..2
145
+ when 'CTC'
146
+ 1..4
147
+ when 'AT'
148
+ 1..8
149
+ else
150
+ 1..1000
151
+ end
152
+ end
153
+
154
+ def available_words_range(suffix = nil)
155
+ case suffix
156
+ when 'TM'
157
+ 1..256
158
+ when 'Z'
159
+ 1..12
160
+ when 'T', 'TC', 'TS', 'C', 'CC', 'CS'
161
+ 1..120
162
+ when 'CTH'
163
+ 1..2
164
+ when 'CTC'
165
+ 1..4
166
+ when 'AT'
167
+ 1..8
168
+ else
169
+ 1..500
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ def device_class
176
+ KvDevice
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PlcAccess
4
+ module Protocol
5
+ module Mitsubishi
6
+ class FxDevice < QDevice
7
+ def name
8
+ @suffix + @number.to_s.rjust(4, '0')
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end