create changelog entry
[debian/openrocket] / core / src-extra / altimeter / SerialDownload.java
1 package altimeter;
2
3 import gnu.io.CommPortIdentifier;
4 import gnu.io.PortInUseException;
5 import gnu.io.SerialPort;
6 import gnu.io.UnsupportedCommOperationException;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.util.ArrayList;
11 import java.util.Enumeration;
12
13 /**
14  * Class to interface the PerfectFlite Alt15K/WD altimeter.
15  * 
16  * Also includes a main method that retrieves all flight profiles and saves them to files.
17  * 
18  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19  */
20
21 public class SerialDownload {
22         private static final boolean DEBUG = false;
23         
24         private static final int MAGIC = 666;
25         
26         private final CommPortIdentifier portID;
27         private SerialPort port = null;
28         private InputStream is = null;
29         
30         
31
32         @SuppressWarnings("unchecked")
33         public static String[] getNames() {
34                 ArrayList<String> list = new ArrayList<String>();;
35                 
36                 Enumeration pids = CommPortIdentifier.getPortIdentifiers();
37
38                 while (pids.hasMoreElements()) {
39                     CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement();
40
41                     if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL)
42                         list.add(pid.getName());
43                 }
44                 return list.toArray(new String[0]);
45         }
46
47         
48         
49         
50
51         @SuppressWarnings("unchecked")
52         public SerialDownload(String name) throws IOException {
53                 CommPortIdentifier portID = null;
54                 
55                 Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
56                 while (portIdentifiers.hasMoreElements()) {
57                     CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
58                     
59                     if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
60                        pid.getName().equals(name)) {
61                         portID = pid;
62                         break;
63                     }
64                 }
65                 
66                 if (portID==null) {
67                         throw new IOException("Port '"+name+"' not found.");
68                 }
69                 this.portID = portID;
70         }
71         
72         
73         
74         
75         
76         
77         public void readData() throws IOException, PortInUseException {
78                 long t0 = -1;
79                 long t;
80                 
81                 int previous = MAGIC;
82                 
83                 
84                 try {
85                         open();
86                         
87                         System.err.println("Ready to read...");
88                         while (true) {
89                                 int c = is.read();
90                                 t = System.nanoTime();
91                                 if (t0 < 0)
92                                         t0 = t;
93                                 
94                                 System.out.printf("%10.6f %d\n", ((double)t-t0)/1000000000.0, c);
95                                 
96                                 if (previous == MAGIC) {
97                                         previous = c;
98                                 } else {
99                                         System.out.printf("# Altitude: %5d\n", previous*256 + c);
100                                         previous = MAGIC;
101                                 }
102                                 
103                                 if (c < 0)
104                                         break;
105                         }
106                         
107                         
108                 } catch (UnsupportedCommOperationException e) {
109                         // TODO Auto-generated catch block
110                         e.printStackTrace();
111                 } finally {
112                         close();
113                 }
114         }
115
116         
117         
118         private void open() throws PortInUseException, IOException, 
119                         UnsupportedCommOperationException {
120                 
121                 if (port != null) {
122                         System.err.println("ERROR: open() called with port="+port);
123                         Thread.dumpStack();
124                         close();
125                 }
126                 
127                 if (DEBUG) {
128                         System.err.println("  Opening port...");
129                 }
130
131                 port = (SerialPort)portID.open("OpenRocket",1000);
132                 
133                 port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, 
134                                 SerialPort.PARITY_NONE);
135
136                 port.setInputBufferSize(1);
137                 port.setOutputBufferSize(1);
138
139                 is = port.getInputStream();
140         }
141         
142         
143         private void close() {
144                 if (DEBUG)
145                         System.err.println("  Closing port");
146                 
147                 SerialPort p = port;
148                 port = null;
149                 is = null;
150                 if (p != null)
151                         p.close();
152         }
153         
154         
155         
156
157         
158         public static void main(String[] arg) throws Exception {
159                 
160                 String device = null;
161                 String[] devices = SerialDownload.getNames();
162                 for (int i=0; i<devices.length; i++) {
163                         if (devices[i].matches(".*USB.*")) {
164                                 device = devices[i];
165                                 break;
166                         }
167                 }
168                 if (device == null) {
169                         System.err.println("Device not found.");
170                         return;
171                 }
172                 
173                 
174                 System.err.println("Selected device "+device);
175                 
176                 
177                 SerialDownload p = new SerialDownload(device);
178                 
179                 p.readData();
180                 
181         }
182         
183         
184 }