Java Marine API-寻找NMEA数据 [英] Java Marine API - looking for NMEA data

查看:134
本文介绍了Java Marine API-寻找NMEA数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是从Adafruit Ultimate GPS(标准为NMEA 0183)接收纬度和经度GPS信息到我的Java应用程序.我正在使用Java Marine API来做到这一点.然后,当前位置将与时间戳一起写入数据库.

My ultimate goal is to receive lat and long GPS info from an Adafruit Ultimate GPS (NMEA 0183 standard) to my Java app. I am using the Java Marine API to do this. Current location will then be written to a DB along with timestamp.

到目前为止,我已经成功(我认为)将RXTX配置为通过USB端口启用coms.Java Marine API附带了一些示例.java文件.下面的示例应扫描所有现有的COM端口并查找NMEA数据-但是当我运行它时,我只会得到输出

So far I have successfully (I think) configured RXTX to enable coms over the USB port. The Java Marine API comes with some example .java files. The example below should scan through all existing COM ports and seek NMEA data - but when I run it I just get the output

扫描端口/dev/tty.Bluetooth-Incoming-Port

GPS已连接,当我通过终端访问时,我可以看到它正在传输数据.

The GPS is connected, and when I access it via Terminal I can see it streaming data.

请原谅此文本转储,我有点不了解,并且不确定哪些部分相关.

Please forgive this text dump, I am a little out of my depth and am unsure what parts are of relevance.

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入站端口卡住了吗?如何查看tty.usbserial端口?

So I think it's stuck looking at the tty.Bluetooth-Incoming-Port? How might I get it to look at the tty.usbserial port?

谢谢

推荐答案

您已经很容易建立联系^^

you're close to get connected ^^

当您扫描系统中的通讯端口时,只需将它们打印出来即可!

when you scan your system for com-ports, just print them out!

while (e.hasMoreElements()) {
    CommPortIdentifier id = (CommPortIdentifier) e.nextElement();
    if(serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        System.out.println(serialPortId.getName());//print the serial port name
    }
}

您的输出应该类似于

本地lib版本= RXTX-2.1-7
Java库版本= RXTX-2.1-7
/dev/ttyS1
/dev/ttyS0

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
/dev/ttyS1
/dev/ttyS0

现在您知道串行端口的名称,就可以使用此信息打开该端口!假设您的GPS已连接到串口ttyS1 ...

now that you know the name of the serial port you can use this information to open that port! Let's say your GPS is connected to serial port ttyS1...

再次进入您的代码并像这样修改它:

go again into your code and adapt it like this:

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设备创建了虚拟串行端口!

this only works because your usb-device creates a virtual serial port!

这篇关于Java Marine API-寻找NMEA数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆