3 import gnu.io.CommPortIdentifier;
4 import gnu.io.PortInUseException;
5 import gnu.io.SerialPort;
6 import gnu.io.UnsupportedCommOperationException;
8 import java.io.FileInputStream;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.util.ArrayList;
14 import java.util.Enumeration;
17 * Class to interface the PerfectFlite Alt15K/WD altimeter.
19 * Also includes a main method that retrieves all flight profiles and saves them to files.
21 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24 public class RotationLogger {
25 private static final boolean DEBUG = false;
27 private static final int BYTES = 65536;
30 private final CommPortIdentifier portID;
31 private SerialPort port = null;
32 private InputStream is = null;
33 private OutputStream os = null;
37 @SuppressWarnings("unchecked")
38 public static String[] getNames() {
39 ArrayList<String> list = new ArrayList<String>();;
41 Enumeration pids = CommPortIdentifier.getPortIdentifiers();
43 while (pids.hasMoreElements()) {
44 CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement();
46 if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL)
47 list.add(pid.getName());
49 return list.toArray(new String[0]);
56 @SuppressWarnings("unchecked")
57 public RotationLogger(String name) throws IOException {
58 CommPortIdentifier portID = null;
60 Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
61 while (portIdentifiers.hasMoreElements()) {
62 CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
64 if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
65 pid.getName().equals(name)) {
72 throw new IOException("Port '"+name+"' not found.");
82 public void readData() throws IOException, PortInUseException {
85 int[] data = new int[BYTES];
87 FileOutputStream rawdump = null;
93 System.err.println("Sending dump mode command...");
95 for (int i=0; i<16; i++) {
99 } catch (InterruptedException ignore) { }
102 System.err.println("Waiting for response...");
108 System.err.printf("Received spurious c=%d\n",c);
112 System.err.println("Received response.");
116 System.err.println("Opening 'rawdump'...");
117 rawdump = new FileOutputStream("rawdump");
121 System.err.println("Performing dump...");
125 byte[] buffer = new byte[1024];
127 for (int count=0; count < BYTES; ) {
128 if ((BYTES-count) < buffer.length) {
129 buffer = new byte[BYTES-count];
132 int n = is.read(buffer);
134 System.err.println("Error condition, n="+n);
138 rawdump.write(buffer, 0, n);
140 for (int i=0; i<n; i++) {
141 data[count+i] = unsign(buffer[i]);
144 if (count - printCount > 1024) {
145 System.err.println("Read "+count+" bytes...");
151 System.err.println("Verifying checksum...");
152 int reported = is.read();
155 for (int i=0; i < data.length; i++) {
158 if (computed == reported) {
159 System.err.println("Checksum ok ("+computed+")");
161 System.err.println("Error in checksum, computed="+computed+
162 " reported="+reported);
165 System.err.println("Communication done.");
167 } catch (UnsupportedCommOperationException e) {
168 // TODO Auto-generated catch block
182 //////////// Data interpretation //////////////
185 private static void convertData(int[] data) {
187 System.err.println("Converting data...");
189 int lastBuffer = data[0xffff];
190 if (lastBuffer < 0 || lastBuffer > 3) {
191 System.err.println("Illegal last accessed buffer: "+lastBuffer);
194 System.err.println("Last used buffer: "+lastBuffer);
196 for (int i=4; i>0; i--) {
197 int n = (lastBuffer + i) % 4;
200 convertBuffer(data, n * (BYTES/4), bufNumber);
206 private static void convertBuffer(int[] data, int position, int bufNumber) {
209 startPosition = data[position + 0xfd] << 8 + data[position+0xfe];
211 // 50 samples per 128 bytes
212 int startTime = (startPosition -position) * 50 / 128;
214 System.err.println(" Buffer "+ bufNumber + " (at position "+position+")...");
215 System.err.println(" Start position "+startPosition+" time "+startTime);
217 System.out.println("# Buffer "+bufNumber);
218 System.out.println("# Start position t="+startTime);
222 for (int page = 0; page < 128; page++) {
223 int pageStart = position + page * 128;
225 if (pageStart == startPosition) {
226 System.out.println("# ---clip---");
229 for (int i=0; i<125; i += 5) {
230 int sample1, sample2;
232 int start = pageStart + i;
233 // System.err.println("page="+page+" i="+i+
234 // " position="+position+" pageStart="+pageStart+" start="+start);
236 sample1 = (data[start] << 2) + (data[start+1] >> 6);
237 sample2 = ((data[start+1] & 0x3f) << 4) + (data[start+2] >> 4);
238 System.out.printf("%d %4d %4d %4d\n", bufNumber, t, sample1, sample2);
241 sample1 = ((data[start+2] & 0x0f) << 6) + (data[start+3] >> 2);
242 sample2 = ((data[start+3] & 3) << 8) + data[start+4];
243 System.out.printf("%d %4d %4d %4d\n", bufNumber, t, sample1, sample2);
251 private void open() throws PortInUseException, IOException,
252 UnsupportedCommOperationException {
255 System.err.println("ERROR: open() called with port="+port);
261 System.err.println(" Opening port...");
264 port = (SerialPort)portID.open("OpenRocket",1000);
266 port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
267 SerialPort.PARITY_NONE);
269 port.setInputBufferSize(1);
270 port.setOutputBufferSize(1);
272 is = port.getInputStream();
273 os = port.getOutputStream();
277 private void close() {
279 System.err.println(" Closing port");
290 private static int unsign(byte b) {
300 public static void main(String[] arg) throws Exception {
302 if (arg.length > 2) {
303 System.err.println("Illegal arguments.");
306 if (arg.length == 1) {
307 FileInputStream is = new FileInputStream(arg[0]);
308 byte[] buffer = new byte[BYTES];
309 int n = is.read(buffer);
311 System.err.println("Could read only "+n+" bytes");
315 int[] data = new int[BYTES];
316 for (int i=0; i<BYTES; i++) {
317 data[i] = unsign(buffer[i]);
321 for (int i=0; i<BYTES; i++) {
324 checksum = checksum%256;
325 System.err.println("Checksum: "+checksum);
332 String device = null;
333 String[] devices = RotationLogger.getNames();
334 for (int i=0; i<devices.length; i++) {
335 if (devices[i].matches(".*USB.*")) {
340 if (device == null) {
341 System.err.println("Device not found.");
346 System.err.println("Selected device "+device);
349 RotationLogger p = new RotationLogger(device);