Module: Aurora::ModBus::RTU

Defined in:
lib/aurora/modbus/slave.rb

Instance Method Summary collapse

Instance Method Details

#read_rtu_response(io) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aurora/modbus/slave.rb', line 33

def read_rtu_response(io)
  # Read the slave_id and function code
  msg = read(io, 2)

  function_code = msg.getbyte(1)
  case function_code
  when 1, 2, 3, 4, 65, 66
    # read the third byte to find out how much more
    # we need to read + CRC
    msg += read(io, 1)
    msg += read(io, msg.getbyte(2) + 2)
  when 5, 6, 15, 16
    # We just read in an additional 6 bytes
    msg += read(io, 6)
  when 22
    msg += read(io, 8)
  when 0x80..0xff
    msg += read(io, 3)
  else
    log "Rx (#{msg.size} bytes): " + logging_bytes(msg)
    raise ::ModBus::Errors::IllegalFunction, "Illegal function: #{function_code}"
  end
  msg
end