1 / 13

leJOS Protocols

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

Download Presentation

leJOS Protocols

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. leJOS Protocols

  2. 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

  3. 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()).

  4. 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().

  5. LLC Reliable Handler • Guarantees reliable delivery using checksums,acks and single bit sequence number • Packet organization Checksum Sequence Number Data

  6. 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.

  7. 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

  8. 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

  9. 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); • } • }

  10. 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]; • } • }

  11. 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()); • } • } • }

  12. 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(); • } • } • }

  13. References • lejos.sourceforge.net • rcxcomm package

More Related