class Modbus::PDU::WriteSingleCoilRequest

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

Constants

FUNC_CODE

Attributes

start_addr[RW]
value[RW]

Public Class Methods

new(data = nil, func_code = nil) click to toggle source

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

@param data [Modbus::ProtocolData] The protocol data to decode.

Calls superclass method Modbus::PDU::new
# File lib/modbus/pdu/write_single_coil.rb, line 21
def initialize(data = nil, func_code = nil)
  @start_addr = 0
  @value      = 0
  super
end

Public Instance Methods

decode(data) click to toggle source

Decodes a PDU from protocol data.

@param data [Modbus::ProtocolData] The protocol data to decode.

# File lib/modbus/pdu/write_single_coil.rb, line 32
def decode(data)
  @start_addr = data.shift_word
  @value      = data.shift_word
end
encode() click to toggle source

Encodes a PDU into protocol data.

@return [Modbus::ProtocolData] The protocol data representation of this object.

Calls superclass method Modbus::PDU#encode
# 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
length() click to toggle source

Returns the length of the PDU in bytes.

@return [Integer] The length.

# File lib/modbus/pdu/write_single_coil.rb, line 54
def length
  5
end
validate() click to toggle source

Validates the PDU. Raises exceptions if validation fails.

# 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