Class: ModBus::TCPSlave
- Inherits:
-
Client::Slave
- Object
- Client::Slave
- ModBus::TCPSlave
- Defined in:
- lib/rmodbus/tcp_slave.rb
Overview
TCP slave implementation
Constant Summary
Constants inherited from Client::Slave
Instance Attribute Summary collapse
-
#transaction ⇒ Object
readonly
Returns the value of attribute transaction.
Attributes inherited from Client::Slave
Attributes included from Options
#raise_exception_on_mismatch, #read_retries, #read_retry_timeout
Attributes included from Debug
#logger, #raise_exception_on_mismatch, #read_retries, #read_retry_timeout
Instance Method Summary collapse
-
#initialize(uid, io) ⇒ TCPSlave
constructor
A new instance of TCPSlave.
-
#read_pdu ⇒ Object
private
overide method for RTU over TCP implamentaion.
-
#send_pdu(pdu) ⇒ Object
private
overide method for RTU over TCP implamentaion.
Methods inherited from Client::Slave
#check_response_mismatch, #coils, #discrete_inputs, #holding_registers, #input_registers, #mask_write_register, #query, #read_coil, #read_coils, #read_discrete_input, #read_discrete_inputs, #read_holding_register, #read_holding_registers, #read_input_register, #read_input_registers, #read_write_multiple_registers, #write_multiple_coils, #write_multiple_registers, #write_single_coil, #write_single_register
Methods included from Debug
Constructor Details
#initialize(uid, io) ⇒ TCPSlave
Returns a new instance of TCPSlave.
17 18 19 20 |
# File 'lib/rmodbus/tcp_slave.rb', line 17 def initialize(uid, io) @transaction = 0 super(uid, io) end |
Instance Attribute Details
#transaction ⇒ Object (readonly)
Returns the value of attribute transaction.
14 15 16 |
# File 'lib/rmodbus/tcp_slave.rb', line 14 def transaction @transaction end |
Instance Method Details
#read_pdu ⇒ Object (private)
overide method for RTU over TCP implamentaion
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rmodbus/tcp_slave.rb', line 36 def read_pdu loop do header = @io.read(7) if header trn = header[0,2].unpack('n')[0] len = header[4,2].unpack('n')[0] msg = @io.read(len-1) log "Rx (#{(header + msg).size} bytes): " + logging_bytes(header + msg) if trn == @transaction return msg else log "Transaction number mismatch. A packet is ignored." end end end end |
#send_pdu(pdu) ⇒ Object (private)
overide method for RTU over TCP implamentaion
25 26 27 28 29 30 31 32 |
# File 'lib/rmodbus/tcp_slave.rb', line 25 def send_pdu(pdu) @transaction = 0 if @transaction.next > 65535 @transaction += 1 msg = @transaction.to_word + "\0\0" + (pdu.size + 1).to_word + @uid.chr + pdu @io.write msg log "Tx (#{msg.size} bytes): " + logging_bytes(msg) end |