Start of Tutorial > Start of Trail |
In lejos 2.0 support for writing your own comms protocol stacks has be added.
The protocol stacks are based on the abstract josx.rcxcomm.PacketHandler
interface. All packet handlers must implement three methods:
If the stack is going to support acknowledgement of the receipt of packets (acks) then the lowest handler should also implement the methods:
On the PC, the method
public void close();
should be supported to close the Tower.
Also, on the PC the method
public setListen(boolean listen);
should be supported and when listen is true, keep-alive messages should be sent to
a serial tower at least every 3 seconds. Doing this in isPacketAvailable()
and
isAckAvailable()
will normally suffice.
The next higher level layer should support integrity and optionally reliable delivery. Integrity will normally be done by checksums which should have been read in as part of the packet. Reliable delivery involves using the lower level handler to send and receive acks, and checking sequence numbers. If each packet is immediately acknowledged a one-bit sequence number should suffice. Integrity and reliable delivery could be separated into separate layers, but it is normally more efficient to do this in one layer.
The constructor of a PacketHandler is passed the lower level packet handler. This is then available in the protected lowerHandler variable. The lowest level handler has no parameters.
For example:
PacketHandler packetHandler =
(PacketHandler) new IntegrityPacketHandler(
(PacketHandler) new LowestPacketHandler());
Extra levels of packet handlers can be added, for example an addressing layer. Addressing packet handlers implement point-to-point connections between two ports. They need to support
public void open(byte source, byte destination);
An example of a three level stack is:
PacketHandler packetHandler =
(PacketHandler) AddressingPacketHandler(
(PacketHandler) new IntegrityPacketHandler(
(PacketHandler) new LowestPacketHandler()));
Streams that implement java.io.InputStream and java.io.OutputStream can be
implemented using protocol stacks by use of the abstract port interface RCXAbstractPort
.
To create an RCX Port class that uses aPacketHandler protocol stack, you extend
RCXAbstractPort and call super()
in the constructor and pass it the packet handler,
for example:
public class MyRCXPort extends RCXAbstractPort {
public MyRCXPort() throws IOException {
super((PacketHandler) new IntegrityPacketHandler(
(PacketHandler) new LowestPacketHandler()));
}
}
If the port is to support addressing you need pass the constructor the source and destination ports and call the packet handlers open method, for example:
public class MyRCXAddressingPort extends RCXAbstractPort {
public MyRCXAddressingPort(byte source, byte dest) throws IOException {
super((PacketHandler) new AddressingHandler(
(PacketHandler) new IntegrityHandler(
(PacketHandler) new LowestHandler(true)));
packetHandler.open(source, dest);
}
}
The constructor for LowestHandler
is assumed here to have a parameter that tells
it whether addressing is in use, as this might affect the format of the packet.
Four example RCX Ports are provided as part of the josx.rcxcomm package with lejos 2.0. They are:
Start of Tutorial > Start of Trail |