Page 104 - Hacking Roomba
P. 104

Chapter 4 — Building a Roomba Bluetooth Interface                  85



                             RXTX is open-source implementation of the Sun Microsystems Java Communications API
                             (CommAPI). The CommAPI is used to talk to serial and parallel port devices. RXTX works
                             on all the operating systems that the CommAPI does, as well as many others. The only differ-
                             ence in use between RXTX and the official Java CommAPI is the import statement at the
                             top of the code. Otherwise the two function identically.

                             For more information about the Java Communication API, see http://java.sun.com/
                             products/javacomm/. And to learn more about RXTX, visit http://rxtx.org/.




                             With the above code structure, you’ll end up using the same boilerplate setup code at the
                             beginning of any RoombaComm program:
                             String portname = “/dev/cu.KeySerial1”;
                             RoombaCommSerial roombacomm = new RoombaCommSerial();
                             if( !roombacomm.connect(portname) ) {
                               System.out.println(“Couldn’t connect to “+portname);
                               System.exit(1);
                             }
                             roombacomm.startup();
                             roombacomm.control();
                             roombacomm.pause(100);

                             This chunk of code does several important things:
                                 Specifies a serial port to use by name

                                 Creates a new RoombaCommSerial object
                                 Attempts to connect to that serial port
                                 Sends the START and CONTROL ROI commands to the Roomba

                             Of these, the connect() method is the most important and most difficult. To create it you
                             use the various aspects of the RXTX library to find the serial port by name, open it, and set the
                             port parameters to match what the Roomba expects. It’s shown along with the other methods
                             used above in Listing 4-1.
                             The internals of connect() are based on what is a standard way of using RXTX to
                             open serial ports. You might be inclined to pass in the serial port name as a string (such
                             as /dev/cu.KeySerial1) and expect connect() to work, but RXTX uses more complex
                             CommPortIdentifier objects and can’t support a simple string-based lookup. So the first
                             part of connect() deals with searching through the list of all ports to find one that is both
                             a serial port and has the desired name. When the right port is found, the port is opened, and
                             its communication parameters are set to what Roomba expects.
                             The main deviation from the standard way of opening serial ports with RXTX is the
                             if(waitForDSR) {} clause after the port is opened. That clause loops for up to six
                             seconds waiting for the DSR (Data Set Ready) line to be true. For some reason when using
   99   100   101   102   103   104   105   106   107   108   109