简体   繁体   English

使用DataInputStream / socket从GPS设备读取数据

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

How to read data From GPS Device? 如何从GPS设备读取数据?

I am reading data from GPS device using socket & DataInputStream but while i am fetching data i am getting something ASCII encoded string and at the end of that string i have my answer string which i want to use. 我正在使用套接字和DataInputStream从GPS设备读取数据,但是在获取数据时,我得到了一些ASCII编码的字符串,并且在该字符串的末尾有要使用的答案字符串。 So how can i get continuously data without such complex character set and get exact string. 因此,如何在没有如此复杂的字符集的情况下连续获取数据并获得准确的字符串。 I have also tried using serial port. 我也尝试过使用串口。

Here what I have tried. 这是我尝试过的。

//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));

You can communicate with the GPS chip either in normed NMEA format, or with the chips manufaturer custom binary protocol. 您可以采用标准NMEA格式与GPS芯片进行通信,也可以与芯片制造商的自定义二进制协议进行通信。
Make sure that you have setup the baud rate and (virtual) com port correctly. 确保正确设置了波特率和(虚拟)COM端口。

The default protocoll is always NMEA. 默认协议始终为NMEA。 NMEA messages start with a "$". NMEA消息以“ $”开头。 You always should get a $GPRMC message 您总是应该收到一条$ GPRMC消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM