嗨,我想做的Java桌面應用程序,我從我的Java字符串變量從seril端口接收價值,我通過硬件設備接收數據,這將不斷拋出數據我現在將數據存儲在字符串變量現在我只想從中得到一些特定的數據。如何讀取特定的字符串?
所以,我怎麼能在下面的格式實現這個我收到的數據
$GPRMC,235949.799,V,,,,,0.00,0.00,050180,,,N*46
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
$GPGGA,235949.799,,,,,0,0,,,M,,M,,*4F
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79
$GPGLL,,,,,235949.799,V,N*7D
$GPTXT,01,01,02,ANTSTATUS=OPEN*2B
此字符串連續接收
我想從這個字符串只有一線獲得從每一個字符串
這裏是我的代碼
public class ListPortClass
{
private int times = 0;
public static void main(String[] s)
{
BufferedReader br = null;
try
{
String sCurrentLine;
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM6");
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Port in use!");
}
else {
System.out.println(portIdentifier.getName());
SerialPort serialPort = (SerialPort) portIdentifier.open("ListPortClass", 300);
int b = serialPort.getBaudRate();
System.out.println(Integer.toString(b));
serialPort.setSerialPortParams(300, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream mOutputToPort = serialPort.getOutputStream();
InputStream mInputFromPort = serialPort.getInputStream();
String mValue = "DEBUG_GPS";
System.out.println("beginning to Write . \r\n");
mOutputToPort.write(mValue.getBytes());
System.out.println("DEBUG_GPS Command Written to Port. \r\n");
mOutputToPort.flush();
System.out.println("Waiting for Reply \r\n");
Thread.sleep(500);
byte mBytesIn [];// = new byte[48];
//for(int i=0;i<=mBytesIn.length;i++){
mInputFromPort.read();
try{
while(true){
int a = mInputFromPort.read();
char c=(char)a;
//String c =String.valueOf(a);
//System.out.print(c);
String temp = Character.toString(c);
System.out.print(temp);
}}
catch(IOException e)
{System.out.println(e);}
mOutputToPort.close();
mInputFromPort.close();
}
}
catch (Exception ex)
{
System.out.println("Exception : " + ex.getMessage());
}
}
}
這裏是我的更新代碼
public static void main(String[] s)
{
BufferedReader br = null;
try
{
String sCurrentLine;
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM6");
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Port in use!");
}
else {
System.out.println(portIdentifier.getName());
SerialPort serialPort = (SerialPort) portIdentifier.open("ListPortClass", 300);
int b = serialPort.getBaudRate();
System.out.println(Integer.toString(b));
serialPort.setSerialPortParams(300, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream mOutputToPort = serialPort.getOutputStream();
InputStream mInputFromPort = serialPort.getInputStream();
String mValue = "DEBUG_GPS";
System.out.println("beginning to Write . \r\n");
mOutputToPort.write(mValue.getBytes());
System.out.println("DEBUG_GPS Command Written to Port. \r\n");
mOutputToPort.flush();
System.out.println("Waiting for Reply \r\n");
Thread.sleep(500);
try {
br = new BufferedReader (new InputStreamReader(mInputFromPort));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
}
catch(IOException e){
e.printStackTrace();
}
//mOutputToPort.close();
//mInputFromPort.close();
}
}
catch (Exception ex)
{
System.out.println("Exception : " + ex.getMessage());
}
}
}
我沒有下面的代碼更新的代碼
你是什麼意思「每個字符串的第一行」?你是說你想把字符串分成幾行嗎?這是一個通過使用[gpsd](http://www.catb.org/gpsd/)並通過JSON與之交談可以更容易完成的任務嗎? – chrylis
我想這個字符串從我的GPS設備拋出每個字符串$ GPRMC,235949.799,V ,,,,, 0.00,0.00,050180 ,,, N * 46 – user3456343