Java Marine API – 寻找NMEA数据
作者:互联网
我的最终目标是从Adafruit Ultimate GPS(NMEA 0183标准)到我的Java应用程序接收lat和长GPS信息.我正在使用Java Marine API来执行此操作.然后将当前位置与时间戳一起写入DB.
到目前为止,我已成功(我认为)配置RXTX以通过USB端口启用com. Java Marine API附带了一些示例.java文件.下面的例子应该扫描所有现有的COM端口并寻找NMEA数据 – 但是当我运行它时,我只得到输出
扫描端口/dev/tty.Bluetooth-Incoming-Port
GPS已连接,当我通过终端访问它时,可以看到它的流数据.
请原谅这个文本转储,我有点超出我的深度,我不确定哪些部分是相关的.
public class SerialPortExample implements SentenceListener {
public SerialPortExample() {
init();}
public void readingPaused() {
System.out.println("-- Paused --");}
public void readingStarted() {
System.out.println("-- Started --");}
public void readingStopped() {
System.out.println("-- Stopped --");}
public void sentenceRead(SentenceEvent event) {
// here we receive each sentence read from the port
System.out.println(event.getSentence());}
private SerialPort getSerialPort() {
try {
Enumeration<?> e = CommPortIdentifier.getPortIdentifiers();
while (e.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
SerialPort sp = (SerialPort) id.open("SerialExample", 30);
sp.setSerialPortParams(4800, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream is = sp.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buf = new BufferedReader(isr);
System.out.println("Scanning port " + sp.getName());
// try each port few times before giving up
for (int i = 0; i < 5; i++) {
try {
String data = buf.readLine();
if (SentenceValidator.isValid(data)) {
System.out.println("NMEA data found!");
return sp;}
} catch (Exception ex) {
ex.printStackTrace();}}
is.close();
isr.close();
buf.close();}
}
System.out.println("NMEA data was not found..");
} catch (Exception e) {
e.printStackTrace();}
return null;}
private void init() {
try {SerialPort sp = getSerialPort();
if (sp != null) {
InputStream is = sp.getInputStream();
SentenceReader sr = new SentenceReader(is);
sr.addSentenceListener(this);
sr.start(); }
} catch (IOException e) {
e.printStackTrace();}}
public static void main(String[] args) {
new SerialPortExample();}}
所以我认为它看着tty.Bluetooth-Incoming-Port?我怎样才能看到tty.usbserial端口?
谢谢
解决方法:
你接近连接^^
当您扫描系统以获取com-ports时,只需打印出来!
while (e.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
if(serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(serialPortId.getName());//print the serial port name
}
}
你的输出应该是这样的
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
/dev/ttyS1
/dev/ttyS0
既然您知道串口的名称,您可以使用此信息打开该端口!假设你的GPS连接到串口ttyS1 ……
再次进入你的代码,并像这样适应:
enumComm = CommPortIdentifier.getPortIdentifiers();
while(enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (serialPortId.getName().equalsIgnoreCase("/dev/ttyS1")) { //HERE you get your serial port
try {
SerialPort sp = (SerialPort) id.open("SerialExample", 30);
//continue with your code here
} catch (PortInUseException e) {
System.out.println("port in use");
}
}
}
这只能起作用,因为你的usb设备创建了一个虚拟串口!
标签:java,gps,rxtx 来源: https://codeday.me/bug/20190529/1175109.html