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, #string_endian, #text, #text=

Constructor Details

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

Returns a new instance of OmronDevice.

Raises:

  • (ArgumentError)


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
# 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
  raise ArgumentError, "Invalid device suffix: #{@suffix}" if @suffix && !SUFFIXES.include?(@suffix)
  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



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

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



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

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)


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

def bit_device?
  !!bit
end

#channel_deviceObject



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

def channel_device
  return self unless bit_device?

  self.class.new suffix, channel
end

#nameObject



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

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

#next_deviceObject



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

def next_device
  self + 1
end

#suffix_codeObject



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

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

#suffix_for_code(code) ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !!channel
end