Displaying Port Properties


The PortPropertyPage.java file allows the end user to display and set the properties of the port used to send and receive data. The various properties of the port that the PortPropertyPage.java file displays are port name, baud rate, flow control in, flow control out, data bits, stop bits, and parity bits.

Listing 5-2 shows the PortPropertyPage.java file:

Listing 5-2: The PortPropertyPage.java File

start example
 /*Imports required Comm classes*/ import javax.comm.*; /*Imports required javax.swing package classes.*/ import javax.swing.*; import javax.swing.event.*; /*Imports required java.awt package classes.*/ import java.awt.*; import java.awt.event.*; /*Imports required java.util package classes.*/ import java.util.*; /* class PortPropertyPage - This class is the Port Property dialog window,  Enable the end user to change the property of port. Constructor:    PortPropertyPage-This constructor creates GUI. Methods:    setParity- Sets Parity bit.    setStopbits - Sets Stop bit.    setDatabits - Set Data bit    setBaudRate - Sets Baud rate.    getCancelButton - Returns object of cancel button.    getOkButton -  Returns object of ok button.    getFlowControlOut - Returns flow control out value.    getFlowControlIn - Returns flow control in value.    getParity - Returns Parity bit value.    getDatabits - Returns Data bit value.    getStopbits - Returns Stop bit value.    getBaudRate - Returns Baud rate value.    getCommPortName - Returns  port name.    getCommPort -  Returns object of CommPort class */ class PortPropertyPage extends JFrame   implements ActionListener  {    /*Declare object of Enumeration class.*/    Enumeration listport;    /*Declare object of JButton class.*/    JButton okbutton;    JButton cancelbutton;    /*Declare object of JPanel class.*/    JPanel buttonpanel;    JPanel propertylist;    /*Declare object of JLabel class.*/    JLabel portlistlabel;    JLabel flowlabel;    JLabel paritylabel;    JLabel buadlabel;    JLabel databitlabel;    JLabel flowcontrollabel;    JLabel stoplabel;    /*Declare object of JComboBox class.*/    JComboBox comportcombo;    JComboBox flowcombo;    JComboBox stopcombo;    JComboBox databitcombo;    JComboBox paritycombo;    JComboBox buadcombo;    JComboBox flowcontrolcombo;    /*     Declare object of CommPortIdentifier class.    */    CommPortIdentifier portId;    public PortPropertyPage()    {       /*Set title of main window.*/       super("Port Property Page");       /*       Declare and initialize object of Container class.       */       Container contentpane=getContentPane();       comportcombo=new JComboBox();       listport=CommPortIdentifier.getPortIdentifiers();        while (listport.hasMoreElements())       {          portId=(CommPortIdentifier)listport.nextElement();          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)           {             comportcombo.addItem(portId.getName());          }        }       /*Initialize Object of JComboBox.*/       flowcombo=new JComboBox();       flowcombo.addActionListener(this);       /*       Declare and initialize object of GridBagLayout class       */       GridBagLayout gridbaglayout=new GridBagLayout();       /*       Declare and initialize object of GridBagConstraints class       */       GridBagConstraints gridbagconstraint=new GridBagConstraints();       /*       Initialize object of JPanel class and set its layout as gridbaglayout.       */       propertylist=new JPanel(gridbaglayout);          /*Set background color as white.*/       propertylist.setBackground(Color.white);       /*       Initialize object of JLabel class and gives its name.       */       portlistlabel=new JLabel("Port Name",JLabel.RIGHT);       /*       Initialize object of JLabel class and gives its name.       */       flowlabel=new JLabel("Flow Control In");       /*       Set constraints for gridbagconstraint.       */       gridbagconstraint.fill=GridBagConstraints.HORIZONTAL;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbagconstraint.insets=new Insets(5, 5, 5, 5);       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=0;       gridbagconstraint.weightx=1.0;       gridbagconstraint.weighty=1.0;       gridbaglayout.setConstraints(portlistlabel, gridbagconstraint);       /*Add portlistlabel label to panel.*/       propertylist.add(portlistlabel);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbagconstraint.insets=new Insets(5, 5, 5, 5);       gridbaglayout.setConstraints(comportcombo, gridbagconstraint);       /*Add comportcombo combo box to panel.*/       propertylist.add(comportcombo);       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowlabel, gridbagconstraint);       /*Add flowlabel label to panel.*/       propertylist.add(flowlabel);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowcombo, gridbagconstraint);       /*Add comportcombo combo box to panel.*/       propertylist.add(flowcombo);       /*       Initialize object of JLabel class and gives its name.       */       databitlabel=new JLabel("Data Bits",JLabel.RIGHT);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=2;       gridbagconstraint.anchor=GridBagConstrain ts.CENTER;       gridbaglayout.setConstraints(databitlabel, gridbagconstraint);       /*Add databitlabel label to panel.*/       propertylist.add(databitlabel);       /*Initialize object of JComboBox class.*/       databitcombo=new JComboBox();       databitcombo.addActionListener(this);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=2;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(databitcombo, gridbagconstraint);       /*Add comportcombo combo box to panel.*/       propertylist.add(databitcombo);       /*       Initialize object of JLabel class and gives its name.       */       paritylabel=new JLabel("Parity Bits", JLabel.RIGHT);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=3;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(paritylabel, gridbagconstraint);       /*Add paritylabel label to panel.*/       propertylist.add(paritylabel);       /*Initialize object of JComboBox class.*/       paritycombo=new JComboBox();       paritycombo.addActionListener(this);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=3;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(paritycombo, gridbagconstraint);       /*Add paritycombo combo to panel.*/       propertylist.add(paritycombo);       /*       Initialize object of JLabel class and gives its name.       */       buadlabel=new JLabel("Baud Rate", JLabel.RIGHT);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=2;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(buadlabel, gridbagconstraint);       /*Add buadlabel label to panel.*/       propertylist.add(buadlabel);       /*Initialize object of JComboBox class.*/       buadcombo=new JComboBox();       buadcombo.addActionListener(this);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=3;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(buadcombo, gridbagconstraint);       /*Add buadcombo combo to panel.*/       propertylist.add(buadcombo);       /*       Initialize object of JLabel class and gives its name.       */       flowcontrollabel=new JLabel("Flow Control Out",JLabel.RIGHT);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=2;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowcontrollabel, gridbagconstraint);       /*Add flowcontrollabel label to panel.*/       propertylist.add(flowcontrollabel);       /*Initialize object of JComboBox class.*/       flowcontrolcombo=new JComboBox();       flowcontrolcombo.addActionListener(this);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=3;       gridbagconstraint.gridy=1;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(flowcontrolcombo, gridbagconstraint);       /*Add flowcontrolcombo combo to panel.*/       propertylist.add(flowcontrolcombo);       /*       Initialize object of JLabel class and gives its name.       */       stoplabel=new JLabel("Stop Bit",JLabel.RIGHT);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=2;       gridbagconstraint.gridy=2;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(stoplabel, gridbagconstraint);       /*Add stoplabel label to panel.*/       propertylist.add(stoplabel);       /*Initialize object of JComboBox class.*/       stopcombo=new JComboBox();       stopcombo.addActionListener(this);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.gridx=3;       gridbagconstraint.gridy=2;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(stopcombo, gridbagconstraint);       /*Add stopcombo combo to panel.*/       propertylist.add(stopcombo);       contentpane.add(propertylist);       /* Fill buadcombo combobox. */       buadcombo.addItem("300");       buadcombo.addItem("2400");       buadcombo.addItem("9600");       buadcombo.addItem("14400");       buadcombo.addItem("28800");       buadcombo.addItem("38400");       buadcombo.addItem("57600");       buadcombo.addItem("152000");       buadcombo.setSelectedItem("9600");       /*Fill flowcombo combobox.*/       flowcombo.addItem("None");       flowcombo.addItem("Xon/Xoff In");       flowcombo.addItem("RTS/CTS In");       /*Fill flowcontrolcombo combobox.*/       flowcontrolcombo.addItem("None");       flowcontrolcombo.addItem("Xon/Xoff Out");       flowcontrolcombo.addItem("RTS/CTS Out");       /*Fill stopcombo combobox.*/       stopcombo.addItem("1");       stopcombo.addItem("1.5");       stopcombo.addItem("2");       /*Fill paritycombo combobox.*/       paritycombo.addItem("None");       paritycombo.addItem("Even");       paritycombo.addItem("Odd");       /*Fill databitcombo combobox.*/       databitcombo.addItem("5");       databitcombo.addItem("6");       databitcombo.addItem("7");       databitcombo.addItem("8");       databitcombo.setSelectedItem("8");       /*       Initialize object of JButton and set its label       */       okbutton=new JButton("OK");       /*       Initialize object of JButton and set its label       */       cancelbutton=new JButton("Cancel");       /*Initialize object of JPanel.*/       buttonpanel=new JPanel();       /*       Set buttonpanel  layout as gridbaglayout.       */       buttonpanel.setLayout(gridbaglayout);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.fill = GridBagConstraints.HORIZONTAL;       gridbagconstraint.insets=new Insets(5, 75, 5, 5);       gridbagconstraint.gridx=0;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(okbutton, gridbagconstraint);       /*Add button to panel*/       buttonpanel.add(okbutton);       /*Set constraints for gridbagconstraint.*/       gridbagconstraint.insets=new Insets(5, 5, 5, 75);       gridbagconstraint.gridx=1;       gridbagconstraint.gridy=0;       gridbagconstraint.anchor=GridBagConstraints.CENTER;       gridbaglayout.setConstraints(cancelbutton, gridbagconstraint);       /*Add button to panel*/       buttonpanel.add(cancelbutton);       contentpane.add(buttonpanel,BorderLayout.SOUTH);       addWindowListener(new WindowAdapter()       {          public void windowClosed(WindowEvent we)          {             setVisible(false);          }       });       /*Set frame size.*/       setSize(400,200);    }    public void actionPerformed(ActionEvent e)     {       JComboBox cb = (JComboBox)e.getSource();       String petName = (String)cb.getSelectedItem();    }    /*    getCommPort - Gives object of  CommPort class    Parameters: NA    Return Value: CommPortIdentifier    */    public CommPortIdentifier getCommPort()    {       CommPortIdentifier comp=null;       try       {          comp= javax.comm.CommPortIdentifier.getPortIdentifier((String)comportcombo.getSelectedItem());       }       catch(NoSuchPortException e)       {          JOptionPane.showMessageDialog(null,"open", "Port not exists", JOptionPane.PLAIN_MESSAGE);       }       return comp;    }    /*    getCommPortName - Gives name of selected port.    Parameters: NA    Return Value:String    */    public String getCommPortName()    {       return (String)comportcombo.getSelectedItem();    }       /*    getBaudRate - Gives baud rate.    Parameters: NA    Return Value:int    */    public int getBaudRate()    {       return Integer.parseInt((String)buadcombo.getSelectedItem());    }    /*    getDatabits - Gives Databits.    Parameters: NA    Return Value:int    */    public int getDatabits()    {       return Integer.parseInt((String)databitcombo.getSelectedItem());    }    /*    getStopbits - Gives Stop bit.    Parameters: NA    Return Value:int    */    public int getStopbits()    {       return Integer.parseInt((String)stopcombo.getSelectedItem());    }    /*       getParity - Gives parity bit.       Parameters: NA       Return Value:int    */    public int getParity()    {       String paritybit=(String)paritycombo.getSelectedItem();       if (paritybit.equals("None"))        {          return SerialPort.PARITY_NONE;       }       else if (paritybit.equals("Even"))        {          return SerialPort.PARITY_EVEN;       }       else if (paritybit.equals("Odd"))        {           return SerialPort.PARITY_ODD;       }       else       {             return SerialPort.PARITY_NONE;       }    }    /*    getFlowControlIn - Gives type of flow control.       Parameters: NA       Return Value:int    */    public int getFlowControlIn()    {       String flowcomboin=(String)flowcombo.getSelectedItem();       if (flowcomboin.equals("None"))        {          return SerialPort.FLOWCONTROL_NONE;       }       if (flowcomboin.equals("Xon/Xoff Out"))        {          return SerialPort.FLOWCONTROL_XONXOFF_OUT;       }       if (flowcomboin.equals("Xon/Xoff In"))        {          return SerialPort.FLOWCONTROL_XONXOFF_IN;       }       if (flowcomboin.equals("RTS/CTS In"))        {          return SerialPort.FLOWCONTROL_RTSCTS_IN;       }       if (flowcomboin.equals("RTS/CTS Out"))        {          return SerialPort.FLOWCONTROL_RTSCTS_OUT;       }       return SerialPort.FLOWCONTROL_NONE;    }    /*    getFlowControlOut - Gives type of flow out control.    Parameters: NA    Return Value:int    */    public int getFlowControlOut()    {       String flowcontrol=(String)flowcontrolcombo.getSelectedItem();       if (flowcontrol.equals("None"))        {          return SerialPort.FLOWCONTROL_NONE;       }       if (flowcontrol.equals("Xon/Xoff Out"))        {          return SerialPort.FLOWCONTROL_XONXOFF_OUT;       }       if (flowcontrol.equals("Xon/Xoff In"))        {          return SerialPort.FLOWCONTROL_XONXOFF_IN;       }       if (flowcontrol.equals("RTS/CTS In"))        {          return SerialPort.FLOWCONTROL_RTSCTS_IN;       }       if (flowcontrol.equals("RTS/CTS Out"))        {          return SerialPort.FLOWCONTROL_RTSCTS_OUT;       }       return SerialPort.FLOWCONTROL_NONE;    }    /*    getOkButton - Gives object of JButton class    Parameters: NA    Return Value: JButton    */    public JButton getOkButton()    {       return okbutton;    }    /*    getCancelButton - Gives object of JButton class    Parameters: NA    Return Value: JButton    */    public JButton getCancelButton()    {       return cancelbutton;    }    /*       setBaudRate - Sets value of baud rate.       Parameters: budrate       Return Value: NA    */    public void setBaudRate(int budrate)    {       buadcombo.setSelectedItem(String.valueOf(budrate));    }    /*    setDatabits - Sets value of Data bit.    Parameters: databit    Return Value: NA    */    public void  setDatabits(int databit)    {    databitcombo.setSelectedItem(String.valueOf(databit));    }    /*       setStopbits - Sets value of Stop bit.       Parameters: - stopbit       Return Value: NA    */    public void  setStopbits(int stopbit)    {    stopcombo.setSelectedItem(String.valueOf(stopbit));    }    /*       setParity - Sets value of Parity bit.       Parameters: parity       Return Value: NA    */    public void  setParity(int parity)    {       switch(parity)       {          case SerialPort.PARITY_NONE:          stopcombo.setSelectedItem("None");          break;          case SerialPort.PARITY_EVEN:          stopcombo.setSelectedItem("Even");          break;          case SerialPort.PARITY_ODD:          stopcombo.setSelectedItem("Odd");          break;          default:          stopcombo.setSelectedItem("None");       }    } } 
end example

Download this listing.

The above listing enables the end user to set the properties of the port. Figure 5-4 shows the interface of the PortPropertyPage.java file:

click to expand: this figure shows the interface that displays the properties of the port selected by the end user.
Figure 5-4: The Interface of the PortPropertyPage.java File

The user interface of the PortPropertyPage.java file displays various properties of the port. The PortPropertyPage.java file retrieves and specifies the properties of a port using the following get and set methods:

  • The setParity() and getParity() methods of the PortPropertyPage class specify and retrieve the parity bit value of the port.

  • The setStopbits() and getStopbits() methods of the PortPropertyPage class specify and retrieve the value of the stop bit of the port.

  • The setBaudRate() and getBaudRate() methods of the PortPropertyPage class set and retrieve the baud rate property of the COM port.

  • The setDatabits() and getDatabits() methods of the PortPropertyPage class specify and retrieve the value of the data bit property of the port.

  • The getCancelButton() and getOKButton() methods of the PortPropertyPage class create the objects of the Cancel and OK buttons.

  • The getFlowControlOut() and getFlowControlIn() methods of the PortPropertyPage class retrieve the out and in values of the flow control property.

  • The getCommPorts() method of the PortPropertyPage class retrieves all the COM ports on a computer. The getCommPortName() method returns the name of the COM ports on a computer.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

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