[英]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.