Java Reference
In-Depth Information
24.9.1 Javelin demonstration program
The program
SendTempDataFromJavelin
shown below runs in the Javelin
evaluation card and provides temperature readings obtained from a Dallas Semi-
conductor chip DS1620 that is installed on the breadboard. Via the serial line, the
processor receives requests from the
SerialToJavelin
program discussed in
Chapter 23 and transmits back a temperature value. The program creates a
receive
and a
transmit
UART virtual peripheral object to provide two-way serial commu-
nications. Each constructor assigns two I/O pins to the appropriate function and
sets the serial mode for 9600 baud.
The reset signal starts the
main()
routine, which begins by creat-
ing an instance of
TempDataGenJavelin
. Since we might want to use
this program as a template for obtaining other kinds of sensor data, we let
TempDataGenJavelin
extend
DataGenerator
,which is an abstract class
(Javelin does not allow interfaces). Any class that extends
DataGenerator
must override
getData()
with a method that returns an
int
value.
The process in
SendTempDataFromJavelin
then goes into a loop and
immediately invokes
checkPW()
. This method uses a simple protocol that
requires that the request for data first include a short password number. The
checkPW()
method invokes
receiveInt()
,which waits for the requestor to
transmit a 2-byte number that matches the password value. The method receives
2bytes in big-endian format. That is, the most significant byte arrives first and the
least significant byte last. Then an
int
value is made from the 2 bytes (remember
that in the Javelin, the integers are a maximum of 2 bytes long.)
If the password value is valid, then a string confirmation is transmitted to
the requestor over the serial line. Back in the main loop, the data
int
value is
obtained from the
DataGenerator
via
getData()
and then transmitted as
2bytes in big-endian format (since the Java specifications require a big-endian
representation). The process then loops back to the
receiveInt()
method
again and waits for the next data request.
import stamp.core.*;
/**
* This program transmits temperature readings from the DS1620 chip when it
* receives a request over the serial line. The request must include a password
* number at the start. If the password is OK, then a temperature value is
* obtained and transmitted. The
"
raw
"
temperature readings are obtained
* via the TempDataGenJavelin class, which in turn uses the DS1620 class
* provided in the stamp.peripheral.sensor.temperature package. The serial
* communications code uses the stamp.core.Uart virtual peripheral class.
**/
public class
SendTempDataFromJavelin
{
Search WWH ::
Custom Search