Class: PlcAccess::Protocol::Omron::CModeProtocol

Inherits:
Protocol
  • Object
show all
Defined in:
lib/plc_access/protocol/omron/c_mode_protocol.rb

Constant Summary collapse

DELIMITER =
"\r"
TERMINATOR =
"*\r"
TIMEOUT =
1.0

Instance Attribute Summary collapse

Attributes inherited from Protocol

#host, #log_level, #port

Instance Method Summary collapse

Methods inherited from Protocol

#[], #[]=, #available_bits_range, #available_words_range, #destination_ipv4, #get_bit_from_device, #get_from_devices, #get_word_from_device, #self_ipv4, #set_bit_to_device, #set_bits_to_device, #set_to_devices, #set_word_to_device, #set_words_to_device

Constructor Details

#initialize(options = {}) ⇒ CModeProtocol

Returns a new instance of CModeProtocol.



36
37
38
39
40
41
42
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 36

def initialize(options = {})
  super
  @port = options[:port] || `ls /dev/tty.usb*`.split("\n").map(&:chomp).first
  @baudrate = 38_400
  @unit_no = 0
  @comm = nil
end

Instance Attribute Details

#baudrateObject

Returns the value of attribute baudrate.



30
31
32
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 30

def baudrate
  @baudrate
end

#unit_noObject

Returns the value of attribute unit_no.



30
31
32
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 30

def unit_no
  @unit_no
end

Instance Method Details

#closeObject



64
65
66
67
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 64

def close
  @comm&.close
  @comm = nil
end

#get_bits_from_device(count, device) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 73

def get_bits_from_device(count, device)
  device = device_by_name device

  # convert to the channel device
  from = device.channel_device
  to = (device + count).channel_device
  c = [to - from, 1].max

  # get value as words
  words = get_words_from_device(c, device)

  # convert to bit devices
  index = device.bit
  bits = []
  count.times do
    i = index / 16
    b = index % 16
    f = 1 << b
    bits << ((words[i] & f) == f)
    index += 1
  end
  bits
end

#get_words_from_device(count, device) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 97

def get_words_from_device(count, device)
  device = device_by_name(device).channel_device

  # make read packet
  packet = read_packet_with device
  packet << "#{device.channel.to_s.rjust(4, '0')}#{count.to_s.rjust(4, '0')}"
  packet << fcs_for(packet).to_s(16).upcase.rjust(2, '0')
  packet << TERMINATOR
  @logger.debug("> #{dump_packet packet}")

  # send command
  open
  send packet

  # receive response
  words = []
  terminated = false
  loop do
    res = receive
    data = ''
    if res
      ec = error_code(res)
      raise "Error response: #{ec.to_i(16).rjust(2, '0')}" unless ec.zero?

      if res[-2, 2] == TERMINATOR
        fcs = fcs_for(res[0..-5])
        raise "Not matched FCS expected #{fcs.to_s(16).rjust(2, '0')}" unless fcs == res[-4, 2].to_i(16)

        data = res[7..-5]
        terminated = true
      elsif res[-1, 1] == DELIMITER
        fcs = fcs_for(res[0..-4])
        raise "Not matched FCS expected #{fcs.to_s(16).rjust(2, '0')}" unless fcs == res[-3, 2].to_i(16)

        data = res[7..-4]
      end
      len = data.length
      index = 0
      while index < len
        words << data[index, 4].to_i(16)
        index += 4
      end
      return words if terminated
    else
      break
    end
  end
  []
end

#openObject



44
45
46
47
48
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 44

def open
  open!
rescue StandardError
  nil
end

#open!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/plc_access/protocol/omron/c_mode_protocol.rb', line 50

def open!
  return false unless @port

  begin
    # port, baudrate, bits, stop bits, parity(0:none, 1:even, 2:odd)
    @comm ||= SerialPort.new(@port, @baudrate, 7, 2, 1).tap do |s|
      s.read_timeout = (TIMEOUT * 1000.0).to_i
    end
  rescue StandardError => e
    p e
    nil
  end
end