Class: Modbus::PDU::ReadRegistersResponse

Inherits:
Modbus::PDU
  • Object
show all
Defined in:
lib/modbus/pdu/read_registers.rb

Overview

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

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) ⇒ ReadRegistersResponse

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

Parameters:



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

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

Instance Attribute Details

#reg_valuesObject

Returns the value of attribute reg_values.



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

def reg_values
  @reg_values
end

Instance Method Details

#byte_countInteger

Returns the length of the register values in bytes.

Returns:

  • (Integer)

    The length.



108
109
110
# File 'lib/modbus/pdu/read_registers.rb', line 108

def byte_count
  @reg_values.size * 2
end

#decode(data) ⇒ Object

Decodes a PDU from protocol data.

Parameters:



86
87
88
89
# File 'lib/modbus/pdu/read_registers.rb', line 86

def decode(data)
  byte_count = data.shift_byte
  byte_count.div(2).times { @reg_values.push data.shift_word }
end

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



96
97
98
99
100
101
# File 'lib/modbus/pdu/read_registers.rb', line 96

def encode
  data = super
  data.push_byte byte_count
  @reg_values.each { |value| data.push_word value }
  data
end

#lengthInteger

Returns the length of the PDU in bytes.

Returns:

  • (Integer)

    The length.



117
118
119
120
# File 'lib/modbus/pdu/read_registers.rb', line 117

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

#validateObject

Validates the PDU. Raises exceptions if validation fails.



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

def validate

end