public int getNextByte() { int byteToReturn = -1; try { byteToReturn = fileArray[fileIndex]; if (byteToReturn != 0) { byteToReturn = byteToReturn & 0xff; } fileIndex++; } catch (Exception e) { System.out.println("Problem with getNextByte()"); e.printStackTrace(); fileIndex = 0; } return byteToReturn; }
// runs the thread. this is the thread's loop public void run() { // do this as long as the thread is running while (running) { try { // read bytes in from the serial port, and get a dataArray back: /* New in 1.4: checks to see if the port has been initialized. If not, assumes it's to read from a file and tries to do so. */ if (port != null) { dataArray = getPacket(); } else { if (bytesAvailable() > 0) { dataArray = getPacket(fileArray); } else { running = false; } } if (dataArray != null) { dataFrame = new XBeeDataFrame(dataArray); switch (dataFrame.apiId) { case ZNET_IOPACKET: // process a ZNet IO packet dataFrame.parseZNetFrame(); break; case SERIES1_RX16PACKET: dataFrame.parseXBeeRX16Frame(); // process a series 1 packet break; case SERIES1_IOPACKET: dataFrame.parseXBeeIOFrame(); // process a series 1 packet break; } } // when we have a dataArray, set available = true: available = true; // not being used at the moment, since XBeeEvent is not getting triggered: // if we have a valid data frame, generate an event: if (xBeeMethod != null && dataArray != null) { // generate an XBeeEvent: try { xBeeMethod.invoke(parent, new Object[] {this}); } catch (Exception e) { System.out.println("Problem with XBeeEvent()"); e.printStackTrace(); xBeeMethod = null; } } // gives the main sketch back the processor try { sleep(sleepRate); // Should we sleep? } catch (Exception e) { // Nothing for now. We'll get here if interrupt() is called } } catch (Exception e) { System.out.println("Exception in XBeeReader.run():"); e.printStackTrace(); } } running = false; fileEmpty = true; }