Presentation is loading. Please wait.

Presentation is loading. Please wait.

LeJOS Protocols. Serial Class Serial class found in josx.platform.rcx package Serial class found in josx.platform.rcx package RCX – IR, RCX – RCX communication.

Similar presentations


Presentation on theme: "LeJOS Protocols. Serial Class Serial class found in josx.platform.rcx package Serial class found in josx.platform.rcx package RCX – IR, RCX – RCX communication."— Presentation transcript:

1 leJOS Protocols

2 Serial Class Serial class found in josx.platform.rcx package Serial class found in josx.platform.rcx package RCX – IR, RCX – RCX communication RCX – IR, RCX – RCX communication sendPacket(byte[] aBuffer, int aOffset, int aLen) sendPacket(byte[] aBuffer, int aOffset, int aLen) Returns a true or a false based on whether the send was completed or not 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 Sends a packet to an IR tower or to a RCX isPacketAvailable() isPacketAvailable() Checks to see if a packet is available Checks to see if a packet is available readPacket(byte[] aBuffer) readPacket(byte[] aBuffer) Reads a packet received by the RCX, if available Reads a packet received by the RCX, if available

3 rcxcomm package Provides methods for communicating between the PC and the RCX Provides methods for communicating between the PC and the RCX 2 types of protocols based on LLC and LNP 2 types of protocols based on LLC and LNP IR signal – physical layer IR signal – physical layer Before any kind of communication the ports need to be opened(open()). Before any kind of communication the ports need to be opened(open()). Likewise at the end the ports need to be closed(close()). Likewise at the end the ports need to be closed(close()).

4 LLC 2 messaging layers 2 messaging layers LLC Handler LLC Handler LLCReliableHandler LLCReliableHandler Deals with data packets and acks. Deals with data packets and acks. Allows data to be sent in both directions Allows data to be sent in both directions Broadcast protocol Broadcast protocol Data is read from the input stream at the default port by using read(). 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(). 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 Guarantees reliable delivery using checksums,acks and single bit sequence number Packet organization Packet organization Sequence NumberData Checksum

6 RCXLNP Port/Addressing Based on LNP protocol ( leJOS 2.1.0 onwards) Based on LNP protocol ( leJOS 2.1.0 onwards) Has 3 messaging layers Has 3 messaging layers LNPHandler LNPHandler LNPIntegrityHandler LNPIntegrityHandler LNPAddressingHandler LNPAddressingHandler RCXLNPPort class supports only broadcasting. RCXLNPPort class supports only broadcasting. RCXLNPAddressing class supports point to point addressing. RCXLNPAddressing class supports point to point addressing.

7 RCXLNP Port/Addressing 2 types of addressing 2 types of addressing Between specific RCX’s Between specific RCX’s Between a PC and a RCX. Between a PC and a RCX. 2 types of packets 2 types of packets Integrity Integrity Addressing Addressing Integrity packets are broadcast. Integrity packets are broadcast. Addressing packets have specific source and destination addresses. Addressing packets have specific source and destination addresses. Opcodes used to distinguish between broadcasting and addressing. Opcodes used to distinguish between broadcasting and addressing. 0xf0 – Broadcasts 0xf0 – Broadcasts 0xf1 – Addressing 0xf1 – Addressing

8 RCXLNP Addressing LNPHandler LNPHandler Implements the LNP packet format Implements the LNP packet format LNPIntegrityHandler LNPIntegrityHandler Handles the checksums for reliability Handles the checksums for reliability LNPAddressingHandler LNPAddressingHandler Implements addressing Implements addressing OpCode Size of the Packet Data Checksum Destination Source

9 Examples import josx.platform.rcx.*; import josx.platform.rcx.*; class Sender class Sender { public static void main(String [] args) { public static void main(String [] args) { Sender s = new Sender(); Sender s = new Sender(); try { try { for(byte i=0;i<200;++i) { for(byte i=0;i<200;++i) { Button.RUN.waitForPressAndRelease(); Button.RUN.waitForPressAndRelease(); Sound.beep(); Sound.beep(); s.sendByte(i); s.sendByte(i); } } } catch(Exception e){} catch(Exception e){} } } private byte[] packet = {(byte)0xf7, (byte)0x00}; private byte[] packet = {(byte)0xf7, (byte)0x00}; /** /** * Send a single byte * Send a single byte */ */ protected void sendByte(byte b) { protected void sendByte(byte b) { packet[1] = b; packet[1] = b; josx.platform.rcx.Serial.sendPacket(packet, 0, 2); josx.platform.rcx.Serial.sendPacket(packet, 0, 2); } } }

10 import josx.platform.rcx.*; import josx.platform.rcx.*; public class Receiver { public class Receiver { public static void main(String [] args) throws Exception { public static void main(String [] args) throws Exception { Receiver r = new Receiver(); Receiver r = new Receiver(); for(;;) { for(;;) { if(Serial.isPacketAvailable()) { if(Serial.isPacketAvailable()) { Sound.beep(); Sound.beep(); LCD.showNumber(r.receiveByte()); LCD.showNumber(r.receiveByte()); } } private byte[] buffer = new byte[10]; private byte[] buffer = new byte[10]; /** /** * Receive a single byte * Receive a single byte */ */ protected byte receiveByte() { protected byte receiveByte() { josx.platform.rcx.Serial.readPacket(buffer); josx.platform.rcx.Serial.readPacket(buffer); return buffer[1]; return buffer[1]; } } }

11 import java.io.*; import java.io.*; import josx.rcxcomm.*; import josx.rcxcomm.*; public class ReadSensor { public class ReadSensor { public static void main(String[] args) { public static void main(String[] args) { try { try { RCXPort port = new RCXPort(); RCXPort port = new RCXPort(); InputStream is = port.getInputStream(); InputStream is = port.getInputStream(); OutputStream os = port.getOutputStream(); OutputStream os = port.getOutputStream(); DataInputStream dis = new DataInputStream(is); DataInputStream dis = new DataInputStream(is); DataOutputStream dos = new DataOutputStream(os); DataOutputStream dos = new DataOutputStream(os); System.out.println("Reading Light Sensor"); System.out.println("Reading Light Sensor"); int sendTime = (int)System.currentTimeMillis(); int sendTime = (int)System.currentTimeMillis(); for(int i=0;i<20;i++) { for(int i=0;i<20;i++) { dos.writeByte(1); dos.writeByte(1); dos.flush(); dos.flush(); int n = dis.readShort(); int n = dis.readShort(); System.out.println("Received " + n); System.out.println("Received " + n); } } System.out.println("Time = " + ((int)System.currentTimeMillis() - sendTime)); System.out.println("Time = " + ((int)System.currentTimeMillis() - sendTime)); } } catch (Exception e) { catch (Exception e) { System.out.println("Exception " + e.getMessage()); System.out.println("Exception " + e.getMessage()); } } }

12 import java.io.*; import java.io.*; import josx.rcxcomm.*; import josx.rcxcomm.*; import josx.platform.rcx.*; import josx.platform.rcx.*; public class SensorReader { public class SensorReader { public static void main(String args[]) { public static void main(String args[]) { int sensorID, sensorValue; int sensorID, sensorValue; RCXPort port = null; RCXPort port = null; try { try { port = new RCXPort(); port = new RCXPort(); DataOutputStream out = new DataOutputStream(port.getOutputStream()); DataOutputStream out = new DataOutputStream(port.getOutputStream()); while (true) { while (true) { sensorID = port.getInputStream().read(); sensorID = port.getInputStream().read(); sensorValue = Sensor.readSensorValue(sensorID, 0); sensorValue = Sensor.readSensorValue(sensorID, 0); LCD.showNumber(sensorValue); LCD.showNumber(sensorValue); out.writeShort(sensorValue); out.writeShort(sensorValue); out.flush(); out.flush(); } } } catch (IOException ioE) { } catch (IOException ioE) { LCD.showNumber(1111); LCD.showNumber(1111); } finally { } finally { port.close(); port.close(); } } }

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


Download ppt "LeJOS Protocols. Serial Class Serial class found in josx.platform.rcx package Serial class found in josx.platform.rcx package RCX – IR, RCX – RCX communication."

Similar presentations


Ads by Google