Class: Modbus::PDU::WriteMultipleRegistersRequest

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

Overview

PDU for modbus function “read holding register” (request 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) ⇒ WriteMultipleRegistersRequest

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

Parameters:



21
22
23
24
25
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 21

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

Instance Attribute Details

#reg_valuesObject

Returns the value of attribute reg_values.



14
15
16
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 14

def reg_values
  @reg_values
end

#start_addrObject

Returns the value of attribute start_addr.



14
15
16
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 14

def start_addr
  @start_addr
end

Instance Method Details

#byte_countInteger

Returns the length of the register values in bytes.

Returns:

  • (Integer)

    The length.



63
64
65
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 63

def byte_count
  reg_count * 2
end

#decode(data) ⇒ Object

Decodes a PDU from protocol data.

Parameters:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 32

def decode(data)
  @start_addr = data.shift_word

  reg_count = data.shift_word
  fail ClientError, "Register count must be in (1..127), got #{reg_count}" unless (1..127).include?(reg_count)

  byte_count = data.shift_byte
  fail ClientError, "Byte count does not match available data, expected #{byte_count} bytes, got #{data.size} bytes." unless byte_count == data.size

  reg_count.times { @reg_values.push data.shift_word }
end

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



49
50
51
52
53
54
55
56
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 49

def encode
  data = super
  data.push_word @start_addr
  data.push_word reg_count
  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.



81
82
83
84
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 81

def length
  # +1 for func_code, +2 for starting address, +2 for reg_count, +1 for byte_count
  byte_count + 6
end

#reg_countInteger

Returns the number of registers to write.

Returns:

  • (Integer)

    The number of registers.



72
73
74
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 72

def reg_count
  @reg_values.size
end

#validateObject

Validates the PDU. Raises exceptions if validation fails.



89
90
91
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 89

def validate

end