Class: Modbus::TCPADU
- Inherits:
-
Object
- Object
- Modbus::TCPADU
- Defined in:
- lib/modbus/adu/tcp_adu.rb
Overview
ADU (Application Data Unit) for transport over TCP
Instance Attribute Summary collapse
-
#pdu ⇒ Object
Returns the value of attribute pdu.
-
#protocol_ident ⇒ Object
Returns the value of attribute protocol_ident.
-
#transaction_ident ⇒ Object
Returns the value of attribute transaction_ident.
-
#unit_ident ⇒ Object
Returns the value of attribute unit_ident.
Instance Method Summary collapse
-
#decode(type, buffer, conn) ⇒ true, false
Decodes an ADU from wire format and sets the attributes of this object.
-
#encode ⇒ String
Encodes the ADU into the wire format.
-
#initialize(pdu = nil, transaction_ident = 0) ⇒ TCPADU
constructor
Initializes a new TCPADU instance.
Constructor Details
#initialize(pdu = nil, transaction_ident = 0) ⇒ TCPADU
Initializes a new TCPADU instance.
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
#pdu ⇒ Object
Returns the value of attribute pdu.
10 11 12 |
# File 'lib/modbus/adu/tcp_adu.rb', line 10 def pdu @pdu end |
#protocol_ident ⇒ Object
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_ident ⇒ Object
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_ident ⇒ Object
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.
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 |
#encode ⇒ String
Encodes the ADU into the wire format.
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 |