Java Reference
In-Depth Information
Before the USB (Universal Serial Bus) came along, it seemed that parallel ports would dom-
inate for such peripherals because manufacturers were starting to make video cameras, scan-
ners, and the like that worked over parallel ports. Now, however, USB has become the main
attachment mode for such devices. A Java Standards Request (JSR-80) documents a standard
API for accessing USB devices under Java, but it has not been widely used. You can down-
load a reference implementation from Source Forge . You can find a competing Java API for
USB at jUSB .
Sadly, the Communications API has been languishing in recent years, and it is even hard to
find copies of the official implementation. The download for this topic includes “comm.jar,”
but does not have the native code libraries, so the examples will compile but not function.
There is an alternative implementation called RxTx that may be usable for some of these ex-
amples.
Oracle has indicated that it plans to bring the “device access API” from Java ME into the
Java SE world in 2014, and adapt it for Java SE conventions. This will replace the
javax.comm API and hopefully do so as a full-fledged part of Java. Recipes will be added
here when this API is fleshed out enough that it's possible to write about it.
The Communications API in a Nutshell
The Communications API is centered around the abstract class CommPort and its two sub-
classes, SerialPort and ParallelPort , which describe two types of ports found on desktop
computers. The constructors for these classes are intentionally nonpublic; you use the static
factory method CommPortIdentifier.getPortIdentifiers() to get a list of ports, choose
(or let the user choose) a port from this list, and call this CommPortIdentifier 's open()
method to receive a CommPort object. You cast the CommPort reference to a nonabstract sub-
class representing a particular type of communications device. At present, the subclass must
be either SerialPort or ParallelPort .
Each of these subclasses has some methods that apply only to that type. For example, the
SerialPort class has a method to set baud rate, parity, and the like, while the Paral-
lelPort class has methods for setting the “port mode” to original PC mode, bidirectional
mode, etc.
Both subclasses also have methods that allow you to use the standard Java event model to re-
ceive notification of events such as data available for reading and output buffer empty. You
can also receive notification of type-specific events such as ring indicator for a serial port
and out-of-paper for a parallel port. (Parallel ports were originally for printers and still use
their terminology in a few places.)
Search WWH ::




Custom Search