Class: Modbus::PDU::WriteSingleCoilRequest

Inherits:
Modbus::PDU
  • Object
show all
Defined in:
lib/modbus/pdu/write_single_coil.rb

Overview

PDU for modbus function “read single coil” (request message)

Direct Known Subclasses

WriteSingleCoilResponse

Constant Summary collapse

FUNC_CODE =
0x05

Constants inherited from Modbus::PDU

REQ_PDU_MAP, RSP_PDU_MAP

Instance Attribute Summary collapse

Attributes inherited from Modbus::PDU

#creation_time, #func_code

Instance Method Summary collapse

Methods inherited from Modbus::PDU

create

Constructor Details

#initialize(data = nil, func_code = nil) ⇒ WriteSingleCoilRequest

Initializes a new PDU instance. Decodes from protocol data if given.

Parameters:



21
22
23
24
25
# File 'lib/modbus/pdu/write_single_coil.rb', line 21

def initialize(data = nil, func_code = nil)
  @start_addr = 0
  @value      = 0
  super
end

Instance Attribute Details

#start_addrObject

Returns the value of attribute start_addr.



14
15
16
# File 'lib/modbus/pdu/write_single_coil.rb', line 14

def start_addr
  @start_addr
end

#valueObject

Returns the value of attribute value.



14
15
16
# File 'lib/modbus/pdu/write_single_coil.rb', line 14

def value
  @value
end

Instance Method Details

#decode(data) ⇒ Object

Decodes a PDU from protocol data.

Parameters:



32
33
34
35
# File 'lib/modbus/pdu/write_single_coil.rb', line 32

def decode(data)
  @start_addr = data.shift_word
  @value      = data.shift_word
end

#encodeModbus::ProtocolData

Encodes a PDU into protocol data.

Returns:



42
43
44
45
46
47
# File 'lib/modbus/pdu/write_single_coil.rb', line 42

def encode
  data = super
  data.push_word @start_addr
  data.push_word @value
  data
end

#lengthInteger

Returns the length of the PDU in bytes.

Returns:

  • (Integer)

    The length.



54
55
56
# File 'lib/modbus/pdu/write_single_coil.rb', line 54

def length
  5
end

#validateObject

Validates the PDU. Raises exceptions if validation fails.



61
62
63
# File 'lib/modbus/pdu/write_single_coil.rb', line 61

def validate
  fail ClientError, "Value must be 0x0000 or 0xFF00, got '#{@value}'" unless [0x0000, 0xFF00].include?(@value)
end