1 import lejos.pc.comm.*;
5 * This is a PC sample. It connects to the NXT, and then
6 * sends an integer and waits for a reply, 100 times.
8 * Compile this program with javac (not nxjc), and run it
11 * You need pccomm.jar on the CLASSPATH and the jlibnxt
12 * DLL or shared library on the Java library path.
18 * Your NXT should be running a sample such as USBReceive.
20 * @author Lawrie Griffiths
23 public class USBSend {
25 public static void main(String[] args) {
26 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.USB);
28 NXTInfo[] nxtInfo = null;
31 nxtInfo = nxtComm.search(null, NXTCommFactory.USB);
32 } catch (NXTCommException e) {
33 System.out.println("Exception in search");
36 if (nxtInfo.length == 0) {
37 System.out.println("No NXT Found");
42 nxtComm.open(nxtInfo[0]);
43 } catch (NXTCommException e) {
44 System.out.println("Exception in open");
47 InputStream is = nxtComm.getInputStream();
48 OutputStream os = nxtComm.getOutputStream();
49 DataInputStream inDat = new DataInputStream(is);
50 DataOutputStream outDat = new DataOutputStream(os);
52 for(int i=0;i<100;i++)
58 } catch (IOException ioe) {
59 System.out.println("IO Exception writing bytes");
61 try {x = inDat.readInt();}
62 catch (IOException ioe) {
63 System.out.println(ioe);
65 System.out.println("Sent "+i+ " Received "+x);
71 } catch (IOException ioe) {
72 System.out.println(ioe);
77 } catch (InterruptedException ie) {}
81 } catch (IOException ioe) {}