Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/rmodbus/ext.rb

Instance Method Summary collapse

Instance Method Details

#getword(index) ⇒ Object

Get word by index

Parameters:

  • index (Integer)

    index first bytes of word

Returns:

  • unpacked word



22
23
24
# File 'lib/rmodbus/ext.rb', line 22

def getword(index)
  self[index, 2].unpack1("n")
end

#unpack_bitsObject

unpack a string of bytes into an array of integers (0 or 1) representing the bits in those bytes, according to how the ModBus protocol represents coils.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rmodbus/ext.rb', line 7

def unpack_bits
  result = []
  each_byte do |b|
    8.times do
      # least significant bits first within each byte
      result << (b & 0x01)
      b >>= 1
    end
  end
  result
end