class SGS::NMEA

Attributes

args[RW]

Parse and create NMEA strings for various purposes.

checksum[RW]

Parse and create NMEA strings for various purposes.

valid[RW]

Parse and create NMEA strings for various purposes.

Public Class Methods

new() click to toggle source
# File lib/sgs/nmea.rb, line 38
def initialize
  @args = Array.new
  @valid = false
  @checksum = 0
end
parse(str) click to toggle source

Parse an NMEA string into its component parts.

# File lib/sgs/nmea.rb, line 46
def self.parse(str)
  nmea = new
  if nmea.parse(str) < 0
    nmea = nil
  end
  nmea
end

Public Instance Methods

compute_csum(str) click to toggle source

Compute an NMEA checksum

# File lib/sgs/nmea.rb, line 125
def compute_csum(str)
  @checksum = 0
  str.each_byte {|ch| @checksum ^= ch}
  @checksum
end
is_gprmc?() click to toggle source

Is the current line a GPRMC message?

# File lib/sgs/nmea.rb, line 71
def is_gprmc?
  @args[0] == "GPRMC"
end
make_gprmc(gps) click to toggle source

Output a GPRMC message

# File lib/sgs/nmea.rb, line 101
def make_gprmc(gps)
  @valid = true
  @args = Array.new
  @args[0] = "GPRMC"
  @args[1] = gps.time.strftime("%H%M%S.") + "%03d" % (gps.time.usec / 1000)
  @args[2] = 'A'
  @args.concat gps.location.latitude_array
  @args.concat gps.location.longitude_array("%03d%07.4f")
  @args[7] = "%.2f" % gps.sog
  @args[8] = "%.2f" % Bearing.radians_to_d(gps.cmg)
  @args[9] = gps.time.strftime("%d%m%y")
  @args.concat ['', '']
  @args << 'A'
end
parse(str) click to toggle source

Parse an NMEA string into its component parts.

# File lib/sgs/nmea.rb, line 56
def parse(str)
  str.chomp!
  if str[0] != "$"
    return -1
  end
  str, sum = str[1..-1].split('*')
  if sum.nil? or sum.to_i(16) != compute_csum(str)
    return -1
  end
  @args = str.split(',')
  @args.count
end
parse_gprmc() click to toggle source

Parse a GPRMC message

“GPRMC”, “211321.000”, “A”, “5309.7743”, “N”, “00904.5576”, “W”, “0.17”, “78.41”, “200813”, “”, “”, “A”
# File lib/sgs/nmea.rb, line 78
def parse_gprmc
  if @args.count < 12 or @args.count > 13
    return nil
  end
  gps = SGS::GPS.new
  gps.is_valid if @args[2] == "A"
  hh = @args[1][0..1].to_i
  mm = @args[1][2..3].to_i
  ss = @args[1][4..-1].to_f
  us = (ss % 1.0 * 1000000)
  ss = ss.to_i
  dd = @args[9][0..1].to_i
  mn = @args[9][2..3].to_i
  yy = @args[9][4..5].to_i + 2000
  gps.time = Time.gm(yy, mn, dd, hh, mm, ss, us)
  gps.location = Location.parse ll_nmea(@args[3,4]), ll_nmea(@args[5,6])
  gps.sog = @args[7].to_f
  gps.cmg = Bearing.dtor @args[8].to_f
  gps
end
to_s() click to toggle source

Convert an array of component parts into an NMEA string.

# File lib/sgs/nmea.rb, line 118
def to_s
  str = @args.join(',')
  "$%s*%02X" % [str, compute_csum(str)]
end

Private Instance Methods

ll_nmea(args) click to toggle source

Convert NMEA lat/long to something useful.

# File lib/sgs/nmea.rb, line 134
def ll_nmea(args)
  args[0].gsub(/(\d\d\.)/, ' \1') + args[1]
end