stamp.core
Class Uart

java.lang.Object
  |
  +--stamp.core.VirtualPeripheral
        |
        +--stamp.core.Uart

public class Uart
extends VirtualPeripheral

A virtual peripheral UART. Each instance of this class provides asynchronous serial communication in a single direction. For full-duplex communications create two Uart objects, one for each direction.

The Uart class uses 256 byte buffers for both transmit and receive. The buffer size is not configurable. If a handshaking pin is specified for a receive Uart then the peer will be signalled to stop transmitting when there are 16 bytes still free in the buffer.


Field Summary
static int dirReceive
          The value to be using in the direction parameter to create a receiver.
static int dirTransmit
          The value to be using in the direction parameter to create a transmitter.
static boolean dontInvert
          The value for dataInvert and hsInvert to indicate that inverted logic should not be used.
static boolean invert
          The value for dataInvert and hsInvert to indicate that inverted logic should be used.
static int speed1200
          The speed value for 1200 baud.
static int speed14400
          The speed value for 14400 baud.
static int speed19200
          The speed value for 19200 baud.
static int speed2400
          The speed value for 2400 baud.
static int speed28800
          The speed value for 28800 baud.
static int speed38400
          The speed value for 38400 baud.
static int speed4800
          The speed value for 4800 baud.
static int speed57600
          The speed value for 57600 baud.
static int speed600
          The speed value for 600 baud.
static int speed7200
          The speed value for 7200 baud.
static int speed9600
          The speed value for 9600 baud.
static int stop1
          Value to generate 1 stop bit.
static int stop2
          Value to generate 2 stop bits.
static int stop3
          Value to generate 3 stop bits.
static int stop4
          Value to generate 4 stop bits.
static int stop5
          Value to generate 5 stop bits.
static int stop6
          Value to generate 6 stop bits.
static int stop7
          Value to generate 7 stop bits.
static int stop8
          Value to generate 8 stop bits.
 
Constructor Summary
Uart(int direction, int dataPin, boolean dataInvert, int hsPin, boolean hsInvert, int baudRate, int stopBits)
          Creates and initialises a new Uart for simplex communication using hardware (RTS/CTS) handshaking.
Uart(int direction, int dataPin, boolean dataInvert, int baudRate, int stopBits)
          Creates and initialises a new Uart for simplex communication using no handshaking.
Uart(int direction, int dataPin, boolean dataInvert, int hsPin, int baudRate, int stopBits)
          Creates and initialises a new Uart for simplex communication using hardware (RTS/CTS) handshaking.
 
Method Summary
 boolean byteAvailable()
          Test whether any bytes are available in the receive buffer.
 int receiveByte()
          Receives a byte from a receive Uart.
 void restart(int direction, int dataPin, boolean dataInvert, int hsPin, boolean hsInvert, int baudRate, int stopBits)
          Reinitialises a Uart for simplex communication using hardware (RTS/CTS) handshaking.
 void restart(int direction, int dataPin, boolean dataInvert, int baudRate, int stopBits)
          Reinitialises the Uart for simplex communication using no handshaking.
 void restart(int direction, int dataPin, boolean dataInvert, int hsPin, int baudRate, int stopBits)
          Reinitialises the Uart for simplex communication using hardware (RTS/CTS) handshaking.
 boolean sendBufferEmpty()
          Check space in the transmit buffer.
 boolean sendBufferFull()
          Check for space in the transmit buffer.
 void sendByte(int data)
          Adds a byte to the transmit buffer.
 void sendString(String data)
          Transmits a String.
 void setDirection(int direction)
          Change transmission direction.
 void start()
          Starts the Uart running.
 void stop()
          Stops the Uart.
 
Methods inherited from class java.lang.Object
equals
 

Field Detail

dirReceive

public static final int dirReceive
The value to be using in the direction parameter to create a receiver.

dirTransmit

public static final int dirTransmit
The value to be using in the direction parameter to create a transmitter.

speed600

public static final int speed600
The speed value for 600 baud.

speed1200

public static final int speed1200
The speed value for 1200 baud.

speed2400

public static final int speed2400
The speed value for 2400 baud.

speed4800

public static final int speed4800
The speed value for 4800 baud.

speed7200

public static final int speed7200
The speed value for 7200 baud.

speed9600

public static final int speed9600
The speed value for 9600 baud.

speed14400

public static final int speed14400
The speed value for 14400 baud.

speed19200

public static final int speed19200
The speed value for 19200 baud.

speed28800

public static final int speed28800
The speed value for 28800 baud.

speed38400

public static final int speed38400
The speed value for 38400 baud.

speed57600

public static final int speed57600
The speed value for 57600 baud.

invert

public static final boolean invert
The value for dataInvert and hsInvert to indicate that inverted logic should be used.

dontInvert

public static final boolean dontInvert
The value for dataInvert and hsInvert to indicate that inverted logic should not be used.

stop1

public static final int stop1
Value to generate 1 stop bit.

stop2

public static final int stop2
Value to generate 2 stop bits.

stop3

public static final int stop3
Value to generate 3 stop bits.

stop4

public static final int stop4
Value to generate 4 stop bits.

stop5

public static final int stop5
Value to generate 5 stop bits.

stop6

public static final int stop6
Value to generate 6 stop bits.

stop7

public static final int stop7
Value to generate 7 stop bits.

stop8

public static final int stop8
Value to generate 8 stop bits.
Constructor Detail

Uart

public Uart(int direction,
            int dataPin,
            boolean dataInvert,
            int baudRate,
            int stopBits)
Creates and initialises a new Uart for simplex communication using no handshaking. The constructor calls the start() method so the Uart will begin working immediately.
Parameters:
direction - whether the Uart is to be used transmit or receive mode. Valid options are Uart.dirTransmit and Uart.dirReceive.
dataPin - the Javelin Stamp I/O pin to use for sending or receiving data.
dataInvert - whether the data should be inverted or not. Valid options are Uart.invert and Uart.dontInvert.
baudRate - the baud rate to use.
stopBits - the number of stop bits to use. Valid values are between Uart.stop1 and Uart.stop8.

Uart

public Uart(int direction,
            int dataPin,
            boolean dataInvert,
            int hsPin,
            int baudRate,
            int stopBits)
Creates and initialises a new Uart for simplex communication using hardware (RTS/CTS) handshaking. The constructor calls the start() method so the Uart will begin working immediately.
Parameters:
direction - whether the Uart is to be used transmit or receive mode. Valid options are Uart.dirTransmit and Uart.dirReceive.
dataPin - the Javelin Stamp I/O pin to use for sending or receiving data.
dataInvert - whether the data should be inverted or not. Valid options are Uart.invert and Uart.dontInvert.
hsPin - the pin to use for hardware handshaking.
baudRate - the baud rate to use.
stopBits - the number of stop bits to use. Valid values are between Uart.stop1 and Uart.stop8.

Uart

public Uart(int direction,
            int dataPin,
            boolean dataInvert,
            int hsPin,
            boolean hsInvert,
            int baudRate,
            int stopBits)
Creates and initialises a new Uart for simplex communication using hardware (RTS/CTS) handshaking. The constructor calls the start() method so the Uart will begin working immediately.
Parameters:
direction - whether the Uart is to be used transmit or receive mode. Valid options are Uart.dirTransmit and Uart.dirReceive.
dataPin - the Javelin Stamp I/O pin to use for sending or receiving data.
dataInvert - whether the data should be inverted or not. Valid options are Uart.invert and Uart.dontInvert.
hsPin - the pin to use for hardware handshaking.
hsInvert - whether the logic of the handshaking pin should be inverted or not.
baudRate - the baud rate to use.
stopBits - the number of stop bits to use. Valid values are between Uart.stop1 and Uart.stop8.
Method Detail

start

public void start()
Starts the Uart running. The constructor will start the Uart when it is first created. If it is later stopped with stop() then start() will start it again with the same set of parameters.

stop

public void stop()
Stops the Uart.

setDirection

public void setDirection(int direction)
Change transmission direction.
Parameters:
direction - new direction

restart

public void restart(int direction,
                    int dataPin,
                    boolean dataInvert,
                    int baudRate,
                    int stopBits)
Reinitialises the Uart for simplex communication using no handshaking. The Uart will begin working immediately.
Parameters:
direction - whether the Uart is to be used transmit or receive mode. Valid options are Uart.dirTransmit and Uart.dirReceive.
dataPin - the Javelin Stamp I/O pin to use for sending or receiving data.
dataInvert - whether the data should be inverted or not. Valid options are Uart.invert and Uart.dontInvert.
baudRate - the baud rate to use.
stopBits - the number of stop bits to use. Valid values are between Uart.stop1 and Uart.stop8.

restart

public void restart(int direction,
                    int dataPin,
                    boolean dataInvert,
                    int hsPin,
                    int baudRate,
                    int stopBits)
Reinitialises the Uart for simplex communication using hardware (RTS/CTS) handshaking. The Uart will begin working immediately.
Parameters:
direction - whether the Uart is to be used transmit or receive mode. Valid options are Uart.dirTransmit and Uart.dirReceive.
dataPin - the Javelin Stamp I/O pin to use for sending or receiving data.
dataInvert - whether the data should be inverted or not. Valid options are Uart.invert and Uart.dontInvert.
hsPin - the pin to use for hardware handshaking.
baudRate - the baud rate to use.
stopBits - the number of stop bits to use. Valid values are between Uart.stop1 and Uart.stop8.

restart

public void restart(int direction,
                    int dataPin,
                    boolean dataInvert,
                    int hsPin,
                    boolean hsInvert,
                    int baudRate,
                    int stopBits)
Reinitialises a Uart for simplex communication using hardware (RTS/CTS) handshaking. The Uart will begin working immediately.
Parameters:
direction - whether the Uart is to be used transmit or receive mode. Valid options are Uart.dirTransmit and Uart.dirReceive.
dataPin - the Javelin Stamp I/O pin to use for sending or receiving data.
dataInvert - whether the data should be inverted or not. Valid options are Uart.invert and Uart.dontInvert.
hsPin - the pin to use for hardware handshaking
hsInvert - whether the logic of the handshaking pin should be inverted or not.
baudRate - the baud rate to use.
stopBits - the number of stop bits to use. Valid values are between Uart.stop1 and Uart.stop8.

sendBufferFull

public boolean sendBufferFull()
Check for space in the transmit buffer. If the VP is not install say that the buffer is full.
Returns:
transmit buffer is full

sendBufferEmpty

public boolean sendBufferEmpty()
Check space in the transmit buffer.
Returns:
transmit buffer is empty (and nothing being sent now)

sendByte

public void sendByte(int data)
Adds a byte to the transmit buffer. The byte is queued for transmit at the end of the buffer. If the buffer is already full then sendByte() will block until there is room in the buffer.
Parameters:
data - the data to transmit. Only the low 8 bits of the parameter are used.

sendString

public void sendString(String data)
Transmits a String.
Parameters:
data - the string to transmit.

receiveByte

public int receiveByte()
Receives a byte from a receive Uart. This method will block until a byte is available.
Returns:
the next byte in the receive buffer.

byteAvailable

public boolean byteAvailable()
Test whether any bytes are available in the receive buffer. Can also be used to test if the transmit buffer is empty.
Returns:
true if there is at least one byte available in the receive buffer, or at least one byte free in the transmit buffer.