130 likes | 263 Views
leJOS Protocols. Serial Class. Serial class found in josx.platform.rcx package RCX – IR, RCX – RCX communication sendPacket(byte[] aBuffer, int aOffset, int aLen) Returns a true or a false based on whether the send was completed or not Sends a packet to an IR tower or to a RCX
E N D
Serial Class • Serial class found in josx.platform.rcx package • RCX – IR, RCX – RCX communication • sendPacket(byte[] aBuffer, int aOffset, int aLen) • Returns a true or a false based on whether the send was completed or not • Sends a packet to an IR tower or to a RCX • isPacketAvailable() • Checks to see if a packet is available • readPacket(byte[] aBuffer) • Reads a packet received by the RCX, if available
rcxcomm package • Provides methods for communicating between the PC and the RCX • 2 types of protocols based on LLC and LNP • IR signal – physical layer • Before any kind of communication the ports need to be opened(open()). • Likewise at the end the ports need to be closed(close()).
LLC • 2 messaging layers • LLC Handler • LLCReliableHandler • Deals with data packets and acks. • Allows data to be sent in both directions • Broadcast protocol • Data is read from the input stream at the default port by using read(). • Data is written to the output stream at the default port by using write().
LLC Reliable Handler • Guarantees reliable delivery using checksums,acks and single bit sequence number • Packet organization Checksum Sequence Number Data
RCXLNP Port/Addressing • Based on LNP protocol ( leJOS 2.1.0 onwards) • Has 3 messaging layers • LNPHandler • LNPIntegrityHandler • LNPAddressingHandler • RCXLNPPort class supports only broadcasting. • RCXLNPAddressing class supports point to point addressing.
RCXLNP Port/Addressing • 2 types of addressing • Between specific RCX’s • Between a PC and a RCX. • 2 types of packets • Integrity • Addressing • Integrity packets are broadcast. • Addressing packets have specific source and destination addresses. • Opcodes used to distinguish between broadcasting and addressing. • 0xf0 – Broadcasts • 0xf1 – Addressing
RCXLNP Addressing • LNPHandler • Implements the LNP packet format • LNPIntegrityHandler • Handles the checksums for reliability • LNPAddressingHandler • Implements addressing Destination Source Data Checksum OpCode Size of the Packet
Examples • import josx.platform.rcx.*; • class Sender • { • public static void main(String [] args) { • Sender s = new Sender(); • try { • for(byte i=0;i<200;++i) { • Button.RUN.waitForPressAndRelease(); • Sound.beep(); • s.sendByte(i); • } • } • catch(Exception e){} • } • private byte[] packet = {(byte)0xf7, (byte)0x00}; • /** • * Send a single byte • */ • protected void sendByte(byte b) { • packet[1] = b; • josx.platform.rcx.Serial.sendPacket(packet, 0, 2); • } • }
import josx.platform.rcx.*; • public class Receiver { • public static void main(String [] args) throws Exception { • Receiver r = new Receiver(); • for(;;) { • if(Serial.isPacketAvailable()) { • Sound.beep(); • LCD.showNumber(r.receiveByte()); • } • } • } • private byte[] buffer = new byte[10]; • /** • * Receive a single byte • */ • protected byte receiveByte() { • josx.platform.rcx.Serial.readPacket(buffer); • return buffer[1]; • } • }
import java.io.*; • import josx.rcxcomm.*; • public class ReadSensor { • public static void main(String[] args) { • try { • RCXPort port = new RCXPort(); • InputStream is = port.getInputStream(); • OutputStream os = port.getOutputStream(); • DataInputStream dis = new DataInputStream(is); • DataOutputStream dos = new DataOutputStream(os); • System.out.println("Reading Light Sensor"); • int sendTime = (int)System.currentTimeMillis(); • for(int i=0;i<20;i++) { • dos.writeByte(1); • dos.flush(); • int n = dis.readShort(); • System.out.println("Received " + n); • } • System.out.println("Time = " + ((int)System.currentTimeMillis() - sendTime)); • } • catch (Exception e) { • System.out.println("Exception " + e.getMessage()); • } • } • }
import java.io.*; • import josx.rcxcomm.*; • import josx.platform.rcx.*; • public class SensorReader { • public static void main(String args[]) { • int sensorID, sensorValue; • RCXPort port = null; • try { • port = new RCXPort(); • DataOutputStream out = new DataOutputStream(port.getOutputStream()); • while (true) { • sensorID = port.getInputStream().read(); • sensorValue = Sensor.readSensorValue(sensorID, 0); • LCD.showNumber(sensorValue); • out.writeShort(sensorValue); • out.flush(); • } • } catch (IOException ioE) { • LCD.showNumber(1111); • } finally { • port.close(); • } • } • }
References • lejos.sourceforge.net • rcxcomm package