Home : Course Map : Chapter 23 :
Port Communications Classes
JavaTech
Course Map
Chapter 23

Access Platform
System Properties
  Demo 1
Run Ext. Programs
  Demo 2
Java Comm API
Port Classes
Port Operations
  Demo 3   Demo 4   Demo 5
Serial Port IO
Demo: Serial Port
  Code Listings

    Demo 6
Parallel Port
Port I/O Apps
Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

We discuss here the primary classes in javax.comm that you need for input/output over serial and parallel ports. We divide these classes into the port classes that give information about and access to the ports on a platform, the port event classes and interfaces that allow for responding to data and state changes on ports, and exceptions.

Port Classes

The abstract class CommPort holds methods to control and perform I/O over a specific port. These methods will be overrriden by subclasses for either the serial and parallel port and for the particular platform. The class CommPortIdentifier provides information about the ports on the system and can create an instance of a Commport subclass for a given port.

CommPort
This abstract class for port representation includes various methods such as getInputStream() and getOutputStream() to transmit and receive data over the port.

The abstract subclasses of CommPort include:

SerialPort - This class works with RS-232 ports. Methods in this class provide for control, monitoring, transmission and reception. You can set parameters such as the baud rate, parity, numbers of stop bits and data bits and can choose flow control protocols. Individual control pins, such as the DTR (Data Terminal Ready) and CTS (Clear to Send), can be set.

ParallelPort - This class works with the 8-bit IEEE-1284 parallel (or printer) port. Methods allow for setting the port mode such as the extended and enhanced modes. Also, the transmission can be suspended and restarted (useful for pausing and restarting a printout) and several status messages can be read such as paper out and printer busy.

Non-abstract subclasses of SerialPort and ParallelPort will be created in the javax.comm for a particular platform with factory method in CommPortIdentifier discussed next.. You do not create instances of these classes directly from their constructors.

CommPortIdentifier
An instance of this class, discussed further in the next section, provides information about a specific port, such as the port's name and type, but it does not set ownership of the port or allow for any control or I/O over the port. Instead, you use the open() method of an instance of CommPortIdentifier for a specific port to obtain a instance of a CommPort subclass for that port unless it is owned by some other program. You do not obtain instances of either CommPort subclasses or CommPortIdentifier directly from their constructors but with factory methods in CommPortIdentifier.


Port Event Classes and Interfaces

A program can invoke a read method on a stream and wait for the method to return (unblock) when data arrives. A different approach is to read from a port's input stream only when the JVM detects data on the port and it fires a port event to a listener. The following classes and interfaces allow for such an event-listener arrangement for port I/O.

SerialPortEvent & interface SerialPortEventListener
A class that implements the interface SerialPortEventListener will override the method

    void serialEvent (SerialPortEvent ev)

The class should also add an instance of itself to a serial port's listener list so that it will receive serial events (that is, invoke the serialEvent() method above) when particular conditions occur (see the various notify methods below).

The SerialPort class includes the method

  void addEventListener (SerialPortEventListener listener)

for adding listeners. If more than one listener attempts to add itself, this method will throw java.util.TooManyListenersException.

The SerialPort class also includes the following methods

  • notifyOnBreakInterrupt (boolean enable)
  • notifyOnCarrierDetect (boolean enable)
  • notifyOnCTS (boolean enable)
  • notifyOnDataAvailable (boolean enable)
  • notifyOnDSR (boolean enable)
  • notifyOnFramingError (boolean enable)
  • notifyOnOutputEmpty (boolean enable)
  • notifyOnOverrunError (boolean enable)
  • notifyOnParityError (boolean enable)
  • notifyOnRingIndicator (boolean enable)

that inform the serial port when it should fire an event. So, for example, if mySerialPort is a reference to an instance of SerialPort, you can use the following

   mySerialPort.notifyOnDataAvailable (true);

to obtain events when data arrives on the port. If more than one of the notify conditions is activate, you can find out which condition led to the firing of an event by using the SerialPortEvent method

   int getEventType ()

which returns an integer value. This value that can be compared to one of the constants in the class, such as DATA_AVAILABLE, to determine what caused the event.

ParallelPortEvent & interface ParallelPortEventListener
A class that implements the interface ParallelPortEventListener will override the method

    void parallelPortEvent (ParallelPortEvent ev)

The class should also add an instance of itself to a parallel port's listener list so that it will receive parallel events (that is, invoke the parallelEvent() method above) when particular conditions occur (see the various notify methods below).

The ParallelPort class includes the method

  void addEventListener (ParallelPortEventListener listener)

for adding listeners. If more than one listener attempts to add itself, this method will throw java.util.TooManyListenersException.

The ParallelPort class also includes the following methods

  • notifyOnBuffer (boolean notify)
  • notifyOnError (boolean notify)

that inform the paralle port that it should fire an event if the output buffer becomes empty or if there is an error on the port. THe ParallelPortEvent class also has the method

   int getEventType ()

to indicate which of the conditions led to the firing of the event.

Interface CommPortOwnershipListener
This interface allows for a port owner to respond in a given manner whenever another object attempts to open the same port. For example, if a class implements this interface then it must override the method

   void ownershipChange (int type)

When it obtains the CommPortIdentifier for a particular port, the class will invoke the method

  void addPortOwnershipListener (CommPortOwnershipListener lstnr)

to add itself to the CommPortOwnership listener list for that port.

If portOwnerA opens a port, and then portOwnerB tries to open the same port, the ownershipChange() method for portOwnerA will be invoked. The integer parameter will indicate that a request to open the port has been attempted and the portOwnerA can then decide whether to close the port and allow portOwnerB to take ownership of the port or not.

Port Exceptions

Methods in the above classes can throw the following exceptions :

NoSuchPortException - thrown by the getPortIdentifier() methods in CommPortIdentifier when the port ID isn't recognised.

PortInUseException - thrown by the open(String appname, int timeout) method in CommPortIdentifier when attempting to open a port already opened.

UnsupportedCommOperationException - thrown by the open(java.io.FileDescriptor fd) method in CommPortIdentifier when attempting this on a platform that doesn't provide for a FileDescriptor object to represent a port.

Also thrown by setMode(int) in ParallelPort and setSerialPortParams(...) in SerialPort when the mode and parameters, resp., are not allowed for a particular port.

The methods enableReceiveThreshold(int thresh), enableReceiveTimeout(int rcvTimeout), and enableReceiveFraming(int framingByte) in CommPort also throw this exception.

 

References and Web Resources

 

Latest update: Dec. 13, 2004

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.