Java Reference
In-Depth Information
myPort
.
setSerialPortParams
(
BAUD
,
SerialPort
.
DATABITS_8
,
SerialPort
.
STOPBITS_1
,
SerialPort
.
PARITY_NONE
);
// Get the input and output streams
is
=
new
new
BufferedReader
(
new
new
InputStreamReader
(
thePort
.
getInputStream
()));
os
=
new
new
PrintStream
(
thePort
.
getOutputStream
(),
true
true
);
}
/** Hold a conversation - in this case a *very* simple one. */
protected
protected
void
void
holdConversation
()
throws
throws
IOException
{
System
.
out
.
println
(
"Ready to read and write port."
);
os
.
println
(
HELLO
);
String response
=
is
.
readLine
();
System
.
out
.
printf
(
"I said %s, and the other end replied %s%n"
,
HELLO
,
response
);
// Finally, clean up.
iif
(
is
!=
null
null
)
is
.
close
();
iif
(
os
!=
null
null
)
os
.
close
();
}
}
See Also
The online source includes
javacomm/CommPortOpen.java
, which provides a framework for
using Java Communications API; it does pretty much all the work except for actually reading
or writing. You subclass
CommPortOpen
and write a
converse()
method to read and write on
the
InputStream
and
OutputStream
, which are exposed as the inherited fields
is
and
os
,
respectively.
Table 10-5
lists additional programs in the online source package.