Class: Modbus::PDU::ReadRegistersRequest

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

Overview

Base class PDU for modbus register based functions (request 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) ⇒ ReadRegistersRequest

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

Parameters:



19
20
21
22
23
# File 'lib/modbus/pdu/read_registers.rb', line 19

def initialize(data = nil, func_code = nil)
  @start_addr = 0
  @reg_count  = 0
  super
end

Instance Attribute Details

#reg_countObject

Returns the value of attribute reg_count.



12
13
14
# File 'lib/modbus/pdu/read_registers.rb', line 12

def reg_count
  @reg_count
end

#start_addrObject

Returns the value of attribute start_addr.



12
13
14
# File 'lib/modbus/pdu/read_registers.rb', line 12

def start_addr
  @start_addr
end

Instance Method Details

#decode(data) ⇒ Object

Decodes a PDU from protocol data.

Parameters:



30
31
32
33
# File 'lib/modbus/pdu/read_registers.rb', line 30

def decode(data)
  @start_addr = data.shift_word
  @reg_count  = data.shift_word
end

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



40
41
42
43
44
45
# File 'lib/modbus/pdu/read_registers.rb', line 40

def encode
  data = super
  data.push_word @start_addr
  data.push_word @reg_count
  data
end

#lengthInteger

Returns the length of the PDU in bytes.

Returns:

  • (Integer)

    The length.



52
53
54
# File 'lib/modbus/pdu/read_registers.rb', line 52

def length
  5
end

#validateObject

Validates the PDU. Raises exceptions if validation fails.



59
60
61
# File 'lib/modbus/pdu/read_registers.rb', line 59

def validate
  fail ClientError, "Register count must be in (1..127), got '#{@reg_count.inspect}'" unless (1..127).include?(@reg_count)
end