@Test public void testReceivePacket() { int[] response = getPacket("7E 00 02 8A 06 6F"); assertNotNull(response); assertEquals(5, response.length); assertEquals(0x8A, response[2]); }
private int[] getPacket() { int[] inputBuffer = new int[180]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int length = 0; int cnt = 0; int checksum = 0; boolean escaped = false; logger.trace("XBEE: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("XBEE RX buffer overrun - resetting!"); } logger.trace("RX XBEE: {}", String.format("%02X %c", val, val)); if (escaped) { escaped = false; val = val ^ XBEE_XOR; } else if (val == XBEE_ESCAPE) { escaped = true; continue; } switch (rxState) { case WAITING: if (val == XBEE_FLAG) { rxState = RxStateMachine.RECEIVE_LEN1; } continue; case RECEIVE_LEN1: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_LEN2; length = val << 8; break; case RECEIVE_LEN2: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_DATA; length += val + 3; if (length > inputBuffer.length) { logger.debug("XBEE RX length too long ({}) - ignoring!", length); return null; } break; case RECEIVE_DATA: checksum += val; inputBuffer[cnt++] = val; break; default: break; } if (cnt == length) { if ((checksum & 0xff) == 255) { return Arrays.copyOfRange(inputBuffer, 0, length); } else { return null; } } } return null; }
XBeeFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[180]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int length = 0; int cnt = 0; int checksum = 0; boolean escaped = false; logger.trace("XBEE: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("XBEE RX buffer overrun - resetting!"); } logger.trace("RX XBEE: {}", String.format("%02X %c", val, val)); if (escaped) { escaped = false; val = val ^ XBEE_XOR; } else if (val == XBEE_ESCAPE) { escaped = true; continue; } switch (rxState) { case WAITING: if (val == XBEE_FLAG) { rxState = RxStateMachine.RECEIVE_LEN1; } continue; case RECEIVE_LEN1: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_LEN2; length = val << 8; break; case RECEIVE_LEN2: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_DATA; length += val + 3; if (length > inputBuffer.length) { logger.debug("XBEE RX length too long ({}) - ignoring!", length); return null; } break; case RECEIVE_DATA: checksum += val; inputBuffer[cnt++] = val; break; default: break; } if (cnt == length) { if ((checksum & 0xff) == 255) { return Arrays.copyOfRange(inputBuffer, 0, length); } else { return null; } } } return null; } }
XBeeFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[180]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int length = 0; int cnt = 0; int checksum = 0; boolean escaped = false; logger.trace("XBEE: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("XBEE RX buffer overrun - resetting!"); } logger.trace("RX XBEE: {}", String.format("%02X %c", val, val)); if (escaped) { escaped = false; val = val ^ XBEE_XOR; } else if (val == XBEE_ESCAPE) { escaped = true; continue; } switch (rxState) { case WAITING: if (val == XBEE_FLAG) { rxState = RxStateMachine.RECEIVE_LEN1; } continue; case RECEIVE_LEN1: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_LEN2; length = val << 8; break; case RECEIVE_LEN2: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_DATA; length += val + 3; if (length > inputBuffer.length) { logger.debug("XBEE RX length too long ({}) - ignoring!", length); return null; } break; case RECEIVE_DATA: checksum += val; inputBuffer[cnt++] = val; break; default: break; } if (cnt == length) { if ((checksum & 0xff) == 255) { return Arrays.copyOfRange(inputBuffer, 0, length); } else { return null; } } } return null; }  }
XBeeFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[180]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int length = 0; int cnt = 0; int checksum = 0; boolean escaped = false; logger.trace("XBEE: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("XBEE RX buffer overrun - resetting!"); } logger.trace("RX XBEE: {}", String.format("%02X %c", val, val)); if (escaped) { escaped = false; val = val ^ XBEE_XOR; } else if (val == XBEE_ESCAPE) { escaped = true; continue; } switch (rxState) { case WAITING: if (val == XBEE_FLAG) { rxState = RxStateMachine.RECEIVE_LEN1; } continue; case RECEIVE_LEN1: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_LEN2; length = val << 8; break; case RECEIVE_LEN2: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_DATA; length += val + 3; if (length > inputBuffer.length) { logger.debug("XBEE RX length too long ({}) - ignoring!", length); return null; } break; case RECEIVE_DATA: checksum += val; inputBuffer[cnt++] = val; break; default: break; } if (cnt == length) { if ((checksum & 0xff) == 255) { return Arrays.copyOfRange(inputBuffer, 0, length); } else { return null; } } } return null; }  void start(final ZigBeePort serialPort); void setClosing(); void close(); boolean isAlive(); void setCommandTimeout(int commandTimeout); void setTransactionTimeout(int transactionTimeout); void addEventListener(XBeeEventListener listener); void removeEventListener(XBeeEventListener listener); Future<XBeeResponse> sendRequestAsync(final XBeeCommand command); XBeeResponse sendRequest(final XBeeCommand command); XBeeEvent eventWait(final Class<?> eventClass); }
XBeeFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[180]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int length = 0; int cnt = 0; int checksum = 0; boolean escaped = false; logger.trace("XBEE: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("XBEE RX buffer overrun - resetting!"); } logger.trace("RX XBEE: {}", String.format("%02X %c", val, val)); if (escaped) { escaped = false; val = val ^ XBEE_XOR; } else if (val == XBEE_ESCAPE) { escaped = true; continue; } switch (rxState) { case WAITING: if (val == XBEE_FLAG) { rxState = RxStateMachine.RECEIVE_LEN1; } continue; case RECEIVE_LEN1: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_LEN2; length = val << 8; break; case RECEIVE_LEN2: inputBuffer[cnt++] = val; rxState = RxStateMachine.RECEIVE_DATA; length += val + 3; if (length > inputBuffer.length) { logger.debug("XBEE RX length too long ({}) - ignoring!", length); return null; } break; case RECEIVE_DATA: checksum += val; inputBuffer[cnt++] = val; break; default: break; } if (cnt == length) { if ((checksum & 0xff) == 255) { return Arrays.copyOfRange(inputBuffer, 0, length); } else { return null; } } } return null; }  void start(final ZigBeePort serialPort); void setClosing(); void close(); boolean isAlive(); void setCommandTimeout(int commandTimeout); void setTransactionTimeout(int transactionTimeout); void addEventListener(XBeeEventListener listener); void removeEventListener(XBeeEventListener listener); Future<XBeeResponse> sendRequestAsync(final XBeeCommand command); XBeeResponse sendRequest(final XBeeCommand command); XBeeEvent eventWait(final Class<?> eventClass);  }