Class: LadderDrive::Protocol::Omron::OmronDevice

Inherits:
LadderDrive::PlcDevice show all
Defined in:
lib/ladder_drive/protocol/omron/omron_device.rb

Constant Summary collapse

SUFFIXES =
%w(M H D T C A)

Constants inherited from LadderDrive::PlcDevice

LadderDrive::PlcDevice::ESC_SUFFIXES, LadderDrive::PlcDevice::NUMBER_TYPE_DEC, LadderDrive::PlcDevice::NUMBER_TYPE_DEC_HEX, LadderDrive::PlcDevice::NUMBER_TYPE_HEX

Instance Attribute Summary collapse

Attributes inherited from LadderDrive::PlcDevice

#number, #value

Instance Method Summary collapse

Methods inherited from LadderDrive::PlcDevice

#bool, #bool=, #device_by_suffix_number, #device_code, #input?, program_area_device, #reset, #set_text, status_from_plc_device, status_to_plc_device, #text, #text=

Constructor Details

#initialize(a, b = nil, c = nil) ⇒ OmronDevice

Returns a new instance of OmronDevice.



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
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 37

def initialize a, b = nil, c=nil
  case a
  when Array
=begin
    case a.size
    when 4
      @suffix = suffix_for_code(a[3])
      @channel = ((a[2] << 8 | a[1]) << 8) | a[0]
    end
=end
  else
    if b
      @suffix = a.upcase if a
      @channel = b.to_i
      @bit = c.to_i if c
    else
      if /^(M|H|D|T|C|A)?([0-9]+)(\.([0-9]{1,2}))?$/i =~ a
        @suffix = $1.upcase if $1
        @channel = $2.to_i
        @bit = $4.to_i if $4
      end
    end
  end
  case @suffix
  when "T", "C"
    raise "#{self.name} is not allowed as a bit device." if @bit
  end
end

Instance Attribute Details

#bitObject (readonly)

Returns the value of attribute bit.



33
34
35
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 33

def bit
  @bit
end

#channelObject (readonly)

Returns the value of attribute channel.



33
34
35
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 33

def channel
  @channel
end

#suffixObject (readonly)

Returns the value of attribute suffix.



33
34
35
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 33

def suffix
  @suffix
end

Instance Method Details

#+(value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 101

def + value
  if bit
    v = channel * 16 + bit + value
    c = v / 16
    b = v % 16
    self.class.new suffix, c, b
  else
    self.class.new suffix, channel + value
  end
end

#-(value) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 112

def - value
  case value
  when OmronDevice
    d = value
    raise "Can't subtract between different device type." if self.bit_device? ^ d.bit_device?
    if bit
      (channel * 16 + bit) - (d.channel * 16 + d.bit)
    else
      channel - d.channel
    end
  else
    value = value.to_i
    if bit
      v = [channel * 16 + bit - value, 0].max
      c = v / 16
      b = v % 16
      self.class.new suffix, c, b
    else
      self.class.new suffix, [channel - value, 0].max
    end
  end
end

#bit_device?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 87

def bit_device?
  !!bit
end

#channel_deviceObject



66
67
68
69
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 66

def channel_device
  return self unless bit_device?
  self.class.new suffix, channel
end

#nameObject



75
76
77
78
79
80
81
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 75

def name
  if bit
    "#{suffix}#{channel}.#{bit.to_s.rjust(2, '0')}"
  else
    "#{suffix}#{channel}"
  end
end

#next_deviceObject



83
84
85
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 83

def next_device
  self + 1
end

#suffix_codeObject



96
97
98
99
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 96

def suffix_code
  index = SUFFIXES.index suffix
  index ? SUFFIX_CODES[index] : 0
end

#suffix_for_code(code) ⇒ Object



91
92
93
94
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 91

def suffix_for_code code
  index = SUFFIX_CODES.index code
  index ? SUFFIXES[index] : nil
end

#valid?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ladder_drive/protocol/omron/omron_device.rb', line 71

def valid?
  !!channel
end