Class: Modbus::PDU
- Inherits:
-
Object
- Object
- Modbus::PDU
- Defined in:
- lib/modbus/pdu/pdu.rb,
lib/modbus/pdu/exception.rb,
lib/modbus/pdu/read_bits.rb,
lib/modbus/pdu/read_coils.rb,
lib/modbus/pdu/read_registers.rb,
lib/modbus/pdu/read_input_status.rb,
lib/modbus/pdu/write_single_coil.rb,
lib/modbus/pdu/read_input_registers.rb,
lib/modbus/pdu/read_holding_registers.rb,
lib/modbus/pdu/write_multiple_registers.rb
Overview
Base class modelling a Modbus PDU (Protocol Data Unit)
Direct Known Subclasses
Exception, ReadBitsRequest, ReadBitsResponse, ReadRegistersRequest, ReadRegistersResponse, WriteMultipleRegistersRequest, WriteMultipleRegistersResponse, WriteSingleCoilRequest
Defined Under Namespace
Classes: Exception, ReadBitsRequest, ReadBitsResponse, ReadCoilsRequest, ReadCoilsResponse, ReadHoldingRegistersRequest, ReadHoldingRegistersResponse, ReadInputRegistersRequest, ReadInputRegistersResponse, ReadInputStatusRequest, ReadInputStatusResponse, ReadRegistersRequest, ReadRegistersResponse, WriteMultipleRegistersRequest, WriteMultipleRegistersResponse, WriteSingleCoilRequest, WriteSingleCoilResponse
Constant Summary collapse
- REQ_PDU_MAP =
Maps the Modbus function code to the corresponding class (for request messages)
{}
- RSP_PDU_MAP =
Maps the Modbus function code to the corresponding class (for response messages)
{}
Instance Attribute Summary collapse
-
#creation_time ⇒ Object
readonly
Returns the value of attribute creation_time.
-
#func_code ⇒ Object
readonly
Returns the value of attribute func_code.
Class Method Summary collapse
-
.create(type, func_code, data) ⇒ Modbus::PDU
Factory method for creating PDUs.
Instance Method Summary collapse
-
#encode ⇒ Modbus::ProtocolData
Encodes a PDU into protocol data.
-
#initialize(data = nil, func_code = nil) ⇒ PDU
constructor
Initializes a new PDU instance.
Constructor Details
#initialize(data = nil, func_code = nil) ⇒ PDU
Initializes a new PDU instance. Decodes from protocol data if given.
72 73 74 75 76 77 |
# File 'lib/modbus/pdu/pdu.rb', line 72 def initialize(data = nil, func_code = nil) @creation_time = Time.now.utc @func_code = func_code || self.class::FUNC_CODE self.decode data if data end |
Instance Attribute Details
#creation_time ⇒ Object (readonly)
Returns the value of attribute creation_time.
45 46 47 |
# File 'lib/modbus/pdu/pdu.rb', line 45 def creation_time @creation_time end |
#func_code ⇒ Object (readonly)
Returns the value of attribute func_code.
45 46 47 |
# File 'lib/modbus/pdu/pdu.rb', line 45 def func_code @func_code end |
Class Method Details
.create(type, func_code, data) ⇒ Modbus::PDU
Factory method for creating PDUs. Decodes a PDU from protocol data and returns a new PDU instance.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/modbus/pdu/pdu.rb', line 55 def self.create(type, func_code, data) map = { :request => REQ_PDU_MAP, :response => RSP_PDU_MAP }[type] fail ArgumentError, "Type is expected to be :request or :response, got #{type}" unless map # 0x80 is the offset in case of a modbus exception klass = func_code > 0x80 ? PDU::Exception : map[func_code] fail IllegalFunction, "Unknown function code 0x#{func_code.to_s(16)}" if klass.nil? klass.new data, func_code end |
Instance Method Details
#encode ⇒ Modbus::ProtocolData
Encodes a PDU into protocol data.
84 85 86 87 88 |
# File 'lib/modbus/pdu/pdu.rb', line 84 def encode data = ProtocolData.new data.push_byte @func_code data end |