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 />

off in the modem so that you don't see duplicates of everything you type. (Even my password<br />

originally appeared on the screen in clear text. I replaced it with asterisks manually.) And no<br />

effort at all is made to perform terminal emulation of any sort. Furthermore, there's no way to<br />

exit the program and close the port. Terminating it with a Ctrl-C forces abnormal execution<br />

that fails to release control of the port. Nonetheless, it's amazing just how quick and easy it is<br />

to write a program that communicates with a simple serial port device. Communicating with a<br />

basic daisy-wheel printer would be no harder.<br />

17.3.2 Port Properties<br />

The javax.comm.CommPort class has a number of driver-dependent methods for adjusting the<br />

properties of the port. These properties are mostly generic characteristics, like buffer size, that<br />

may be implemented in software. More specific properties of a particular type of port, like the<br />

baud rate of a serial port or the mode of the parallel port, must be set using a more specific<br />

subclass, like javax.comm.SerialPort or javax.comm.ParallelPort.<br />

The five generic properties are receive threshold, time-out value, receive framing byte, input<br />

buffer size, and output buffer size. Four of these properties—receive threshold, receive timeout,<br />

receive framing and input buffer size—determine exactly how and when the input stream<br />

blocks. The receive threshold specifies the number of bytes that must be available before a<br />

call to read() returns. The receive time-out specifies the number of milliseconds that must<br />

pass before a call to read() returns. The input buffer size specifies how large a buffer will be<br />

provided for the serial port. If the buffer fills up, the read() method returns.<br />

For instance, if the receive threshold is set to 5, read() won't return until at least 5 bytes are<br />

available. If the receive timeout is set to 10 milliseconds, read() will wait 10 milliseconds<br />

before returning. However, if data becomes available before 10 milliseconds are up, read()<br />

returns immediately. For example, if the receive threshold is set to 5 bytes and the receive<br />

time-out is set to 10 milliseconds, then read() will wait until either 10 milliseconds pass or 5<br />

bytes are available before returning. If the input buffer size is set and the receive threshold is<br />

set, the lower of the two values must be reached before read() will return. Finally, if receive<br />

framing is enabled, all reads return immediately, regardless of the other values. Table 17.1<br />

summarizes.<br />

Receive<br />

Threshold<br />

Receive<br />

Time-out<br />

Table 17.1. When Does read() Return?<br />

Receive<br />

Framing<br />

Input<br />

Buffer Size<br />

read( ) Returns When<br />

disabled disabled disabled b bytes Returns when any data is available.<br />

n bytes disabled disabled b bytes<br />

Returns when either n or b bytes are available,<br />

whichever is less.<br />

disabled t ms disabled b bytes<br />

Returns after t milliseconds or when any data is<br />

available.<br />

n bytes t ms disabled b bytes<br />

Returns after t milliseconds or when either n or b<br />

bytes are available, whichever is less.<br />

disabled disabled enabled b bytes Returns immediately.<br />

n bytes disabled enabled b bytes Returns immediately.<br />

disabled t ms enabled b bytes Returns immediately.<br />

n bytes t ms enabled b bytes Returns immediately.<br />

440

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

Saved successfully!

Ooh no, something went wrong!