e4cedde22b6d6b9c0deedc6c2a506a94f32d0516
[fw/altos] / ao-tools / altosui / AltosSerial.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 /*
19  * Deal with TeleDongle on a serial port
20  */
21
22 package altosui;
23
24 import java.lang.*;
25 import java.io.*;
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29 import gnu.io.*;
30 import altosui.AltosSerialMonitor;
31
32 /*
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
35  * threads.
36  */
37 class AltosSerialReader implements Runnable {
38         InputStream     serial_in;
39         LinkedBlockingQueue<String> monitor_queue;
40         LinkedBlockingQueue<String> reply_queue;
41         Thread input_thread;
42         String line;
43
44         public void run () {
45                 int c;
46
47                 try {
48                         for (;;) {
49                                 c = serial_in.read();
50                                 if (Thread.interrupted())
51                                         break;
52                                 if (c == -1)
53                                         continue;
54                                 if (c == '\r')
55                                         continue;
56                                 synchronized(this) {
57                                         if (c == '\n') {
58                                                 if (line != "") {
59                                                         if (line.startsWith("VERSION"))
60                                                                 monitor_queue.put(line);
61                                                         else
62                                                                 reply_queue.put(line);
63                                                         line = "";
64                                                 }
65                                         } else {
66                                                 line = line + (char) c;
67                                         }
68                                 }
69                         }
70                 } catch (IOException e) {
71                 } catch (InterruptedException e) {
72                 }
73         }
74
75         public String get_telem() throws InterruptedException {
76                 String s = monitor_queue.take();
77                 System.out.println(s);
78                 return s;
79         }
80
81         public String get_reply() throws InterruptedException {
82                 return reply_queue.take();
83         }
84
85         public void flush () {
86                 synchronized(this) {
87                         if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
88                                 line = "";
89                         reply_queue.clear();
90                 }
91         }
92
93         public boolean opened() {
94                 return serial_in != null;
95         }
96
97         public void close() {
98                 if (serial_in != null) {
99                         try {
100                                 serial_in.close();
101                         } catch (IOException e) {
102                         }
103                         serial_in = null;
104                 }
105                 if (input_thread != null) {
106                         try {
107                                 input_thread.interrupt();
108                                 input_thread.join();
109                         } catch (InterruptedException e) {
110                         }
111                         input_thread = null;
112                 }
113         }
114
115         public void open(File name) throws FileNotFoundException {
116                 close();
117                 serial_in = new FileInputStream(name);
118                 input_thread = new Thread(this);
119                 input_thread.start();
120         }
121         public void open(CommPort c) throws IOException {
122                 close();
123                 try {
124                 c.enableReceiveTimeout(1000);   /* icky. the read method cannot be interrupted */
125                 } catch (UnsupportedCommOperationException ee) {
126                 }
127                 serial_in = c.getInputStream();
128                 input_thread = new Thread(this);
129                 input_thread.start();
130         }
131         public AltosSerialReader () {
132                 serial_in = null;
133                 input_thread = null;
134                 line = "";
135                 monitor_queue = new LinkedBlockingQueue<String> ();
136                 reply_queue = new LinkedBlockingQueue<String> ();
137         }
138
139 }
140
141 public class AltosSerial {
142         OutputStream serial_out = null;
143         AltosSerialReader reader = null;
144
145         public String get_telem() throws InterruptedException {
146                 return reader.get_telem();
147         }
148
149         CommPort comm_port = null;
150
151         public void close() {
152                 try {
153                         serial_out.close();
154                 } catch (IOException ee) {
155                 }
156                 reader.close();
157                 if (comm_port != null) {
158                         comm_port.close();
159                 }
160         }
161
162         public void open(File serial_name) throws FileNotFoundException {
163                 reader.open(serial_name);
164                 serial_out = new FileOutputStream(serial_name);
165         }
166
167         public void open(CommPort c) throws IOException {
168                 reader.open(c);
169                 serial_out = c.getOutputStream();
170         }
171
172         public void connect(String port_name) throws IOException, NoSuchPortException, PortInUseException {
173                 comm_port = new RXTXPort(port_name);
174                 open(comm_port);
175         }
176
177         void init() {
178                 reader = new AltosSerialReader();
179         }
180
181         public AltosSerial() {
182                 init();
183         }
184
185         public AltosSerial(File serial_name) throws FileNotFoundException {
186                 init();
187                 open(serial_name);
188         }
189
190         public AltosSerial(CommPort comm_port) throws IOException {
191                 init();
192                 open(comm_port);
193         }
194 }