Class: Escalator::Protocol::Mitsubishi::McProtocol
- Inherits:
-
Protocol
- Object
- Protocol
- Escalator::Protocol::Mitsubishi::McProtocol
show all
- Defined in:
- lib/escalator/protocol/mitsubishi/mc_protocol.rb
Instance Attribute Summary
Attributes inherited from Protocol
#host, #log_level, #port
Instance Method Summary
collapse
Methods inherited from Protocol
#get_from_devices, #set_bit_to_device, #set_to_devices, #set_word_to_device
Constructor Details
#initialize(options = {}) ⇒ McProtocol
Returns a new instance of McProtocol.
30
31
32
33
34
35
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 30
def initialize options={}
super
@host = options[:host] || "192.168.0.10"
@port = options[:port] || 5010
prepare_device_map
end
|
Instance Method Details
#close ⇒ Object
47
48
49
50
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 47
def close
@socket.close if @socket
@socket = nil
end
|
#device_by_name(name) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 123
def device_by_name name
case name
when String
QDevice.new name
when EscDevice
local_device_of name
else
name
end
end
|
#get_bit_from_device(device) ⇒ Object
52
53
54
55
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 52
def get_bit_from_device device
device = device_by_name device
get_bits_from_device(1, device).first
end
|
#get_bits_from_device(count, device) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 57
def get_bits_from_device count, device
device = device_by_name device
packet = make_packet(body_for_get_bits_from_deivce(count, device))
@logger.debug("> #{dump_packet packet}")
open
@socket.write(packet.pack("c*"))
@socket.flush
res = receive
bits = []
count.times do |i|
v = res[11 + i / 2]
if i % 2 == 0
bits << ((v >> 4) != 0)
else
bits << ((v & 0xf) != 0)
end
end
@logger.debug("get #{device.name} => #{bits}")
bits
end
|
#get_word_from_device(device) ⇒ Object
90
91
92
93
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 90
def get_word_from_device device
device = device_by_name device
get_words_from_device(1, device).first
end
|
#get_words_from_device(count, device) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 95
def get_words_from_device(count, device)
device = device_by_name device
packet = make_packet(body_for_get_words_from_deivce(count, device))
@logger.debug("> #{dump_packet packet}")
open
@socket.write(packet.pack("c*"))
@socket.flush
res = receive
words = []
res[11, 2 * count].each_slice(2) do |pair|
words << pair.pack("c*").unpack("v").first
end
@logger.debug("get from: #{device.name} => #{words}")
words
end
|
#open ⇒ Object
37
38
39
40
41
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 37
def open
open!
rescue
nil
end
|
#open! ⇒ Object
43
44
45
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 43
def open!
@socket ||= TCPSocket.open(@host, @port)
end
|
#receive ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 136
def receive
res = []
len = 0
begin
Timeout.timeout(1.0) do
loop do
c = @socket.read(1)
next if c.nil? || c == ""
res << c.bytes.first
len = res[7,2].pack("c*").unpack("v*").first if res.length >= 9
break if (len + 9 == res.length)
end
end
rescue Timeout::Error
puts "*** ERROR: TIME OUT ***"
end
@logger.debug("< #{dump_packet res}")
res
end
|
#set_bits_to_device(bits, device) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 78
def set_bits_to_device bits, device
device = device_by_name device
packet = make_packet(body_for_set_bits_to_device(bits, device))
@logger.debug("> #{dump_packet packet}")
open
@socket.write(packet.pack("c*"))
@socket.flush
res = receive
@logger.debug("set #{bits} to:#{device.name}")
end
|
#set_words_to_device(words, device) ⇒ Object
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/escalator/protocol/mitsubishi/mc_protocol.rb', line 111
def set_words_to_device words, device
device = device_by_name device
packet = make_packet(body_for_set_words_to_device(words, device))
@logger.debug("> #{dump_packet packet}")
open
@socket.write(packet.pack("c*"))
@socket.flush
res = receive
@logger.debug("set #{words} to: #{device.name}")
end
|