Home : Course Map : Chapter 23 :
Serial Port IO
JavaTech
Course Map
Chapter 23

Access Platform
System Properties
  Demo 1
Run Ext. Programs
  Demo 2
Java Comm API
Port Classes
Port Operations
  Demo 3   Demo 4   Demo 5
Serial Port IO
Demo: Serial Port
  Code Listings

    Demo 6
Parallel Port
Port I/O Apps
Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The javax.comm serial port classes assume the system has one or more ports following the RS-232 (or EIA232) standard. The standard originated in the 1960s and dealt with a computer talking to a display terminal. This explains some of the names describing the 9 pins for the DB9 connector in Table 22.1. (See the references for a description of the less common 25-pin DB25 connector.)

Serial ports send and receive one bit at a time using a positive voltage (between 3 and 25V) to indicate a 0 bit and a negative voltage (between -3 to -25V) to indicate a 1 bit. The duration of a voltage depends inversely on the baud rate.

An asynchronous serial protocol (not included in the RS-232 standard) is required to determine how to decode the bits into bytes. The standard protocol groups the bits into a standard data unit (SDU) consisting typically of 7 data bits (for ASCII) or 8 data bits. To indicate where a SDU begins, a start bit value of value 0 is sent. To indicate the end of a SDU, the group ends with one or two stop bits each of value 1. To combat noise and bit errors, a parity bit is usually included. For even parity the bit value is 1 if the number of one bit in the SDU is even.  For odd parity the bit value is 1 if the number of one bit is odd. So, depending on the protocol settings, the total number of bits sent for an SDU can vary from 9 bits (7 data bits, 1 stop bit and no parity) up to 12 bits (8 data bits, 2 stop bits and a parity bit.)

The SerialPort class provides a single method to set the baud rate, number of data bits, number of stop bits, and the parity:

 void SetSerialPortParams (int baud, int dataBits, int stopBits, int parity)

If a port does not support any of the values passed in the parameters, the method will throw the UnsupportedCommOperationException. 

The SerialPort class includes a set of constants to use for these parameters such as

DATABITS_7
DATABITS_8
STOPBITS_1
STOPBITS_2
PARITY_NONE
PARITY_EVEN
PARITY_ODD

The two devices connected via the serial line need flow control settings to determine who is sending and who is receiving and when to switch between the two states. The XON/XOFF and RTS/CTS are the two primary protocols for this.  The former is a software protocol while the latter is implemented in hardware. The methods

  void setFlowControlMode (int protocol)
  int getFlowControlMode ()

provide for setting and getting the flow control mode. The protocol value sets both input and output protocols with a bitwise AND of the constants in the SerialPort class:

FLOWCONTROL_NONE
FLOWCONTROL_XONXOFF_IN
FLOWCONTROL_XONXOFF_OUT
FLOWCONTROL_RTSCTS_IN
FLOWCONTROL_RTSCTS_OUT

The set method will throw the UnsupportedCommOperationException if the protocol value is invalid.

Pin

Name

Abbr.

Direction

Function

1

Carrier Detect

CD

In

Modem & destination modem connected

2

Receive Data

RD

In

Data from modem

3

Transmit Data

TD

Out

Data to modem

4

Data Terminal Ready

DTR

Out

Computer ready to send & receive

6

Data Set Ready

DSR

In

Modem ready to send  & receive

7

Request to Send

RTS

Out

Computer waiting to send

8

Clear to Send

CTS

In

Modem ready to receive

9

Ring Indicator

RI

In

Modem says phone is ringing

Table: Pin assignments for the DB9 serial connector on a computer.
The pin functions are for the case of a computer connected to a modem.


The table shows the pin assignments for an RS-232 9-pin connector on a computer. The Function column indicates the purpose of each pin for the case of a computer connected to a modem. The serial line sends and receives only one bit at a time but it uses separate lines for transmission and reception and it uses six other lines (not counting the ground line) for setting up the protocol for the communications.

Serial Line Connections

The javax.comm class CommPort provides methods to access the six control lines. For the two output control wires, DTR and RTS, there are methods both to set the line and to find the current setting:

void setDTR (boolean val)
boolean isDTR ()
void setRTS (Boolean val)
boolean isRTS ()

For the other four input control lines, there are  methods to find their current state:

boolean isCTS ()
boolean isDSR ()
boolean isRI ()
boolean isCD ()

For connecting to devices other than modems, it is often unnecessary to use all of these  control lines. In the serial line demonstration program below, for example, we only the CTS and RTS (plus the ground line) lines are active.  

Note: When you start to setup serial lines, you will find that the cables and connectors vary according to whether you connect a computer to a device like a modem or connect a computer to another computer or a device to another device.

Table 22.1 shows the pins for the connector at the computer. There is a one to one correspondence in the numbering of the pins on the computer's male connector and the modem's female connector. That is, pin 2 on the computer connects to receptor number 2 on the modem's connector, pin 3 connects to receptor 3 on the modem, and so forth. This obviously cannot hold for connecting two computers that both have male connectors as in Table 22.1. Instead a so-called null modem cable is required. It connects pin 2 (RD) on computer A to pin 3 (TD) on computer B, connects pin 3 (TD) on computer A to pin 2 (TD), and so forth.

See Ref. Strangio for explanations of the different types of connectors and cables.


References and Web Resources

 

Latest update: Dec. 13, 2004

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.