-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathSerialPortExample.java
174 lines (151 loc) · 4.57 KB
/
SerialPortExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* SerialPortExample.java
* Copyright (C) 2011 Kimmo Tuukkanen
*
* This file is part of Java Marine API.
* <http://ktuukkan.github.io/marine-api/>
*
* 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;
/**
* Serial port example using GNU/RXTX libraries (see readme.txt). Scans through
* all COM ports and seeks for NMEA 0183 data with default settings (4800
* baud, 8 data bits, 1 stop bit and no parity). If NMEA data is found, starts
* printing out all sentences the device is broadcasting.
*
* Notice that on Linux you may need to set read/write privileges on correct
* port (e.g. {@code sudo chmod 666 /dev/ttyUSB0}) or add your user in
* dialout group before running this example.
*
* @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);
sp.enableReceiveTimeout(1000);
sp.enableReceiveThreshold(0);
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();
}
}