23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

standard messages on other wires. Each return wire corresponded to a particular message, like<br />

"Out of paper" or "Printer busy." Modern parallel ports allow full, bidirectional<br />

communication.<br />

The javax.comm.ParallelPort class is a concrete subclass of javax.comm.CommPort that<br />

provides various methods and constants useful for working with parallel ports and devices.<br />

The main purposes of the class are to allow the programmer to inspect, adjust, and monitor<br />

changes in the settings of the parallel port. Simple input and output are accomplished with the<br />

methods of the superclass, CommPort. ParallelPort has a single public constructor, but that<br />

shouldn't be used by applications. Instead, you should simply call the open() method of a<br />

CommPortIdentifier that maps to the port you want to communicate with, then cast it to<br />

ParallelPort:<br />

CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier("LPT2");<br />

if (cpi.getType() == CommPortIdentifier.PORT_PARALLEL) {<br />

try {<br />

ParallelPort printer = (ParallelPort) cpi.open ();<br />

}<br />

catch (PortInUseException e) {}<br />

}<br />

Methods in the ParallelPort class fall into roughly four categories:<br />

• Methods that adjust the port mode<br />

• Methods to control the port<br />

• Methods to inspect the state of the port<br />

• Methods that listen for changes in the state of the port<br />

17.5.1 Parallel Port Modes<br />

Like most other computer hardware, parallel ports have evolved over the last two decades.<br />

Modern parallel ports support bidirectional communication and other features never<br />

envisioned for the original parallel port that was only supposed to send data to a daisy-wheel<br />

printer. However, older peripherals may not work with newer parallel ports, so they can, if<br />

necessary, be downgraded to any of several various compatibility modes. All of these are<br />

available as named int constants in the javax.comm.ParallelPort class:<br />

ParallelPort.LPT_MODE_ANY // Use the most advanced mode possible.<br />

ParallelPort.LPT_MODE_SPP // Original lineprinter mode. Unidirectional<br />

// transfer from PC to printer. Most<br />

compatible<br />

// with older peripherals.<br />

ParallelPort.LPT_MODE_PS2 // Byte at a time, bidirectional mode as<br />

// introduced in the IBM PS/2 family.<br />

ParallelPort.LPT_MODE_EPP // Extended parallel port.<br />

ParallelPort.LPT_MODE_ECP // Enhanced capabilities port.<br />

ParallelPort.LPT_MODE_NIBBLE // Nibble (4 bits, half a byte) at a time<br />

mode,<br />

// bidirectional, used by some Hewlett Packard<br />

// equipment.<br />

The mode the parallel port uses is returned by the getMode() method and set by passing the<br />

appropriate constant to the setMode() method:<br />

453

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!