Pub. Date:
Elsevier Science
Programming Lego Mindstorms with Java

Programming Lego Mindstorms with Java


Current price is , Original price is $31.95. You

Temporarily Out of Stock Online

Please check back later for updated availability.

This item is available online through Marketplace sellers.


Lego robots! The first book that teaches you to program Lego Mindstorms using Java Lego Mindstorms are a new generation of Lego Robots that can be manipulated using microcomputers, light and touch sensors, an infrared transmitter and CD-ROMs. Since Lego launched Lego Mindstorms in late 1998 sales have skyrocketed - with no sign of slowing down. Mindstorms have captured the imagination of adults and children alike, creating a subculture of Mindstorm enthusiasts around the world. The kits are now a staple part of engineering and computer science classes at many high profile Universities.

Up until very recently, the only languages available to program Lego Mindstorms were NQC, pbForth, and legOS. This is the first book detailing how to program Lego Mindstorms using the newly released Java Virtual Machine for Lego Mindstorm programming. Programming Lego Mindstorms provides readers with all of the information they need to construct and program Lego Mindstorm Robots.

  • The first book available on how to program Lego Mindstorms with Java
  • The perfect gift for parents and kids alike!

Product Details

ISBN-13: 9781928994558
Publisher: Elsevier Science
Publication date: 06/08/2002
Edition description: BK&CD-ROM
Pages: 448
Product dimensions: 7.38(w) x 9.29(h) x 1.22(d)

About the Author

Dario Laverde is currently a developer at ActionWorld, Inc. where he participated in the design and implementation of the PlayLink Internet Gaming System. Previous to Action World, he worked at InterWorld, Inc. where he developed a chat system (server and clients). Before that, he was a senior software and electrical engineer at Loral Electronics Systems for a number of years.

Read an Excerpt

Overview of the Java Communications Extension API (JCE API) The Java Communications API 2.0 was released in 1998 as the standard communication mechanism for serial (RS232/434) and parallel (IEEE 1284) ports using pure Java. That is, standard Java code will run across all compliant platforms, using the official interface. The current implementations are found in the following locations:

*Windows and Solaris
*Mac OS For the most part, communication is performed through standard input and output streams in Java. These streams are provided from the underlying ports through a common interface. All the ports extend a common abstract class (called CommPort) that provides the basic representation of a given port. This allows for standard mechanisms to open, read/write, and close ports. This is one of the two primary functions provided by the Java Communications Extension (JCE) API. The other is port management and ownership, which is described below.

Similar to other Java APIs, the JCE API makes use of the observer pattern; event notifications are triggered upon a change of port ownership, as are specific data notifications (as with the case of serial ports). The Java Communications Extension library primarily consists of one package: javax.comm.*. This library contains the classes, interfaces and exceptions listed in Table 2.1.

 Classes                         Interfaces                                                      Exceptions CommPort                        CommDriver                                                      NoSuchPortException CommPortIdentifier      CommPortOwnershipListener                       PortInUseException ParallelPort            ParallelPortEventListner                        UnsupportedCommOperationException ParallelPortEvent       SerialPortEventListner 



We will go into the architecture of the Java Communications API in greater detail later; but what stands out from the list at first glance are the event classes and event listener classes. This allows the framework to handle asynchronous notifications of not just specific serial port and parallel port events, but also of port ownership changes and updates. Hence one would know when a specific port is available, or one could wait until it is available or closed.The classes of note above (other than the encapsulation of serial and parallel objects of course) are the CommPort and CommPortIdentifier classes.

The CommPort is an abstract class that serves as the base class for all ports, enabling you to manage and handle all the ports in the same way. It contains the minimal and basic common methods that one would expect from all communication ports, except for one: open() is handled by the CommPortIdentifier. The CommPort’s notable methods are getOutputStream(), getInputStream(), and close().

This leads us to the CommPortIdentifier class through which we will create, manage and identify all ports. Think of this class as a port factory that allows you to discover and enumerate all the ports in the system and handle the ownership of those ports.

The CommDriver Java interface gives the developers of new communication port drivers the ability to add to the Java Communications API in a consistent manner. We will also examine how one would use this to add a new custom port.

Lastly, as listed in Table 2.1, there are custom Java exceptions in this API that allows for notification of errors specific to this framework that might occur: if a given port is already in use, if a requested port does not exist, and if a specific operation on a given port is not supported. One would think that all of CommPort’s abstract methods should be implemented by all ports, as they are a general list. This exception allows for any future type of port to be added easily, as the CommPort contract is not too rigid. For example, a future port XYZ, might not be expected to implement the abstract enableReceiveTimeout() method in the CommPort class if the underlying hardware cannot support this kind of behavior. Basically one would first test to see if a function is supported before trying to use it. If the unsupported exception is thrown, it can then be caught and handled gracefully.

Understanding the JCE Framework

As stated, the Java Communications API provides a clean and simple framework for allowing access to both serial and parallel ports from 100 percent Java code. This permits you to write applications ranging from modem software to custom printer drivers, or provide access from Java to a multitude of devices that interface with serial and parallel ports. These include devices ranging from mp3 players or video capture devices to home automation devices (such as X10 devices). Table 2.2 shows some of the features and limitations of the Java Communications API that will be addressed in the following sections.

Table 2.2 Features and Limitations of the Java Communications API Features                        Limitations Port Discovery and Enumeration  Limited to serial and parallel ports. Port Ownership Management       Difficult for third party implementations of new device protocols to be added to the core API. Asynchronous event based I/O    Initial configuration could be made easier. Clean encapsulation of underlying native ports for several platforms including Windows, Solaris, Linux and Macintosh 

Port Discovery and Enumeration

In this section we will illustrate how to enumerate all ports available on a system. This is the first step with all applications that use the Java Communications API. The following classes and methods highlight the enumeration process:

*CommPort Provides input and output streams to the ports and is the base class to SerialPort and ParallelPort.
*SerialPort Extends CommPort to represent a RS232 serial port
*CommPortIdentifier Provides control of ports in a system. Serves as both a port identifier object and a port ID. Note this distinction which at first glance can be confusing. A port identifier object is not a port object but identifies and interfaces with an associated port object.
*public static Enumeration getPortIdentifiers() This static method will provide a complete identifier list of all ports (serial and parallel) on a system.
*public String getName() The name of this port identifier
*public int getPortType() The type of port this port identifier represents, namely one of the following:

*CommPortIdentifier.PORT_PARALLEL The code shown in Figure 2.1 illustrates how the API is used to list all the available serial ports for the entire system (computer). This code can be found on the companion CD with this book. Figure 2.1

import javax.comm.CommPortIdentifier; import javax.comm.SerialPort; import javax.comm.PortInUseException; import java.util.Enumeration; import java.util.Vector; public class TestEnumeration {     public static void main(String args[]) {         Vector portslist = TestEnumeration.getAvailableSerialPorts();         System.out.println("found "+portslist.size()+" open ports");     }     public static Vector getAvailableSerialPorts() {         CommPortIdentifier pId=null;         SerialPort sPort=null;         Enumeration pList=null;         boolean foundport=false;         pList = CommPortIdentifier.getPortIdentifiers();         String port=null;         Vector ports=new Vector();         if(!pList.hasMoreElements()) {             System.err.print("warning: no ports found - ");             System.err.println("make sure file is found");             return ports;         }         while (pList.hasMoreElements()) {             pId = (CommPortIdentifier) pList.nextElement();             if (pId.getPortType() == CommPortIdentifier.PORT_SERIAL) {                 foundport=true;                 try {                 } catch (PortInUseException e) {                 } finally {                 }             }         }         return ports;     } Sample Output: >java TestEnumeration  COM1 is open  COM2 is open  COM3 is closed  COM4 is closed  found 2 open ports

Starting with main(), the call to the getAvailableSerialPorts() method will return a vector of strings of the available serial ports’ names. In this simple example, we get the vector and print its length, telling us how many serial ports there are.

As we step into the getAvailableSerialPorts() method, we find the usual first step for enumerating all the ports:

CommPortIdentifier.getPortIdentifiers() is called.

If no ports are found, a warning message is displayed; this is unusual and usually indicates a configuration problem, namely that the file is in the wrong place.

We then loop through the enumeration, checking the port type with each iteration. If it’s a serial port type, we open the port to see if it is available.

Now, why does CommPortIdentifier use the open() method in one step instead of first retrieving the port and then having CommPort invoke the open() method? This clarifies the concept that port discovery and control (ownership) must be done in only one central point: the CommPortIdentifier. Perhaps CommPortIdentifier should have been renamed to "CommPortManager" or "CommPortFactory," but then it would require breaking out the ID portion to a separate class; or perhaps the factory could have implemented it as an interface. A factory implementing an interface? Well okay, perhaps not, but nonetheless this chapter will mention CommPortIdentifier more than any other class, due to its many responsibilities.

Table of Contents

Chapter 1
Chapter 2
The Java Communications API
Chapter 3
Communicating with the RCXPort API
Chapter 4
Communicating with the RCXJava API
Chapter 5
The leJOS System
Chapter 6
Programming for the leJOS Environment
Chapter 7
leJOS Tools
Chapter 8
leJOS Internals
Chapter 9
Programming LEGO MINDSTORMS with Jini

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews