[英]How to read particular string?
嗨,我正在嘗試制作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());
}
}
}
我沒有按照以下代碼更新代碼
您已經在代碼中定義了一個BufferedReader對象,但尚未使用。 如果使用,則可以使用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
代碼,而不是兩者都使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.