OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / RCXPortTest / RCXPortTest.java
1 \r
2 import lejos.rcxcomm.*;\r
3 import java.io.*;\r
4 import lejos.nxt.*;\r
5 \r
6 /**\r
7  * Test of NXT emulation of the leJOS RCX RCXPort class.\r
8  * \r
9  * Run the LLCSensorReader sample on the RCX, and the NXT \r
10  * will read the raw sensor value of the sensor connected to\r
11  * the RCX S2 port.\r
12  * \r
13  * Requires a Mindsensors NRLink adapter connected to sensor\r
14  * port S1 on the NXT.\r
15  * \r
16  * @author Lawrie Griffiths\r
17  *\r
18  */\r
19 public class RCXPortTest {\r
20 \r
21         public static void main(String[] args) throws Exception {\r
22             try {\r
23               RCXPort port = new RCXPort(SensorPort.S1);\r
24               String reading = "Reading Sensor";\r
25               String rcvd = "Received ";\r
26 \r
27               InputStream is = port.getInputStream();\r
28               OutputStream os = port.getOutputStream();\r
29               DataInputStream dis = new DataInputStream(is);\r
30               DataOutputStream dos = new DataOutputStream(os);\r
31 \r
32               LCD.drawString(reading,0,0);\r
33               LCD.refresh();\r
34               int sendTime = (int)System.currentTimeMillis();\r
35               for(int i=0;i<20;i++) {\r
36                 dos.writeByte(1);\r
37                 dos.flush();\r
38 \r
39                 int n = dis.readShort();\r
40 \r
41                 LCD.drawString(rcvd,0,1);\r
42                 LCD.drawInt(n,4,10,1);\r
43                 LCD.refresh();\r
44               }\r
45               LCD.drawString("Time: ", 0, 2);\r
46               LCD.drawInt((int)System.currentTimeMillis() - sendTime,6,6,2);\r
47               LCD.refresh();\r
48               Button.waitForPress();\r
49             }\r
50             catch (Exception e) {\r
51               LCD.drawString("Exception",0,7);\r
52               LCD.refresh();\r
53               Button.waitForPress();\r
54             }\r
55         }\r
56 }\r