Class: MmGPS::Beacon
- Inherits:
-
Object
- Object
- MmGPS::Beacon
- Includes:
- Enumerable
- Defined in:
- lib/mm_gps/mm_gps_beacon.rb
Overview
Main interface. Represents a connection to a MarvelMind beacon/hedgehog. You may want (and can) to have more than one instance.
Constant Summary collapse
- START_TOKEN =
"\xFFG"
Instance Attribute Summary collapse
-
#baud ⇒ Object
readonly
Returns the value of attribute baud.
-
#last_pkt ⇒ Object
readonly
Returns the value of attribute last_pkt.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#close ⇒ Beacon
Close the serialport.
-
#closed? ⇒ Bool
Check wether the serialport is closed.
-
#each {|pkt| ... } ⇒ Object
Iterates
block
to each packet. -
#get_packet ⇒ Hash|Array
Reads a raw packet, checks its CRC, and returns its contents as a Hash.
-
#get_raw_packet ⇒ String
Reads a raw packet.
-
#initialize(port, baud: 115200) ⇒ Beacon
constructor
Open a new connection on the given serial port.
- #open ⇒ Beacon (also: #reopen)
-
#sync ⇒ Beacon
deprecated
Deprecated.
No more needed, since this version automatically takes care of the incomplete packages.
-
#trap(signal = "INT") ⇒ Object
Installs a signal handler for the given signal, default to ‘SIGINT’, which closes the serialport connection.
Constructor Details
#initialize(port, baud: 115200) ⇒ Beacon
Open a new connection on the given serial port. It also encapsulates the underlying SerialPort object instance, setting a read timeout of 1 s and enabling the binary mode.
33 34 35 36 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 33 def initialize(port, baud: 115200) @port, @baud = port, baud self.open end |
Instance Attribute Details
#baud ⇒ Object (readonly)
Returns the value of attribute baud.
25 26 27 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 25 def baud @baud end |
#last_pkt ⇒ Object (readonly)
Returns the value of attribute last_pkt.
24 25 26 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 24 def last_pkt @last_pkt end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
25 26 27 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 25 def port @port end |
Instance Method Details
#close ⇒ Beacon
Close the serialport
53 54 55 56 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 53 def close @sp.close unless (!@sp || @sp.closed?) return self end |
#closed? ⇒ Bool
Check wether the serialport is closed
79 80 81 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 79 def closed? return @sp.closed? end |
#each {|pkt| ... } ⇒ Object
Iterates block
to each packet
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 98 def each return enum_for(:each) unless block_given? loop do begin yield self.get_packet, @last_pkt rescue MmGPSError => e warn "Packet Error: #{e.inspect}, reason: #{e.data[:reason]}" warn "Packet: #{MmGPS.hexify(e.data[:packet])}" if e.data[:reason] == :nocrc then warn "CRC16: #{e.data[:crc]}" end rescue IOError => e warn "Port closed #{e.inspect}" return end end end |
#get_packet ⇒ Hash|Array
Reads a raw packet, checks its CRC, and returns its contents as a Hash.
132 133 134 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 132 def get_packet return MmGPS::parse_packet(self.get_raw_packet) end |
#get_raw_packet ⇒ String
Reads a raw packet.
120 121 122 123 124 125 126 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 120 def get_raw_packet pkt = @buffer.next if pkt.empty? then raise MmGPSError.new("Data unavailable", {reason: :noavail}) end return @last_pkt = pkt end |
#open ⇒ Beacon Also known as: reopen
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 62 def open self.close @sp = SerialPort.new(@port, "baud" => @baud) @sp.read_timeout = 1000 #1 sec @sp.binmode @last_pkt = ''.force_encoding(Encoding::BINARY) @buffer = Buffer.new(START_TOKEN) @buffer.when_updating do @sp.read(1) end return self end |
#sync ⇒ Beacon
No more needed, since this version automatically takes care of the incomplete packages.
Reads and discards incoming bytes until the START_TOKEN marker is received. Call this metod immediately after opening the connection and before start reading the data.
89 90 91 92 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 89 def sync warn "Beacon#sync is deprecated and no more necessary!" return self end |
#trap(signal = "INT") ⇒ Object
Installs a signal handler for the given signal, default to ‘SIGINT’, which closes the serialport connection. Further readings are likely to trigger an IOError.
43 44 45 46 47 48 |
# File 'lib/mm_gps/mm_gps_beacon.rb', line 43 def trap(signal="INT") Signal.trap(signal) do @sp.close puts "\nBeacon port: #{@sp.inspect}" end end |