Tecnico

Solo disponible en BuenasTareas
  • Páginas : 20 (4788 palabras )
  • Descarga(s) : 4
  • Publicado : 12 de julio de 2010
Leer documento completo
Vista previa del texto
11
Programming Serial and Parallel Ports
11.0. Introduction
11:

Chapter 11

Peripheral devices are usually external to the computer.* Printers, mice, video cameras, scanners, data/fax modems, plotters, robots, telephones, light switches, weather gauges, Palm Computing Platform devices, and many others exist “out there,” beyond the confines of your desktop or server machine. We need a wayto reach out to them. 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 printersand 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. One can imagine that future releases of Java Communications mightexpand the structure to include USB support (Sun has admitted that this is a possibility) and maybe other bus-like devices. This chapter† aims to teach you the principles of controlling these many kinds of devices in a machine-independent way using the Java Communications API, which is in package javax.comm.
* Conveniently ignoring things like “internal modem cards” on desktop machines! † Thischapter was originally going to be a book. Ironic, since my first book for O’Reilly was originally going to be a chapter. So it goes.

306

11.0.

INTRODUCTION

307

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 tothe 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-portprinter/plotter driver.

The Communications API
The Communications API is centered around the abstract class CommPort and its two subclasses, SerialPort and ParallelPort, which describe the two main 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 informationfrom Chapter 9 to communicate with the device on that port. However, the constructors for these classes are intentionally non-public. 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 castthe CommPort object to a non-abstract subclass representing a particular 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” tooriginal 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, output buffer empty, and type-specific events such as ring indicator for a serial port and out-of-paper for a parallel port—as we’ll see, the parallel ports were originally for printers, and still use...
tracking img