Start of Tutorial > Start of Trail |
josx.rcxcomm
is a package for communication between a PC and the RCX.
It uses standard Java IO Streams to send primitive Java data types from the PC to the RCX, or from the RCX to the PC. Either end can initiate communication, and data can be sent in both directions.
import josx.rcxcomm.*;
import java.io.*;
public class TestRCXComm {
public static void main(String [] args) {
try {
RCXPort port = new RCXPort();
OutputStream os = port.getOutputStream();
os.write(123);
os.flush();
}
catch (IOException ioe) {
...
}
}
To read a byte back you would add:
InputStream is = port.getInputStream();
is.read();
To send other Java primitive types, such as int, you add:
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(123456);
dos.flush();
Both serial and USB towers are supported on Windows. Currently only serial towers are supported for Linux and Mac OSX.
lejosjc
. An extra jar
(rcxcomm.jar
) is used automatically.
On the PC or Mac you compile with javac
and add pcrcxcomm.jar
from the lejos lib directory to your CLASSPATH.
The port used for the IR Tower can be set in two ways:
RCXBean
. This
provides a Java Bean interface for sending and receiving data from the RCX.
setListen
.
setListen(true)
when you want to receive data and setListen(false)
to conserve
the serial tower batteries. You don't need to call setListen
for USB towers.
setListen
is called automatically by the PC version of getInputStream()
. You
can call setListen(false)
if you dont need to listen immediately and you want to
conserve batteries.
With lejos 2.0 there are alternative versions of RCXPort. These are:
rcxcomm/examples
directory.
See the browser example for how to browse the Internet from the RCX using a socket
proxy on the PC, and servlet for how to turn the RCX into a Web Server using an Http
Proxy on the PC.
Start of Tutorial > Start of Trail |