plc_access 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab5af068754de94a85c011c2649a60b913bf7d5767f19b6c7acccddf95dc13ad
4
- data.tar.gz: 8f0750599d28cceff1c12816236ce7766f29120e0ed985f56e05a7cee4c4c1e9
3
+ metadata.gz: c7944febc355a406f20d64952237fb1cb58d718c079d9b1743535ce495d5a153
4
+ data.tar.gz: d76735035b4e7749ef4beb234b5cad4392090fe892ae6321a3ae24e14a54cc25
5
5
  SHA512:
6
- metadata.gz: f4723143d4b6f0ffaf4eb56925b04dbb9eb14fe61bbab1ee216383876bbd187969761af2237120d415fc496cb16e3e8f24312b612ac4e28cae9f3816af62f0ba
7
- data.tar.gz: 584140d65859c52ec474286a53e1ec551b84c9c401bf3613d57457151d730ecc23c472458bbfa35bf83ca073fb3f93fd6ac117d1e348b64e8f7938497c731ba2
6
+ metadata.gz: a8a256b8f7e4cf5d3a7e1cbf6d7d21168ceaccb0927279c1427f293bb603622b648ae1d660649262d10204f8146f926d9f7ec6cf8ec38a2ae6089c475b0f4d39
7
+ data.tar.gz: 7d5e7fc1ca634bb9397f4f752b91efa4eecdc3535db7915e01f3e3a0f9b06b57ec250f8d69c4eafba5afbc313d30d17b2f1db629eaef66b15348dbf3da9ea6fa
data/CHANGES.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGES
2
2
 
3
+ ## 0.2.1
4
+
5
+ - Add byte order support for string/ushort conversion per PLC type (Mitsubishi: little-endian, Keyence/Omron: big-endian)
6
+ - Accept plc object in as_ushort/to_string to auto-detect byte order
7
+ - Allow Protocol#[]= to pad with zeros or truncate when array size differs from count
8
+ - Allow Protocol#[]= to accept arrays without specifying count
9
+
3
10
  ## 0.1.3
4
11
 
5
12
  - Rename Array#as_string to Array#to_string
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- plc_access (0.1.3)
4
+ plc_access (0.2.1)
5
5
  serialport (~> 1.3, >= 1.3.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
- json (2.10.1)
11
+ json (2.18.1)
12
12
  language_server-protocol (3.17.0.4)
13
13
  lint_roller (1.1.0)
14
14
  parallel (1.26.3)
data/README.md CHANGED
@@ -73,10 +73,17 @@ plc["DM0", 10] = [0, 1, 2, 3, 4].as_int # => [0, 0, 1, 0, 2, 0, 3, 0, 4, 0]
73
73
 
74
74
  plc["MR0", 10].to_int # => [0, 1, 2, 3, 4]
75
75
 
76
- # Set string to data memory (ten wors from DM0) as ushort values.
77
- plc["DM0", 10] = "PLC Access".as_ushort()
78
- # Get string from data memory (ten wors from DM0).
79
- plc["DM0", 10].to_string()
76
+ # Set string to data memory (ten words from DM0) as ushort values.
77
+ # The byte order for string conversion differs by PLC type
78
+ # (e.g., Mitsubishi uses little-endian, Keyence/Omron use big-endian).
79
+ # Pass plc to automatically use the correct byte order.
80
+ plc["DM0", 10] = "PLC Access".as_ushort(plc)
81
+ # Get string from data memory (ten words from DM0).
82
+ plc["DM0", 10].to_string(plc)
83
+
84
+ # You can also specify the length and plc together.
85
+ plc["DM0", 5] = "PLC Access".as_ushort(10, plc)
86
+ plc["DM0", 5].to_string(10, plc)
80
87
 
81
88
  ```
82
89
 
@@ -43,8 +43,17 @@ module ActAsType
43
43
  pack("f*").unpack("S*")
44
44
  end
45
45
 
46
- def to_string length=nil, encoding=Encoding::UTF_8
47
- s = pack("v*")
46
+ def to_string length=nil, encoding=Encoding::UTF_8, endian: :little
47
+ if length.respond_to?(:string_endian)
48
+ endian = length.string_endian
49
+ length = nil
50
+ end
51
+ if encoding.respond_to?(:string_endian)
52
+ endian = encoding.string_endian
53
+ encoding = Encoding::UTF_8
54
+ end
55
+ format = endian == :big ? "n*" : "v*"
56
+ s = pack(format)
48
57
  if length
49
58
  s = s[0, length].delete("\000")
50
59
  end
@@ -55,10 +64,19 @@ module ActAsType
55
64
 
56
65
  refine String do
57
66
 
58
- def as_ushort length=nil, encoding=Encoding::UTF_8
59
- a = self.encode(encoding).unpack("v*")
67
+ def as_ushort length=nil, encoding=Encoding::UTF_8, endian: :little
68
+ if length.respond_to?(:string_endian)
69
+ endian = length.string_endian
70
+ length = nil
71
+ end
72
+ if encoding.respond_to?(:string_endian)
73
+ endian = encoding.string_endian
74
+ encoding = Encoding::UTF_8
75
+ end
76
+ format = endian == :big ? "n*" : "v*"
77
+ a = self.encode(encoding).unpack(format)
60
78
  return a unless length
61
-
79
+
62
80
  s = (length + 1) / 2
63
81
  a += [0] * [s - a.length, 0].max
64
82
  a[0, s]
@@ -170,6 +170,10 @@ module PlcAccess
170
170
  raise "ERROR: return #{res} for set_bits_to_device(#{words}, #{device.name})" unless res == ack_packet
171
171
  end
172
172
 
173
+ def string_endian
174
+ :little
175
+ end
176
+
173
177
  def device_by_name(name)
174
178
  case name
175
179
  when String
@@ -160,6 +160,10 @@ module PlcAccess
160
160
  end
161
161
  end
162
162
 
163
+ def string_endian
164
+ :little
165
+ end
166
+
163
167
  def device_by_name(name)
164
168
  case name
165
169
  when String
@@ -107,6 +107,10 @@ module PlcAccess
107
107
  def -(other)
108
108
  self.class.new suffix, [number - other, 0].max
109
109
  end
110
+
111
+ def string_endian
112
+ :little
113
+ end
110
114
  end
111
115
  end
112
116
  end
@@ -147,6 +147,10 @@ module PlcAccess
147
147
  alias word value
148
148
  alias word= value=
149
149
 
150
+ def string_endian
151
+ :big
152
+ end
153
+
150
154
  def text(len = 8)
151
155
  n = (len + 1) / 2
152
156
  d = self
@@ -155,14 +159,16 @@ module PlcAccess
155
159
  a << d.value
156
160
  d = d.next_device
157
161
  end
158
- s = a.pack('n*').split("\x00").first
162
+ format = string_endian == :big ? 'n*' : 'v*'
163
+ s = a.pack(format).split("\x00").first
159
164
  s ? s[0, len] : ''
160
165
  end
161
166
 
162
167
  def set_text(value, len = 8)
163
168
  value = value[0, len]
164
169
  value << "\00" unless value.length.even?
165
- a = value.unpack('n*')
170
+ format = string_endian == :big ? 'n*' : 'v*'
171
+ a = value.unpack(format)
166
172
  d = self
167
173
  a.each do |v|
168
174
  d.value = v
@@ -46,6 +46,15 @@ module PlcAccess
46
46
  @device_type = type
47
47
  end
48
48
 
49
+ def string_endian
50
+ case @device_type
51
+ when :fx, :q
52
+ :little
53
+ else
54
+ :big
55
+ end
56
+ end
57
+
49
58
  private
50
59
 
51
60
  def device_class
@@ -58,6 +58,10 @@ module PlcAccess
58
58
 
59
59
  TIMEOUT = 1.0
60
60
 
61
+ def string_endian
62
+ :big
63
+ end
64
+
61
65
  # abstract methods
62
66
 
63
67
  def open; end
@@ -169,7 +173,7 @@ module PlcAccess
169
173
  v = [v] unless v.is_a? Array
170
174
  case args[0]
171
175
  when String
172
- self[args[0], 1] = v
176
+ self[args[0], v.size] = v
173
177
  when Range
174
178
  self[args[0].first, args[0].count] = v
175
179
  else
@@ -181,7 +185,8 @@ module PlcAccess
181
185
  c = args[1]
182
186
  values = args[2]
183
187
  values = [values] unless values.is_a? Array
184
- raise ArgumentError, "Count #{c} is not match #{args[2].size}." unless c == values.size
188
+ values += [0] * (c - values.size) if values.size < c
189
+ values = values[0, c] if values.size > c
185
190
 
186
191
  a = []
187
192
  if d.bit_device?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlcAccess
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plc_access
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katsuyoshi Ito
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-02-22 00:00:00.000000000 Z
10
+ date: 2026-02-14 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: serialport
@@ -78,7 +77,6 @@ metadata:
78
77
  homepage_uri: https://github.com/ito-soft-design/plc_access
79
78
  source_code_uri: https://github.com/ito-soft-design/plc_access
80
79
  changelog_uri: https://github.com/ito-soft-design/plc_access/CHANGES.md
81
- post_install_message:
82
80
  rdoc_options: []
83
81
  require_paths:
84
82
  - lib
@@ -93,8 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
91
  - !ruby/object:Gem::Version
94
92
  version: '0'
95
93
  requirements: []
96
- rubygems_version: 3.4.19
97
- signing_key:
94
+ rubygems_version: 3.6.5
98
95
  specification_version: 4
99
96
  summary: The PlcAccess communicates with PLCs.
100
97
  test_files: []