com.rm5248.serial
Class SerialPort

java.lang.Object
  extended by com.rm5248.serial.SerialPort
All Implemented Interfaces:
java.io.Closeable

public class SerialPort
extends java.lang.Object
implements java.io.Closeable

A SerialPort object represents a SerialPort object on the system. When creating a SerialPort object, you give the SerialPort the name of the port that you wish to open. When opening a SerialPort and setting values, an IllegalArgumentException may be thrown if any of the values are NULL. When getting and settings the properties of the SerialPort using one of the getXXX() or setXXX() methods, an IllegalStateException will be thrown if the port has been closed.

Author:
rm5248

Nested Class Summary
static class SerialPort.BaudRate
          Represents the BaudRate that the SerialPort uses.
static class SerialPort.DataBits
          Represents the number of bits that the serial port uses.
static class SerialPort.FlowControl
          The Flow control scheme for the data, typically None.
static class SerialPort.Parity
          The parity bit for the data.
static class SerialPort.StopBits
          The number of stop bits for data.
 
Constructor Summary
SerialPort(java.lang.String portName)
          Open the specified port, 9600 baud, 8 data bits, 1 stop bit, no parity, no flow control
SerialPort(java.lang.String portName, boolean keepSettings)
          Open up a serial port, but allow the user to keep the current settings of the serial port.
SerialPort(java.lang.String portName, SerialPort.BaudRate rate)
          Open the specified port, no flow control
SerialPort(java.lang.String portName, SerialPort.BaudRate rate, SerialPort.DataBits data)
          Open the specified port, no flow control
SerialPort(java.lang.String portName, SerialPort.BaudRate rate, SerialPort.DataBits data, SerialPort.StopBits stop)
          Open the specified port, no parity or flow control
SerialPort(java.lang.String portName, SerialPort.BaudRate rate, SerialPort.DataBits data, SerialPort.StopBits stop, SerialPort.Parity parity)
          Open the specified port, no flow control
SerialPort(java.lang.String portName, SerialPort.BaudRate rate, SerialPort.DataBits data, SerialPort.StopBits stop, SerialPort.Parity parity, SerialPort.FlowControl flow)
          Open the specified port, defining all options
 
Method Summary
 void close()
          Close the serial port, and all input streams
 void finalize()
           
 SerialPort.BaudRate getBaudRate()
          Get the baud rate of the serial port.
 SerialPort.DataBits getDataBits()
          Get the number of data bits.
 SerialPort.FlowControl getFlowControl()
          Get the flow control for the serial port.
 java.io.InputStream getInputStream()
          Get the input stream to be used to read from this SerialPort.
static int getMajorNativeVersion()
          Get the major version of the native code.
static int getMajorVersion()
          Get the major version of this library.
static int getMinorNativeVersion()
          Get the minor version of the native code.
static int getMinorVersion()
          Get the minor version of this library.
 java.io.OutputStream getOutputStream()
          Get the output stream used to write data to this device.
 SerialPort.Parity getParity()
          Get the parity of the serial port.
 java.lang.String getPortName()
          Get the name of the serial port that this object represents.
 SerialLineState getSerialLineState()
          Get the serial line state for the specified serial port.
static java.lang.String[] getSerialPorts()
           Get an array of all the serial ports on the system.
 SerialPort.StopBits getStopBits()
          Get the number of stop bits.
 boolean isClosed()
          See if the port has been closed already.
 void setBaudRate(SerialPort.BaudRate rate)
          Set the Baud Rate for this port.
 void setDataSize(SerialPort.DataBits data)
          Set the data bits size, after the port has been opened.
 void setFlowControl(SerialPort.FlowControl flow)
          Set the flow control for the serial port
 void setParity(SerialPort.Parity parity)
          Set the parity of the serial port, after the port has been opened.
 void setSerialChangeListener(SerialChangeListener listen)
          Set the listener which will get events when there is activity on the serial port.
 void setSerialLineState(SerialLineState state)
          Set the serial line state to the parameters given.
 void setStopBits(SerialPort.StopBits stop)
          Set the stop bits of the serial port, after the port has been opened.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SerialPort

public SerialPort(java.lang.String portName)
           throws NoSuchPortException,
                  NotASerialPortException
Open the specified port, 9600 baud, 8 data bits, 1 stop bit, no parity, no flow control

Parameters:
portName - The name of the port to open
Throws:
NoSuchPortException - If this port does not exist
NotASerialPortException - If the specified port is not a serial port

SerialPort

public SerialPort(java.lang.String portName,
                  boolean keepSettings)
           throws NoSuchPortException,
                  NotASerialPortException
Open up a serial port, but allow the user to keep the current settings of the serial port.

Parameters:
portName - The port to open
keepSettings - If true, will simply open the serial port without setting anything. If false, this method acts the same as SerialPort( String portName )
Throws:
NoSuchPortException - If the port does not exist
NotASerialPortException - If the port is not in fact a serial port

SerialPort

public SerialPort(java.lang.String portName,
                  SerialPort.BaudRate rate)
           throws NoSuchPortException,
                  NotASerialPortException
Open the specified port, no flow control

Parameters:
portName - The name of the port to open
rate - The Baud Rate to open this port at
Throws:
NoSuchPortException - If this port does not exist
NotASerialPortException - If the specified port is not a serial port

SerialPort

public SerialPort(java.lang.String portName,
                  SerialPort.BaudRate rate,
                  SerialPort.DataBits data)
           throws NoSuchPortException,
                  NotASerialPortException
Open the specified port, no flow control

Parameters:
portName - The name of the port to open
rate - The Baud Rate to open this port at
data - The number of data bits
Throws:
NoSuchPortException - If this port does not exist
NotASerialPortException - If the specified port is not a serial port

SerialPort

public SerialPort(java.lang.String portName,
                  SerialPort.BaudRate rate,
                  SerialPort.DataBits data,
                  SerialPort.StopBits stop)
           throws NoSuchPortException,
                  NotASerialPortException
Open the specified port, no parity or flow control

Parameters:
portName - The name of the port to open
rate - The Baud Rate to open this port at
data - The number of data bits
stop - The number of stop bits
Throws:
NoSuchPortException - If this port does not exist
NotASerialPortException - If the specified port is not a serial port

SerialPort

public SerialPort(java.lang.String portName,
                  SerialPort.BaudRate rate,
                  SerialPort.DataBits data,
                  SerialPort.StopBits stop,
                  SerialPort.Parity parity)
           throws NoSuchPortException,
                  NotASerialPortException
Open the specified port, no flow control

Parameters:
portName - The name of the port to open
rate - The Baud Rate to open this port at
data - The number of data bits
stop - The number of stop bits
parity - The parity of the line
Throws:
NoSuchPortException - If this port does not exist
NotASerialPortException - If the specified port is not a serial port

SerialPort

public SerialPort(java.lang.String portName,
                  SerialPort.BaudRate rate,
                  SerialPort.DataBits data,
                  SerialPort.StopBits stop,
                  SerialPort.Parity parity,
                  SerialPort.FlowControl flow)
           throws NoSuchPortException,
                  NotASerialPortException
Open the specified port, defining all options

Parameters:
portName - The name of the port to open
rate - The Buad Rate to open this port at
data - The number of data bits
stop - The number of stop bits
parity - The parity of the line
flow - The flow control of the line
Throws:
NoSuchPortException - If this port does not exist
NotASerialPortException - If the specified port is not a serial port
Method Detail

setBaudRate

public void setBaudRate(SerialPort.BaudRate rate)
Set the Baud Rate for this port.

Parameters:
rate -

getInputStream

public java.io.InputStream getInputStream()
Get the input stream to be used to read from this SerialPort.

Returns:
The input stream.

getOutputStream

public java.io.OutputStream getOutputStream()
Get the output stream used to write data to this device.

Returns:
The output stream.

close

public void close()
Close the serial port, and all input streams

Specified by:
close in interface java.io.Closeable

isClosed

public boolean isClosed()
See if the port has been closed already.

Returns:

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object

setStopBits

public void setStopBits(SerialPort.StopBits stop)
Set the stop bits of the serial port, after the port has been opened.

Parameters:
stop -

setDataSize

public void setDataSize(SerialPort.DataBits data)
Set the data bits size, after the port has been opened.

Parameters:
data -

setParity

public void setParity(SerialPort.Parity parity)
Set the parity of the serial port, after the port has been opened.

Parameters:
parity -

getSerialLineState

public SerialLineState getSerialLineState()
                                   throws java.io.IOException
Get the serial line state for the specified serial port.

Returns:
Throws:
java.io.IOException

setSerialLineState

public void setSerialLineState(SerialLineState state)
Set the serial line state to the parameters given.

Parameters:
state -

getBaudRate

public SerialPort.BaudRate getBaudRate()
Get the baud rate of the serial port.

Returns:

getDataBits

public SerialPort.DataBits getDataBits()
Get the number of data bits.

Returns:

getStopBits

public SerialPort.StopBits getStopBits()
Get the number of stop bits.

Returns:

getParity

public SerialPort.Parity getParity()
Get the parity of the serial port.

Returns:

getFlowControl

public SerialPort.FlowControl getFlowControl()
Get the flow control for the serial port.

Returns:

setFlowControl

public void setFlowControl(SerialPort.FlowControl flow)
Set the flow control for the serial port

Parameters:
flow -

setSerialChangeListener

public void setSerialChangeListener(SerialChangeListener listen)
Set the listener which will get events when there is activity on the serial port. Note that this activity does NOT include receive and transmit events - this is changes on the lines of the serial port, such as RI, DSR, and DTR. If listen is null, will remove the listener.

Parameters:
listen - The listener which gets events

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getPortName

public java.lang.String getPortName()
Get the name of the serial port that this object represents.

Returns:

getMajorVersion

public static int getMajorVersion()
Get the major version of this library. For example, if this is version 0.2, this returns 0


getMinorVersion

public static int getMinorVersion()
Get the minor version of this library. For example, if this is version 0.2, this returns 2.


getMajorNativeVersion

public static int getMajorNativeVersion()
Get the major version of the native code. This should match up with getMajorVersion(), although this is not guaranteed. For example, if this is version 0.2, this returns 0


getMinorNativeVersion

public static int getMinorNativeVersion()
Get the minor version of the native code. This should match up with getMinorVersion(), although this is not guaranteed. For example, if this is version 0.2, this returns 2.


getSerialPorts

public static java.lang.String[] getSerialPorts()

Get an array of all the serial ports on the system. For example, on Windows this will return { "COM1", "COM3", .... } depending on how many serial devices you have plugged in. On Linux, this returns { "/dev/ttyS0", "/dev/ttyUSB0", "/dev/symlink", ... } It will not resolve symlinks, such that if there is a symlink from /dev/symlink to /dev/ttyUSB0 , they will both show up.

NOTE: this will only return ports that you have permissions to open.

Returns: