Class: ModBus::RTUSlave

Inherits:
Client::Slave show all
Includes:
RTU
Defined in:
lib/rmodbus/rtu_slave.rb

Overview

RTU slave implementation

Examples:

RTUClient.connect(port, baud, opts) do |cl|
  cl.with_slave(uid) do |slave|
    slave.holding_registers[0..100]
  end
end

See Also:

Constant Summary

Constants inherited from Client::Slave

Client::Slave::Exceptions

Instance Attribute Summary

Attributes inherited from Client::Slave

#uid

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

Methods included from RTU

#clean_input_buff, #crc16, #read, #read_rtu_request, #read_rtu_response, #serve

Methods inherited from Client::Slave

#check_response_mismatch, #coils, #discrete_inputs, #holding_registers, #initialize, #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

#log, #logging_bytes

Constructor Details

This class inherits a constructor from ModBus::Client::Slave

Instance Method Details

#read_pduObject (private)

overide method for RTU implamentaion

See Also:

  • Slave#query


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rmodbus/rtu_slave.rb', line 31

def read_pdu
  msg = read_rtu_response(@io)

  log "Rx (#{msg.size} bytes): " + logging_bytes(msg)

  if msg.getbyte(0) == @uid
    return msg[1..-3] if msg[-2,2].unpack('S<')[0] == crc16(msg[0..-3])
    log "Ignore package: don't match CRC"
  else
    log "Ignore package: don't match uid ID"
  end
  loop do
    #waite timeout
    sleep(0.1)
  end
end

#send_pdu(pdu) ⇒ Object (private)

overide method for RTU implamentaion

See Also:

  • Slave#query


19
20
21
22
23
24
25
26
27
# File 'lib/rmodbus/rtu_slave.rb', line 19

def send_pdu(pdu)
  msg = @uid.chr + pdu
  msg << [crc16(msg)].pack("S<")

  clean_input_buff
  @io.write msg

  log "Tx (#{msg.size} bytes): " + logging_bytes(msg)
end