Java Marine API - looking for NMEA data

只愿长相守 提交于 2019-12-12 11:50:37

问题


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.

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

Scanning port /dev/tty.Bluetooth-Incoming-Port

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();}}

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?

With thanks


回答1:


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
    }
}

your output should be something like

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

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");
        }
    }
}

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



来源:https://stackoverflow.com/questions/21466396/java-marine-api-looking-for-nmea-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!