X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=nxtOSEK%2Flejos_nxj%2Fsrc%2Fjava%2Fpctools%2Flejos%2Fpc%2Ftools%2FConnector.java;fp=nxtOSEK%2Flejos_nxj%2Fsrc%2Fjava%2Fpctools%2Flejos%2Fpc%2Ftools%2FConnector.java;h=ce2b715192ac49fd0aedaa9704561efe3756a4fa;hb=393799f55f039bfc438ca78f3fb4da4926e7105b;hp=0000000000000000000000000000000000000000;hpb=8f6f0ffd2e250519dddcc87d1782c37bfc432db3;p=nxt-jsp%2Flejos_nxj.git diff --git a/nxtOSEK/lejos_nxj/src/java/pctools/lejos/pc/tools/Connector.java b/nxtOSEK/lejos_nxj/src/java/pctools/lejos/pc/tools/Connector.java new file mode 100644 index 0000000..ce2b715 --- /dev/null +++ b/nxtOSEK/lejos_nxj/src/java/pctools/lejos/pc/tools/Connector.java @@ -0,0 +1,114 @@ +import java.io.*; + +import lejos.pc.comm.*; + + /** + * conneccts to a NXT using either Bluetooth or USB and builds input and output data streams. + *@author Roger Glassey 22/08/2007 + */ + +public class Connector +{ + private boolean _usb = false; + DataInputStream dataIn; + DataOutputStream dataOut; + InputStream is; + OutputStream os; + NXTComm nxtComm; + /** + * + * @param NXT can be the friendly name of the NXT or a 16 character address + * @param useUSB + * @return true if connection was made + */ + public boolean startConnector(String NXT, boolean useUSB) + { + NXTInfo[] nxtInfo ; + _usb = useUSB; + if(_usb) + { + nxtComm = new NXTCommLibnxt(); + System.out.println("searching"); + nxtInfo = nxtComm.search(null, NXTCommFactory.USB); + if (nxtInfo.length == 0) + { + System.out.println("No NXT Found"); + return false; + } + nxtComm.open(nxtInfo[0]); + System.out.println(" Opened "+nxtInfo[0].name); + } + else + { + nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH); + if(NXT == null || NXT == " ") + { + System.out.println("search for all"); + nxtInfo = nxtComm.search(NXT, NXTCommFactory.BLUETOOTH); + } + else if(NXT.length()<8) + { + System.out.println("search for " +NXT); + nxtInfo = nxtComm.search(NXT, NXTCommFactory.BLUETOOTH); + + } + else + { + nxtInfo = new NXTInfo[1]; + nxtInfo[0] = new NXTInfo("unknown ",NXT);// NXT is actually address + } + if (nxtInfo.length == 0) + { + System.out.println("No NXT Found: is BT adatper on? is NXT on? "); + System.exit(1); + } + System.out.println("Connecting to " + nxtInfo[0].name+" "+nxtInfo[0].btDeviceAddress); + boolean opened = nxtComm.open(nxtInfo[0]); + if (!opened) { + System.out.println("Failed to open " + nxtInfo[0].name+" "+nxtInfo[0].btDeviceAddress); + System.exit(1); + } + System.out.println("Connected to " + nxtInfo[0].name); + } + is = nxtComm.getInputStream(); + dataIn = new DataInputStream( nxtComm.getInputStream()); + os = nxtComm.getOutputStream(); + dataOut = new DataOutputStream(os); + return true; + } + /** + * @return the InputStream for this connection; + */ + public InputStream getInputStream(){return is;} + + /** + * @return the DataInputStream for this connection; + */ + public DataInputStream getDataIn(){return dataIn;} + + /** + * @return the OutputSteram for this connection; + */ + public OutputStream getOutputStream(){return os;} + + /** + * @return the DataOutputStream for this connection + */ + public DataOutputStream getDataOut() {return dataOut;} + + + public static void main(String[] args) + { + Connector con = new Connector(); + con.startConnector("NXT",false); +// DataInputStream din= btm.getDataIn(); +// while(true) +// { +// try{ System.out.println(din.readFloat());} catch(IOException e){} +// } + + } + + } + +