2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 * Deal with TeleDongle on a serial port
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.LinkedList;
28 import java.util.Iterator;
30 import altosui.AltosSerialMonitor;
33 * This class reads from the serial port and places each received
34 * line in a queue. Dealing with that queue is left up to other
37 class AltosSerialReader implements Runnable {
38 InputStream serial_in;
39 LinkedList<LinkedBlockingQueue<String>> monitors;
40 LinkedBlockingQueue<String> reply_queue;
50 if (Thread.interrupted())
59 if (line.startsWith("VERSION")) {
60 for (int e = 0; e < monitors.size(); e++) {
61 LinkedBlockingQueue<String> q = monitors.get(e);
65 reply_queue.put(line);
69 line = line + (char) c;
73 } catch (IOException e) {
74 } catch (InterruptedException e) {
78 public String get_reply() throws InterruptedException {
79 return reply_queue.take();
82 public void add_monitor(LinkedBlockingQueue<String> q) {
86 public void remove_monitor(LinkedBlockingQueue<String> q) {
90 public void flush () {
92 if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
98 public boolean opened() {
99 return serial_in != null;
102 public void close() {
103 if (serial_in != null) {
106 } catch (IOException e) {
110 if (input_thread != null) {
112 input_thread.interrupt();
114 } catch (InterruptedException e) {
120 public void open(File name) throws FileNotFoundException {
122 serial_in = new FileInputStream(name);
123 input_thread = new Thread(this);
124 input_thread.start();
126 public void open(CommPort c) throws IOException {
129 c.enableReceiveTimeout(1000); /* icky. the read method cannot be interrupted */
130 } catch (UnsupportedCommOperationException ee) {
132 serial_in = c.getInputStream();
133 input_thread = new Thread(this);
134 input_thread.start();
136 public AltosSerialReader () {
140 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
141 reply_queue = new LinkedBlockingQueue<String> ();
146 public class AltosSerial {
147 OutputStream serial_out = null;
148 AltosSerialReader reader = null;
150 CommPort comm_port = null;
152 public void close() {
155 } catch (IOException ee) {
158 if (comm_port != null) {
163 public void open(File serial_name) throws FileNotFoundException {
164 reader.open(serial_name);
165 serial_out = new FileOutputStream(serial_name);
168 public void open(CommPort c) throws IOException {
170 serial_out = c.getOutputStream();
173 public void connect(String port_name) throws IOException, NoSuchPortException, PortInUseException {
174 comm_port = new RXTXPort(port_name);
179 reader = new AltosSerialReader();
182 public void add_monitor(LinkedBlockingQueue<String> q) {
183 reader.add_monitor(q);
186 public void remove_monitor(LinkedBlockingQueue<String> q) {
187 reader.remove_monitor(q);
190 public AltosSerial() {
194 public AltosSerial(File serial_name) throws FileNotFoundException {
199 public AltosSerial(CommPort comm_port) throws IOException {