class Modbus::PDU::WriteMultipleRegistersRequest

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

Constants

FUNC_CODE

Attributes

reg_values[RW]
start_addr[RW]

Public Class Methods

new(data = nil, func_code = nil) click to toggle source

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

@param data [Modbus::ProtocolData] The protocol data to decode.

Calls superclass method Modbus::PDU::new
# File lib/modbus/pdu/write_multiple_registers.rb, line 21
def initialize(data = nil, func_code = nil)
  @start_addr = 0
  @reg_values = []
  super
end

Public Instance Methods

byte_count() click to toggle source

Returns the length of the register values in bytes.

@return [Integer] The length.

# File lib/modbus/pdu/write_multiple_registers.rb, line 63
def byte_count
  reg_count * 2
end
decode(data) click to toggle source

Decodes a PDU from protocol data.

@param data [Modbus::ProtocolData] The protocol data to decode.

# 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
encode() click to toggle source

Encodes a PDU into protocol data.

@return [Modbus::ProtocolData] The protocol data representation of this object.

Calls superclass method Modbus::PDU#encode
# 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
length() click to toggle source

Returns the length of the PDU in bytes.

@return [Integer] The length.

# 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_count() click to toggle source

Returns the number of registers to write.

@return [Integer] The number of registers.

# File lib/modbus/pdu/write_multiple_registers.rb, line 72
def reg_count
  @reg_values.size
end
validate() click to toggle source

Validates the PDU. Raises exceptions if validation fails.

# File lib/modbus/pdu/write_multiple_registers.rb, line 89
def validate

end