Class: Modbus::Transaction::Server
- Defined in:
- lib/modbus/transaction/server.rb
Class Method Summary collapse
-
.recv_adu(buffer, conn) ⇒ true, false
Try to decode a response ADU from some recevied bytes and handle the ADU if decoding was successful.
Instance Method Summary collapse
- #handle_read_holding_registers ⇒ Object
- #handle_read_input_registers ⇒ Object
-
#handle_request(adu) ⇒ Object
Handles a recevied ADU and calls the relevant callback.
- #handle_write_multiple_registers ⇒ Object
- #read_registers(offset, response_class) ⇒ Object
-
#send_pdu(pdu) ⇒ Modbus::TCPADU
Constructs a ADU using a PDU and send it asynchronusly to the server.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Modbus::Transaction::Base
Class Method Details
.recv_adu(buffer, conn) ⇒ true, false
Try to decode a response ADU from some recevied bytes and handle the ADU if decoding was successful.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/modbus/transaction/server.rb', line 17 def self.recv_adu(buffer, conn) adu = Modbus::TCPADU.new if adu.decode :request, buffer, conn transaction = self.new conn transaction.handle_request adu return true else return false end end |
Instance Method Details
#handle_read_holding_registers ⇒ Object
68 69 70 |
# File 'lib/modbus/transaction/server.rb', line 68 def handle_read_holding_registers read_registers 40001, PDU::ReadHoldingRegistersResponse end |
#handle_read_input_registers ⇒ Object
63 64 65 |
# File 'lib/modbus/transaction/server.rb', line 63 def handle_read_input_registers read_registers 30001, PDU::ReadInputRegistersResponse end |
#handle_request(adu) ⇒ Object
Handles a recevied ADU and calls the relevant callback. The corresponding request ADU is matched and cleaned up.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/modbus/transaction/server.rb', line 48 def handle_request(adu) @request_adu = adu transaction = TRANSACTIONS.find { |t| adu.pdu.is_a? t[:request] } fail IllegalFunction, "Unknown PDU #{adu.pdu.inspect}" unless transaction fail ServerDeviceFailure, "Unexpected last sent PDU: #{@request_adu.pdu.inspect}" unless @request_adu.pdu.is_a? transaction[:request] pdu = send transaction[:handler] send_pdu pdu rescue ModbusError => error send_pdu PDU::Exception.create(adu.pdu.func_code, error) end |
#handle_write_multiple_registers ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/modbus/transaction/server.rb', line 81 def handle_write_multiple_registers reg_count = @conn.write_registers 40001 + @request_adu.pdu.start_addr, @request_adu.pdu.reg_values pdu = PDU::WriteMultipleRegistersResponse.new pdu.start_addr = @request_adu.pdu.start_addr pdu.reg_count = reg_count pdu end |
#read_registers(offset, response_class) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/modbus/transaction/server.rb', line 73 def read_registers(offset, response_class) reg_values = @conn.read_registers offset + @request_adu.pdu.start_addr, @request_adu.pdu.reg_count pdu = response_class.new pdu.reg_values = reg_values pdu end |
#send_pdu(pdu) ⇒ Modbus::TCPADU
Constructs a ADU using a PDU and send it asynchronusly to the server. The created ADU is stored internally and is matched to the response when the response is available.
36 37 38 39 40 |
# File 'lib/modbus/transaction/server.rb', line 36 def send_pdu(pdu) @response_adu = TCPADU.new pdu, @request_adu.transaction_ident @conn.send_data @response_adu.encode self end |