datagps.java

来自「gpsLogger是一个基于手机或PDA的个人移动导航(车载导航)系统中记录gp」· Java 代码 · 共 271 行

JAVA
271
字号
import waba.ui.Button;
import waba.ui.Container;
import waba.ui.ControlEvent;
import waba.ui.Edit;
import waba.ui.Event;
import waba.ui.InputDialog;
import waba.ui.MessageBox;
import superwaba.ext.xplat.io.gps.GPS;
import waba.ui.MainWindow;
import waba.ui.*;
import waba.io.*;
//import superwaba.ext.xplat.io.gps.GPS;
import waba.sys.*;
import waba.fx.*;
import waba.io.DataStream;
import waba.io.File;
import waba.io.SerialPort;

public class dataGps extends Container {
	dataCapture dataCapture;
	SerialPort sp;
	private Timer gpsTimer;
	MessageBox mbComPort, mbBluetoothHelp;
	/** Buffer size may be adjusted as needed... */
	public static final int BUF_LEN = 200;
	byte[] buf = new byte[BUF_LEN];
	char[] cbuf = new char[BUF_LEN];
	StringBuffer sb = new StringBuffer(BUF_LEN);
	String[] text = new String[]{"","","","","","","",""};
	String szPortNumber;
	double[] diLocation = new double[2];
	int msgCount;
	int iCounter = 0;
		
	public Time lastFix=new Time(); // Stores the time of lastFix. 
	public double direction = -1.0f; // Stores the direction  
	public double velocity = -1.0f; // Stores the velocity
	public Button btnRestartGps, btnHelp;
	dataGps dataGps;
	
	public dataGps(){//configures for gps data stream entry point...		
		if ((gpsDataLogger.szPlatform.compareTo("Java")==0)||
				(gpsDataLogger.szPlatform.compareTo("Win32")==0)||
				(gpsDataLogger.szPlatform.compareTo("Linux")==0)){
			//...haven't tested this: may work... may not work w/out mods. 
			sp = new SerialPort(SerialPort.USB,9600);
		}else if ((gpsDataLogger.szPlatform.compareTo("PalmOS/SDL")==0)||
				(gpsDataLogger.szPlatform.compareTo("PalmOS")==0)){					
			sp = new SerialPort( SerialPort.BLUETOOTH, 9600);
		}else{//"WindowsCE",or "PocketPC"...what else??
			sp = new SerialPort( gpsDataLogger.iPortNumber, 9600);
		}
		sp.setReadTimeout(100);//...previously set at 2000
		sp.setFlowControl(false);
		text[0]="GPS Initialising";
		gpsTimer = addTimer(1000);
		nextMessage();		
	}	
	public void onStart() {	
		btnHelp = new Button("Bluetooth Help");
		btnHelp.setGap(5);
		add(btnHelp,LEFT+5,BOTTOM-5);
		
		btnRestartGps = new Button( "Reset COM port");
		btnRestartGps.setGap(5);
		add(btnRestartGps,RIGHT-5,BOTTOM-5);
		
		//gpsTimer = addTimer(100);
	}	
	
	public void onPaint(Graphics g){//this prints out the little strings on rePaint...
		g.setForeColor(foreColor);
		g.setBackColor(backColor);
		g.setFont(new Font("SW",Font.BOLD,14));
		g.fillRect(0,0,width,height); // really necessary?
		
		int y = 50;//up and down start for string[0]
		int x = 25;//left to right start for string [0]
		int n = text.length;
		for (int i =0; i < n; i++,y+=fmH)
			g.drawText(text[i], x, y);
	}
	public void processInput(){
		String message;
		if ((message=nextMessage())!=null){			
			String[] sp=Convert.tokenizeString(message,',');			
			if (sp[0].equals("$GPRMC")){
				diLocation[0] = toCoordinate(sp[3], sp[4].charAt(0));//[3] = Lat, [4] = N
				diLocation[1] = toCoordinate(sp[5], sp[6].charAt(0));//[5] = Long, [6] = W				
				
				if (sp[1].length() >= 6){//[1] = time	
					//below converts Greenwich to Local time, with "15" the appropriate value for Alaska
					//lastFix.hour   = (Convert.toInt(sp[1].substring(0, 2)))+ 15;
					lastFix.hour   = (Convert.toInt(sp[1].substring(0, 2)));
					lastFix.minute = Convert.toInt(sp[1].substring(2, 4));
					lastFix.second = Convert.toInt(sp[1].substring(4, 6));
					lastFix.millis = 0;
				}
				gpsDataLogger.szLat = Convert.toString(diLocation[0],4);
				gpsDataLogger.szLong = Convert.toString(diLocation[1],4);
				gpsDataLogger.szLatNs = sp[4];
				gpsDataLogger.szLongEw = sp[6];
				gpsDataLogger.szTime = lastFix.toString();
				//String s = sp[2];
				gpsDataLogger.szValid = sp[2];
				
				
				velocity = Convert.toDouble(sp[7]); //knots
				direction = Convert.toDouble(sp[8]); //degrees
				
				text[0] = "lat: " + diLocation[0];
				text[1] = "lon: " + diLocation[1];
				text[2] = "fix: " + lastFix.toString();
				text[3] = "valid:"+ gpsDataLogger.szValid;
				text[4] = "direction: " + direction;
				text[5] = "speed: " + velocity;
				text[6] = sp[3] + sp[4];
				text[7] = sp[5] + sp[6];
			}
		}else{
			text[0] ="No message " + (msgCount++);
			text[1] = text[2] = text[3] = text[4] = text[5] = "";
		}
		repaint();
		//iRestartFlag = 0;
	}
	public int getPreferredWidth(){
		if (text==null) 
			return FILL;
		int w = 0;
		for (int i =text.length-1; i >= 0; i--)
			w = Math.max(w, fm.getTextWidth(text[i]));
		return w;
	}
	public int getPreferredHeight(){
		return fmH*text.length;
	}	
	// Returns the location array. The latitude is stored in position 0, and the longitude in position 1. 
	public double[] getLocation(){
		return diLocation;
	}
	// Returns the latitude.
	public double getLatitude(){
		return diLocation[0];
	}
	// Returns the longitude.
	public double getLongitude(){
		return diLocation[1];
	}
	private String nextMessage(){
		// put the fields in the stack
		StringBuffer sb=this.sb;
		char []cbuf = this.cbuf;
		byte []buf = this.buf;
		
		int bufCount=0;
		int maxAvailable=0;
		sb.setLength(0);
		while (true){
			int len = 0;
			while (bufCount-- > 0){
				byte b = buf[len];
				if ((b==13 || b==10) && (len > 0 || sb.length() > 0)){ // end of line and buffer not empty?
					// append the last read chars
					if (len > 0)
						sb.append(cbuf,0,len);
					// check here if we need to remove the *. avoid an extra string creation
					len = sb.length();
					if (sb.charAt(len-3) == '*')
						sb.setLength(len-3);
					// return the string
					return sb.toString();
				}
				cbuf[len++] = (char)b;
			}
			if (len > 0)
				sb.append(cbuf,0,len);
			
			// fetch data
			int available=sp.readCheck();
			if (available==0)
				return "0 available";
			if (available!=-1)
				maxAvailable=available;
			else
				sp.readBytes(buf,0,1);
			
			int toRead = (available > BUF_LEN || available == -1) ? BUF_LEN : available;
			bufCount = sp.readBytes(buf,0,toRead);
			if (bufCount==-1)
				return ">negative one"; // nothing read
			if (bufCount==0)
				return "0 read ("+toRead+" to read ("+available+" available ("+maxAvailable+")))";
		}
	}	
	public void restartGps(){
		sp.close();
		if (gpsDataLogger.szPlatform.compareTo("PalmOS/SDL")==0){					
			sp = new SerialPort( SerialPort.BLUETOOTH, 9600);
		}else{
			sp = new SerialPort( gpsDataLogger.iPortNumber, 9600);
		}
		sp.setReadTimeout(100);//...previously set at 2000
		sp.setFlowControl(false);
		text[0]="GPS Initialising";
		text[1] = text[2] = text[3] = text[4] = text[5] = text[6] = text[7] = "";
		gpsTimer = addTimer(1000);
		nextMessage();		
	}	
	public double toCoordinate(String s,char dir){
		double deg = 0;
		int i = s.indexOf('.'); 
		if (i>=0){
			int divider=1;
			int size = s.length();
			for (int d=size-i-1;d > 0;d--)
				divider*=10;
			deg=(double)Convert.toInt(s.substring(0,i-2))+
			((double)Convert.toInt(s.substring(i-2,i))+
					(double)Convert.toInt(s.substring(i+1,size))/divider)/60;
			if (dir == 'S' || dir == 'W')
				deg=-deg;
		}return deg;
	}	
	public void onEvent(Event e){
		int iBytes = 6;
		switch (e.type){
		/*************************************************************************
		 * Below timer event is structured so that if there are 15 iterations 
		 * of less than 5 bytes of data in the bluetooth serial queue, the serial
		 * port is closed. This takes 15 seconds. It's crude, but it works.
		 * 
		 * The processInput() function grabs and parses the NMEA strings into 
		 * the szStrings that are used by the application.
		 *************************************************************************/
		case ControlEvent.TIMER:
			if (sp.isOpen()){
				iBytes = sp.readCheck();
				if (iBytes < 5){
					iCounter++;
					if(iCounter == 15){
						sp.close();
						text[0]="GPS Signal Lost";
						text[1] = text[2] = text[3] = text[4] = text[5] = text[6] = text[7] = "";
						repaint();
						iCounter = 0;
						gpsDataLogger.szValid = "";
						gpsDataLogger.szTime = "";
					}
				}else{
					iCounter = 0;
					processInput();
				}break;}					
		case ControlEvent.PRESSED:
			if (e.target == btnRestartGps){
				InputDialog id = new InputDialog("Reset COM port", " Please enter a different| COM port number, or leave| the current one and press,|    'Restart'. | The default COM port is 8.",Convert.toString(gpsDataLogger.iPortNumber), new String[]{"Restart","Cancel"});
				id.popupBlockingModal();			
				if (id.getPressedButtonIndex()==0){
					String szTemp = id.getValue();
					gpsDataLogger.iPortNumber = Convert.toInt(szTemp);
					restartGps(); }break;			
			}
			else if (e.target == btnHelp){				
				mbBluetoothHelp = new MessageBox("Bluetooth Help","Dell Axim settings are: |1)GPS Program Port = COM3, GPS |hardware port = COM1 2.)Bluetooth |Incoming port = COM7, GARMIN GPS |10 = COM8. For Lap/Desktop, determine |what COM port Bluetooth is using, then | press the button at the lower right and | adjust the COM port accordingly.");	
				mbBluetoothHelp.setUnpopDelay(15000);
				mbBluetoothHelp.popupBlockingModal();	
			}break;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?