Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Alternate Data Transfer Methods

At first glance it might appear that the IR port on the RCX is the only way to transfer data, but actually the RCX is capable of sending and receiving data using other media. Using a Morse code setup with a combination of light sensor and light actuator, it is also possible to receive and send data across distances (Figure 11–12a). Data transmission isn't just limited to the light spectrum; a motor and a touch sensor could also interpret physical movement as data (Figure 11–12b). In fact, any compatible actuator–sensor combination can transmit data. For example, a heating coil and a temperature sensor could conceivably transmit and receive data using the differences in temperature as the data medium (although this would be very inefficient). We could even eliminate the need for actuators and sensors completely and connect an output port from one RCX directly to a sensor port on another RCX, with the sensor port interpreting changes in the voltage as a signal.

Figure 11–12 Alternate data media.

To accomplish any data transfer we must first be able to encode the signals into numbers. As most programmers are familiar with, a byte is made up of eight bits (binary digits). A bit has two states represented by an on–off signal (usually described as 0 or 1). These eight bits create binary numbers, which can in turn be converted to decimal numbers. A sequence of bits are ordered, with the first bit having a value of 1, the next as 2, then 4, then 8, and so on (Table 11–1). Thus the sequence 00000001 has a value of 1, the sequence 10000001 has a value of 129, and the sequence 11010110 has a value of 214 (2 + 4 + 16 + 64 + 128). Using these eight values, any number between 0 and 255 can be represented. The key to the whole encoding scheme is to be able to transmit light signals (or others) by representing 0 or 1.

Table 11-1 Bit Values



















Now that we know how to encode bits we just need to figure out how to send a bit. As we know, a bit is just a signal, either on or off. Transmission of bits is generally done by synchronizing the clocks on the sender and receiver. They agree on a transmission speed, and one single bit is sent for each time interval. Given the preceding mechanical example using a touch sensor, if a transmission speed of one bit per second is agreed on, then for each second during transmission the touch sensor is either pressed or released. If you recall modem speeds, a typical phone modem can handle 33,600 bits per second. In our touch sensor example the baud rate is 1 bit per second—not very fast.

The IR port does a fine job of transmitting data, and visible light data transmission takes up additional ports on the RCX, so obviously if someone attempts this on the RCX they are doing it for a learning experience. For this reason I'm not going to include code examples of this in action because it would take away from the experience of learning data transmission. But it is interesting to note how this could fit into the Java API. Assuming a programmer has figured out how to transmit and receive a single byte, it's possible to take this up a notch and incorporate it into streams.

First an InputStream and OutputStream need to be created. This is simple: Just extend the abstract InputStream and OutputStream classes in the java.io package. InputStream has a simple abstract method called read() that reads the next byte of data from the stream. Conversely, OutputStream has an abstract method called write() that writes a byte to the stream. Using the visible light example previously described, the classes might be called Light-InputStream and LightOutputStream. Once these are created, it's as easy as using these classes in the constructor for DataInputStream and DataOutput-Stream to transmit integers, floating-point numbers, and other data types.


Both read() and write() use int data types to transmit a byte because a byte is a signed value, meaning it can only store numbers with a range of –128 to +127. However, the methods read() and write() need unsigned byte values of 0 to 255, hence an int value is used instead.

  • + Share This
  • 🔖 Save To Your Account