@Test public void testReceivePacket_CRLF_End() { int[] response = getPacket("BCAST:000D6F000005A666,04=test" + CRLF); assertNotNull(response); assertEquals(30, response.length); assertEquals('B', response[0]); }
private int[] getPacket() { int[] inputBuffer = new int[120]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int binaryLength = 0; logger.trace("TELEGESIS: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("TELEGESIS RX buffer overrun - resetting!"); } logger.trace("RX Telegesis: {}", String.format("%02X %c", val, val)); switch (rxState) { case WAITING: if (val == '\n' || val == '\r') { continue; } inputBuffer[inputBufferLength++] = val; rxState = RxStateMachine.RECEIVE_CMD; break; case RECEIVE_CMD: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if (val == ':') { rxState = RxStateMachine.RECEIVE_ASCII; } break; case RECEIVE_ASCII: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if ((val == '=' || val == ':') && inputBufferLength > 2) { char[] chars = new char[2]; chars[0] = (char) inputBuffer[inputBufferLength - 3]; chars[1] = (char) inputBuffer[inputBufferLength - 2]; try { binaryLength = Integer.parseInt(new String(chars), 16); rxState = RxStateMachine.RECEIVE_BINARY; } catch (NumberFormatException e) { } } break; case RECEIVE_BINARY: inputBuffer[inputBufferLength++] = val; if (--binaryLength == 0) { rxState = RxStateMachine.RECEIVE_ASCII; } break; default: break; } } return null; }
TelegesisFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[120]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int binaryLength = 0; logger.trace("TELEGESIS: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("TELEGESIS RX buffer overrun - resetting!"); } logger.trace("RX Telegesis: {}", String.format("%02X %c", val, val)); switch (rxState) { case WAITING: if (val == '\n' || val == '\r') { continue; } inputBuffer[inputBufferLength++] = val; rxState = RxStateMachine.RECEIVE_CMD; break; case RECEIVE_CMD: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if (val == ':') { rxState = RxStateMachine.RECEIVE_ASCII; } break; case RECEIVE_ASCII: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if ((val == '=' || val == ':') && inputBufferLength > 2) { char[] chars = new char[2]; chars[0] = (char) inputBuffer[inputBufferLength - 3]; chars[1] = (char) inputBuffer[inputBufferLength - 2]; try { binaryLength = Integer.parseInt(new String(chars), 16); rxState = RxStateMachine.RECEIVE_BINARY; } catch (NumberFormatException e) { } } break; case RECEIVE_BINARY: inputBuffer[inputBufferLength++] = val; if (--binaryLength == 0) { rxState = RxStateMachine.RECEIVE_ASCII; } break; default: break; } } return null; } }
TelegesisFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[120]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int binaryLength = 0; logger.trace("TELEGESIS: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("TELEGESIS RX buffer overrun - resetting!"); } logger.trace("RX Telegesis: {}", String.format("%02X %c", val, val)); switch (rxState) { case WAITING: if (val == '\n' || val == '\r') { continue; } inputBuffer[inputBufferLength++] = val; rxState = RxStateMachine.RECEIVE_CMD; break; case RECEIVE_CMD: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if (val == ':') { rxState = RxStateMachine.RECEIVE_ASCII; } break; case RECEIVE_ASCII: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if ((val == '=' || val == ':') && inputBufferLength > 2) { char[] chars = new char[2]; chars[0] = (char) inputBuffer[inputBufferLength - 3]; chars[1] = (char) inputBuffer[inputBufferLength - 2]; try { binaryLength = Integer.parseInt(new String(chars), 16); rxState = RxStateMachine.RECEIVE_BINARY; } catch (NumberFormatException e) { } } break; case RECEIVE_BINARY: inputBuffer[inputBufferLength++] = val; if (--binaryLength == 0) { rxState = RxStateMachine.RECEIVE_ASCII; } break; default: break; } } return null; } TelegesisFrameHandler(ZigBeeDongleTelegesis zigBeeDongleTelegesis); }
TelegesisFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[120]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int binaryLength = 0; logger.trace("TELEGESIS: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("TELEGESIS RX buffer overrun - resetting!"); } logger.trace("RX Telegesis: {}", String.format("%02X %c", val, val)); switch (rxState) { case WAITING: if (val == '\n' || val == '\r') { continue; } inputBuffer[inputBufferLength++] = val; rxState = RxStateMachine.RECEIVE_CMD; break; case RECEIVE_CMD: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if (val == ':') { rxState = RxStateMachine.RECEIVE_ASCII; } break; case RECEIVE_ASCII: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if ((val == '=' || val == ':') && inputBufferLength > 2) { char[] chars = new char[2]; chars[0] = (char) inputBuffer[inputBufferLength - 3]; chars[1] = (char) inputBuffer[inputBufferLength - 2]; try { binaryLength = Integer.parseInt(new String(chars), 16); rxState = RxStateMachine.RECEIVE_BINARY; } catch (NumberFormatException e) { } } break; case RECEIVE_BINARY: inputBuffer[inputBufferLength++] = val; if (--binaryLength == 0) { rxState = RxStateMachine.RECEIVE_ASCII; } break; default: break; } } return null; } TelegesisFrameHandler(ZigBeeDongleTelegesis zigBeeDongleTelegesis); void start(final ZigBeePort serialPort); void setClosing(); void close(); boolean isAlive(); void queueFrame(TelegesisCommand request); void setCommandTimeout(int commandTimeout); void setTransactionTimeout(int transactionTimeout); void addEventListener(TelegesisEventListener listener); void removeEventListener(TelegesisEventListener listener); Future<TelegesisCommand> sendRequestAsync(final TelegesisCommand command); TelegesisStatusCode sendRequest(final TelegesisCommand command); Future<TelegesisEvent> waitEventAsync(final Class<?> eventClass); TelegesisEvent eventWait(final Class<?> eventClass); }
TelegesisFrameHandler { private int[] getPacket() { int[] inputBuffer = new int[120]; int inputBufferLength = 0; RxStateMachine rxState = RxStateMachine.WAITING; int binaryLength = 0; logger.trace("TELEGESIS: Get Packet"); while (!closeHandler) { int val = serialPort.read(); if (val == -1) { continue; } if (inputBufferLength >= inputBuffer.length) { inputBufferLength = 0; rxState = RxStateMachine.WAITING; logger.debug("TELEGESIS RX buffer overrun - resetting!"); } logger.trace("RX Telegesis: {}", String.format("%02X %c", val, val)); switch (rxState) { case WAITING: if (val == '\n' || val == '\r') { continue; } inputBuffer[inputBufferLength++] = val; rxState = RxStateMachine.RECEIVE_CMD; break; case RECEIVE_CMD: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if (val == ':') { rxState = RxStateMachine.RECEIVE_ASCII; } break; case RECEIVE_ASCII: if (val == '\n' || val == '\r') { return Arrays.copyOfRange(inputBuffer, 0, inputBufferLength); } inputBuffer[inputBufferLength++] = val; if ((val == '=' || val == ':') && inputBufferLength > 2) { char[] chars = new char[2]; chars[0] = (char) inputBuffer[inputBufferLength - 3]; chars[1] = (char) inputBuffer[inputBufferLength - 2]; try { binaryLength = Integer.parseInt(new String(chars), 16); rxState = RxStateMachine.RECEIVE_BINARY; } catch (NumberFormatException e) { } } break; case RECEIVE_BINARY: inputBuffer[inputBufferLength++] = val; if (--binaryLength == 0) { rxState = RxStateMachine.RECEIVE_ASCII; } break; default: break; } } return null; } TelegesisFrameHandler(ZigBeeDongleTelegesis zigBeeDongleTelegesis); void start(final ZigBeePort serialPort); void setClosing(); void close(); boolean isAlive(); void queueFrame(TelegesisCommand request); void setCommandTimeout(int commandTimeout); void setTransactionTimeout(int transactionTimeout); void addEventListener(TelegesisEventListener listener); void removeEventListener(TelegesisEventListener listener); Future<TelegesisCommand> sendRequestAsync(final TelegesisCommand command); TelegesisStatusCode sendRequest(final TelegesisCommand command); Future<TelegesisEvent> waitEventAsync(final Class<?> eventClass); TelegesisEvent eventWait(final Class<?> eventClass);  }