001: /**
002: * The NexStar class encapsulates a NexStar telescope.
003: * Copyright (C) 1999-2001 Mark Hale
004: * @author Mark Hale
005: */package JSci.astro.telescope;
006:
007: import java.io.*;
008: import javax.comm.*;
009:
010: public final class NexStar extends Object {
011: private SerialPort serial;
012: private InputStreamReader in;
013: private OutputStreamWriter out;
014:
015: /**
016: * Convert RA from a string to a number.
017: */
018: public static int raToInt(String ra) {
019: final int hrs = Integer.valueOf(ra.substring(0, 2)).intValue();
020: final int mins = Integer.valueOf(ra.substring(3, 5)).intValue();
021: final int secs = Integer.valueOf(ra.substring(6, 8)).intValue();
022: return hrs * 600 + mins * 10 + secs;
023: }
024:
025: /**
026: * Convert dec from a string to a number.
027: */
028: public static int decToInt(String dec) {
029: final int degs = Integer.valueOf(dec.substring(0, 3))
030: .intValue();
031: final int mins = Integer.valueOf(dec.substring(4, 6))
032: .intValue();
033: final int secs = Integer.valueOf(dec.substring(7, 9))
034: .intValue();
035: if (degs >= 0)
036: return degs * 600 + mins * 10 + secs;
037: else
038: return degs * 600 - mins * 10 - secs;
039: }
040:
041: /**
042: * Convert alt from a string to a number.
043: */
044: public static int altToInt(String alt) {
045: final int degs = Integer.valueOf(alt.substring(0, 3))
046: .intValue();
047: final int mins = Integer.valueOf(alt.substring(4, 6))
048: .intValue();
049: final int secs = Integer.valueOf(alt.substring(7, 9))
050: .intValue();
051: if (degs >= 0)
052: return degs * 600 + mins * 10 + secs;
053: else
054: return degs * 600 - mins * 10 - secs;
055: }
056:
057: /**
058: * Convert az from a string to a number.
059: */
060: public static int azToInt(String az) {
061: final int degs = Integer.valueOf(az.substring(0, 3)).intValue();
062: final int mins = Integer.valueOf(az.substring(4, 6)).intValue();
063: final int secs = Integer.valueOf(az.substring(7, 9)).intValue();
064: return degs * 600 + mins * 10 + secs;
065: }
066:
067: /**
068: * Constructs a NexStar object.
069: */
070: public NexStar(String port) {
071: try {
072: CommPortIdentifier portID = CommPortIdentifier
073: .getPortIdentifier(port);
074: serial = (SerialPort) portID.open("NexStar", 10);
075: serial.setSerialPortParams(9600, SerialPort.DATABITS_8,
076: SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
077: in = new InputStreamReader(serial.getInputStream());
078: out = new OutputStreamWriter(serial.getOutputStream());
079: } catch (NoSuchPortException e) {
080: System.err
081: .println("Port does not exist: " + e.getMessage());
082: e.printStackTrace();
083: } catch (PortInUseException e) {
084: System.err.println("Port is in use by another process: "
085: + e.getMessage());
086: e.printStackTrace();
087: } catch (UnsupportedCommOperationException e) {
088: } catch (IOException e) {
089: }
090: }
091:
092: /**
093: * Returns the current RA and dec.
094: */
095: public synchronized String getRADec() throws IOException {
096: sendCmd("E");
097: return readString();
098: }
099:
100: /**
101: * Returns the current az and alt.
102: */
103: public synchronized String getAzAlt() throws IOException {
104: sendCmd("Z");
105: return readString();
106: }
107:
108: /**
109: * Goto the coordinates.
110: */
111: public synchronized void gotoRADec(String ra, String dec)
112: throws IOException {
113: final int raBytes = raToInt(ra);
114: final int decBytes = decToInt(dec);
115: out.write('R');
116: out.write((byte) (raBytes >> 8));
117: out.write((byte) (raBytes));
118: out.write(',');
119: out.write((byte) (decBytes >> 8));
120: out.write((byte) (decBytes));
121: out.flush();
122: }
123:
124: /**
125: * Goto the coordinates.
126: */
127: public synchronized void gotoAzAlt(String az, String alt)
128: throws IOException {
129: final int azBytes = azToInt(az);
130: final int altBytes = altToInt(alt);
131: out.write('A');
132: out.write((byte) (azBytes >> 8));
133: out.write((byte) (azBytes));
134: out.write(',');
135: out.write((byte) (altBytes >> 8));
136: out.write((byte) (altBytes));
137: out.flush();
138: }
139:
140: /**
141: * Check whether the scope is moving.
142: */
143: public synchronized boolean isMoving() throws IOException {
144: sendCmd("L");
145: return readString().equals("1");
146: }
147:
148: /**
149: * Sends a command to the scope.
150: */
151: private void sendCmd(String cmd) throws IOException {
152: out.write(cmd);
153: out.flush();
154: }
155:
156: /**
157: * Reads a string from the scope, dropping the terminating #.
158: */
159: private String readString() throws IOException {
160: StringBuffer msg = new StringBuffer();
161: int ch = in.read();
162: while (ch != '#') {
163: msg.append(ch);
164: ch = in.read();
165: }
166: return msg.toString();
167: }
168:
169: /**
170: * Closes the connection to the scope.
171: */
172: public synchronized void close() throws IOException {
173: in.close();
174: out.close();
175: serial.close();
176: }
177: }
|