Java Reference
In-Depth Information
static void getPorts ()
{
// First get the list of ports on this platform
Enumeration port
-
list =
CommPortIdentifier.getPortIdentifiers ();
// Scan through the list and get the serial ports
while (port
-
list.hasMoreElements ()) {
CommPortIdentifier port
-
id = (CommPortIdentifier)
port
-
list.nextElement ();
if (port
-
id.getPortType () ==
CommPortIdentifier.PORT
-
SERIAL) {
fNumSerialPorts
--
++;
fSerialTable
--
.put (port
-
id.getName (), port
-
id);
}
}
} // getPorts
When the user selects a serial port from the menu, the
actionPerformed()
method is invoked. The following code shows the actions taken to open the port:
...The
actionPerformed()
method in the class
SerialToJavelin
...
public void actionPerformed (ActionEvent e)
{
...
tests for other commands...
// Scan the serial ports names to look for a match
// to the menu items.
Enumeration enum
-
ports = fSerialTable
--
.keys ();
while (enum
-
ports.hasMoreElements ()) {
String port
-
name = (String)enum
-
ports.nextElement ();
if (command.equals (port
-
name)) {
fSelectedPortID
--
=
(CommPortIdentifier) fSerialTable
--
.get
(port
-
name);
if (fCurrentPort
--
!= null) fCurrentPort
--
.close ();
// Open port. Allow for 20 seconds block.
try {
fCurrentPort
--
=
(SerialPort) fSelectedPortID
--
.open (
Search WWH ::
Custom Search