Class: Snap7::DataBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-snap7/data_structures/data_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_addr) ⇒ DataBlock

Returns a new instance of DataBlock.



6
7
8
9
# File 'lib/ffi-snap7/data_structures/data_block.rb', line 6

def initialize(db_addr)
  @number    = db_addr.scan(/\d+/).first.to_i
  @variables = []
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/ffi-snap7/data_structures/data_block.rb', line 4

def number
  @number
end

#variablesObject (readonly)

Returns the value of attribute variables.



4
5
6
# File 'lib/ffi-snap7/data_structures/data_block.rb', line 4

def variables
  @variables
end

Instance Method Details

#add_variable(var) ⇒ Object



12
13
14
15
# File 'lib/ffi-snap7/data_structures/data_block.rb', line 12

def add_variable(var)
  var.db = self
  @variables << var
end

#decode(data) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/ffi-snap7/data_structures/data_block.rb', line 24

def decode(data)
  @variables.map do |var|
    {
      :ident   => var.ident,
      :address => var.address,
      :value   => var.decode(data)
    }
  end
end

#sizeObject



18
19
20
21
# File 'lib/ffi-snap7/data_structures/data_block.rb', line 18

def size
  last_var = @variables.sort { |a,b| a.byte <=> b.byte }.last
  last_var.byte + last_var.byte_size
end