3 import gnu.io.CommPortIdentifier;
4 import gnu.io.PortInUseException;
5 import gnu.io.SerialPort;
6 import gnu.io.UnsupportedCommOperationException;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.util.ArrayList;
11 import java.util.Enumeration;
14 * Class to interface the PerfectFlite Alt15K/WD altimeter.
16 * Also includes a main method that retrieves all flight profiles and saves them to files.
18 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21 public class SerialDownload {
22 private static final boolean DEBUG = false;
24 private static final int MAGIC = 666;
26 private final CommPortIdentifier portID;
27 private SerialPort port = null;
28 private InputStream is = null;
32 @SuppressWarnings("unchecked")
33 public static String[] getNames() {
34 ArrayList<String> list = new ArrayList<String>();;
36 Enumeration pids = CommPortIdentifier.getPortIdentifiers();
38 while (pids.hasMoreElements()) {
39 CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement();
41 if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL)
42 list.add(pid.getName());
44 return list.toArray(new String[0]);
51 @SuppressWarnings("unchecked")
52 public SerialDownload(String name) throws IOException {
53 CommPortIdentifier portID = null;
55 Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
56 while (portIdentifiers.hasMoreElements()) {
57 CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
59 if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
60 pid.getName().equals(name)) {
67 throw new IOException("Port '"+name+"' not found.");
77 public void readData() throws IOException, PortInUseException {
87 System.err.println("Ready to read...");
90 t = System.nanoTime();
94 System.out.printf("%10.6f %d\n", ((double)t-t0)/1000000000.0, c);
96 if (previous == MAGIC) {
99 System.out.printf("# Altitude: %5d\n", previous*256 + c);
108 } catch (UnsupportedCommOperationException e) {
109 // TODO Auto-generated catch block
118 private void open() throws PortInUseException, IOException,
119 UnsupportedCommOperationException {
122 System.err.println("ERROR: open() called with port="+port);
128 System.err.println(" Opening port...");
131 port = (SerialPort)portID.open("OpenRocket",1000);
133 port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
134 SerialPort.PARITY_NONE);
136 port.setInputBufferSize(1);
137 port.setOutputBufferSize(1);
139 is = port.getInputStream();
143 private void close() {
145 System.err.println(" Closing port");
158 public static void main(String[] arg) throws Exception {
160 String device = null;
161 String[] devices = SerialDownload.getNames();
162 for (int i=0; i<devices.length; i++) {
163 if (devices[i].matches(".*USB.*")) {
168 if (device == null) {
169 System.err.println("Device not found.");
174 System.err.println("Selected device "+device);
177 SerialDownload p = new SerialDownload(device);