The javax.comm Package


JCA provides the javax.comm package, which is a Java extension Application Programming Interface (API). The standard Java Development Kit (JDK) does not provide this API. The javax.comm package provides classes and interfaces that can communicate with serial ports and parallel ports but cannot communicate with other ports, such as USB.

Classes

JCA defines three levels of classes: high-level, low-level, and driver-level classes. High-level classes manage the ownership of and access to communication ports. Low-level classes provide an interface to physical ports. Driver-level classes provide an interface between low-level classes and operation systems.

The javax.comm package contains various classes that you can use to develop applications to interact with the communication ports on a computer. The classes in the javax.comm package are:

  • CommPort: Is an abstract class that describes the communication ports available on a particular computer. The CommPort class defines various methods that you can use to control input/output operations on ports. The most commonly used methods in the CommPort class are:

    • close(): Closes a communication port.

    • disableRecieveFraming(): Is an abstract method that disables receive framing.

    • disableReceiveTimeout(): Is an abstract method that disables receive timeout.

    • enableRecieveFraming(): Is an abstract method that enables receive framing. The enableRecieveFraming () method is driver-dependent. The method allows receive framing only if driver support enables receive framing.

    • enableReceiveTimeout(): Is an abstract method that enables receive time out. The enableReceiveTimeOut() method is driver-dependent.

    • getInputBufferSize(): Is an abstract method that returns the size of the input buffer. The getInputBufferSize() returns an integer value.

    • getOutputBufferSize(): Is an abstract method that returns the size of the output buffer. The getOutputBufferSize() returns an integer value.

    • getInputStream(): Is an abstract method that returns the input stream. The input stream is the only way to receive data from the communication port. The getInputStream() method returns an InputStream type value.

    • getName(): Retrieves a string that represents the name of the communication port.

    • toString(): Returns the string representation of a communication port.

    • getOutputStream(): Returns an output stream. This stream works as a mediator to send data to the communication port.

    • isReceiveTimeoutEnabled(): Is an abstract method that verifies whether or not receive timeout is enabled. The isReceiveTimeoutEnabled() method returns a Boolean value.

    • isReceiveFramingEnabled(): Is an abstract method that verifies whether or not receive framing is enabled. The isReceiveFramingEnabled() method returns a Boolean value.

    • setOutputBufferSize(): Is an abstract method that sets the size of the output buffer.

    • setInputBufferSize(): Is an abstract method that sets the size of the input buffer.


  • CommPortIdentifier: Controls access and communication between a Java application and ports. The CommPortIdentifier class also defines methods for various operations, such as determining available communication ports, opening communication ports, and determining the ownership of ports. The commonly used methods in the CommPortIdentifier class are:

    • addPortName(): Adds the name of a port to the port name list.

    • addPortOwnershipListener(): Registers a Java application to receive event notification from the port when a change occurs in the ownership of the port. The three types of events are PORT_OWNED, PORT_UNOWNED, and PORT_OWNERSHIP_REQUESTED. The PORT_OWNED event specifies that a port has an owner. The PORT_UNOWNED event specifies that the port does not have an owner. The PORT_OWNERSHIP_REQUESTED event specifies that a Java application wants to relinquish ownership of the port.

    • getCurrentOwner(): Obtains the name of the current owner of a port.

    • getName(): Acquires the name of the port.

    • getPortIdentifier(): Obtains an enumeration object that contains a list of the available ports. The enumeration object contains objects of type CommPortIdentifier object.

    • getPortType(): Returns an integer value that represents the port type.

    • open(): Opens a port for communication, if the port is free. The open() method acquires exclusive ownership of the port if there is no other owner of the requested port.

    • isCurrentlyOwned(): Checks the ownership of a port, whether it is true or false. If the requested port is owned by a device, the method returns true; and if the requested port is not owned, the method returns false.

    • removePortOwnershipListener(): Unregisters the CommPortOwnershipListener listener.


  • ParallelPort: Is a subclass of the CommPort class. The ParallelPort class describes a low-level interface to a parallel port. The most commonly used methods in the ParallelPort class are:

    • addEventListener(): Is an abstract method that registers an event listener for a parallel port. You can add only one event listener on one parallel port.

    • removeEventListener(): Is an abstract method that unregisters an event listener for a parallel port.

    • getOutputBufferFree(): Is an abstract method that retrieves the number of bytes available in the output buffer. The getOutputBufferFree() method returns an integer value.

    • isPaperOut(): Is an abstract method that verifies if the port indicates a printer is in the Out of Paper state. The isPaperOut() method returns a Boolean value. For example, the method returns true if the printer's tray does not contain paper; else, the method returns false.

    • isPrinterBusy(): Is an abstract method that verifies if the port indicates the Printer Busy state. The isPrinterBusy() method returns a Boolean value. For example, the method returns true if the printer is busy; else, the method returns false.

    • isPrinterSelected(): Is an abstract method that verifies if the port indicates a printer in the selected state. The isPrinterSelected() method returns a Boolean value. The method returns true if the printer is in the selected state; else, the method returns false.

    • isPrinterTimedOut(): Is an abstract method that verifies if the port indicates that a printer has timed out. The isPrinterTimedOut() method returns a Boolean value. The method returns true if the printer has timed out; else, the method returns false.

    • notifyOnBuffer(): Is an abstract method that conveys that the communication application should be notified when the output buffer is empty.

    • notifyOnError(): Is an abstract method that conveys that the communication application should be notified when an error occurs on a port.

    • restart(): Is an abstract method that resumes output after an error occurs.

    • getMode(): Is an abstract method that retrieves the currently configured mode of the port.

    • setMode(): Is an abstract method that sets the mode of a printer port. The different printer modes are LPT_MODE_SPP, LPT_MODE_PS2, LPT_MODE_EPP, and LPT_MODE_ECP.

    • suspend(): Is an abstract method that suspends output.

    • removeEventListener(): Is an abstract method that unregisters an event listener.

    • isPrinterError(): Is an abstract method that returns a Boolean value to specify if an error occurred on a printer.


  • ParallelPortEvent: Defines the events of the parallel port. The ParallelPortEvent class defines some static variables, such as PAR_EV_ERROR and PAR_EV_BUFFER, which you can use within the methods defined in the class. The PAR_EV_ERROR variable indicates that an error has occurred at a port. The PAR_EV_BUFFER variable indicates that the port output buffer is empty. The methods defined in the ParallelPortEvent class are:

    • getEventType(): Returns an integer value that represents the type of event. The getEventType() method may return the PAR_EV_ERROR or PAR_EV_BUFFER static variable.

    • getNewValue(): Returns the new value of the changed state of a parallel port. The getNewValue() method returns a Boolean value.

    • getOldValue(): Returns the old Boolean value of the state change of a parallel port.


  • SerialPort: Is a subclass of CommPort that provides access to a different serial port on a computer. In addition, the SerialPort class also defines methods that allow port to open and close and data to be sent and received. The SerialPort class also defines various static integer variables that the methods defined in the SerialPort class use. The most commonly used methods in the SerialPort class are:

    • getBaudRate(): Returns an integer value that represents the current baud rate of a serial port. The getBaudRate() method is an abstract method.

    • getDataBits(): Returns an integer value that represents the current number of data bits configured on a serial port. The getDataBits() method is an abstract method. The return integer value can be equal to the static variables DATABITS_5, DATABITS_6, DATABITS_7, or DATABITS_8. Table 1-1 describes these variables.

    • getFlowControlMode(): Returns an integer value that represents the current flow control mode configured on a serial port. The getFlowControlMode() method is an abstract method.

    • getParity(): Returns an integer value that represents the current parity setting that is configured. The getParity() method is an abstract method.

    • getStopBits(): Returns an integer value that represents the currently defined stop bits. The getStopBits() method is an abstract method. The returned integer can be equal to the value of the STOPBITS_1, STOPBITS_2, or STOPBITS_3 static variables. Table 1-1 describes the static variables.

    • isCD(): Is an abstract method that retrieves the state of the Carrier Detect (CR) bit in Universal Asynchronous Receiver/Transmitter (UART). The isCD() method returns a Boolean value.

    • isCTS(): Is an abstract method that retrieves the state of the Clear To Send (CTS) bit in UART. The isCTS() method returns a Boolean value.

    • isDSR(): Is an abstract method that retrieves the state of the Data Set Ready (DSR) bit in UART. The isDSR() method returns a Boolean value.

    • isDTR(): Is an abstract method that retrieves the state of the Data Terminal Ready (DTR) bit in UART. The isDTR() method returns a Boolean value.

    • isRI(): Is an abstract method that retrieves the state of the Ring Indicator (RI) bit in UART. The isRI() method returns a Boolean value.

    • isRTS(): Is an abstract method that retrieves the state of the Request To Send (RTS) bit in UART. The isRTS() method returns a Boolean value.

    • notifyOnBreakInterrupt(): Is an abstract method that conveys that the communication application must be receive notification from the port when there is a break interrupt on the line.

    • notifyOnCarrierDetect(): Is an abstract method that conveys that the communication application must receive event notification from the port when the Carrier Detect (CD) bit changes.

    • notifyOnCTS(): Is an abstract method that conveys that the communication application must receive event notification from the port when the Clear To Send (CTS) bit changes.

    • notifyOnDataAvailable(): Is an abstract method that conveys that the communication application must receive event notification from the port when input data is available.

    • notifyOnDSR(): Is an abstract method that conveys that the communication application must receive event notification from the port when the Data Set Ready (DSR) bit changes.

    • notifyOnFramingError(): Is an abstract method that conveys that the communication application must receive event notification from the port when a framing error occurs.

    • notifyOnOutputEmpty(): Is an abstract method that conveys that the communication application must receive event empty.

    • notifyOnOverrunError(): Is an abstract method that conveys that the communication application must receive event notification from the port when an overrun error occurs.

    • notifyOnParityError(): Is an abstract method that conveys that it must receive event notification from the port when a parity bit error occurs.

    • notifyOnRingIndicator(): Is an abstract method that conveys that the communication application must receive event notification from the port when the RI bit changes.

    • removeEventListener(): Is an abstract method that unregisters the registered event.

    • setDTR(): Is an abstract method that sets the Data Terminal ready (DTR) bit in URAT.

    • setFlowControlMode(): Is an abstract method that retrieves the current flow control mode.

    • setRTS(): Is an abstract method that sets the DTR bit in URAT.


    The SerialPort class also defines some static variables that the methods defined in the SerialPort class use, as described in Table 1-1:

    Table 1-1: Static Variables of the SerialPort Class

    Variable Name

    Description

    DATABITS_5

    Represents 5-data bit format.

    DATABITS_6

    Represents 6-data bit format.

    DATABITS_7

    Represents 7-data bit format.

    DATABITS_8

    Represents 8-data bit format.

    STOPBITS_1

    Represents that the number of STOP bits is 1.

    STOPBITS_2

    Represents that the number of STOP bits is 2.

    STOPBITS_1_5

    Represents that the number of STOP bits is 1-1/2.

    PARITY_NONE

    Represents that there is no parity bit.

    PARITY_ODD

    Represents an ODD parity scheme.

    PARITY_EVEN

    Represents an EVEN parity scheme.

    PARITY_MARK

    Represents a MARK parity scheme.

    PARITY_SPACE

    Represents a SPACE parity scheme.

    FLOWCONTROL_NONE

    Represents that flow control is off.

    FLOWCONTROL_RTSCTS_IN

    Represents RTC/CTS flow control on input.

    FLOWCONTROL_RTSCTS_OUT

    Represents RTC/CTS flow control on output.

    FLOWCONTROL_XONXOFF_IN

    Represents XON/XOFF flow control on input.

    FLOWCONTROL_XONXOFF_OUT

    Represents XON/XOFF flow control on output.

  • SerialPortEvent: Defines the events of the serial port. The various methods defined in the SerialPortEvent class are:

    • getEventType(): Returns an integer in the form of a static variable that represents the type of event, such as Break Interrupt (BR), Framing Error (FE), Overrun Error (OR), OUTPUT_BUFFER_EMPTY, DATA_AVAILABLE, or Parity Error (PE). For example, the DATA_AVAILABLE variable indicates that data is available on the serial port.

    • getNewValue(): Returns a new value for the state change of a serial port. The getNewValue() method returns a Boolean value.

    • getOldValue(): Returns the old value of the state change of a serial port. The getOldValue() method returns a Boolean value.


Interfaces

The javax.comm package provides interfaces that you can use to develop applications. You use the interfaces to communicate with the communication ports. The various interfaces defined in the javax.comm package are:

  • CommDriver

  • CommPortOwnershipListener

  • ParallelPortEventListener

  • SerialPortEventListener

The CommDriver interface is a part of a loadable device driver interface. The methods defined in the CommDriver interface are:

  • initialize(): Registers the port name with the CommPortIdentifier class. In addition, the initialize() method also loads any required native libraries. The initialize() method is an abstract method.

  • getCommPort(): Returns an object that extends the SerialPort or ParallelPort class. The open() method of the CommPortIdentifier class invokes the getCommPort() method.

The CommPortOwnershipListener interface broadcasts various port ownership events across an application. Opening a port triggers and broadcasts the CommPortOwnershipListener event of type PORT_OWNED. Closing a port triggers and broadcasts the CommPortOwnershipListener event of type PORT_UNOWNED.

The ParallelPortEventListener interface broadcasts the events of parallel ports using the parallelEvent() method. The parallelEvent() is the only method defined in the ParallelPortEventListener interface.

The SerialPortEventListener interface broadcasts the events of serial ports using the serialEvent() method. The serialEvent() is the only method defined in the SerialPortEventListener interface.

Exception Handling

The javax.comm package provides three exception classes that you can use to develop communication applications. An exception class catches the different exceptions that a communication application throws when you use the javax.comm package. Table 1-2 describes various exception classes:

Table 1-2: Exception Classes

Exceptions Classes

Description

NoSuchPortException

Catches the exception that the Java application throws when the application is unable to find the specified port.

PortInUseException

Catches the exception that the Java application throws if the specified port is in use.

UnsupportedCommOperationException

Catches the exception that the Java application throws when the port driver does not allow you to perform a specified operation on the port.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

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