?? gpsreader.java
字號:
package Reader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.io.*;
import javax.microedition.*;
import java.io.*;
public class GPSReader implements Runnable {
private InputStreamReader reader;
private Thread runner;
private RecordBuffer buffer;
private static final long BREAK = 2000;
private static final int LINE_DELIMITER = 13;
private Logger logger;
private StreamConnection conn = null;
//private FileConnection conn = null;
private String com = new String();
private InputStream is;
public GPSReader(Logger logger, String com) {
this.logger = logger;
this.com = com;
this.buffer = new RecordBuffer();
}
private synchronized void connect() throws IOException {
// conn = (FileConnection) Connector.open(com);
conn = (StreamConnection) Connector.open(com);
// conn = (StreamConnection) Connector.open(com, Connector.READ);
reader = new InputStreamReader(conn.openInputStream());
// System.out.print("OOOOOOOOOOOOO");
}
/*private synchronized void BuildReader() throws IOException{
InputStream is;
String p = new String();
p = "C:/Users/Jack/Desktop/gps.txt";
reader =new InputStreamReader(getClass().getResourceAsStream(p));
//conn = (FileConnection )Connector.open("C://Users//Jack//Desktop//Gps.dat");
System.out.println(")))))))))");
// conn = (StreamConnection)Connector.open("comm:0;baudrate=9000");
// StringBuffer str = new StringBuffer();
// reader = new InputStreamReader(conn.openInputStream());
System.out.println(")))))))))");
System.out.println(")))))))))");
}*/
private synchronized void disconnect() {
try {
if (reader != null)
reader.close();
if (is != null)
is.close();
} catch (IOException e) {
}
reader = null;
is = null;
}
// try {
// if (reader != null)
// reader.close();
// if (conn != null)
// conn.close();
// } catch (IOException e) {
// }
// reader = null;
// conn = null;
// }
public Record getRecord() throws EmptyBufferException {
return buffer.getRecord();
}
public synchronized boolean isConnected() {
return is != null && reader != null;
// return conn != null && reader != null;
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
runner = null;
disconnect();
}
public void run() {
int startType = Parser.TYPE_START;
int type = Parser.TYPE_NA;
Record record = new Record();
while (Thread.currentThread() == runner) {
try {
if (!isConnected())
connect();
// BuildReader();
String output = new String();
// int i = 0;
int input;
// int len = (int)conn.getLength();
while ((input = reader.read()) != LINE_DELIMITER)
output += (char) input;
output = output.substring(1, output.length() - 1);
try {
type = Parser.parse(output, record);
// System.out.print("PPPPPPPPPPPPPPP");
buffer.putRecord(record);
// str.append((char)input);
}
catch (UnsupportedTypeException ue) {
type = Parser.TYPE_NA;
}
catch (ParseException pe) {
logger.appendString("Parse exception: " + pe.getMessage());
pe.printStackTrace();
}
if (type != Parser.TYPE_NA) {
if (type == startType) {
disconnect();
startType = Parser.TYPE_START;
record = new Record();
try {
Thread.sleep(BREAK);
} catch (InterruptedException e) {
logger.appendString(e.getMessage());
}
}
else if (startType == Parser.TYPE_START) {
startType = type;
}
}
}
catch (IOException ie) {
try {
Thread.sleep(BREAK);
} catch (InterruptedException e) {
logger.appendString("IO exception: " + e.getMessage());
}
ie.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -