OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / USBSend / USBSend.java
1 import lejos.pc.comm.*;
2 import java.io.*;
3  
4 /**
5  * This is a PC sample. It connects to the NXT, and then
6  * sends an integer and waits for a reply, 100 times.
7  * 
8  * Compile this program with javac (not nxjc), and run it 
9  * with java.
10  * 
11  * You need pccomm.jar on the CLASSPATH and the jlibnxt
12  * DLL or shared library on the Java library path.
13  * 
14  * Run the program by:
15  * 
16  *   java USBSend
17  * 
18  * Your NXT should be running a sample such as USBReceive. 
19  * 
20  * @author Lawrie Griffiths
21  *
22  */
23 public class USBSend {
24         
25         public static void main(String[] args) {
26                 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.USB);
27                 
28                 NXTInfo[] nxtInfo = null;
29                 
30                 try {
31                         nxtInfo = nxtComm.search(null, NXTCommFactory.USB);
32                 } catch (NXTCommException e) {
33                         System.out.println("Exception in search");
34                 }
35                 
36                 if (nxtInfo.length == 0) {
37                         System.out.println("No NXT Found");
38                         System.exit(1);
39                 }
40
41                 try {
42                         nxtComm.open(nxtInfo[0]);
43                 } catch (NXTCommException e) {
44                         System.out.println("Exception in open");
45                 }
46                 
47                 InputStream is = nxtComm.getInputStream();
48                 OutputStream os = nxtComm.getOutputStream();
49                 DataInputStream inDat = new DataInputStream(is);
50                 DataOutputStream outDat = new DataOutputStream(os);
51                 int x = 0;
52                 for(int i=0;i<100;i++) 
53                 {
54                         try {
55                            outDat.writeInt(i);
56                            outDat.flush();
57         
58                         } catch (IOException ioe) {
59                                 System.out.println("IO Exception writing bytes");
60                         }
61                  try {x = inDat.readInt();}
62                  catch (IOException ioe) {
63                    System.out.println(ioe);
64                  }            
65                System.out.println("Sent "+i+ " Received "+x);
66                 }
67                 
68                 try {
69                         inDat.close();
70                         outDat.close();
71                 } catch (IOException ioe) {
72                         System.out.println(ioe);
73                 }
74                 
75                 try {
76                         Thread.sleep(1000);
77                 } catch (InterruptedException ie) {}
78                 
79                 try {
80                         nxtComm.close();
81                 } catch (IOException ioe) {}
82         }
83
84 }