Class: Modbus::PDU::ReadBitsResponse

Inherits:
Modbus::PDU show all
Defined in:
lib/modbus/pdu/read_bits.rb

Overview

Base class PDU for modbus bit based functions (response message)

Direct Known Subclasses

ReadCoilsResponse, ReadInputStatusResponse

Constant Summary

Constants inherited from Modbus::PDU

REQ_PDU_MAP, RSP_PDU_MAP

Instance Attribute Summary collapse

Attributes inherited from Modbus::PDU

#creation_time, #func_code

Instance Method Summary collapse

Methods inherited from Modbus::PDU

create

Constructor Details

#initialize(data = nil, func_code = nil) ⇒ ReadBitsResponse

Initializes a new PDU instance. Decodes from protocol data if given.

Parameters:



76
77
78
79
# File 'lib/modbus/pdu/read_bits.rb', line 76

def initialize(data = nil, func_code = nil)
  @bit_values = []
  super
end

Instance Attribute Details

#bit_valuesObject

Returns the value of attribute bit_values.



69
70
71
# File 'lib/modbus/pdu/read_bits.rb', line 69

def bit_values
  @bit_values
end

Instance Method Details

#byte_countInteger

Returns the length of the register values in bytes.

Returns:

  • (Integer)

    The length.



116
117
118
# File 'lib/modbus/pdu/read_bits.rb', line 116

def byte_count
  @bit_values.size
end

#decode(data) ⇒ Object

Decodes a PDU from protocol data.

Parameters:



86
87
88
89
90
91
92
93
94
95
# File 'lib/modbus/pdu/read_bits.rb', line 86

def decode(data)
  byte_count = data.shift_byte
  byte_count.times do
    byte = data.shift_byte

    8.times do |bit|
      @bit_values.push byte[bit] == 1
    end
  end
end

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



102
103
104
105
106
107
108
109
# File 'lib/modbus/pdu/read_bits.rb', line 102

def encode
  data = super
  data.push_byte byte_count
  @bit_values.each do |value|
    data.push_byte value
  end
  data
end

#lengthInteger

Returns the length of the PDU in bytes.

Returns:

  • (Integer)

    The length.



125
126
127
128
# File 'lib/modbus/pdu/read_bits.rb', line 125

def length
  # +1 for func_code, +1 for byte_count
  byte_count + 2
end

#validateObject

Validates the PDU. Raises exceptions if validation fails.



133
134
135
# File 'lib/modbus/pdu/read_bits.rb', line 133

def validate

end