All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.remote.rcx.RCXAbstractPort Maven / Gradle / Ivy

Go to download

leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.

The newest version!
package lejos.remote.rcx;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import lejos.utility.Delay;

/** 
 * RCXAbstractPort provides an interface similar to java.net.Socket
 * Adapted from original code created by the LEGO3 Team at DTU-IAU
 * RCXAbstractPort implements input and output stream handling and input
 * buffering. It uses a packet handler for sending and receivng packets.
 * This version is abstract because it has no packet handler defined.
 * Specific versions of RCXAbstractPort override the constructor and
 * set up the packet handler to use a specific protocol stack. 
 * 
 * @author Brian Bagnall
 * @author Lawrie Griffiths
 * 
 */
public abstract class RCXAbstractPort {

   private boolean portOpen = true;
   private Listener listener;
   private int timeOut = 0;

   private RCXInputStream rcxin;
   private RCXOutputStream rcxout;
   protected PacketHandler packetHandler;

   /**
    * Constructor for the RCXAbstractPort.
    * Opens the port, and sets the protocol packet handler.
    * @param handler the packet handler
    */
   public RCXAbstractPort(PacketHandler handler) throws IOException {
      packetHandler = handler;
      rcxin = new RCXInputStream(this);
      rcxout = new RCXOutputStream(packetHandler);
      listener = new Listener();
      listener.setDaemon(true);
      listener.start();
   }

   /** Returns an input stream for this RCXPort.
    * @return an input stream for reading bytes from this RCXPort.
    */
   public InputStream getInputStream() {
      return (InputStream) rcxin;
   }

   /** Returns an output stream for this RCXPort.
    * @return an output stream for writing bytes to this RCXPort.
    */
   public OutputStream getOutputStream() {
      return (OutputStream) rcxout;
   }

   /**
    * Resets sequence numbers for this port 
    */
   public void reset() {
     packetHandler.reset();
   }

   /** Closes this RCXPort, stopping the Listener thread.
    */
   public void close() {
      portOpen = false;
   }

   /** Getter for property timeOut.
    * @return Value of property timeOut.
    */
   public int getTimeOut() {
      return timeOut;
   }

   /** Setter for property timeOut.
    * @param timeOut New value of property timeOut.
    */
   public void setTimeOut(int timeOut) {
      this.timeOut = timeOut;
   }

   private byte [] inPacket = new byte[2];

   /** Listener class runs a thread that reads and buffers bytes.
    * Allows a maximum of two bytes in a packet.
    */
   private class Listener extends Thread {
      public void run() {
         while (portOpen) {
            if (packetHandler.isPacketAvailable()) {
              int r = packetHandler.receivePacket(inPacket);
              for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy