Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/rmodbus/ext.rb
Instance Method Summary collapse
-
#byteswap ⇒ Object
(also: #wordswap)
Swap every pair of elements.
-
#from_32f ⇒ Object
Given an array of 32-bit floats, turn it into an array of 16-bit unsigned integers, doubling the size.
-
#from_32i ⇒ Object
Given an array of 32bit signed integers, turn it into an array of 16 bit unsigned integers, doubling the size.
-
#from_32u ⇒ Object
Given an array of 32bit unsigned integers, turn it into an array of 16 bit unsigned integers, doubling the size.
-
#pack_bits ⇒ Object
pack an array of bits into a string of bytes, as the ModBus protocol dictates for coils.
-
#to_16i ⇒ Object
Given an array of 16-bit unsigned integers, turn it into an array of 16-bit signed integers.
-
#to_32f ⇒ Object
Given an array of 16-bit unsigned integers, turn it into an array of 32-bit floats, halving the size.
-
#to_32i ⇒ Object
Given an array of 16-bit unsigned integers, turn it into 32-bit signed integers, halving the size.
-
#to_32u ⇒ Object
Given an array of 16-bit unsigned integers, turn it into 32-bit unsigned integers, halving the size.
Instance Method Details
#byteswap ⇒ Object Also known as: wordswap
Swap every pair of elements
36 37 38 39 |
# File 'lib/rmodbus/ext.rb', line 36 def byteswap even_elements_check each_slice(2).flat_map(&:reverse) end |
#from_32f ⇒ Object
Given an array of 32-bit floats, turn it into an array of 16-bit unsigned integers, doubling the size.
55 56 57 |
# File 'lib/rmodbus/ext.rb', line 55 def from_32f pack("g*").unpack("n*") end |
#from_32i ⇒ Object
Given an array of 32bit signed integers, turn it into an array of 16 bit unsigned integers, doubling the size
79 80 81 |
# File 'lib/rmodbus/ext.rb', line 79 def from_32i pack("l>*").unpack("n*") end |
#from_32u ⇒ Object
Given an array of 32bit unsigned integers, turn it into an array of 16 bit unsigned integers, doubling the size
74 75 76 |
# File 'lib/rmodbus/ext.rb', line 74 def from_32u pack("N*").unpack("n*") end |
#pack_bits ⇒ Object
pack an array of bits into a string of bytes, as the ModBus protocol dictates for coils
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rmodbus/ext.rb', line 85 def pack_bits # pack each slice of 8 bits per byte, # forward order (bits 0-7 in byte 0, 8-15 in byte 1, etc.) # non-multiples of 8 are just 0-padded each_slice(8).map do |slice| byte = 0 # within each byte, bit 0 is the LSB, # and bit 7 is the MSB slice.reverse_each do |bit| byte <<= 1 byte |= 1 if bit.positive? end byte end.pack("C*") end |
#to_16i ⇒ Object
Given an array of 16-bit unsigned integers, turn it into an array of 16-bit signed integers.
43 44 45 |
# File 'lib/rmodbus/ext.rb', line 43 def to_16i pack("n*").unpack("s>*") end |
#to_32f ⇒ Object
Given an array of 16-bit unsigned integers, turn it into an array of 32-bit floats, halving the size. The pairs of 16-bit elements should be in big endian order.
49 50 51 52 |
# File 'lib/rmodbus/ext.rb', line 49 def to_32f even_elements_check pack("n*").unpack("g*") end |
#to_32i ⇒ Object
Given an array of 16-bit unsigned integers, turn it into 32-bit signed integers, halving the size. The pairs of 16-bit elements should be in big endian order.
68 69 70 71 |
# File 'lib/rmodbus/ext.rb', line 68 def to_32i even_elements_check pack("n*").unpack("l>*") end |
#to_32u ⇒ Object
Given an array of 16-bit unsigned integers, turn it into 32-bit unsigned integers, halving the size. The pairs of 16-bit elements should be in big endian order.
61 62 63 64 |
# File 'lib/rmodbus/ext.rb', line 61 def to_32u even_elements_check pack("n*").unpack("N*") end |