|
簡介
如果您的電腦上有 GPS 衛星接收器,在該接收器接上後,衛星定位的訊息會從 ComPort 源源不斷的傳回來,這些訊息是以 NMEA 格式編碼的文字訊息,其格式如下所示。
$GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05
$GPGSV,3,1,12,26,89,000,36,29,73,000,00,28,38,000,00,18,35,000,00*79
$GPGSV,3,2,12,09,27,000,00,21,27,000,41,15,23,000,00,10,18,000,00*79
$GPGSV,3,3,12,08,15,000,00,22,07,000,,19,07,000,,03,-01,000,*51
$GPRMC,113347.950,V,0000.0000,N,00000.0000,E,0.00,,120305,,*00
$GPGGA,113348.950,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,0.0,M,0.0,0000*76
$GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05
$GPRMC,113348.950,V,0000.0000,N,00000.0000,E,0.00,,120305,,*0F
$GPGGA,113349.950,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,0.0,M,0.0,0000*77
$GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05
$GPRMC,113349.950,V,0000.0000,N,00000.0000,E,0.00,,120305,,*0E
其中,最重要的資訊是座標資訊,紀錄在以 GPSGGA 開頭的欄位中,以下是其欄位的說明:
標頭 ,收訊時間點,經度座標,北或南,緯度座標 ,東或西,品質,衛星數量,....
$GPGGA,113348.950,0000.0000,N ,00000.0000,E ,0 ,00 ,50.0,0.0,M,0.0,M,0.0,0000*76
從 ComPort 讀取衛星座標
在配備有 GPS 的電腦或手機當中,這個資訊會從某個 COM port (例如:COM1) 當中傳入,因此,只要一個從 ComPort 讀取資料的無窮迴圈,就能不斷顯示您現在的衛星座標。
但是如果每個程式都自行去開啟 ComPort,那麼,第一個開啟的人將會搶到該 ComPort,於是第二個開啟的程式將無法開啟 ComPort 而導致失敗。
// 請注意,這個成是沒有被測試過,請自行除錯。
class Gps
{
static String gpsPort = "COM9";
// 讀取 comPort 的方法
static void Main(String[] args)
{
SerialPort port = new SerialPort(gpsPort, 4800);
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.ReadTimeout = SerialPort.InfiniteTimeout;
port.Open();
try
{
while (true)
{
string line = gpsPort.ReadLine();
Console.WriteLine(line);
}
}
catch {}
port.DiscardInBuffer();
port.Dispose();
}
}
|
Post preview:
Close preview