Class: PlcAccess::PlcDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/plc_access/protocol/plc_device.rb

Constant Summary collapse

NUMBER_TYPE_DEC =
0
NUMBER_TYPE_DEC_HEX =
1
NUMBER_TYPE_HEX =
2
ESC_SUFFIXES =
%w[X Y M - C T L SC CC TC D - CS TS H SD].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil) ⇒ PlcDevice

Returns a new instance of PlcDevice.



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
# File 'lib/plc_access/protocol/plc_device.rb', line 51

def initialize(a, b = nil)
  @suffix = nil
  @value = 0
  case a
  when Integer
    @suffix = self.class::ESC_SUFFIXES[a]
    @number = b
  when String, Symbol
    a = a.to_s # convert to string if it's a symbol
    if b
      @suffix = a.upcase
      @number = b
    else
      /([A-Z]+)?([0-9A-F]+)/i =~ a
      @suffix = (::Regexp.last_match(1) || '').upcase
      case number_type
      when NUMBER_TYPE_DEC_HEX
        n = ::Regexp.last_match(2).to_i
        @number = (n / 100) * 16 + (n % 100)
      when NUMBER_TYPE_HEX
        @number = ::Regexp.last_match(2).to_i(16)
      else
        @number = ::Regexp.last_match(2).to_i
      end
    end
  end
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



28
29
30
# File 'lib/plc_access/protocol/plc_device.rb', line 28

def number
  @number
end

#suffixObject (readonly)

Returns the value of attribute suffix.



28
29
30
# File 'lib/plc_access/protocol/plc_device.rb', line 28

def suffix
  @suffix
end

#valueObject Also known as: word

Returns the value of attribute value.



29
30
31
# File 'lib/plc_access/protocol/plc_device.rb', line 29

def value
  @value
end

Class Method Details

.program_area_deviceObject



46
47
48
# File 'lib/plc_access/protocol/plc_device.rb', line 46

def program_area_device
  @program_area_device ||= new 'PRG0'
end

.status_from_plc_deviceObject



42
43
44
# File 'lib/plc_access/protocol/plc_device.rb', line 42

def status_from_plc_device
  @status_from_plc_device ||= new 'SD1'
end

.status_to_plc_deviceObject



38
39
40
# File 'lib/plc_access/protocol/plc_device.rb', line 38

def status_to_plc_device
  @status_to_plc_device ||= new 'SD0'
end

Instance Method Details

#+(other) ⇒ Object



112
113
114
# File 'lib/plc_access/protocol/plc_device.rb', line 112

def +(other)
  device_by_suffix_number suffix, number + other
end

#-(other) ⇒ Object



116
117
118
# File 'lib/plc_access/protocol/plc_device.rb', line 116

def -(other)
  device_by_suffix_number suffix, [number - other, 0].max
end

#bit_device?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/plc_access/protocol/plc_device.rb', line 108

def bit_device?
  suffixes_for_bit.include? @suffix
end

#boolObject



135
136
137
138
139
140
141
142
# File 'lib/plc_access/protocol/plc_device.rb', line 135

def bool
  case @value
  when Integer
    @value != 0
  else
    !!@value
  end
end

#bool=(v) ⇒ Object



144
145
146
# File 'lib/plc_access/protocol/plc_device.rb', line 144

def bool=(v)
  @value = v
end

#device_by_suffix_number(suffix, number) ⇒ Object

NOTE: override at subclass It should get from plc



100
101
102
# File 'lib/plc_access/protocol/plc_device.rb', line 100

def device_by_suffix_number(suffix, number)
  self.class.new suffix, number
end

#device_codeObject



183
184
185
# File 'lib/plc_access/protocol/plc_device.rb', line 183

def device_code
  self.class::ESC_SUFFIXES.index @suffix
end

#input?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/plc_access/protocol/plc_device.rb', line 120

def input?
  suffixes_for_input.include? @suffix
end

#nameObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/plc_access/protocol/plc_device.rb', line 79

def name
  case number_type
  when NUMBER_TYPE_DEC
    "#{@suffix}#{@number}"
  when NUMBER_TYPE_DEC_HEX
    a = [@number / 16, @number % 16]
    ns = begin
      s = a.last.to_s.rjust(2, '0')
      s = a.first.to_s + s unless a.first.zero?
      s
    end
    "#{@suffix}#{ns}"
  when NUMBER_TYPE_HEX
    ns = @number.to_s(16).upcase
    ns = "0#{ns}" unless /^[0-9]/ =~ ns
    "#{@suffix}#{ns}"
  end
end

#next_deviceObject



104
105
106
# File 'lib/plc_access/protocol/plc_device.rb', line 104

def next_device
  device_by_suffix_number @suffix, @number + 1
end

#resetObject



187
188
189
# File 'lib/plc_access/protocol/plc_device.rb', line 187

def reset
  @value = 0
end

#set_text(value, len = 8) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/plc_access/protocol/plc_device.rb', line 167

def set_text(value, len = 8)
  value = value[0, len]
  value << "\00" unless value.length.even?
  format = string_endian == :big ? 'n*' : 'v*'
  a = value.unpack(format)
  d = self
  a.each do |v|
    d.value = v
    d = d.next_device
  end
end

#string_endianObject



150
151
152
# File 'lib/plc_access/protocol/plc_device.rb', line 150

def string_endian
  :big
end

#text(len = 8) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/plc_access/protocol/plc_device.rb', line 154

def text(len = 8)
  n = (len + 1) / 2
  d = self
  a = []
  n.times do
    a << d.value
    d = d.next_device
  end
  format = string_endian == :big ? 'n*' : 'v*'
  s = a.pack(format).split("\x00").first
  s ? s[0, len] : ''
end

#text=(value) ⇒ Object



179
180
181
# File 'lib/plc_access/protocol/plc_device.rb', line 179

def text=(value)
  set_text value
end