Introduction


Peripheral devices are usually external to the computer.[1] Printers, mice, video cameras, scanners, data/fax modems, plotters, robots, telephones, light switches, weather gauges, Personal Digital Assistants (PDAs), and many others exist "out there," beyond the confines of your desktop or server machine. We need a way to reach out to them.

[1] Conveniently ignoring things like "internal modem cards" on desktop machines!

The Java Communications API not only gives us that but cleverly unifies the programming model for dealing with a range of external devices. It supports both serial (RS232/434, COM, or tty) and parallel ( printer, LPT) ports. We'll cover this in more detail later, but briefly, serial ports are used for modems and occasionally printers, and parallel ports are used for printers and sometimes (in the PC world) for Zip drives and other peripherals.

Before USB (Universal Serial Bus) came along, it seemed that parallel ports would dominate for such peripherals, as manufacturers were starting to make video cameras, scanners, and the like. Now, however, USB has become the main attachment mode for such devices. A Java Standards Request (JSR) is in the works to build a standard API for accessing USB devices under Java, but it has not progressed to the release stage. A reference implementation can be downloaded from http://sourceforge.net/projects/javax-usb. A competing Java API for USB can be found at http://jusb.sourceforge.net. Since the JSR is not completed, I do not document its use here in this edition.

This chapter aims to teach you the principles of controlling many kinds of devices in a machine-independent way using the Java Communications API, which is in package javax.comm .

I'll start this chapter by showing you how to get a list of available ports and how to control simple serial devices like modems. Such details as baud rate, parity, and word size are attended to before we can write commands to the modem, read the results, and establish communications. We'll move on to parallel (printer) ports and then look at how to transfer data synchronously (using read/write calls directly) and asynchronously (using Java listeners). Then we build a simple phone dialer that can call a friend's voice phone for you a simple phone controller, if you will. The discussion ends with a serial-port printer/plotter driver.

The Communications API

The Communications API is centered around the abstract class CommPort and its two subclasses, SerialPort and ParallelPort, which describe two types of ports found on desktop computers. CommPort represents a general model of communications and has general methods like getInputStream( ) and getOutputStream( ) that allow you to use the information from Chapter 10 to communicate with the device on that port.

However, the constructors for these classes are intentionally nonpublic. Rather than constructing them, you instead use the static factory method CommPortIdentifier.getPortIdentifiers( ) to get a list of ports, 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 subclass 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 ParallelPort 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 receive 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.)

About the Code Examples in This Chapter

Java Communication is a standard extension, so it is not a required part of the Java API, which in turn means that your vendor probably didn't ship it. You may need to download the Java Communications API from Sun's Java web site, http://java.sun.com, or from your system vendor's web site, and install it. If your platform or vendor doesn't ship it, you may need to find, modify, compile, and install some C code. And, naturally enough, to run some of the examples, you will need additional peripheral devices beyond those normally provided with a desktop computer. Batteries and peripheral devices are not included with the purchase of this book.

See Also

Elliotte Rusty Harold's book Java I/O contains a chapter that discusses the Communications API in considerable detail, as well as some background issues such as baud rate that we take for granted here. Rusty also discusses some details that I have glossed over, such as the ability to set receive timeouts and buffer sizes.

This book is about portable Java. If you want the gory, low-level details of setting device registers on a 16451 UART on an ISA or PCI PC, you'll have to look elsewhere; several books cover these topics. If you really need the hardware details for I/O ports on other platforms such as Sun Workstations and the Palm Computing Platform, consult either the vendor's documentation and/or the available open source operating systems that run on that platform.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

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