Java 如何替换字符串继续
我在java中编写解码gps的代码,gps发送给我继续字符串,我可以在代码中看到,但当我解码它时,只有一个字符串被解码,工作停止,因为我的字符串必须为空,下一个字符串我尝试这样做,但没有成功。有人知道这种情况吗Java 如何替换字符串继续,java,string,Java,String,我在java中编写解码gps的代码,gps发送给我继续字符串,我可以在代码中看到,但当我解码它时,只有一个字符串被解码,工作停止,因为我的字符串必须为空,下一个字符串我尝试这样做,但没有成功。有人知道这种情况吗 package communication; import javax.comm.*; import java.util.*; import java.io.*; import new8.*; class Serial { public static void main(String
package communication;
import javax.comm.*;
import java.util.*;
import java.io.*;
import new8.*;
class Serial
{
public static void main(String args[]) throws UnsupportedCommOperationException, IOException, TooManyListenersException
{
int c=1;
String wantedPortName = "COM6";
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
while(portIdentifiers.hasMoreElements())
{
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
pid.getName().equals(wantedPortName))
{
portId = pid;
break;
}
}
if(portId == null)
{
System.err.println("Could not find serial port " + wantedPortName);
System.exit(1);
}
else
{
System.out.println("system find gps reciever");
}
SerialPort port = null;
try {
port = (SerialPort) portId.open(
"RMC",
1);
System.out.println("all are ok");
} catch(PortInUseException e) {
System.err.println("Port already in use: " + e);
System.exit(1);
}
port.setSerialPortParams(
4800,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
BufferedReader is = null;
try {
is = new BufferedReader(new InputStreamReader(port.getInputStream()));
System.out.println("data is ok");
} catch (IOException e) {
System.err.println("Can't open input stream: write-only");
is = null;
}
String pt=null;
while(true)
{
is.readLine();
is.readLine();
String st = is.readLine();
System.out.print("("+c+")");
c++;
new8 obj1=new new8();
obj1.decode(st);
System.out.println(st);
st=st.replace(st, "");
}
if (is != null) is.close();
/*if (os != null) os.close();*/
if (port != null) port.close();
}
}
您想要实现的是通过COM端口解析来自GPS接收器的NMEA数据。
我在C(开源项目导航系统“Navit”)中见过这样的代码。
然而,我记得序列号可能会被破坏,因此,他们检查 如果消息中包含“$GP”preambel。以及NMEA行中提供的校验和是否正确
我建议您看看Navit代码(尽管它是用C编写的)。您不是第一个从COM端口解码NMEA的人。代码应该如下所示,以获得正确的流程。没有说任何关于逻辑的东西
if (is != null) {
//String pt = null;
while (true) {
String st = is.readLine();
if (st == null) {
break;
}
st = is.readLine();
if (st == null) {
break;
}
st = is.readLine();
if (st == null) {
break;
}
System.out.print("(" + c + ")");
c++;
new8 obj1 = new new8();
obj1.decode(st);
System.out.println(st);
st = st.replace(st, "");
}
is.close();
}
你能格式化你的代码吗?请格式化你的代码以便于阅读,并添加完整的堆栈跟踪。指出代码中的哪一行引发异常。我认为您应该重新表述您的问题,我认为很多人都不会理解。抱歉,它仍然不起作用。只有一个字符串被解码,并且由于某些错误而停止工作。我成功解码了NMEA0183协议。并在我的电脑屏幕上显示NMEA 0183字符串continue。但问题是,如果我手动将字符串解码成功,我的下一个字符串将无法继续解码。即使您修复了错误,代码也远不能正常工作:NMEA语句以不同的间隔出现:RMC每秒,GSV例如每5秒或10秒。您必须检查消息是否正确($GPRMC)