KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > astro > telescope > NexStar


1 /**
2 * The NexStar class encapsulates a NexStar telescope.
3 * Copyright (C) 1999-2001 Mark Hale
4 * @author Mark Hale
5 */

6
7 package JSci.astro.telescope;
8
9 import java.io.*;
10 import javax.comm.*;
11
12 public final class NexStar extends Object JavaDoc {
13         private SerialPort serial;
14         private InputStreamReader in;
15         private OutputStreamWriter out;
16         /**
17         * Convert RA from a string to a number.
18         */

19         public static int raToInt(String JavaDoc ra) {
20                 final int hrs=Integer.valueOf(ra.substring(0,2)).intValue();
21                 final int mins=Integer.valueOf(ra.substring(3,5)).intValue();
22                 final int secs=Integer.valueOf(ra.substring(6,8)).intValue();
23                 return hrs*600+mins*10+secs;
24         }
25         /**
26         * Convert dec from a string to a number.
27         */

28         public static int decToInt(String JavaDoc dec) {
29                 final int degs=Integer.valueOf(dec.substring(0,3)).intValue();
30                 final int mins=Integer.valueOf(dec.substring(4,6)).intValue();
31                 final int secs=Integer.valueOf(dec.substring(7,9)).intValue();
32                 if(degs>=0)
33                         return degs*600+mins*10+secs;
34                 else
35                         return degs*600-mins*10-secs;
36         }
37         /**
38         * Convert alt from a string to a number.
39         */

40         public static int altToInt(String JavaDoc alt) {
41                 final int degs=Integer.valueOf(alt.substring(0,3)).intValue();
42                 final int mins=Integer.valueOf(alt.substring(4,6)).intValue();
43                 final int secs=Integer.valueOf(alt.substring(7,9)).intValue();
44                 if(degs>=0)
45                         return degs*600+mins*10+secs;
46                 else
47                         return degs*600-mins*10-secs;
48         }
49         /**
50         * Convert az from a string to a number.
51         */

52         public static int azToInt(String JavaDoc az) {
53                 final int degs=Integer.valueOf(az.substring(0,3)).intValue();
54                 final int mins=Integer.valueOf(az.substring(4,6)).intValue();
55                 final int secs=Integer.valueOf(az.substring(7,9)).intValue();
56                 return degs*600+mins*10+secs;
57         }
58         /**
59         * Constructs a NexStar object.
60         */

61         public NexStar(String JavaDoc port) {
62                 try {
63                         CommPortIdentifier portID=CommPortIdentifier.getPortIdentifier(port);
64                         serial=(SerialPort)portID.open("NexStar",10);
65                         serial.setSerialPortParams(9600,SerialPort.DATABITS_8,
66                                 SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
67                         in=new InputStreamReader(serial.getInputStream());
68                         out=new OutputStreamWriter(serial.getOutputStream());
69                 } catch(NoSuchPortException e) {
70                         System.err.println("Port does not exist: "+e.getMessage());
71                         e.printStackTrace();
72                 } catch(PortInUseException e) {
73                         System.err.println("Port is in use by another process: "+e.getMessage());
74                         e.printStackTrace();
75                 } catch(UnsupportedCommOperationException e) {
76                 } catch(IOException e) {}
77         }
78         /**
79         * Returns the current RA and dec.
80         */

81         public synchronized String JavaDoc getRADec() throws IOException {
82                 sendCmd("E");
83                 return readString();
84         }
85         /**
86         * Returns the current az and alt.
87         */

88         public synchronized String JavaDoc getAzAlt() throws IOException {
89                 sendCmd("Z");
90                 return readString();
91         }
92         /**
93         * Goto the coordinates.
94         */

95         public synchronized void gotoRADec(String JavaDoc ra,String JavaDoc dec) throws IOException {
96                 final int raBytes=raToInt(ra);
97                 final int decBytes=decToInt(dec);
98                 out.write('R');
99                 out.write((byte)(raBytes>>8));
100                 out.write((byte)(raBytes));
101                 out.write(',');
102                 out.write((byte)(decBytes>>8));
103                 out.write((byte)(decBytes));
104                 out.flush();
105         }
106         /**
107         * Goto the coordinates.
108         */

109         public synchronized void gotoAzAlt(String JavaDoc az,String JavaDoc alt) throws IOException {
110                 final int azBytes=azToInt(az);
111                 final int altBytes=altToInt(alt);
112                 out.write('A');
113                 out.write((byte)(azBytes>>8));
114                 out.write((byte)(azBytes));
115                 out.write(',');
116                 out.write((byte)(altBytes>>8));
117                 out.write((byte)(altBytes));
118                 out.flush();
119         }
120         /**
121         * Check whether the scope is moving.
122         */

123         public synchronized boolean isMoving() throws IOException {
124                 sendCmd("L");
125                 return readString().equals("1");
126         }
127         /**
128         * Sends a command to the scope.
129         */

130         private void sendCmd(String JavaDoc cmd) throws IOException {
131                 out.write(cmd);
132                 out.flush();
133         }
134         /**
135         * Reads a string from the scope, dropping the terminating #.
136         */

137         private String JavaDoc readString() throws IOException {
138                 StringBuffer JavaDoc msg=new StringBuffer JavaDoc();
139                 int ch=in.read();
140                 while(ch!='#') {
141                         msg.append(ch);
142                         ch=in.read();
143                 }
144                 return msg.toString();
145         }
146         /**
147         * Closes the connection to the scope.
148         */

149         public synchronized void close() throws IOException {
150                 in.close();
151                 out.close();
152                 serial.close();
153         }
154 }
155
156
Popular Tags