Class: LadderDrive::PlcDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/ladder_drive/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)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil) ⇒ PlcDevice



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
# File 'lib/ladder_drive/plc_device.rb', line 53

def initialize a, b = nil
  @suffix = nil
  @value = 0
  case a
  when Integer
    @suffix = 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 = ($1 || "").upcase
      case number_type
      when NUMBER_TYPE_DEC_HEX
        n = $2.to_i
        @number = (n / 100) * 16 + (n % 100)
      when NUMBER_TYPE_HEX
        @number = $2.to_i(16)
      else
        @number = $2.to_i
      end
    end
  end
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#suffixObject (readonly)

Returns the value of attribute suffix.



28
29
30
# File 'lib/ladder_drive/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/ladder_drive/plc_device.rb', line 29

def value
  @value
end

Class Method Details

.program_area_deviceObject



47
48
49
# File 'lib/ladder_drive/plc_device.rb', line 47

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

.status_from_plc_deviceObject



43
44
45
# File 'lib/ladder_drive/plc_device.rb', line 43

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

.status_to_plc_deviceObject



39
40
41
# File 'lib/ladder_drive/plc_device.rb', line 39

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

Instance Method Details

#+(value) ⇒ Object



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

def + value
  device_by_suffix_number self.suffix, self.number + value
end

#-(value) ⇒ Object



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

def - value
  device_by_suffix_number self.suffix, [self.number - value, 0].max
end

#bit_device?Boolean



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

def bit_device?
  suffixes_for_bit.include? @suffix
end

#boolObject



139
140
141
142
143
144
145
146
# File 'lib/ladder_drive/plc_device.rb', line 139

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

#bool=(v) ⇒ Object



147
# File 'lib/ladder_drive/plc_device.rb', line 147

def bool= v; @value = v; end

#device_by_suffix_number(suffix, number) ⇒ Object

NOTE: override at subclass

It should get from plc


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

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

#device_codeObject



175
176
177
# File 'lib/ladder_drive/plc_device.rb', line 175

def device_code
  ESC_SUFFIXES.index @suffix
end

#input?Boolean



124
125
126
# File 'lib/ladder_drive/plc_device.rb', line 124

def input?
  suffixes_for_input.include? @suffix
end

#nameObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ladder_drive/plc_device.rb', line 81

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 == 0
      s
    end
    "#{@suffix}#{ns}"
  when NUMBER_TYPE_HEX
    ns = @number.to_s(16).upcase
    ns = "0" + ns unless /^[0-9]/ =~ ns
    "#{@suffix}#{ns}"
  else
    nil
  end
end

#next_deviceObject



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

def next_device
  device_by_suffix_number @suffix, @number + 1
end

#resetObject



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

def reset
  @value = 0
end

#set_text(value, len = 8) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/ladder_drive/plc_device.rb', line 160

def set_text value, len=8
  value = value[0, len]
  value << "\00" unless value.length % 2 == 0
  a = value.unpack("n*")
  d = self
  a.each do |v|
    d.value = v
    d = d.next_device
  end
end

#text(len = 8) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/ladder_drive/plc_device.rb', line 151

def text len=8
  n = (len + 1) / 2
  d = self
  a = []
  n.times{ a << d.value; d = d.next_device}
  s = a.pack("n*").split("\x00").first
  s ? s[0,len] : ""
end

#text=(value) ⇒ Object



171
172
173
# File 'lib/ladder_drive/plc_device.rb', line 171

def text= value
  set_text value
end