Class: Modbus::PDU

Inherits:
Object
  • Object
show all
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)

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, func_code = nil) ⇒ PDU

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

Parameters:

  • data (Modbus::ProtocolData) (defaults to: nil)

    The protocol data to decode.

  • func_code (Fixnum) (defaults to: nil)

    Modbus function code.



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_timeObject (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_codeObject (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.

Parameters:

  • type (Symbol)

    The type of PDU which should be created. Must be :request or :response.

  • func_code (Integer)

    The modbus function code of the PDU

  • data (Modbus::ProtocolData)

    The protocol data to decode.

Returns:



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

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



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