Class: PlcAccess::Protocol::Omron::OmronDevice

Inherits:
PlcAccess::PlcDevice show all
Defined in:
lib/plc_access/protocol/omron/omron_device.rb

Constant Summary collapse

SUFFIXES =
%w[M H D T C A].freeze

Constants inherited from PlcAccess::PlcDevice

PlcAccess::PlcDevice::ESC_SUFFIXES, PlcAccess::PlcDevice::NUMBER_TYPE_DEC, PlcAccess::PlcDevice::NUMBER_TYPE_DEC_HEX, PlcAccess::PlcDevice::NUMBER_TYPE_HEX

Instance Attribute Summary collapse

Attributes inherited from PlcAccess::PlcDevice

#number, #value

Instance Method Summary collapse

Methods inherited from PlcAccess::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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 36

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

Instance Attribute Details

#bitObject (readonly)

Returns the value of attribute bit.



32
33
34
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 32

def bit
  @bit
end

#channelObject (readonly)

Returns the value of attribute channel.



32
33
34
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 32

def channel
  @channel
end

#suffixObject (readonly)

Returns the value of attribute suffix.



32
33
34
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 32

def suffix
  @suffix
end

Instance Method Details

#+(other) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 97

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

#-(other) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 108

def -(other)
  case other
  when OmronDevice
    d = other
    raise "Can't subtract between different device type." if bit_device? ^ d.bit_device?

    if bit
      (channel * 16 + bit) - (d.channel * 16 + d.bit)
    else
      channel - d.channel
    end
  else
    other = other.to_i
    if bit
      v = [channel * 16 + bit - other, 0].max
      c = v / 16
      b = v % 16
      self.class.new suffix, c, b
    else
      self.class.new suffix, [channel - other, 0].max
    end
  end
end

#bit_device?Boolean

Returns:

  • (Boolean)


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

def bit_device?
  !!bit
end

#channel_deviceObject



61
62
63
64
65
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 61

def channel_device
  return self unless bit_device?

  self.class.new suffix, channel
end

#nameObject



71
72
73
74
75
76
77
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 71

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

#next_deviceObject



79
80
81
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 79

def next_device
  self + 1
end

#suffix_codeObject



92
93
94
95
# File 'lib/plc_access/protocol/omron/omron_device.rb', line 92

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

#suffix_for_code(code) ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !!channel
end