Class: Snap7::Variable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ident, address, type) ⇒ Variable

Returns a new instance of Variable.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ffi-snap7/data_structures/variable.rb', line 8

def initialize(ident, address, type)
  addr_parts = address.scan(/\d+/)

  if address =~ /^DBX/
    fail "Invalid bit address '#{address}'" unless addr_parts.size == 2
  end

  @ident   = ident
  @address = address
  @byte    = addr_parts.first.to_i
  @bit     = addr_parts.last.to_i if addr_parts.size == 2
  @type    = type
end

Instance Attribute Details

#bitObject (readonly)

Returns the value of attribute bit.



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

def bit
  @bit
end

#byteObject (readonly)

Returns the value of attribute byte.



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

def byte
  @byte
end

#dbObject

Returns the value of attribute db.



5
6
7
# File 'lib/ffi-snap7/data_structures/variable.rb', line 5

def db
  @db
end

#identObject (readonly)

Returns the value of attribute ident.



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

def ident
  @ident
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#addressObject



44
45
46
# File 'lib/ffi-snap7/data_structures/variable.rb', line 44

def address
  ["DB#{db.number}", @address].compact.join('.')
end

#bit_sizeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ffi-snap7/data_structures/variable.rb', line 23

def bit_size
  case @address
  when /^DBD/ # double (32 bit)
    32
  when /^DBW/ # word   (16 bit)
    16
  when /^DBB/ # byte   (8 bit)
    8
  when /^DBX/ # bit    (1 bit)
    1
  else
    fail "Invalid address '#{@address}' of variable #{@ident}. Cannot determine size."
  end
end

#byte_sizeObject



39
40
41
# File 'lib/ffi-snap7/data_structures/variable.rb', line 39

def byte_size
  @byte_size ||= (bit_size / 8.0).ceil
end

#decode(data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ffi-snap7/data_structures/variable.rb', line 49

def decode(data)
  var_data = data[byte, byte_size]

  case @type
  when :bool
    var_data.first[@bit] > 0

  when :int8
    var_data.pack('C1').unpack('c').first

  when :uint8
    var_data.pack('C1').unpack('C').first

  when :int16
    var_data.reverse.pack('C2').unpack('s').first

  when :uint16
    var_data.reverse.pack('C2').unpack('S').first

  when :int32
    var_data.reverse.pack('C4').unpack('l').first

  when :uint32
    var_data.reverse.pack('C4').unpack('L').first

  when :float32
    var_data.reverse.pack('C4').unpack('f').first

  else
    fail "Invalid type '#{@type}' of variable #{@ident}. Cannot decode data."
  end
end

#encode(value) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ffi-snap7/data_structures/variable.rb', line 83

def encode(value)
  case @type
  when :int8, :uint8
    [value]

  when :int16, :uint16
    [value].pack('s').unpack('C2').reverse

  when :int32, :uint32
    [value].pack('l').unpack('C4').reverse

  when :float32
    [value].pack('f').unpack('C4').reverse

  else
    fail "Invalid type '#{@type}' of variable #{@ident}. Cannot encode data."
  end
end