Class: LadderDrive::Protocol::Omron::CModeProtocol

Inherits:
Protocol
  • Object
show all
Defined in:
lib/ladder_drive/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, #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, #log_level, #log_level=, #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.



37
38
39
40
41
42
43
44
# File 'lib/ladder_drive/protocol/omron/c_mode_protocol.rb', line 37

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

Instance Attribute Details

#baudrateObject

Returns the value of attribute baudrate.



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

def baudrate
  @baudrate
end

#unit_noObject

Returns the value of attribute unit_no.



31
32
33
# File 'lib/ladder_drive/protocol/omron/c_mode_protocol.rb', line 31

def unit_no
  @unit_no
end

Instance Method Details

#closeObject



65
66
67
68
# File 'lib/ladder_drive/protocol/omron/c_mode_protocol.rb', line 65

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

#get_bits_from_device(count, device) ⇒ Object



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

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



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

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 == 0
      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
      else 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



46
47
48
49
50
# File 'lib/ladder_drive/protocol/omron/c_mode_protocol.rb', line 46

def open
  open!
rescue
  nil
end

#open!Object



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

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 => e
    p e
    nil
  end
end