Class: Modbus::TCPADU

Inherits:
Object
  • Object
show all
Defined in:
lib/modbus/adu/tcp_adu.rb

Overview

ADU (Application Data Unit) for transport over TCP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdu = nil, transaction_ident = 0) ⇒ TCPADU

Initializes a new TCPADU instance.

Parameters:

  • pdu (Modbus::PDU) (defaults to: nil)

    A PDU to preset in the ADU.



17
18
19
20
21
22
# File 'lib/modbus/adu/tcp_adu.rb', line 17

def initialize(pdu = nil, transaction_ident = 0)
  @transaction_ident = transaction_ident
  @protocol_ident    = 0
  @unit_ident        = 0xFF # TODO configurable?
  @pdu               = pdu
end

Instance Attribute Details

#pduObject

Returns the value of attribute pdu.



10
11
12
# File 'lib/modbus/adu/tcp_adu.rb', line 10

def pdu
  @pdu
end

#protocol_identObject

Returns the value of attribute protocol_ident.



10
11
12
# File 'lib/modbus/adu/tcp_adu.rb', line 10

def protocol_ident
  @protocol_ident
end

#transaction_identObject

Returns the value of attribute transaction_ident.



10
11
12
# File 'lib/modbus/adu/tcp_adu.rb', line 10

def transaction_ident
  @transaction_ident
end

#unit_identObject

Returns the value of attribute unit_ident.



10
11
12
# File 'lib/modbus/adu/tcp_adu.rb', line 10

def unit_ident
  @unit_ident
end

Instance Method Details

#decode(type, buffer, conn) ⇒ true, false

Decodes an ADU from wire format and sets the attributes of this object.

Parameters:

  • type (Symbol)

    The type of PDU which should be created.

  • buffer (String)

    The bytes to decode.

  • conn (Modbus::Connection::Base)

    An EM connection object to work on.

Returns:

  • (true, false)

    True, if there where enough bytes in the buffer and decoding was successful.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/modbus/adu/tcp_adu.rb', line 47

def decode(type, buffer, conn)
  data = ProtocolData.new buffer

  # not enough data in buffer to know the length
  return false if data.size < 6

  @transaction_ident = data.shift_word
  @protocol_ident    = data.shift_word
  length             = data.shift_word

  # not enough data in buffer according to length
  return false if data.size < length

  # Strip the consumed bytes off the buffer, thus NOT consumed bytes remain in the buffer!
  buffer.slice!(0..length + 5)

  @unit_ident = data.shift_byte
  func_code   = data.shift_byte
  @pdu        = PDU.create type, func_code, data

  return true

rescue ModbusError => error
  pdu = PDU::Exception.create func_code, error
  adu = TCPADU.new pdu, @transaction_ident
  conn.send_data adu.encode

  return false
end

#encodeString

Encodes the ADU into the wire format.

Returns:

  • (String)

    The encoded ADU.



29
30
31
32
33
34
35
36
37
# File 'lib/modbus/adu/tcp_adu.rb', line 29

def encode
  data = ProtocolData.new
  data.push_word @transaction_ident
  data.push_word @protocol_ident
  data.push_word @pdu.length + 1
  data.push_byte @unit_ident
  data.concat @pdu.encode
  data.to_buffer
end