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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

serial port events: when the runtime detects a change in state at a monitored parallel port, it<br />

fires a parallel port event to the registered parallel port listener. A parallel port event signals<br />

some sort of activity on the parallel port, either an error or an empty output buffer. Parallel<br />

port events are represented by instances of the javax.comm.ParallelPortEvent class, a<br />

subclass of java.util.EventObject:<br />

public class ParallelPortEvent extends EventObject<br />

The ParallelPortEvent class has a single public constructor, but you shouldn't use it.<br />

Instead, the runtime creates and fires parallel port events when it wants to indicate a change<br />

on one of the standard parallel port pins.<br />

public ParallelPortEvent(ParallelPort src, int type, boolean oldValue,<br />

boolean newValue)<br />

The javax.comm.ParallelPortEvent class declares these three public methods:<br />

public int getEventType()<br />

public boolean getNewValue()<br />

public boolean getOldValue()<br />

The getEventType() method returns a named constant from the ParallelPortEvent class<br />

that specifies what caused the event to be fired. There are two possibilities: an error and an<br />

empty output buffer. Each parallel port event has an eventType field; its value should be one<br />

of these mnemonic constants:<br />

ParallelPortEvent.PAR_EV_ERROR // An error occurred on the port.<br />

ParallelPortEvent.PAR_EV_BUFFER // The output buffer is empty.<br />

These represent a change from one state to another, from on to off or off to on. Therefore,<br />

there are also getNewValue() and getOldValue() methods to tell you the state of the pin<br />

before and after the event:<br />

public boolean getNewValue()<br />

public boolean getOldValue()<br />

17.5.5 Parallel Port Event Listeners<br />

There are three steps to respond to parallel port events:<br />

1. Implement the ParallelPortEventListener interface.<br />

2. Register your ParallelPortEventListener object with the ParallelPort object<br />

representing the parallel port you want to monitor.<br />

3. Tell the parallel port the types of events you want to be notified of.<br />

Steps 1 and 2 should be familiar from <strong>Java</strong>Beans and the <strong>Java</strong> 1.1 AWT. Step 3 is used to<br />

avoid getting notifications of events you're not interested in and is similar to the same step for<br />

serial port events.<br />

455

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

Saved successfully!

Ooh no, something went wrong!