Class: Simatic::MemoryMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/simatic/memory_mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbal, args = {}) ⇒ MemoryMapper

Returns a new instance of MemoryMapper.



15
16
17
18
19
20
21
# File 'lib/simatic/memory_mapper.rb', line 15

def initialize verbal, args={}
  @verbal = verbal

  parse_address @verbal
  # puts args[:value]
  self.value = args[:value] unless args[:value].nil?
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



12
13
14
# File 'lib/simatic/memory_mapper.rb', line 12

def address
  @address
end

#areaObject (readonly)

Returns the value of attribute area.



10
11
12
# File 'lib/simatic/memory_mapper.rb', line 10

def area
  @area
end

#bitObject (readonly)

Returns the value of attribute bit.



13
14
15
# File 'lib/simatic/memory_mapper.rb', line 13

def bit
  @bit
end

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/simatic/memory_mapper.rb', line 8

def count
  @count
end

#dbObject (readonly)

Returns the value of attribute db.



11
12
13
# File 'lib/simatic/memory_mapper.rb', line 11

def db
  @db
end

#lengthObject (readonly)

Returns the value of attribute length.



7
8
9
# File 'lib/simatic/memory_mapper.rb', line 7

def length
  @length
end

#raw_dataObject

Returns the value of attribute raw_data.



5
6
7
# File 'lib/simatic/memory_mapper.rb', line 5

def raw_data
  @raw_data
end

#raw_valuesObject (readonly)

Returns the value of attribute raw_values.



4
5
6
# File 'lib/simatic/memory_mapper.rb', line 4

def raw_values
  @raw_values
end

#verbalObject (readonly)

Returns the value of attribute verbal.



3
4
5
# File 'lib/simatic/memory_mapper.rb', line 3

def verbal
  @verbal
end

Instance Method Details

#as_bool(raw) ⇒ Object



167
168
# File 'lib/simatic/memory_mapper.rb', line 167

def as_bool raw
end

#as_date(raw_data) ⇒ Object



185
186
# File 'lib/simatic/memory_mapper.rb', line 185

def as_date raw_data
end

#as_int(raw) ⇒ Object



170
171
# File 'lib/simatic/memory_mapper.rb', line 170

def as_int raw
end

#as_real(raw_data) ⇒ Object



176
177
# File 'lib/simatic/memory_mapper.rb', line 176

def as_real raw_data
end

#as_s5time(raw_data) ⇒ Object



179
180
# File 'lib/simatic/memory_mapper.rb', line 179

def as_s5time raw_data
end

#as_string(raw_data) ⇒ Object



188
189
# File 'lib/simatic/memory_mapper.rb', line 188

def as_string raw_data
end

#as_time(raw_data) ⇒ Object



182
183
# File 'lib/simatic/memory_mapper.rb', line 182

def as_time raw_data
end

#as_uint(raw) ⇒ Object



173
174
# File 'lib/simatic/memory_mapper.rb', line 173

def as_uint raw
end

#parse_address(verbal = @verbal) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/simatic/memory_mapper.rb', line 23

def parse_address verbal = @verbal
  /^(DB\s*(?<db>\d+)(\.|\s*))?(?<area>PI|PQ|I|Q|M|DB|T|C)(?<type>X|B|W|D)?\s*?(?<adr>\d+)(\.(?<bit>\d+))?\s*?(\[(?<count>\d+)\])?$/i =~ verbal
  
  @db      = db    ? db.to_i    : 0
  @count   = count ? count.to_i : 1 
  @address = adr   ? adr.to_i   : nil
  @bit     = bit   ? bit.to_i   : nil

  unless @address
    raise "Adressing by #{verbal} is impossible. Cant parse it."
  end

  case area.upcase
  when 'PI','PQ'
    @area = AreaP
  when 'I'
    @area = AreaI
  when 'Q'
    @area = AreaQ
  when 'M'
    @area = AreaM
  when 'DB'
    @area = AreaDB
  when 'T'
    @area = AreaT
  when 'C'
    @area = AreaC
  end

  case type ? type.upcase : 'X'
  when 'B'
    @length = 1
  when 'W'
    @length = 2
  when 'D'
    @length = 4
  else
    @length = 1
    unless @bit
      raise "Adressing by #{verbal} is impossible. Cant parse it. Is bit expected?"
    end
  end
end

#valueObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/simatic/memory_mapper.rb', line 112

def value
  @raw_values = []

  byte = 0
  while byte < @count*@length
    @raw_values << @raw_data[byte, @length]
    byte += @length
  end

  result = @raw_values

  # .map do |raw|

  #   case type
  #   when :bool

  #     bit = @bit ? 0 : (args[:bit] || nil)

  #     binary_string = raw.unpack('b*').first

  #     if bit
  #       binary_string[bit] == '1' ? true : false
  #     else
  #       binary_string.index('1') ? true : false
  #     end

  #   when :int8
  #     raw.unpack('c').first
  #   when :uint8
  #     raw.unpack('C').first
  #   when :int16
  #     raw.unpack('s>').first
  #   when :uint16
  #     raw.unpack('S>').first # try n
  #   when :int32
  #     raw.unpack('l>').first
  #   when :uint32
  #     raw.unpack('L>').first # try N
  #   when :float32
  #     raw.unpack('g').first
  #   when :float64
  #     raw.unpack('G').first
  #   when :text8
  #     raw.unpack('A*').first
  #   when :text16
  #     raw.unpack('u*').first
  #   else
  #     raw
  #   end
  # end

  result = result.first if result.count == 1
  result
end

#value=(new_value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/simatic/memory_mapper.rb', line 67

def value= new_value
  val_array = (new_value.kind_of? Array) ? new_value : [new_value]

  @raw_values = []
  val_array.each do |v|

    val = case v
      when Simatic::Types::SimaticType
        v
      when Integer
        case @length
        when 1
          Simatic::Types::Byte.new(v)
        when 2
          Simatic::Types::Int.new(v)
        when 4
          Simatic::Types::Dint.new(v)
        else
          raise "Cannot convert #{v.class} to SimaticType class"
        end
      when Float
        Simatic::Types::Real.new(v)
      when TrueClass, FalseClass
        Simatic::Types::Bool.new(v)
      when String
        raise "Cannot write string to plc use SimaticType::S7String class" if length > 1
        Simatic::Types::Char.new(v) if v.length == 1
      when Date
        Simatic::Types::IECDate.new(v)
      when Time
        Simatic::Types::DateAndTime.new(v)
      else
        raise "Unknoun type of write value <#{v.class}> #{v}"
    end

    @raw_values << val.serialize
  end

  @raw_data = @raw_values.inject { |sum, v| sum+v }

  if @raw_data.length != @length*@count
    raise "This values #{val_array} can not be writed because adress #{verbal} mean #{@length*@count} byte(s) instead #{@raw_data.length}"
  end
end