Java Reference
In-Depth Information
System
.
err
.
println
(
"Usage: CommPortSimple deviceName"
);
System
.
exit
(
1
);
}
new
new
CommPortSimple
(
argv
[
0
]).
holdConversation
();
System
.
exit
(
0
);
}
/* Constructor */
public
public
CommPortSimple
(
String devName
)
throws
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
Enumeration
<
CommPortIdentifier
>
pList
=
CommPortIdentifier
.
getPortIdentifiers
();
// Walk the list, looking for the given name
CommPortIdentifier cpi
=
null
null
;
boolean
boolean
atLeastOneSerialPresent
=
false
false
;
while
while
(
pList
.
hasMoreElements
()) {
CommPortIdentifier c
=
pList
.
nextElement
();
iif
(
c
.
getPortType
() !=
CommPortIdentifier
.
PORT_SERIAL
) {
System
.
err
.
println
(
"Not a serial port: "
+
c
.
getName
());
continue
continue
;
}
iif
(
devName
.
equals
(
c
.
getName
())) {
cpi
=
c
;
break
break
;
// found!
}
atLeastOneSerialPresent
=
true
true
;
System
.
out
.
println
(
"Not matched: "
+
c
.
getName
());
}
iif
(
cpi
==
null
null
) {
System
.
err
.
println
(
"Did not find serial port '"
+
devName
+
"'"
);
iif
(
atLeastOneSerialPresent
)
System
.
err
.
println
(
"Try again with one of the listed names"
);
else
System
.
err
.
println
(
"In fact, I didn't see ANY serial ports!"
);
System
.
exit
(
1
);
}
thePort
=
cpi
.
open
(
"JavaCook DataComm"
,
TIMEOUTSECONDS
*
1000
);
SerialPort myPort
= (
SerialPort
)
thePort
;
// set up the serial port