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) {
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());
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?