jSyncManager

org.jSyncManager.JSerial
Class SerialInterface

java.lang.Object
  extended byorg.jSyncManager.JSerial.SerialInterface

public class SerialInterface
extends java.lang.Object

A class for accessing serial port functionality via Java. This class provides serial port functionality to Java programs, allowing them to set various port parameters, read and write to the port, and grab and release the port. The serial functionality is presented as a set of standard Java InputStream/OutputStreams, making it easily interchangable with entities such as java.net.Socket.

Version:
$Revision: 1.2 $
Author:
Brad BARCLAY <bbarclay@jsyncmanager.org>
Last modified by: $Author: yaztromo $ on $Date: 2003/11/20 17:50:58 $.

Field Summary
static int EVENPARITY
          Used to specify the use of even parity.
static int MARKPARITY
          Used to denote the use of mark parity.
static int NOPARITY
          Used to specify the use of no parity.
static int ODDPARITY
          Used to specify the use of odd parity.
static int SPACEPARITY
          Used to denote the use of space parity.
 
Constructor Summary
SerialInterface(byte port)
          Starts a new serial interface using the given port identifier at the default speed (9600bps) using 8 data bits, no parity, and 1 stop bit and starts off with the port in "grabbed" mode.
SerialInterface(byte port, int speed)
          Starts a new serial interface using the given port, at the given speed, using 8 data bits, no parity, and 1 stop bit and starts off with the port in "grabbed" mode.
SerialInterface(byte port, int speed, int databits, int parity, int stopbits, boolean status)
          Starts a new serial interface using the given port at the given speed, using the given values for data bits, parity and stop bits.
 
Method Summary
protected  void finalize()
          Finalizes this object.
 int getCurrentDatabits()
          Used to get the current data bit size.
 int getCurrentParity()
          Returns the current serial interface parity.
 byte getCurrentPort()
          Used to get the current serial port id number.
 int getCurrentSpeed()
          Used to get the current serial speed.
 int getCurrentStopbits()
          Used to get the current stop bit size.
 boolean getDSRDTRState()
          Queries the state of the DSR/DTR handshaking.
 java.io.InputStream getInputStream()
          This method returns the current input stream.
 java.io.OutputStream getOutputStream()
          This method returns the current output stream.
 SerialPeer getPeer()
          Used to get ahold of the handle to the Serial Peer class.
static java.lang.String getPeerVersion()
          Used to get the version number of the native code peer class.
static java.lang.String getPlatformName()
          Used to get the name of the platform to which the native code portion is written.
 boolean getRTSCTSState()
          Queries the state of the RTS/CTS handshaking.
 boolean getXONXOFFState()
          Queries the state of the XON/XOFF handshaking.
 void grabPort()
          This method will grab a released port.
 boolean isReleased()
          Used to check the status of the port assigned to this instance of the SerialInterface.
 void releasePort()
          Releases the port back to the OS's use, but keeps the peer and streams alive.
 void setCurrentDatabits(int databits)
          Used to set the current serial data bit size.
 void setCurrentParity(int parity)
          Used to set the current serial parity.
 void setCurrentSpeed(int speed)
          Used to set the current serial speed.
 void setCurrentStopbits(int stopbits)
          Used to set the current serial stop bit size.
 void setDSRDTR(boolean state)
          A method to set the state of the DSR/DTR handshaking.
 void setRTSCTS(boolean state)
          A method to set the state of the RTS/CTS handshaking.
 void setXONXOFF(boolean state)
          A method to set the state of the XON/XOFF handshaking.
 void transmitBreak(long length)
          Transmits a break signal.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOPARITY

public static final int NOPARITY
Used to specify the use of no parity.

See Also:
Constant Field Values

ODDPARITY

public static final int ODDPARITY
Used to specify the use of odd parity.

See Also:
Constant Field Values

EVENPARITY

public static final int EVENPARITY
Used to specify the use of even parity.

See Also:
Constant Field Values

MARKPARITY

public static final int MARKPARITY
Used to denote the use of mark parity.

See Also:
Constant Field Values

SPACEPARITY

public static final int SPACEPARITY
Used to denote the use of space parity.

See Also:
Constant Field Values
Constructor Detail

SerialInterface

public SerialInterface(byte port)
                throws SerialInitializationException,
                       SerialSpeedException,
                       SerialDataBitException,
                       SerialParityException,
                       SerialStopBitException,
                       SerialGrabException
Starts a new serial interface using the given port identifier at the default speed (9600bps) using 8 data bits, no parity, and 1 stop bit and starts off with the port in "grabbed" mode.

Parameters:
port - the identifier of the serial port to open
Throws:
SerialInitializationException - thrown if the port cannot be opened.
SerialSpeedException - thrown if the given speed is invalid or unsupported.
SerialDataBitException - thrown if the given data bit size is invalid.
SerialParityException - thrown if the given parity is invalid.
SerialStopBitException - thrown if the current stop-bit size is invalid.
SerialGrabException - thrown if status is true, and the port is currently not available.

SerialInterface

public SerialInterface(byte port,
                       int speed)
                throws SerialInitializationException,
                       SerialSpeedException,
                       SerialDataBitException,
                       SerialParityException,
                       SerialStopBitException,
                       SerialGrabException
Starts a new serial interface using the given port, at the given speed, using 8 data bits, no parity, and 1 stop bit and starts off with the port in "grabbed" mode.

Parameters:
port - the identifier of the serial port to open
speed - the speed at which to initialize the port
Throws:
SerialInitializationException - thrown if the port cannot be opened.
SerialSpeedException - thrown if the given speed is invalid or unsupported.
SerialDataBitException - thrown if the given data bit size is invalid.
SerialParityException - thrown if the given parity is invalid.
SerialStopBitException - thrown if the current stop-bit size is invalid.
SerialGrabException - thrown if status is true, and the port is currently not available.

SerialInterface

public SerialInterface(byte port,
                       int speed,
                       int databits,
                       int parity,
                       int stopbits,
                       boolean status)
                throws SerialInitializationException,
                       SerialSpeedException,
                       SerialDataBitException,
                       SerialParityException,
                       SerialStopBitException,
                       SerialGrabException
Starts a new serial interface using the given port at the given speed, using the given values for data bits, parity and stop bits.

Parameters:
port - the identifier of the serial port to open
speed - the speed at which to initialize the port
databits - the number of data bits per byte to transmit/receive
parity - the parity type for a single byte
stopbits - the number of stop bits in a single byte
status - the desired "grab" status. If true the port is grabbed during construction, otherwise everything is prepared, but the port is left in the non-"grabbed" state.
Throws:
SerialInitializationException - thrown if the port cannot be opened.
SerialSpeedException - thrown if the given speed is invalid or unsupported.
SerialDataBitException - thrown if the given data bit size is invalid.
SerialParityException - thrown if the given parity is invalid.
SerialStopBitException - thrown if the current stop-bit size is invalid.
SerialGrabException - thrown if status is true, and the port is currently not available.
Method Detail

getInputStream

public java.io.InputStream getInputStream()
This method returns the current input stream.

Returns:
the serial input stream

getOutputStream

public java.io.OutputStream getOutputStream()
This method returns the current output stream.

Returns:
the serial output stream

getPlatformName

public static java.lang.String getPlatformName()
Used to get the name of the platform to which the native code portion is written.

Returns:
the name of the operating system/environment the peer was written for

getPeerVersion

public static java.lang.String getPeerVersion()
Used to get the version number of the native code peer class.

Returns:
the version number of the peer interface implementation

getCurrentPort

public byte getCurrentPort()
Used to get the current serial port id number.

Returns:
the current serial port id number.

getCurrentSpeed

public int getCurrentSpeed()
Used to get the current serial speed.

Returns:
the current serial speed

setCurrentSpeed

public void setCurrentSpeed(int speed)
                     throws SerialSpeedException
Used to set the current serial speed.

Parameters:
speed - the desired serial communications speed.
Throws:
SerialSpeedException - thrown if the given speed is invalid or unsupported.

getCurrentDatabits

public int getCurrentDatabits()
Used to get the current data bit size.

Returns:
the current number of data bits in a single transmission byte

setCurrentDatabits

public void setCurrentDatabits(int databits)
                        throws SerialDataBitException
Used to set the current serial data bit size.

Parameters:
databits - the desired number of data bits per transmission byte.
Throws:
SerialDataBitException - thrown if the given data bit size is invalid.

getCurrentParity

public int getCurrentParity()
Returns the current serial interface parity.

Returns:
the parity type (ie: EVENPARITY, ODDPARITY, NOPARITY, MARKPARITY).

setCurrentParity

public void setCurrentParity(int parity)
                      throws SerialParityException
Used to set the current serial parity.

Parameters:
parity - the desired parity type (ie: EVENPARITY, ODDPARITY, NOPARITY, MARKPARITY).
Throws:
SerialParityException - thrown if the given parity is invalid.

getCurrentStopbits

public int getCurrentStopbits()
Used to get the current stop bit size.

Returns:
the current number of stop bits per byte.

setCurrentStopbits

public void setCurrentStopbits(int stopbits)
                        throws SerialStopBitException
Used to set the current serial stop bit size.

Parameters:
stopbits - the desired number of stop bits ber transmission byte
Throws:
SerialStopBitException - thrown if the given stop bit size is invalid.

getPeer

public SerialPeer getPeer()
Used to get ahold of the handle to the Serial Peer class. Note that end programmers should never use this, however it is provided for completeness, and mirrors the getPeer() method in java.awt.Component.

Returns:
the current instance of the SerialPeer class

releasePort

public void releasePort()
                 throws SerialReleaseException
Releases the port back to the OS's use, but keeps the peer and streams alive. This method will release the serial port for use by other applications. This method should be called by the finalize() method, but is also available for calling by application developers for flexibility.

Throws:
SerialReleaseException - thrown if there is a problem releasing the current port

grabPort

public void grabPort()
              throws SerialGrabException
This method will grab a released port. If the serial port has been released back to the OS's use, this method will pick it up again if it's available. Otherwise a SerialGrabException is generated.

Throws:
SerialGrabException - thrown if the port is not available, or is already in the "grabbed" state.

isReleased

public boolean isReleased()
Used to check the status of the port assigned to this instance of the SerialInterface. This method will return true if the serial interface is released from its port, and false if otherwise.

Returns:
true if the port has been released, false otherwise.

finalize

protected void finalize()
                 throws java.lang.Throwable
Finalizes this object.

Throws:
java.lang.Throwable - any Throwable that occurs during finalization.

setRTSCTS

public void setRTSCTS(boolean state)
               throws SerialHandshakingException
A method to set the state of the RTS/CTS handshaking. Sets the state of RTS/CTS hardware handshaking.

Parameters:
state - if true, enables RTS/CTS hardware handshaking.
Throws:
SerialHandshakingException - thrown if the serial driver is unable to change modes.

setDSRDTR

public void setDSRDTR(boolean state)
               throws SerialHandshakingException
A method to set the state of the DSR/DTR handshaking. Sets the state of DSR/DTR hardware handshaking.

Parameters:
state - if true, enables hardware handshaking.
Throws:
SerialHandshakingException - thrown if the serial driver is unable to change modes.

setXONXOFF

public void setXONXOFF(boolean state)
                throws SerialHandshakingException
A method to set the state of the XON/XOFF handshaking. Sets the state of XON/XOFF software handshaking. Note that this function will set both the transmit and receive to use XON/XOFF control.

Parameters:
state - if true, enables software handshaking.
Throws:
SerialHandshakingException - thrown if the serial driver is unable to change modes.

transmitBreak

public void transmitBreak(long length)
Transmits a break signal.

Parameters:
length - how long, in milliseconds, to transmit the break signal.

getRTSCTSState

public boolean getRTSCTSState()
Queries the state of the RTS/CTS handshaking.

Returns:
true if RTS/CTS is enabled.

getDSRDTRState

public boolean getDSRDTRState()
Queries the state of the DSR/DTR handshaking.

Returns:
true if DSR/DTR is enabled.

getXONXOFFState

public boolean getXONXOFFState()
Queries the state of the XON/XOFF handshaking.

Returns:
true if XON/XOFF is enabled.

jSyncManager

Copyright (c) 1998 - 2003 Brad BARCLAY and others. All Rights Reserved.