Java gets serial support with the new javax.comm package

Sun's JavaSoft division provides support for RS-232 and parallel devices with standard extensions

1 2 Page 2
Page 2 of 2
    InputStream  input = null;
    OutputStream output;
    SerialPort serialPort = null;
    public GenericSerialReader(String name, int baud, 
                               int timeout) throws Exception {
        CommPortIdentifier portId = 
                     CommPortIdentifier.getPortIdentifier( name );
        serialPort = (SerialPort)portId.openPort 
                     ( "GenericSerialReader", timeout );
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                                       SerialPort.STOPBITS_1, 
                                       SerialPort.PARITY_NONE); //     
        serialPort.setFlowcontrolMode( SerialPort.FLOWCTRL_NONE ); //     
        serialPort.enableRcvThreshold ( 1 );
        serialPort.enableRcvTimeout ( timeout );
        int rosStatus = 0;
        System.out.println("Open device called");
        output = serialPort.getOutputStream();
        input = serialPort.getInputStream();

Writing and reading data

For javax.comm, this is no different than any other read and write method call to the derived output stream.

For write:

    try {
        output.write ( outputArray, 0 , length );

For read:

    try {
        int b = input.read()

Closing the port

Closing the port with javax.comm is no different than with other requests to close a device. This step is very important to javax.comm because it attempts to provide exclusive access. Multiplexing multiple users on a serial line requires a Multiplexor protocol.

    try {
        inout.close();
        output.close();
    } ...

Additional example

A good example also is available when you download the javax.comm API package from the Java Developer Connection (see Resources section).

Interfacing a serial device driver to javax.comm

You just picked up this little computer that runs Java and has serial ports. However, the manufacturer has not provided an implementation. It did give you manuals about some calls you could make via native methods to access the serial port drivers written in C. But how do you interface it to javax.comm so you can use your special hardware device? You guessed it: Implement an interface that interfaces to the native methods.

The file SerialPort.java, which is included with the javax.comm API, expects that there will be other classes that realize the implementation of the abstract methods declared by SerialPort. In our example, we call the implementation SolarisSerial.java. This class requires the following native methods to implement the abstract methods called for in CommPort.java and SerialPort.java:

private native int  openNativePort( ... )
private native void closeNativePort( ... )
private native int read( ... )
private native int write( ... )
private native byte readByte( ... )
private native void writeByte( ... )
private native void setNativeSerialPortParams( ... )
private native int getStatusFlags( ... )
public void setDTR( ... ) 
public native void nativeSetDTR( ... )
public void setRTS( ... ) 
public native void nativeSetRTS( ... )
private native void nativeSendBreak(...)
public native void setRcvFifoTrigger(...)

To support the javax.comm API on a system that currently does not have support, we need to provide implementations for all the methods mentioned above.

Conceptually, we have to provide resource management via open and close; ability to configure and query the port via the set and get methods; and, most importantly, the ability to read and write bytes to the port. In many cases, the above methods are almost one-to-one mappings to the underlying native operating system. The following table lists the functions used on Solaris to implement the native interface layer.

javax.comm native method callUnix OS mappings for javax.comm
private native int openNativePort( ... )Open
private native void closeNativePort( ... )Close
private native int read( ... )Read
private native int write( ... )Write
private native byte readByte( ... )Read
private native void writeByte( ... )Write
private native void setNativeSerialPortParams( ... )Termio/ioctl
private native int getStatusFlags( ... )Termio/ioctl
public void setDTR( ... )Termio/ioctl
public native void nativeSetDTR( ... )Termio/ioctl
public void setRTS( ... )Termio/ioctl
public native void nativeSetRTS( ... )Termio/ioctl
private native void nativeSendBreak(...)Termio/ioctl
public native void setRcvFifoTrigger(...)Termio/ioctl

Conclusion

The javax.comm API provides a modern disciplined approach to serial communications and will move Java into new application spaces, allowing devices like bar code scanners, printers, smart card readers, and hundreds of other serial devices to be connected with ease. The API is easy to use, as demonstrated by the example. It is also easy to port to new hardware platforms. The API has not been tested for high data rate and realtime applications; therefore, developers looking to use the API in those types of environments should perform careful instrumentation with subsequent analysis of the code.

In determining whether the API is suitable for high data rate or mission sensitive applications, look for the following:

  • Characters lost on input
  • Characters lost in output
  • Frequency of flow control
  • Time it takes to deliver an event
  • Character processing times
  • Block processing times

When we first started our series on smart cards, we were lucky if we understood a few native method calls to send bytes to serial devices. We end our smart card series with this article. The software APIs we have been discussing in this series come together from a device point of view. For example, a user developing an application for smart cards can write to some well-defined APIs such as OpenCard Framework or communicate directly using javax.comm -- or alternatively use javax.smartcard, which in turn uses javax.comm.

The javax.comm API facilitates the interfacing of serial and parallel devices to Java. For more information on how javax.comm works with smart cards, look at the iButtons article. Also, we have included several examples in the Resources section to get you started.

Shivaram Mysore is a member of technical staff in JavaSoft's Java Commerce Group. There he designs and develops the client and server side of Java Electronic Commerce Framework. Rinaldo Di Giorgio is a staff engineer in the research department at Sun Microsystems. There he experiments with digital economies.

Learn more about this topic

  • The javax.comm API reference at JavaSoft Developer Connection
    http://java.sun.com/products/javacomm/
  • Excellent discussion on flow control http://www.embedded.com/98/toc9801.htm
  • You can access the RS-232 Serial Communications Tutorial published in PC Communications at http://www.ridgewater.mnscu.edu/classes/dc/io/
  • The book Understanding Data Communications, by Gilbert Held and George E. Friend, provides details on data communications http://www.clbooks.com/sqlnut/SP/search/gtsumt?source=javaworld&isbn=0672309343
  • Serial line analyzers http://www.bb-elec.com/catalog/software/serialte.html
  • Information and Source of OpenBSD Unix. This may be useful to look at how serial, parallel, and other device drivers are written. http://www.OpenBSD.org/
  • The smart card series in JavaWorld magazine began with the "Java smart card primer" http://www.javaworld.com/javaworld/jw-12-1997/jw-12-javadev.html
  • The January issue of JavaWorld ran the second article in the smart card series"Smart cards and the OpenCard Framework" http://www.javaworld.com/javaworld/jw-01-1998/jw-01-javadev.html
  • In JavaWorld's February issue, you can read "Get a jumpstart on the Java Card" http://www.javaworld.com/javaworld/jw-02-1998/jw-02-javadev.html
  • Also in the February issue is "Giving currency to the Java Card API" http://www.javaworld.com/javaworld/jw-02-1998/jw-02-javacard.html
  • For more on Java Card 2.0, see "Understanding Java Card 2.0" in the March issue of JavaWorld http://www.javaworld.com/javaworld/jw-03-1998/jw-03-javadev.html
1 2 Page 2
Page 2 of 2