0

如何从 GPS 设备读取数据?

我正在使用 socket & 从 GPS 设备读取数据,DataInputStream但是当我获取数据时,我得到了一些 ASCII 编码的字符串,并且在该字符串的末尾,我有我想要使用的答案字符串。那么我如何在没有如此复杂的字符集的情况下连续获取数据并获得准确的字符串。我也尝试过使用串口。

这是我尝试过的。

//Using Serial Port //

CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portname);
System.out.println("fired");
// Open port
// Requires owner name and timeout
CommPort port = portId.open("Java Printing", 3000);

// Setup reading from file
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);

// Setup output
OutputStream os = port.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);

int c;
while ((c = bis.read()) != -1) 
{
bos.write(c);
System.out.print((char)c);
}

// Close
bos.close();`enter code here`
bis.close();
port.close();

//Using Normal Socket //

char[] inputChars = new char[1024];
byte[] data = new byte[1024];
int charsRead = 0;
BufferedReader inputStream = null;

System.out.println("1 1 1 1 1");
InputStreamReader isr = new InputStreamReader( s1.getInputStream() );
System.out.println("2 2 2 2 2 2");
inputStream = new BufferedReader( isr );

//Read 1024 characters. Note: This will pause the thread when stream is empty.

System.out.println("Reading from stream:");
while ((charsRead =  inputStream.read(inputChars)) != -1)
{
System.out.println("Chars read from stream: " + charsRead);  
System.out.println("inputChars = "+inputChars);
data = new String(inputChars).getBytes("US-ASCII");
System.out.flush();
}
byte[] decodedBytes = Base64.encodeBase64(data);
System.out.println("decodedBytes " + new String(decodedBytes));
4

1 回答 1

0

您可以使用标准NMEA格式或使用芯片制造商自定义二进制协议与 GPS 芯片进行通信。
确保您已正确设置波特率和(虚拟)com 端口。

默认协议始终为 NMEA。NMEA 消息以“$”开头。您总是应该收到 $GPRMC 消息

于 2013-06-28T15:42:44.977 回答