Class: GPSTool::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/gpstool/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, options = {}) ⇒ Device

Returns a new instance of Device.



11
12
13
14
15
16
# File 'lib/gpstool/device.rb', line 11

def initialize(io, options = {})
	@io = io
	@errors = 0
	
	@options = options
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/gpstool/device.rb', line 19

def errors
  @errors
end

#ioObject (readonly)

Returns the value of attribute io.



18
19
20
# File 'lib/gpstool/device.rb', line 18

def io
  @io
end

Class Method Details

.open(path, options = {}) ⇒ Object



7
8
9
# File 'lib/gpstool/device.rb', line 7

def self.open(path, options = {})
	return self.new(File.open(path, "rb+"), options)
end

Instance Method Details

#clear_ioObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/gpstool/device.rb', line 25

def clear_io
	# Consume any pre-existing data
	pipes = nil
	while (pipes = IO.select([@io], [], [], 0.1))
		puts pipes.inspect
		
		buf = @io.read_nonblock(1024)
		$stderr.puts "(clear) -> " + buf.dump
	end
end

#closeObject



21
22
23
# File 'lib/gpstool/device.rb', line 21

def close
	@io.close
end

#read_message(&block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gpstool/device.rb', line 45

def read_message(&block)
	message_class = @options[:message_class] || Message
	
	while true
		begin
			message = message_class.read(@io, @options)
			
			return unless yield message
		rescue
			@errors += 1
			
			raise
		end
	end
end

#send_message(message) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/gpstool/device.rb', line 36

def send_message(message)
	unless Message === message
		message = Message.parse(message)
	end
	
	$stderr.puts "(write) <- " + message.to_s.dump
	@io.write(message.to_s + "\r\n")
end