Recipe 12.1 Choosing a Port


Problem

You need to know what ports are available on a given computer.

Solution

Use CommPortIdentifier.getPortIdentifiers( ) to return the list of ports.

Discussion

Many kinds of computers are out there. It's unlikely that you'd find yourself running on a desktop computer with no serial ports, but you might find that there is only one and it's already in use by another program. Or you might want a parallel port and find that the computer has only serial ports. This program shows you how to use the static CommPortIdentifier method getPortIdentifiers( ). This gives you an Enumeration (Recipe 7.4) of the serial and parallel ports available on your system. My routine populate( ) processes this list and loads it into a pair of JComboBoxes (graphical choosers; see Recipe 14.1), one for serial ports and one for parallel (a third, unknown, covers future expansion of the API). The routine makeGUI creates the JComboBoxes and arranges to notify us when the user picks one from either of the lists. The name of the selected port is displayed at the bottom of the window. So that you won't have to know much about it to use it, there are public methods. getSelectedName( ) returns the name of the last port chosen by either JComboBox; getSelectedIdentifier( ) returns an object called a CommPortIdentifier corresponding to the selected port name. Figure 12-1 shows the Port Chooser in action.

Figure 12-1. The Communications Port Chooser in action
figs/jcb2_1201.gif


Example 12-1 shows the code.

Example 12-1. PortChooser.java
import java.io.*; import javax.comm.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /**  * Choose a port, any port!  *  * Java Communications is a "standard extension" and must be downloaded  * and installed separately from the JDK before you can even compile this   * program.  */ public class PortChooser extends JDialog implements ItemListener {     /** A mapping from names to CommPortIdentifiers. */     protected HashMap map = new HashMap( );     /** The name of the choice the user made. */     protected String selectedPortName;     /** The CommPortIdentifier the user chose. */     protected CommPortIdentifier selectedPortIdentifier;     /** The JComboBox for serial ports */     protected JComboBox serialPortsChoice;     /** The JComboBox for parallel ports */     protected JComboBox parallelPortsChoice;     /** The JComboBox for anything else */     protected JComboBox other;     /** The SerialPort object */     protected SerialPort ttya;     /** To display the chosen */     protected JLabel choice;     /** Padding in the GUI */     protected final int PAD = 5;     /** This will be called from either of the JComboBoxes when the      * user selects any given item.      */     public void itemStateChanged(ItemEvent e) {         // Get the name         selectedPortName = (String)((JComboBox)e.getSource( )).getSelectedItem( );         // Get the given CommPortIdentifier         selectedPortIdentifier = (CommPortIdentifier)map.get(selectedPortName);         // Display the name.         choice.setText(selectedPortName);     }     /* The public "getter" to retrieve the chosen port by name. */     public String getSelectedName( ) {         return selectedPortName;     }     /* The public "getter" to retrieve the selection by CommPortIdentifier. */     public CommPortIdentifier getSelectedIdentifier( ) {         return selectedPortIdentifier;     }     /** A test program to show up this chooser. */     public static void main(String[] ap) {         PortChooser c = new PortChooser(null);         c.setVisible(true);    // blocking wait         System.out.println("You chose " + c.getSelectedName( ) +             " (known by " + c.getSelectedIdentifier( ) + ").");         System.exit(0);     }     /** Construct a PortChooser --make the GUI and populate the ComboBoxes.      */     public PortChooser(JFrame parent) {         super(parent, "Port Chooser", true);         makeGUI( );         populate( );         finishGUI( );     }     /** Build the GUI. You can ignore this for now if you have not      * yet worked through the GUI chapter. Your mileage may vary.      */     protected void makeGUI( ) {         // CONTENTS OF THIS METHOD OMITTED -- SEE ONLINE SOURCE VERSION     }     /** Populate the ComboBoxes by asking the Java Communications API      * what ports it has.  Since the initial information comes from      * a Properties file, it may not exactly reflect your hardware.      */     protected void populate( ) {         // get list of ports available on this particular computer,         // by calling static method in CommPortIdentifier.         Enumeration pList = CommPortIdentifier.getPortIdentifiers( );         // Process the list, putting serial and parallel into ComboBoxes         while (pList.hasMoreElements( )) {             CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement( );             // System.out.println("Port " + cpi.getName( ));             map.put(cpi.getName( ), cpi);             if (cpi.getPortType( ) == CommPortIdentifier.PORT_SERIAL) {                 serialPortsChoice.setEnabled(true);                 serialPortsChoice.addItem(cpi.getName( ));             } else if (cpi.getPortType( ) == CommPortIdentifier.PORT_PARALLEL) {                 parallelPortsChoice.setEnabled(true);                 parallelPortsChoice.addItem(cpi.getName( ));             } else {                 other.setEnabled(true);                 other.addItem(cpi.getName( ));             }         }         serialPortsChoice.setSelectedIndex(-1);         parallelPortsChoice.setSelectedIndex(-1);     }     protected void finishGUI( ) {         serialPortsChoice.addItemListener(this);         parallelPortsChoice.addItemListener(this);         other.addItemListener(this);         pack( );         addWindowListener(new WindowCloser(this, true));     } }



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net