Class: ModBus::Client::Slave
- Inherits:
-
Object
- Object
- ModBus::Client::Slave
- Defined in:
- lib/rmodbus/client/slave.rb
Constant Summary collapse
- EXCEPTIONS =
{ 1 => IllegalFunction, 2 => IllegalDataAddress, 3 => IllegalDataValue, 4 => SlaveDeviceFailure, 5 => Acknowledge, 6 => SlaveDeviceBus, 8 => MemoryParityError }.freeze
Instance Attribute Summary collapse
-
#uid ⇒ Object
Number of times to retry on read and read timeouts.
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
-
#coils ⇒ ReadWriteProxy
Returns a ModBus::ReadWriteProxy hash interface for coils.
-
#discrete_inputs ⇒ ReadOnlyProxy
Returns a ModBus::ReadOnlyProxy hash interface for discrete inputs.
-
#holding_registers ⇒ ReadWriteProxy
Returns a ModBus::ReadWriteProxy hash interface for holding registers.
-
#initialize(uid, io) ⇒ Slave
constructor
A new instance of Slave.
-
#input_registers ⇒ ReadOnlyProxy
Returns a read/write ModBus::ReadOnlyProxy hash interface for coils.
-
#mask_write_register(addr, and_mask, or_mask) ⇒ Object
Mask a holding register.
-
#query(request) ⇒ String
Request pdu to slave device.
- #read_coil(addr) ⇒ Object
-
#read_coils(addr, ncoils = 1) ⇒ Array
Read coils.
- #read_discrete_input(addr) ⇒ Object
-
#read_discrete_inputs(addr, ninputs = 1) ⇒ Array
Read discrete inputs.
- #read_holding_register(addr) ⇒ Object
-
#read_holding_registers(addr, nregs = 1) ⇒ Array
Read holding registers.
- #read_input_register(addr) ⇒ Object
-
#read_input_registers(addr, nregs = 1) ⇒ Array
Read input registers.
-
#read_write_multiple_registers(addr_r, nregs, addr_w, vals) ⇒ Array
(also: #read_write_holding_registers)
Read/write multiple holding registers.
-
#write_multiple_coils(addr, vals) ⇒ Object
(also: #write_coils)
Write multiple coils.
-
#write_multiple_registers(addr, vals) ⇒ Object
(also: #write_holding_registers)
Write multiple holding registers.
-
#write_single_coil(addr, val) ⇒ Object
(also: #write_coil)
Write a single coil.
-
#write_single_register(addr, val) ⇒ Object
(also: #write_holding_register)
Write a single holding register.
Constructor Details
#initialize(uid, io) ⇒ Slave
Returns a new instance of Slave.
24 25 26 27 |
# File 'lib/rmodbus/client/slave.rb', line 24 def initialize(uid, io) @uid = uid @io = io end |
Instance Attribute Details
#uid ⇒ Object
Number of times to retry on read and read timeouts
13 14 15 |
# File 'lib/rmodbus/client/slave.rb', line 13 def uid @uid end |
Instance Method Details
#coils ⇒ ReadWriteProxy
Returns a ModBus::ReadWriteProxy hash interface for coils
38 39 40 |
# File 'lib/rmodbus/client/slave.rb', line 38 def coils ModBus::ReadWriteProxy.new(self, :coil) end |
#discrete_inputs ⇒ ReadOnlyProxy
Returns a ModBus::ReadOnlyProxy hash interface for discrete inputs
109 110 111 |
# File 'lib/rmodbus/client/slave.rb', line 109 def discrete_inputs ModBus::ReadOnlyProxy.new(self, :discrete_input) end |
#holding_registers ⇒ ReadWriteProxy
Returns a ModBus::ReadWriteProxy hash interface for holding registers
165 166 167 |
# File 'lib/rmodbus/client/slave.rb', line 165 def holding_registers ModBus::ReadWriteProxy.new(self, :holding_register) end |
#input_registers ⇒ ReadOnlyProxy
Returns a read/write ModBus::ReadOnlyProxy hash interface for coils
136 137 138 |
# File 'lib/rmodbus/client/slave.rb', line 136 def input_registers ModBus::ReadOnlyProxy.new(self, :input_register) end |
#mask_write_register(addr, and_mask, or_mask) ⇒ Object
Mask a holding register
222 223 224 225 |
# File 'lib/rmodbus/client/slave.rb', line 222 def mask_write_register(addr, and_mask, or_mask) query("\x16#{addr.to_word}#{and_mask.to_word}#{or_mask.to_word}") self end |
#query(request) ⇒ String
Request pdu to slave device
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/rmodbus/client/slave.rb', line 263 def query(request) tried = 0 response = "" begin ::Timeout.timeout(@read_retry_timeout, ModBusTimeout) do send_pdu(request) response = read_pdu unless uid.zero? end rescue ModBusTimeout log "Timeout of read operation: (#{@read_retries - tried})" tried += 1 retry unless tried >= @read_retries raise ModBusTimeout.new, "Timed out during read attempt" end return nil if response.empty? read_func = response.getbyte(0) if read_func >= 0x80 exc_id = response.getbyte(1) raise EXCEPTIONS[exc_id] unless EXCEPTIONS[exc_id].nil? raise ModBusException.new, "Unknown error" end check_response_mismatch(request, response) if raise_exception_on_mismatch response[2..-1] end |
#read_coil(addr) ⇒ Object
54 55 56 |
# File 'lib/rmodbus/client/slave.rb', line 54 def read_coil(addr) read_coils(addr, 1).first end |
#read_coils(addr, ncoils = 1) ⇒ Array
Read coils
50 51 52 |
# File 'lib/rmodbus/client/slave.rb', line 50 def read_coils(addr, ncoils = 1) query("\x01#{addr.to_word}#{ncoils.to_word}").unpack_bits[0..ncoils - 1] end |
#read_discrete_input(addr) ⇒ Object
125 126 127 |
# File 'lib/rmodbus/client/slave.rb', line 125 def read_discrete_input(addr) read_discrete_inputs(addr, 1).first end |
#read_discrete_inputs(addr, ninputs = 1) ⇒ Array
Read discrete inputs
@param ninputs number inputs
121 122 123 |
# File 'lib/rmodbus/client/slave.rb', line 121 def read_discrete_inputs(addr, ninputs = 1) query("\x02#{addr.to_word}#{ninputs.to_word}").unpack_bits[0..ninputs - 1] end |
#read_holding_register(addr) ⇒ Object
181 182 183 |
# File 'lib/rmodbus/client/slave.rb', line 181 def read_holding_register(addr) read_holding_registers(addr, 1).first end |
#read_holding_registers(addr, nregs = 1) ⇒ Array
Read holding registers
177 178 179 |
# File 'lib/rmodbus/client/slave.rb', line 177 def read_holding_registers(addr, nregs = 1) query("\x03#{addr.to_word}#{nregs.to_word}").unpack("n*") end |
#read_input_register(addr) ⇒ Object
152 153 154 |
# File 'lib/rmodbus/client/slave.rb', line 152 def read_input_register(addr) read_input_registers(addr, 1).first end |
#read_input_registers(addr, nregs = 1) ⇒ Array
Read input registers
148 149 150 |
# File 'lib/rmodbus/client/slave.rb', line 148 def read_input_registers(addr, nregs = 1) query("\x04#{addr.to_word}#{nregs.to_word}").unpack("n*") end |
#read_write_multiple_registers(addr_r, nregs, addr_w, vals) ⇒ Array Also known as: read_write_holding_registers
Read/write multiple holding registers
237 238 239 240 241 242 243 |
# File 'lib/rmodbus/client/slave.rb', line 237 def read_write_multiple_registers(addr_r, nregs, addr_w, vals) s_val = vals.map(&:to_word).join query("\x17#{addr_r.to_word}#{nregs.to_word}#{addr_w.to_word}" \ "#{vals.size.to_word}#{(vals.size * 2).chr}#{s_val}") .unpack("n*") end |
#write_multiple_coils(addr, vals) ⇒ Object Also known as: write_coils
Write multiple coils
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rmodbus/client/slave.rb', line 83 def write_multiple_coils(addr, vals) nbyte = ((vals.size - 1) >> 3) + 1 sum = 0 (vals.size - 1).downto(0) do |i| sum <<= 1 sum |= 1 if vals[i].positive? end s_val = +"" nbyte.times do s_val << (sum & 0xff).chr sum >>= 8 end query("\x0f#{addr.to_word}#{vals.size.to_word}#{nbyte.chr}#{s_val}") self end |
#write_multiple_registers(addr, vals) ⇒ Object Also known as: write_holding_registers
Write multiple holding registers
207 208 209 210 211 212 |
# File 'lib/rmodbus/client/slave.rb', line 207 def write_multiple_registers(addr, vals) s_val = vals.map(&:to_word).join query("\x10#{addr.to_word}#{vals.size.to_word}#{(vals.size * 2).chr}#{s_val}") self end |
#write_single_coil(addr, val) ⇒ Object Also known as: write_coil
Write a single coil
66 67 68 69 70 71 72 73 |
# File 'lib/rmodbus/client/slave.rb', line 66 def write_single_coil(addr, val) if [0, false].include?(val) query("\x05#{addr.to_word}\x00\x00") else query("\x05#{addr.to_word}\xff\x00") end self end |
#write_single_register(addr, val) ⇒ Object Also known as: write_holding_register
Write a single holding register
193 194 195 196 |
# File 'lib/rmodbus/client/slave.rb', line 193 def write_single_register(addr, val) query("\x06#{addr.to_word}#{val.to_word}") self end |