Class: Modbus::PDU::WriteMultipleRegistersResponse

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

Overview

PDU for modbus function “read holding register” (response message)

Constant Summary collapse

FUNC_CODE =
0x10

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

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

Parameters:



108
109
110
111
112
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 108

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.



101
102
103
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 101

def reg_count
  @reg_count
end

#start_addrObject

Returns the value of attribute start_addr.



101
102
103
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 101

def start_addr
  @start_addr
end

Instance Method Details

#decode(data) ⇒ Object

Decodes a PDU from protocol data.

Parameters:



119
120
121
122
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 119

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

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



129
130
131
132
133
134
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 129

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.



141
142
143
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 141

def length
  5
end

#validateObject

Validates the PDU. Raises exceptions if validation fails.



148
149
150
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 148

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