Page 105 - Hacking Roomba
P. 105

86       Part I — Interfacing




                             Bluetooth adapters on Windows, opening a port will succeed, but trying to write to it
                             will fail unless enough time has elapsed for the virtual serial port to be initialized. Waiting
                             for DSR to be true seems to fix this problem. When writing your own code, be sure to
                             include roombacomm.waitForDSR=true; before connecting because this will trigger the
                             waitForDSR clause in connect(). In all example programs in this book, the command-line
                             ones take a -hwhandshake flag, and the GUI ones have a h/w handshake check box to set
                             waitForDSR to true. It’s called h/w handshake for hardware handshake, which is what the
                             DSR is part of. Some slower serial port systems or those with very fast data transfer use extra
                             hardware signals like DSR to indicate when it’s okay to send or receive data. On modern com-
                             puters this handshaking is rarely needed. For now you can hack around the virtual serial port
                             initialization problem by waiting for the DSR line. This will probably eventually be updated,
                             and when it is you’ll be able to simplify your code by removing the waitForDSR clause and
                             related code.

                             When using Windows and Bluetooth adapters, use the -hwhandshake flag or select the h/w
                             handshake check box to make RoombaComm deal with Windows virtual serial ports correctly.



                               Listing 4-1: Important Methods in RoombaComm and
                                           RoombaCommSerial

                               private boolean connect(String portid) {
                                 portname = portid;
                                 boolean success = false;
                                 try {
                                 Enumeration portList =
                               CommPortIdentifier.getPortIdentifiers();
                                 while(portList.hasMoreElements()) {
                                   CommPortIdentifier portId =
                                                    (CommPortIdentifier) portList.nextElement();
                                   if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
                               {
                                     if (portId.getName().equals(portname)) {
                                        port = (SerialPort)portId.open(“roomba serial”,2000);
                                        input  = port.getInputStream();
                                        output = port.getOutputStream();

                               port.setSerialPortParams(rate,databits,stopbits,parity);
                                        port.addEventListener(this);
                                        port.notifyOnDataAvailable(true);
                                        logmsg(“port “+portname+” opened successfully”);
                                        if(waitForDSR) {
                                          int i=40;
                                          while( !port.isDSR() && i-- != 0) {
                                            logmsg(“DSR not ready yet”);
                                            pause(150); // 150*40 = 6 seconds
                                          }
                                          success = port.isDSR();
                                        } else {
   100   101   102   103   104   105   106   107   108   109   110