Class: O3D3XX::PCIC
- Inherits:
-
Object
- Object
- O3D3XX::PCIC
- Defined in:
- lib/o3d3xx/pcic.rb
Instance Method Summary collapse
- #async_result(state = false) ⇒ Object
- #async_trigger ⇒ Object
- #connect(host = "192.168.0.69", port = "50010") ⇒ Object
- #detect_protocol ⇒ Object
- #device_info ⇒ Object
-
#disconnect ⇒ Object
set_version(3).
- #escape_pcic_string(data) ⇒ Object
- #get_lastimage(img = 3) ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ PCIC
constructor
A new instance of PCIC.
- #recv_v3 ⇒ Object
- #send_v3(data) ⇒ Object
- #set_version(version = 3) ⇒ Object
- #sync_trigger ⇒ Object
- #transfer(data) ⇒ Object
- #transfer_v3(data, omit_async = true) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize ⇒ PCIC
Returns a new instance of PCIC.
58 59 60 61 62 63 |
# File 'lib/o3d3xx/pcic.rb', line 58 def initialize() @remote = nil @protocol = 3 @logger = Logger.new(STDOUT) @logger.level = Logger::WARN end |
Instance Method Details
#async_result(state = false) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/o3d3xx/pcic.rb', line 154 def async_result(state=false) data = "p%d" % [ state ? 1 : 0 ] res = true transfer(data) res end |
#async_trigger ⇒ Object
142 143 144 |
# File 'lib/o3d3xx/pcic.rb', line 142 def async_trigger() return transfer("t") end |
#connect(host = "192.168.0.69", port = "50010") ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/o3d3xx/pcic.rb', line 64 def connect(host="192.168.0.69",port="50010") unless @remote == nil disconnect() end @remote = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) @protocol = 3 begin timeout(3) do @remote.connect(Socket.pack_sockaddr_in(port, host)) end rescue @logger.fatal("Unable to connect to tcp://#{host}:#{port}") @remote = nil return false end #@protocol = detect_protocol() #set_version(3) end |
#detect_protocol ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 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 |
# File 'lib/o3d3xx/pcic.rb', line 86 def detect_protocol() proto = nil begin result = nil @logger.debug("detect_protocol ") begin timeout(11) do send_v3("V?") result = @remote.recvfrom(20) @logger.debug("Received: #{result} ") end rescue Timeout::Error => e @logger.fatal("Timeout while receiving data occurred: #{e.}") return end test = result.join() proto = nil proto = 1 if test =~ /(\?)|(\d{2} \d{2} \d{2})\r\n/ proto = 2 if test =~ /\d{4}(\?)|(\d{2} \d{2} \d{2})\r\n/ proto = 3 if test =~ /\d{4}L\d{9}\r\n\d{4}/ proto = 4 if test =~ /L\d{9}\r\n(\?)|(\d{2} \d{2} \d{2})\r\n/ # If the device is in continuous mode and asynchronous messages are enabled # we have to pull the complete frame out of the socket. Otherwise every consecutive # PCIC call will fail due to the fact that we start in the middle of a frame. if(proto == 3 ) ticket,length = test.match(/(\d{4})L(\d{9})\r\n/i).captures timeout(5) do # get the rest of the response trailer = @remote.read(length.to_i-4) end # recv_v3 handles asynchronous messages so let it handle them. dum = recv_v3() if '0000' == ticket end raise 'Unable to detect PCIC protocol' if proto == nil rescue Exception => e puts e. puts e.backtrace.inspect raise('Timeout while detecting version') proto = nil end proto end |
#device_info ⇒ Object
139 140 141 |
# File 'lib/o3d3xx/pcic.rb', line 139 def device_info() transfer("G?") end |
#disconnect ⇒ Object
set_version(3)
82 83 84 85 |
# File 'lib/o3d3xx/pcic.rb', line 82 def disconnect() @remote.close() @remote = nil end |
#escape_pcic_string(data) ⇒ Object
228 229 230 231 |
# File 'lib/o3d3xx/pcic.rb', line 228 def escape_pcic_string(data) result = "" result = data.gsub("\n","\\\\n").gsub("\r","\\\\r") unless data == nil end |
#get_lastimage(img = 3) ⇒ Object
149 150 151 |
# File 'lib/o3d3xx/pcic.rb', line 149 def get_lastimage(img = 3) transfer("I%02d?"%img) end |
#help ⇒ Object
136 137 138 |
# File 'lib/o3d3xx/pcic.rb', line 136 def help() transfer("H?") end |
#recv_v3 ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/o3d3xx/pcic.rb', line 186 def recv_v3() data = nil async = false trailer = nil # The V3 response shall look like this # <4 Byte ticket >L<9 Byte Length in dec>\r\n<4 Byte ticket ><Response>\r\n # receive <ticket>L<length>\r\n to check if we in sync and how much # data we have to receive in the next step timeout(5) do data = @remote.read(16) end @logger.info("Header : %{res}" % {:res => escape_pcic_string(data)}) begin ticket,length = data.match(/(\d{4})L(\d{9})\r\n/i).captures rescue @logger.fatal(">>> rescue: #{data} <<<") end size = length.to_i @logger.info("Length : %d " % size ) if ticket == "0000" @logger.info('ASYNC reply Caught !!!!') async = true else async = false end timeout(5) do trailer = @remote.read(size) end if size < 80 and trailer.length < 80 @logger.debug("Trailer : %{res} <<<<" % {:res => escape_pcic_string(trailer)}) else escaped = escape_pcic_string(trailer) @logger.debug("Trailer : %{start} [...] %{stop}" % {:start => escaped[0..20], :stop => escaped[-20..-1]}) end ticket_trailer = trailer[0..3] unless ticket_trailer == ticket raise "Ticket mismatch. Header ticket: #{ticket} trailer ticket: #{ticket_trailer}" end # remove ticket and trailing \r\n return async,ticket,trailer[4..-3] end |
#send_v3(data) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/o3d3xx/pcic.rb', line 166 def send_v3(data) ticket = "%04d" % [1000+rand(8999)] length = "%09d" % [data.bytesize+6] sendbuf = "#{ticket}L#{length}\r\n#{ticket}#{data}\r\n" @logger.info("Request (#{@protocol}) : %{sendbuf}" % {:sendbuf => escape_pcic_string(sendbuf)}) timeout(5) do @remote.send(sendbuf,0) end end |
#set_version(version = 3) ⇒ Object
128 129 130 131 132 |
# File 'lib/o3d3xx/pcic.rb', line 128 def set_version(version=3) res = transfer("v%02d"%version) @protocol = version res end |
#sync_trigger ⇒ Object
145 146 147 |
# File 'lib/o3d3xx/pcic.rb', line 145 def sync_trigger() transfer("T?") end |
#transfer(data) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/o3d3xx/pcic.rb', line 160 def transfer(data) raise 'Protocol #{@protocol} currently not supported' if @protocol == 1 raise 'Protocol #{@protocol} currently not supported' if @protocol == 2 return transfer_v3(data) if @protocol == 3 raise 'Protocol #{@protocol} currently not supported' if @protocol == 4 end |
#transfer_v3(data, omit_async = true) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/o3d3xx/pcic.rb', line 175 def transfer_v3(data,omit_async=true) send_v3(data) continue = true response = nil begin async,ticket,response = recv_v3() continue = false; continue = true if async and omit_async end while continue return response end |
#version ⇒ Object
133 134 135 |
# File 'lib/o3d3xx/pcic.rb', line 133 def version() return transfer("V?") end |