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 LinkedBlockingQueue<String> monitor_queue;
40 LinkedBlockingQueue<String> reply_queue;
50 if (Thread.interrupted())
59 if (line.startsWith("VERSION"))
60 monitor_queue.put(line);
62 reply_queue.put(line);
66 line = line + (char) c;
70 } catch (IOException e) {
71 } catch (InterruptedException e) {
75 public String get_telem() throws InterruptedException {
76 String s = monitor_queue.take();
77 System.out.println(s);
81 public String get_reply() throws InterruptedException {
82 return reply_queue.take();
85 public void flush () {
87 if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
93 public boolean opened() {
94 return serial_in != null;
98 if (serial_in != null) {
101 } catch (IOException e) {
105 if (input_thread != null) {
107 input_thread.interrupt();
109 } catch (InterruptedException e) {
115 public void open(File name) throws FileNotFoundException {
117 serial_in = new FileInputStream(name);
118 input_thread = new Thread(this);
119 input_thread.start();
121 public void open(CommPort c) throws IOException {
124 c.enableReceiveTimeout(1000); /* icky. the read method cannot be interrupted */
125 } catch (UnsupportedCommOperationException ee) {
127 serial_in = c.getInputStream();
128 input_thread = new Thread(this);
129 input_thread.start();
131 public AltosSerialReader () {
135 monitor_queue = new LinkedBlockingQueue<String> ();
136 reply_queue = new LinkedBlockingQueue<String> ();
141 public class AltosSerial {
142 OutputStream serial_out = null;
143 AltosSerialReader reader = null;
145 public String get_telem() throws InterruptedException {
146 return reader.get_telem();
149 CommPort comm_port = null;
151 public void close() {
154 } catch (IOException ee) {
157 if (comm_port != null) {
162 public void open(File serial_name) throws FileNotFoundException {
163 reader.open(serial_name);
164 serial_out = new FileOutputStream(serial_name);
167 public void open(CommPort c) throws IOException {
169 serial_out = c.getOutputStream();
172 public void connect(String port_name) throws IOException, NoSuchPortException, PortInUseException {
173 comm_port = new RXTXPort(port_name);
178 reader = new AltosSerialReader();
181 public AltosSerial() {
185 public AltosSerial(File serial_name) throws FileNotFoundException {
190 public AltosSerial(CommPort comm_port) throws IOException {