阅读:1479回复:0
【转】基于Java的GPS接收机解析器 (3)
/*
* * GpsParser.java * * This is a simple GPS Parser program, which reads in GPS signal from either * a GPS receiver or a GPS data file, and outputs time and position information * like UTC time, Latitude, Longitude, and Altitude. * * Author : Qingye Jiang (John) * HappyFox Engineering Solutions * qjiang@tsinghua.edu * * 2001-10-15 * */ import java.io.*; import java.util.Calendar; /* * * Thread to parse the GPS signal into position information. * */ public class GpsParser { GpsInfo Info; String GpsDevice, GpsFile; int Factor, Delay; boolean Record, DevOn; /* * * Constructor. * */ public GpsParser(String device, int factor, boolean record, GpsI { GpsDevice = device; Factor = factor; Info = info; if (Factor > 0) Delay = 1000 / Factor; Record = record; DevOn = true; } /** * * Method to start receiving data. * */ public synchronized void start() { DevOn = true; notifyAll(); ParserThread ReceiverThread = new ParserThread(); ReceiverThread.start(); } /** * * Method to stop receiving data and releasing system resource. * */ public synchronized void stop() { DevOn = false; notifyAll(); } /* * * Run method. * */ class ParserThread extends Thread { public void run() { String TempInfo; try { // Open the GPS Receiver device as a fil File InputFile = new File(GpsDevice); FileReader In = new FileReader(InputFile); BufferedReader GpsIn = new BufferedReade if (Factor == 0) { if (Record) { // Create a GPS log file // with Month, Date, Hou // started. String TempStr = Calenda GpsFile = TempStr.substr TempStr.substring(11, 13 File GpsRecord = new Fil FileWriter Out = new Fil PrintWriter GpsOut = new // Monitor the input fro // information from the while (DevOn) { if ((TempInfo = { GpsOut.p if (Temp } } // Close the connection In.close(); Out.close(); } else { // Monitor the input fro // information from the while (DevOn) { if ((TempInfo = { if (Temp } } // Close the connection In.close(); } } else { if (Record) { // Create a GPS log file // with Month, Date, Hou // started. String TempStr = Calenda GpsFile = TempStr.substr TempStr.substring(11, 13 File GpsRecord = new Fil FileWriter Out = new Fil PrintWriter GpsOut = new // Monitor the input fro // information from the while (DevOn) { if ((TempInfo = { GpsOut.p if (Temp sleep(De } } // Close the connection In.close(); Out.close(); } else { // Monitor the input fro // information from the while (DevOn) { if ((TempInfo = { if (Temp sleep(De } } // Close the connection In.close(); } } } catch (IOException e1) { System.out.println("Error opening the GP } catch (InterruptedException e2) { System.out.println("Simulation Mode: Sys } } } /* * method to update position information. This is the method to * GPS signal into position information. * */ private void UpdateInfo(String GpsMsg) { float time, latitude, longitude, altitude; float dilution, separation, age; int i, quality, satellites, reference; String ns, ew; // Remove the message header "$--GGA" i = GpsMsg.indexOf(","); GpsMsg = GpsMsg.substring(i + 1); // Parse the UTC of position hhmmss.ss i = GpsMsg.indexOf(","); time = Float.parseFloat(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetTime(time); // Parse Latitude information. i = GpsMsg.indexOf(","); latitude = Float.parseFloat(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetLatitude(latitude); // Parse Latitude reference information. i = GpsMsg.indexOf(","); ns = GpsMsg.substring(0,1); GpsMsg = GpsMsg.substring(i + 1); Info.SetNS(ns); // Parse Longitude information. i = GpsMsg.indexOf(","); longitude = Float.parseFloat(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetLongitude(longitude); // Parse Longitude reference information. i = GpsMsg.indexOf(","); ew = GpsMsg.substring(0,1); GpsMsg = GpsMsg.substring(i + 1); Info.SetEW(ew); // Remove the GPS Quality indicator i = GpsMsg.indexOf(","); quality = Integer.parseInt(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetGpsQuality(quality); // Remove the number of satellites in use i = GpsMsg.indexOf(","); satellites= Integer.parseInt(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetTotalSatellites(satellites); // Remove the Horizontal dilution of presion i = GpsMsg.indexOf(","); dilution = Float.parseFloat(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetDilution(dilution); // Parse Altitude information. i = GpsMsg.indexOf(","); altitude = Float.parseFloat(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetAltitude(altitude); // Get rid of the "M" identifier. i = GpsMsg.indexOf(","); GpsMsg = GpsMsg.substring(i + 1); // Parse Geoidal separation information. i = GpsMsg.indexOf(","); separation= Float.parseFloat(GpsMsg.substring(0, i)); GpsMsg = GpsMsg.substring(i + 1); Info.SetGeoSeparation(separation); // Get rid of the "M" identifier. i = GpsMsg.indexOf(","); GpsMsg = GpsMsg.substring(i + 1); // Some GPS receiver don't have "Age of Differential GPS // "Differential reference station ID" fields, so we do // these information in this version. } } |
|
|