Class: Modbus::PDU::WriteMultipleRegistersRequest
- Inherits:
-
Modbus::PDU
- Object
- Modbus::PDU
- Modbus::PDU::WriteMultipleRegistersRequest
- 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
Instance Attribute Summary collapse
-
#reg_values ⇒ Object
Returns the value of attribute reg_values.
-
#start_addr ⇒ Object
Returns the value of attribute start_addr.
Attributes inherited from Modbus::PDU
Instance Method Summary collapse
-
#byte_count ⇒ Integer
Returns the length of the register values in bytes.
-
#decode(data) ⇒ Object
Decodes a PDU from protocol data.
-
#encode ⇒ Modbus::ProtocolData
Encodes a PDU into protocol data.
-
#initialize(data = nil, func_code = nil) ⇒ WriteMultipleRegistersRequest
constructor
Initializes a new PDU instance.
-
#length ⇒ Integer
Returns the length of the PDU in bytes.
-
#reg_count ⇒ Integer
Returns the number of registers to write.
-
#validate ⇒ Object
Validates the PDU.
Methods inherited from Modbus::PDU
Constructor Details
#initialize(data = nil, func_code = nil) ⇒ WriteMultipleRegistersRequest
Initializes a new PDU instance. Decodes from protocol data if given.
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_values ⇒ Object
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_addr ⇒ Object
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_count ⇒ Integer
Returns the length of the register values in bytes.
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.
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 |
#encode ⇒ Modbus::ProtocolData
Encodes a PDU into protocol data.
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 |
#length ⇒ Integer
Returns the length of the PDU in bytes.
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_count ⇒ Integer
Returns the number of registers to write.
72 73 74 |
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 72 def reg_count @reg_values.size end |
#validate ⇒ Object
Validates the PDU. Raises exceptions if validation fails.
89 90 91 |
# File 'lib/modbus/pdu/write_multiple_registers.rb', line 89 def validate end |