Sunday, July 20, 2008

NMEA GPS & 22 lines of PowerShell

Inspired by the python article "GPS + Google Maps Mash-up in 42 lines of code", I decided to create something similar in PowerShell:

function Nmea2dec {
    Param ([double]$degrees, $o)
    $deg = [math]::floor($degrees/100.0)
    $frac = (($degrees/100.0) - $deg)/0.6
    $ret = $deg + $frac
    if ($o -eq "S" -or $o -eq "W") {$ret = $ret * (-1)}
    return $ret
}

$port = new-Object System.IO.Ports.SerialPort COM1,4800,None,8,one
$port.open()
$line = ""
while (-not ($line -match ".GPRMC.*")) {
    $line = $port.readline()
}
$splitline = $line.split(",")
$latitude = Nmea2dec $splitline[3] $splitline[4]
$longtitude = Nmea2dec $splitline[5] $splitline[6]
$url = "http://maps.google.com/maps?ie=UTF8&ll=$latitude,$longtitude&q=$latitude,$longtitude&z=16&t=h"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate2("$url")

If you want to play around with the script above, you'll of course need a serial GPS reciever, or you could just use the following line of data:
$GPRMC,182700.000,A,5541.8761,N,01232.0247,E,0.12,52.50,190708,,,A*55

I think I'll continue to play around with GPS data and Google Maps for a while - look out for PS scripts!

No comments: