mm_gps 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6caf7bc745f44789a88d5da54fc90055ec8739ef
4
- data.tar.gz: f75f3b80c68f8e9cca6a5000e502398c4040b689
3
+ metadata.gz: f68f04c6955bd3be0fac1c0e6c9f6744efb2463a
4
+ data.tar.gz: 44d139c72d2ec7fec9eba8a1b9f12c72659bdc5c
5
5
  SHA512:
6
- metadata.gz: cc3f0816082666eab31c7246112b3f7364b252e2e888cf95b86529755d8408834873449f0144ea1ce00996c91414548e4c79349a2a4194459a28a11eb24db2b7
7
- data.tar.gz: 8e5e7beef52535c074fad13583f8b3c4e546bc2eb1e8a5a1f6ddcf435978f9702a5d109c12444d7d8d075b79f5f59de968042849fd04fad12522ded1ce37ed92
6
+ metadata.gz: d8c3dc05456bd59a303cae8b5d4cd082e0b51a82c3a8533d0308c4ba0202a7cb67c254fb63cce29f1397e9a1f89961616c20f6f7eb674db5e1b6fdaa1c1311b3
7
+ data.tar.gz: 51b785d6a816b966cb183f5235346ced4496dc510a020e2472994c7181ed7c6580db6ef6067d4ac1c8fdfa79b6986660dc29dae3fabbf13c5a0db0d0c9ebe07d
data/README.md CHANGED
@@ -33,16 +33,25 @@ beacon = MmGPS::Beacon.new(PORT, BAUD)
33
33
  beacon.trap # installs signal handler for CTRL-C
34
34
 
35
35
  puts "Syncing..."
36
- beacon.sync # discards any byte until the starting sequence "\xFFG" arrives
36
+ begin
37
+ beacon.sync # discards any byte until the starting sequence "\xFFG" arrives
38
+ rescue MmGPSError => e
39
+ puts "Packet Error: #{e.inspect}"
40
+ p e.data[:packet]
41
+ rescue IOError => e
42
+ puts "Port closed #{e.inspect}"
43
+ exit
44
+ end
37
45
 
38
46
  puts "Reading..."
39
47
  while not beacon.closed? do
40
48
  begin
41
49
  p beacon.get_packet
42
- rescue MmGPSException => e
50
+ rescue MmGPSError => e
43
51
  puts "Packet Error: #{e.inspect}"
52
+ p e.data
44
53
  rescue IOError => e
45
- puts "Port closed? #{e.inspect}"
54
+ puts "Port closed #{e.inspect}"
46
55
  exit
47
56
  end
48
57
  end
@@ -50,7 +59,7 @@ end
50
59
 
51
60
  ## Contributing
52
61
 
53
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mm_gps.
62
+ Bug reports and pull requests are welcome [on GitHub ](https://github.com/pbosetti/ruby_mm_gps).
54
63
 
55
64
 
56
65
  ## License
@@ -5,17 +5,19 @@ module MmGPS
5
5
  class Beacon
6
6
  START_TOKEN = "\xFFG".force_encoding(Encoding::BINARY)
7
7
  EMPTY = ''.force_encoding(Encoding::BINARY)
8
+ attr_reader :last_pkt
8
9
 
9
10
  # Open a new connection on the given serial port. It also encapsulates
10
11
  # the underlying SerialPort object instance, setting a read timeout of 1 s
11
12
  # and enabling the binary mode.
12
13
  #
13
14
  # @param port [String] 'COM1' on Windows, '/dev/ttyNNN' on *nix
14
- # @param baus [Fixnum] baudrate, value must be supported by the platform
15
+ # @param baud [Fixnum] baudrate, value must be supported by the platform
15
16
  def initialize(port, baud = 115200)
16
17
  @sp = SerialPort.new(port, "baud" => baud)
17
18
  @sp.read_timeout = 1000 #1 sec
18
19
  @sp.binmode
20
+ @last_pkt = ''.force_encoding(Encoding::BINARY)
19
21
  end
20
22
 
21
23
  # Istalls a signal handler for the given signal, default to SIGINT,
@@ -59,10 +61,17 @@ module MmGPS
59
61
  def get_raw_packet
60
62
  buf = START_TOKEN.dup
61
63
  while true do
62
- buf << (@sp.read(1) || EMPTY)
64
+ char = @sp.read(1)
65
+ unless char
66
+ raise MmGPSError.new("Data unavailable",
67
+ {reason: :noavail, packet:nil})
68
+ else
69
+ buf << char
70
+ end
63
71
  break if buf[-2..-1] == START_TOKEN
64
72
  end
65
- return buf[0...-2]
73
+ @last_pkt = buf[0...-2]
74
+ return @last_pkt
66
75
  end
67
76
 
68
77
  # Reads a raw packet, checks its CRC, and returns its contents as a Hash.
@@ -70,6 +79,12 @@ module MmGPS
70
79
  # @return [Hash] typically in the form `%I(ts x y z f).zip(payload).to_h`
71
80
  def get_packet
72
81
  return MmGPS::parse_packet(self.get_raw_packet)
82
+ rescue MmGPSError => e
83
+ if e.data[:reason] == :noavail then
84
+ return nil
85
+ else
86
+ raise e
87
+ end
73
88
  end
74
89
 
75
90
  end
@@ -1,5 +1,26 @@
1
1
  # Local Exception class.
2
- class MmGPSException < Exception; end
2
+ #
3
+ # The +@data+ attribute holds a Hash with informative content. In particular,
4
+ # +@data [:reason]+ holds a Symbol providing the internal error code, currently
5
+ # one of the following:
6
+ #
7
+ # - +:notype+ when the packet type code is neither 1 nor 2
8
+ # - +:nocrc+ when the CRC16 check fails
9
+ # - +:noavail+ when the serialport was not available for reading (timeout)
10
+ #
11
+ # Typically, the last raw buffer is available in human readable format as:
12
+ #
13
+ # rescue MmGPSError => e
14
+ # puts MmGPS::hexify(e.data[:packet])
15
+ # end
16
+ #
17
+ class MmGPSError < RuntimeError
18
+ attr_reader :data
19
+ def initialize(msg="Error in MmGPS class", data={})
20
+ @data = data
21
+ super(msg)
22
+ end
23
+ end
3
24
 
4
25
  # Manage connection and data decoding with a MarvelMind beacon or hedgehog.
5
26
  module MmGPS
@@ -11,6 +32,17 @@ module MmGPS
11
32
  crc16(str) == 0
12
33
  end
13
34
 
35
+ # Returns a HEX description of a binary buffer
36
+ #
37
+ # @param buf [String] the input buffer
38
+ # @return [String] the HEX description
39
+ def self.hexify(buf)
40
+ len = buf.length
41
+ return (("%02X " * len) % buf.unpack("C#{len}")).chop
42
+ rescue NoMethodError
43
+ return '--'
44
+ end
45
+
14
46
  # Parse the given buffer according to the MarvelMind protocol.
15
47
  # See http://www.marvelmind.com/pics/marvelmind_beacon_interfaces_v2016_03_07a.pdf
16
48
  #
@@ -18,7 +50,9 @@ module MmGPS
18
50
  # @return [Hash|Array] if the system is running, return a Hash with
19
51
  # timestamp, coordinates, and error code (data code 0x0001). Otherwise returns an Array of Hashes for beacons status (data code 0x0002).
20
52
  def self.parse_packet(buf)
21
- raise MmGPSException, "Invalid CRC" unless valid_crc16?(buf)
53
+ unless valid_crc16?(buf) then
54
+ raise MmGPSError.new("Invalid CRC", {reason: :nocrc, packet:buf, crc:crc16(buf)})
55
+ end
22
56
  # warn "Invalid CRC" unless valid_crc16?(buf)
23
57
  header = buf[0..5].unpack('CCS<C')
24
58
  if header[2] == 1 then # Regular GPS Data
@@ -37,7 +71,10 @@ module MmGPS
37
71
  %I(x y z).each {|k| result.last[k] /= 100.0}
38
72
  end
39
73
  else
40
- raise MmGPSException, "Unexpected packet type #{header[2]}"
74
+ unless valid_crc16?(buf) then
75
+ raise MmGPSError.new("Unexpected packet type #{header[2]}",
76
+ {reason: :notype, packet:buf, crc:crc16(buf), type:header[2]})
77
+ end
41
78
  end
42
79
  return result
43
80
  end
@@ -1,3 +1,3 @@
1
1
  module MmGPS
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/test.rb CHANGED
@@ -10,8 +10,9 @@ beacon.trap # installs signal handler for CTRL-C
10
10
  puts "Syncing..."
11
11
  begin
12
12
  beacon.sync # discards any byte until the starting sequence "\xFFG" arrives
13
- rescue MmGPSException => e
14
- puts "Packet Error: #{e.inspect}"
13
+ rescue MmGPSError => e
14
+ puts "Packet Error: #{e.inspect}, reason: #{e.data[:reason]}"
15
+ puts "Packet: #{MmGPS.hexify(e.data[:packet])}"
15
16
  rescue IOError => e
16
17
  puts "Port closed #{e.inspect}"
17
18
  exit
@@ -21,8 +22,9 @@ puts "Reading..."
21
22
  while not beacon.closed? do
22
23
  begin
23
24
  p beacon.get_packet
24
- rescue MmGPSException => e
25
- puts "Packet Error: #{e.inspect}"
25
+ rescue MmGPSError => e
26
+ puts "Packet Error: #{e.inspect}, reason: #{e.data[:reason]}"
27
+ puts "Packet: #{MmGPS.hexify(e.data[:packet])}"
26
28
  rescue IOError => e
27
29
  puts "Port closed #{e.inspect}"
28
30
  exit
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mm_gps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Bosetti