簡體   English   中英

使用DataInputStream / socket從GPS設備讀取數據

[英]Read data from GPS Device using DataInputStream/socket

如何從GPS設備讀取數據?

我正在使用套接字和DataInputStream從GPS設備讀取數據,但是在獲取數據時,我得到了一些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));

您可以采用標准NMEA格式與GPS芯片進行通信,也可以與芯片制造商的自定義二進制協議進行通信。
確保正確設置了波特率和(虛擬)COM端口。

默認協議始終為NMEA。 NMEA消息以“ $”開頭。 您總是應該收到一條$ GPRMC消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM