mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-21 21:25:38 +00:00
first working micro-example that parses the NMEA sentences emitted by the WindVane
This commit is contained in:
@@ -7,4 +7,6 @@ Bundle-Vendor: SAP
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Require-Bundle: com.sap.sailing.nmeaconnector,
|
||||
org.junit4;bundle-version="4.8.2"
|
||||
org.junit4;bundle-version="4.8.2",
|
||||
com.sap.sailing.domain,
|
||||
com.sap.sailing.domain.common
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
$GPGGA,220550,4124.7580,N,08152.2565,W,2,04,4.4,235.1,M,-34.0,M,,*7B
|
||||
$WIMWV,229.0,R,05.4,K,A*1E
|
||||
$WIMWV,228.0,R,07.2,K,A*1B
|
||||
$WIMWV,228.0,R,05.8,K,A*13
|
||||
@@ -8,7 +9,7 @@ $WIMWV,219.0,R,03.2,K,A*1D
|
||||
$WIBAT,1,5*4D
|
||||
$WIMWV,218.0,R,04.0,K,A*19
|
||||
$WIMWV,218.0,R,07.0,K,A*1A
|
||||
i, 19. Dez 2012 11:23:20
|
||||
Mi, 19. Dez 2012 11:23:20
|
||||
Mi, 19. Dez 2012 11:23:25
|
||||
Mi, 19. Dez 2012 11:23:30
|
||||
Mi, 19. Dez 2012 11:23:35
|
||||
|
||||
-181
@@ -1,181 +0,0 @@
|
||||
/*
|
||||
* SerialPortExample.java
|
||||
* Copyright (C) 2011 Kimmo Tuukkanen
|
||||
*
|
||||
* This file is part of Java Marine API.
|
||||
* <http://sourceforge.net/projects/marineapi/>
|
||||
*
|
||||
* Java Marine API is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Java Marine API is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Java Marine API. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.sf.marineapi.example;
|
||||
|
||||
import gnu.io.CommPortIdentifier;
|
||||
import gnu.io.SerialPort;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import net.sf.marineapi.nmea.event.SentenceEvent;
|
||||
import net.sf.marineapi.nmea.event.SentenceListener;
|
||||
import net.sf.marineapi.nmea.io.SentenceReader;
|
||||
import net.sf.marineapi.nmea.sentence.SentenceValidator;
|
||||
|
||||
// If using Java Communications API, remove gnu.io imports above and
|
||||
// use corresponding javax.comm classes:
|
||||
//
|
||||
// import javax.comm.CommPortIdentifier;
|
||||
// import javax.comm.SerialPort;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Serial port example using Java Communications API or RXTX libraries.
|
||||
* Application scans through all existing COM ports and seeks for NMEA data. If
|
||||
* valid data is found, scanning stops and application starts printing out the
|
||||
* sentences it reads from the port.
|
||||
* <ul>
|
||||
* <li><a
|
||||
* href="http://www.oracle.com/technetwork/java/index-jsp-141752.html">Java
|
||||
* Communications API</a>
|
||||
* <li><a href="http://rxtx.qbang.org/">RXTX wiki</a>
|
||||
* </ul>
|
||||
*
|
||||
* @author Kimmo Tuukkanen
|
||||
*/
|
||||
public class SerialPortExample implements SentenceListener {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public SerialPortExample() {
|
||||
init();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see net.sf.marineapi.nmea.event.SentenceListener#readingPaused()
|
||||
*/
|
||||
public void readingPaused() {
|
||||
System.out.println("-- Paused --");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see net.sf.marineapi.nmea.event.SentenceListener#readingStarted()
|
||||
*/
|
||||
public void readingStarted() {
|
||||
System.out.println("-- Started --");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see net.sf.marineapi.nmea.event.SentenceListener#readingStopped()
|
||||
*/
|
||||
public void readingStopped() {
|
||||
System.out.println("-- Stopped --");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see
|
||||
* net.sf.marineapi.nmea.event.SentenceListener#sentenceRead(net.sf.marineapi
|
||||
* .nmea.event.SentenceEvent)
|
||||
*/
|
||||
public void sentenceRead(SentenceEvent event) {
|
||||
// here we receive each sentence read from the port
|
||||
System.out.println(event.getSentence());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan serial ports for NMEA data.
|
||||
*
|
||||
* @return SerialPort from which NMEA data was found, or null if data was
|
||||
* not found in any of the ports.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init serial port and reader.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Startup method, no arguments required.
|
||||
*
|
||||
* @param args None
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
new SerialPortExample();
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ $WIMWV,219.0,R,03.2,K,A*1D
|
||||
$WIBAT,1,5*4D
|
||||
$WIMWV,218.0,R,04.0,K,A*19
|
||||
$WIMWV,218.0,R,07.0,K,A*1A
|
||||
i, 19. Dez 2012 11:23:20
|
||||
Mi, 19. Dez 2012 11:23:20
|
||||
Mi, 19. Dez 2012 11:23:25
|
||||
Mi, 19. Dez 2012 11:23:30
|
||||
Mi, 19. Dez 2012 11:23:35
|
||||
|
||||
+75
-1
@@ -2,16 +2,90 @@ package com.sap.sailing.nmeaconnector.test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import net.sf.marineapi.nmea.event.SentenceEvent;
|
||||
import net.sf.marineapi.nmea.event.SentenceListener;
|
||||
import net.sf.marineapi.nmea.io.SentenceReader;
|
||||
import net.sf.marineapi.nmea.sentence.Sentence;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sap.sailing.domain.base.impl.MillisecondsTimePoint;
|
||||
import com.sap.sailing.domain.common.TimePoint;
|
||||
|
||||
public class ReadWindVaneDataTest {
|
||||
@Test
|
||||
public void readWindVaneFile() throws IOException {
|
||||
public void testWindVaneFilePresence() throws IOException {
|
||||
InputStream is = getClass().getResourceAsStream("/windvane.nmea");
|
||||
assertNotNull(is);
|
||||
is.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWindVaneOutputWithInterlacedTimeStamps() throws IOException, InterruptedException {
|
||||
DateFormat df = new SimpleDateFormat("EE, dd. MMM yyyy hh:mm:ss", Locale.GERMANY);
|
||||
final TimePoint[] lastTimestampFromFile = new TimePoint[1];
|
||||
InputStream is = getClass().getResourceAsStream("/windvane.nmea");
|
||||
PipedInputStream pis = new PipedInputStream();
|
||||
PipedOutputStream pos = new PipedOutputStream(pis);
|
||||
SentenceReader nmeaReader = new SentenceReader(pis);
|
||||
nmeaReader.addSentenceListener(new SentenceListener() {
|
||||
@Override
|
||||
public void sentenceRead(SentenceEvent event) {
|
||||
TimePoint timePoint = new MillisecondsTimePoint(event.getTimeStamp());
|
||||
Sentence sentence = event.getSentence();
|
||||
System.out.println("Last timestamp from file: "+lastTimestampFromFile[0]+", Timestamp in sentence: "+timePoint+
|
||||
", Talker ID: "+sentence.getTalkerId()+", Sentence ID: "+sentence.getSentenceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readingStopped() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readingStarted() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readingPaused() {
|
||||
}
|
||||
});
|
||||
nmeaReader.start();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
while ((line=br.readLine()) != null) {
|
||||
if (line.startsWith("$") || line.startsWith("!")) {
|
||||
// NMEA0183 sentence
|
||||
pos.write(line.getBytes());
|
||||
pos.write(new byte[] { 13, 10 });
|
||||
pos.flush();
|
||||
if (lastTimestampFromFile[0] != null) {
|
||||
lastTimestampFromFile[0] = lastTimestampFromFile[0].plus(1000);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
// Maybe it's an interwoven timestamp. Try out...
|
||||
Date date = df.parse(line);
|
||||
lastTimestampFromFile[0] = new MillisecondsTimePoint(date);
|
||||
} catch (ParseException pe) {
|
||||
pe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
pos.close();
|
||||
br.close();
|
||||
Thread.sleep(10000);
|
||||
nmeaReader.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry exported="true" kind="lib" path="lib/marineapi.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="lib/marineapi.jar">
|
||||
<attributes>
|
||||
<attribute name="javadoc_location" value="file:/c:/data/SAP/sailing/workspace/java/com.sap.sailing.nmeaconnector/marineapi-0.5/doc/javadoc"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
|
||||
@@ -7,3 +7,10 @@ Bundle-Vendor: SAP
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
|
||||
Bundle-ClassPath: .,
|
||||
lib/marineapi.jar
|
||||
Export-Package: net.sf.marineapi.nmea.event,
|
||||
net.sf.marineapi.nmea.io,
|
||||
net.sf.marineapi.nmea.parser,
|
||||
net.sf.marineapi.nmea.sentence,
|
||||
net.sf.marineapi.nmea.util,
|
||||
net.sf.marineapi.provider,
|
||||
net.sf.marineapi.provider.event
|
||||
|
||||
Reference in New Issue
Block a user