2014-03-31 172 views
0

嗨,我想做的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()); 
} 
} 
} 

我没有下面的代码更新的代码

+0

你是什​​么意思“每个字符串的第一行”?你是说你想把字符串分成几行吗?这是一个通过使用[gpsd](http://www.catb.org/gpsd/)并通过JSON与之交谈可以更容易完成的任务吗? – chrylis

+0

我想这个字符串从我的GPS设备抛出每个字符串$ GPRMC,235949.799,V ,,,,, 0.00,0.00,050180 ,,, N * 46 – user3456343

回答

0

你已经在你的代码中定义aBufferedReader对象,但没有使用。如果你使用,如果你可以使用readLine方法。在每一行成功读取后,您可以将其发送给另一个处理方法。

构造您的BufferedReader作为

br = new BufferedReader(new InputStreamReader(mInputFromPort)); 

你想要的是

//for(int i=0;i<=mBytesIn.length;i++){ 

// CUT ALL OF THIS CODE 

// until here 
try { 

     br = new BufferedReader (new InputStreamReader(mInputFromPort)); 
     while ((sCurrentLine = br.readLine()) != null) { 
      System.out.println(sCurrentLine); 
     } 
} catch(IOException e){ 
e.printStackTrace(); 
} 

你不想做mInputFromPort.read();的东西,你现在将要使用的readLine代码不能同时使用

+0

没有工作亲爱的 – user3456343

+0

再次我更新了什么是使用 – user3456343

+0

我已更新你的问题。 –

相关问题