1 6 7 package JSci.astro.telescope; 8 9 import java.io.*; 10 import javax.comm.*; 11 12 public final class NexStar extends Object { 13 private SerialPort serial; 14 private InputStreamReader in; 15 private OutputStreamWriter out; 16 19 public static int raToInt(String 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 28 public static int decToInt(String 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 40 public static int altToInt(String 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 52 public static int azToInt(String 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 61 public NexStar(String 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 81 public synchronized String getRADec() throws IOException { 82 sendCmd("E"); 83 return readString(); 84 } 85 88 public synchronized String getAzAlt() throws IOException { 89 sendCmd("Z"); 90 return readString(); 91 } 92 95 public synchronized void gotoRADec(String ra,String 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 109 public synchronized void gotoAzAlt(String az,String 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 123 public synchronized boolean isMoving() throws IOException { 124 sendCmd("L"); 125 return readString().equals("1"); 126 } 127 130 private void sendCmd(String cmd) throws IOException { 131 out.write(cmd); 132 out.flush(); 133 } 134 137 private String readString() throws IOException { 138 StringBuffer msg=new StringBuffer (); 139 int ch=in.read(); 140 while(ch!='#') { 141 msg.append(ch); 142 ch=in.read(); 143 } 144 return msg.toString(); 145 } 146 149 public synchronized void close() throws IOException { 150 in.close(); 151 out.close(); 152 serial.close(); 153 } 154 } 155 156 | Popular Tags |