Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/rmodbus/ext.rb
Instance Method Summary collapse
-
#from_32f ⇒ Object
Given an array of 32bit Floats, we turn it into an array of 16bit Fixnums, doubling the size.
-
#from_32i ⇒ Object
Given an array of 32bit Fixnum, we turn it into an array of 16bit fixnums, doubling the size.
- #pack_to_word ⇒ Object
-
#to_32f ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit Int in big-endian order, halving the size.
-
#to_32f_le ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit Int in little-endian order, halving the size.
-
#to_32i ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit Float in big-endian order, halving the size.
Instance Method Details
#from_32f ⇒ Object
Given an array of 32bit Floats, we turn it into an array of 16bit Fixnums, doubling the size
49 50 51 |
# File 'lib/rmodbus/ext.rb', line 49 def from_32f self.pack('g*').unpack('n*').each_slice(2).map { |arr| arr.reverse }.flatten end |
#from_32i ⇒ Object
Given an array of 32bit Fixnum, we turn it into an array of 16bit fixnums, doubling the size
60 61 62 |
# File 'lib/rmodbus/ext.rb', line 60 def from_32i self.pack('N*').unpack('n*').each_slice(2).map { |arr| arr.reverse }.flatten end |
#pack_to_word ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rmodbus/ext.rb', line 64 def pack_to_word word = 0 s = "" mask = 0x01 self.each do |bit| word |= mask if bit > 0 mask <<= 1 if mask == 0x100 mask = 0x01 s << word.chr word = 0 end end unless mask == 0x01 s << word.chr else s end end |
#to_32f ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit Int in big-endian order, halving the size
37 38 39 40 |
# File 'lib/rmodbus/ext.rb', line 37 def to_32f raise "Array requires an even number of elements to pack to 32bits: was #{self.size}" unless self.size.even? self.each_slice(2).map { |(lsb, msb)| [msb, lsb].pack('n*').unpack('g')[0] } end |
#to_32f_le ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit Int in little-endian order, halving the size
43 44 45 46 |
# File 'lib/rmodbus/ext.rb', line 43 def to_32f_le raise "Array requires an even number of elements to pack to 32bits: was #{self.size}" unless self.size.even? self.each_slice(2).map { |(lsb, msb)| [lsb, msb].pack('n*').unpack('g')[0] } end |
#to_32i ⇒ Object
Given an array of 16bit Fixnum, we turn it into 32bit Float in big-endian order, halving the size
54 55 56 57 |
# File 'lib/rmodbus/ext.rb', line 54 def to_32i raise "Array requires an even number of elements to pack to 32bits: was #{self.size}" unless self.size.even? self.each_slice(2).map { |(lsb, msb)| [msb, lsb].pack('n*').unpack('N')[0] } end |