Java Reference
In-Depth Information
device like a modem or to another computer. Table 23.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
the table. 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
Atopin 2 (TD), and so forth. See the topic by Strangio for the specifications and
diagrams of various types of connectors and cables [7].
23.4.7 Serial port demo
The demonstration application uses a serial connection to obtain temperature
readings from a device connected to the local platform via a serial port. The
program uses the classes and methods discussed above for communications over
the serial port. This program can provide a template for obtaining data from any
device connected to a serial line. The program can be adapted to work with the
client/server system discussed in Chapter 15 and so provide a complete system
for accessing a remote device over the Internet.
The device is in fact a Java hardware processor board that we discuss in
Chapter 24. We have programmed it to follow a simple protocol for sending data.
This protocol requires that the application on the desktop first send a “password”
(actually a 2-byte numerical value) to the device. If this is accepted, the device
program then sends a 2-byte integer data value for the temperature in units of
0.5 degrees Celsius.
The desktop (or any platform that can handle J2SE and javax.comm )
program consists of two primary classes:
GetJavelinData - this class is in charge of sending and receiving data over the
serial port with the device (a Javelin Stamp board discussed in Chapter 24). This class
implements the Runnable interface and its run() method contains a loop that first
sends the password and then reads the value sent to it from the sensor. It converts each
byte pair received into a 4-byte int value using the techniques discussed in Chapter 9
to convert byte array values to primitive types. Before repeating the data read operation,
the loop pauses for a period whose length is passed via the constructor parameter.
SerialToJavelin - this class provides a graphical user interface with a dropdown
menu on the menu bar that lists the serial ports on the platform. Selecting one of these
causes the program to attempt to open the port. If it succeeds, it passes the port's
SerialPort object to an instance of GetJavelinData . Hitting the Go button
invokes the start() method in the GetJavelinData object, which begins its data
taking loop. Hitting Stop invokes stop() in that object. A text area displays the tem-
perature values which are printed from the GetJavelinData loop via print methods
of the Outputable interface. A file menu provides for saving this data to file.
Search WWH ::




Custom Search