| In the previous page we discussed 
              classes CommPort 
              and CommPortIdentifier, 
              which provide information and access for communications ports. Here 
              we look at how to use those classes.  Listing Ports The CommPortIdentifier 
              class javax.comm.acts 
              as both a source of information about ports on a system and also 
              as a descriptor of an individual port. The static method getPortIdentifers() 
              in the class provides a list of CommPortIdentifier 
              objects for each port, serial and parallel, on the platform.   Enumeration 
              portList = CommPortIdentifier.getPortIdentifiers ();   This enumeration lists the instances of CommPortIdentifier, 
              one for each port. The methods of this class provide information 
              about the particular port such as its name and type via getName() 
              and getPortType(), 
              respectively.   
              
                 
                 
                  | PortList |   
                  | import 
                      javax.comm.*;import java.util.*;
 
 /** List all the ports available on the local machine. **/
 public 
                      class PortList {
 public static void main (String 
                      [] args) {
 
 Enumeration port_list 
                      = CommPortIdentifier.getPortIdentifiers ();
 
 while (port_list.hasMoreElements()) 
                      {
 CommPortIdentifier 
                      port_id =
 (CommPortIdentifier)portList.nextElement 
                      ();
 
 if 
                      (portId.getPortType () == CommPortIdentifier.PORT_SERIAL) 
                      {
 System.out.println ("Serial port: " + port_id.getName());
 }
 else if (port_id.getPortType 
                      () ==
 CommPortIdentifier.PORT_PARALLEL) 
                      {
 System.out.println ("Parallel port: " + port_id.getName 
                      ());
 }
 else
 System.out.println 
                      ("Other port: " + port_id.getName ());
 }
 } // main
 } // class PortList
 |   
 We used the constants PORT_SERIAL 
              and PORT_PARALLEL from the CommPortIdentifier 
              class to test for the port type. On a desktop machine with four 
              serial ports and two parallel ports, the output would go as   
              Serial port: 
                COM1Serial port: COM2
 Serial port: COM3
 Serial port: COM4
 Parallel port: LPT1
 Parallel port: LPT2
 Conversely, you can test for the presence of 
              a port with a particular name as follows.   
              
                 
                  | PortTest |   
                  | import 
                    javax.comm.*; import java.util.*;
 
 /** Look for COM# ports on the local machine. **/
 public class PortTest
 {
 public static void main (String 
                    [] args) {
 String port_name;
 int i = 0;
 while (true){
 i++;
 port_name 
                    = "COM" + i;
 try {
 CommPortIdentifier port_id =
 CommPortIdentifier.getPortIdentifier (portName);
 System.out.println ("Port " + port_name + " exists");
       
                      }catch (NoSuchPortException 
                      e) {
 System.out.println 
                      ("No port " + port_name);
 break;
 }
 }
 } // main
 } // class PortTest
 |    The output then might go as   
              Port COM1 existsPort COM2 exists
 Port COM3 exists
 Port COM4 exists
 No port COM5
  
              The following methods in 
              CommPortIdentifier 
              provide information about the status of a port: 
              boolean isCurrentlyOwned 
                () - 
                indicates if another Java application owns the port.String 
                getCurrentOwner () a description 
                of the Java application that owns a port. Unfortunately, these methods only 
              work properly if the port is owned by a Java application and not 
              by a different type of program. 
 Opening Ports
  
              If a port is available you can take exclusive 
                possession of it via one of the two overloaded open() 
                methods and then use the port for reading and writing to the external 
                device connected to the port.  The CommPortIdentifier  method
 CommPort 
                open (String ownerName, int timeout)
 throws 
                PortInUseException
 takes possession of the port 
                and passes it the name of the owning application. The timeout 
                parameter will determine how long in milliseconds the method will 
                block (that is, not return from the method) while waiting for 
                the port to become available.  For systems such as Unix where 
                ports can be assigned a FileDescriptor, this overloaded open method is provided:     
                CommPort 
                open (java.io.FileDescriptor fd)throws 
                UnsupportedCommOperationException
 Below we show an example where ports are 
                opened. If they are owned by some other application, a PortInUseException 
                is caught. If the owner is a Java program, it can be identified 
                by the name given in the open method, otherwise there is no name 
                available.  
                
                   
                    | PortListOpen |   
                    | import 
                        javax.comm.*;import java.util.*;
 
 /** Check each port to see if it is open. **/
 public class PortListOpen {
 
 public static void main (String [] args) {
 
 Enumeration port_list = CommPortIdentifier.getPortIdentifiers 
                        ();
 
 while (port_list.hasMoreElements 
                        ()) {
 // Get the list of 
                        ports
 CommPortIdentifier 
                        port_id =
 (CommPortIdentifier) 
                        port_list.nextElement ();
 
 // Find each ports 
                        type and name
 if (port_id.getPortType 
                        () == CommPortIdentifier.PORT_SERIAL) {
 System.out.println 
                        ("Serial port: " + port_id.getName ());
 
 } else if (port_id.getPortType 
                        () ==
 CommPortIdentifier.PORT_PARALLEL) 
                        {
 System.out.println 
                        ("Parallel port: " + port_id.getName ());
 
 } else
 System.out.println 
                        ("Other port: " + port_id.getName ());
 
 // Attempt to open 
                        it
 try {
 CommPort 
                        port = port_id.open ("PortListOpen",20);
 System.out.println 
                        ("  Opened successfully");
 port.close 
                        ();
 
 }
 catch  (PortInUseException 
                        pe){
 System.out.println 
                        ("  Open failed");
 String 
                        owner_name = port_id.getCurrentOwner ();
 if 
                        (owner_name == null)
 System.out.println 
                        ("  Port Owned by unidentified app");
 else
 // 
                        The owner name not returned correctly unless it is
 // 
                        a Java program.
 System.out.println 
                        ("  " + owner_name);
 }
 }
 } //main
 } // PortListOpen
 |    Output from this application might go as 
                
              Serial 
                port: COM1Opened successfully
 Serial port: COM2
 Opened successfully
 Serial port: COM3
 Open failed
 Port currently not owned
 Serial port: COM4
 Open failed
 Port currently not owned
 Parallel port: LPT1
 Opened successfully
 Parallel port: LPT2
 Open failed
 Port currently not owned
  
              To monitor a port for changes in its ownership, 
                you can implement the CommPortOwnershipListener 
                interface. You must override the method ownerShipChange(int 
                typeChange), which can detect three types of ownership 
                (ownership attained, port now available, ownership requested)  
                changes. Port 
                Communications Procedure
 The steps to communicate over a port go as 
                follows: 
                Invoke  open() 
                  in the port's CommPortIdentifier 
                  instance to obtain a CommPort 
                  object for that port. Invoke the getInputStream() 
                  and  getOutputStream() 
                  in  the CommPort 
                  object to obtain provide the streams for reading and writing 
                  data from and to the port.Carry out the desired I/O with the streams.
Invoke close() 
                  with the CommPort 
                  object, which will release the port for other applications to 
                  use. Other CommPort 
                methods set parameters for the port communications such as the 
                input/output buffer sizes and how long the read operation will 
                wait for data before returning.
 References and Web Resources Most recent update: Dec. 13, 2004
 |