Java Marine API - NMEA Data Search

My ultimate goal is to get information on armor and long GPS with Adafruit Ultimate GPS (NMEA 0183 standard) on my Java application. For this, I use the Java Marine API. Then the current location will be written to the database along with the time stamp.

So far, I have successfully (I think) configured RXTX to enable coms via a USB port. The Java Marine API comes with some sample .java files. The example below should scan all existing COM ports and look for 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 through the terminal, I see its streaming data.

Please forgive this text dump, I'm a little out of depth and I don’t know which parts matter.

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?

+4
1

^^

com-, !

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 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- ! Serial port schematics

+5

Source: https://habr.com/ru/post/1524593/


All Articles