数据说明如下: $GPRMC 代表GPS推荐的最短数据 204700 UTC_TIME 24小时制的标准时间,按照小时/分钟/秒的格式 A A 或者 V A表示数据"OK",V表示一个警告 3403.868 LAT 纬度值,精确到小数点前4位,后3位 N LAT_DIR N表示北纬,S表示南纬 11709.432 LON 经度值,精确到小数点前5位,后3位 W LON_DIR W表示西经,E表示东经
package earth_survy; import java.util.*; import javax.microedition.io.*; import java.io.*; import com.sun.kjava.*; import com.sun.cldc.io.palm.comm.*; public class GetGPSData extends Spotlet { static Graphics g=Graphics.getGraphics(); static Protocol serialPort = new Protocol(); static String url="0;baudrate=4800;bitsperchar=8;stopbits=1;parity=none"; static InputStream is; // Open the serial Port for Gps Data Input public boolean openPort(){ try { serialPort.open(url,1, true); is=serialPort.openInputStream(); return true; } catch (Exception ex) { return false; } }
//Close the serial Port
public boolean closePort(){ try { is.close(); serialPort.close(); return true; } catch (Exception ex) { return false; } } //Read the GPS data //Mark is "$GPRMC //rdLen is the buffer length //getlen is the return lenth;
public String readGpsData(String mark,int rdlen,int getlen){ byte[] readBuffer = new byte[rdlen]; String rawGpsData; String Gprmc;
while (true){ try{ //Read raw GPS data into a buffer; is.read(readBuffer); rawGpsData=new String(readBuffer); //determin the positon of the Mark==> $GPRMC; int pos=rawGpsData.indexOf(mark); if (pos>-1) { Gprmc=rawGpsData.substring(pos); if (Gprmc.length()>getlen) { Gprmc=Gprmc.substring(0,getlen); break; } } } catch(Exception e){ } } return Gprmc; //end loop }//end method }