Page 135 - Hacking Roomba
P. 135

116       Part I — Interfacing




                             Receiving Serial Events
                             To get serial events, you first have to tell the RXTX SerialPort object that you want to listen
                             to events. The open_port() method in RoombaCommSerial shows how this is done. The
                             important parts are:
                             port = (SerialPort)portId.open(“roomba serial”, 2000);
                             port.addEventListener(this);
                             port.notifyOnDataAvailable(true);
                             That code snippet gets the serial port object, adds RoombaCommSerial as an event listener,
                             and then specifies the kind of event you care about. You’ll find several types of events. The
                             most useful for RoombaComm (and the most reliably implemented in RXTX) is the event that
                             tells you when serial input data is available.

                             The serialEvent() method that will be called when data is available looks like Listing 6-1.


                               Listing 6-1: RoombaCommSerial.serialEvent()

                               byte[] sensor_bytes = new byte[26];
                               byte buffer[] = new byte[26];
                               boolean sensorsValid = false;
                               int bufferLast;

                               synchronized public void serialEvent(SerialPortEvent
                               serialEvent) {
                                 try {
                                   if( serialEvent.getEventType() ==
                               SerialPortEvent.DATA_AVAILABLE ) {
                                     while( input.available()>0 ) {
                                        buffer[bufferLast++] = (byte)input.read();
                                        if( bufferLast==26 ) {
                                          bufferLast = 0;
                                          System.arraycopy(buffer,0,sensor_bytes,0,26);
                                          sensorsValid = true;
                                          computeSensors();
                                        }
                                     } // while
                                   }
                                 } catch (IOException e) {
                                   errorMessage(“serialEvent”, e);
                                 }
                               }



                             This is one of the most complex bits of code in the entire book, and it really isn’t that bad.
                             The first two lines are variables inside of the RoombaCommSerial object that create two byte
                             buffers. The sensor_bytes buffer holds known good sensor data from the last sensor read,
                             and sensorsValid tells you if sensor_bytes is good or not. The other buffer, just called
   130   131   132   133   134   135   136   137   138   139   140