@@ -715,7 +715,7 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND)
715715 }
716716 threadSleep ((int )((Math .random ()+.1 )*100.0 ));
717717 writeBytes (insBytes );
718- printLogDataForDebugging ("Command Transmitted: \t \t \t " + btCommandToString (mCurrentCommand ) + " " + UtilShimmer .bytesToHexStringWithSpacesFormatted (insBytes ));
718+ printLogDataForDebugging ("Command Transmitted: \t \t \t " + btCommandToString (mCurrentCommand ) + ", len=" + insBytes . length + " " + UtilShimmer .bytesToHexStringWithSpacesFormatted (insBytes ));
719719
720720 //TODO: are the two stops needed here? better to wait for ack from Shimmer
721721 if (mCurrentCommand ==STOP_STREAMING_COMMAND
@@ -880,7 +880,7 @@ private void processNotStreamingWaitForResp() {
880880 mWaitForResponse =false ;
881881 mTransactionCompleted =true ;
882882 setInstructionStackLock (false );
883- printLogDataForDebugging ("Response Received:\t \t \t " + btCommandToString (responseCommand ));
883+ printLogDataForDebugging ("Response Received:\t \t \t " + btCommandToString (responseCommand ) + ", len=" + byteBuffer . length + " " + UtilShimmer . bytesToHexStringWithSpacesFormatted ( byteBuffer ) );
884884
885885 // Special case for FW_VERSION_RESPONSE because it
886886 // needs to initialize the Shimmer after releasing
@@ -939,7 +939,7 @@ protected void processPacket() {
939939 && bufferTemp [getPacketSizeWithCrc ()+1 ]==DATA_PACKET ){
940940
941941 if (mBtCommsCrcModeCurrent != BT_CRC_MODE .OFF && !checkCrc (bufferTemp , getPacketSize () + 1 )) {
942- discardFirstBufferByte ();
942+ discardBufferBytesToNextPacket ();
943943 return ;
944944 }
945945
@@ -1002,17 +1002,19 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
10021002 }
10031003 }
10041004 if (mByteArrayOutputStream .size ()>getPacketSizeWithCrc ()+2 ){
1005- printLogDataForDebugging ("Unknown packet error (check with JC): \t Expected: " + (getPacketSizeWithCrc ()+2 ) + "bytes but buffer contains " + mByteArrayOutputStream .size () + "bytes" );
1006- discardFirstBufferByte (); //throw the first byte away
1005+ printLogDataForDebugging ("Unknown packet error: \t Expected: " + (getPacketSizeWithCrc ()+2 ) + "bytes but buffer contains " + mByteArrayOutputStream .size () + "bytes" );
1006+ discardBufferBytesToNextPacket (); // Skip to start of next packet
10071007 }
10081008
10091009 }
10101010 //TODO: ACK in bufferTemp[0] not handled
10111011 //else if
10121012 else {
1013- printLogDataForDebugging ("Packet syncing problem:\t Expected: " + (getPacketSizeWithCrc ()+2 ) + "bytes. Buffer contains " + mByteArrayOutputStream .size () + "bytes"
1014- + "\n Buffer = " + UtilShimmer .bytesToHexStringWithSpacesFormatted (mByteArrayOutputStream .toByteArray ()));
1015- discardFirstBufferByte (); //throw the first byte away
1013+ printLogDataForDebugging ("Unexpected packet header bytes. Expected size: " + getPacketSizeWithCrc ()
1014+ + ", Packet 1 header: " + UtilShimmer .byteToHexStringFormatted (bufferTemp [0 ])
1015+ + ", Packet 2 header: " + UtilShimmer .byteToHexStringFormatted (bufferTemp [getPacketSizeWithCrc () + 1 ])
1016+ + "\n Buffer: " + UtilShimmer .bytesToHexStringWithSpacesFormatted (mByteArrayOutputStream .toByteArray ()));
1017+ discardBufferBytesToNextPacket (); // Skip to start of next packet
10161018 }
10171019 }
10181020
@@ -1052,7 +1054,7 @@ public boolean checkCrc(byte[] bufferTemp, int length) {
10521054 if (mBtCommsCrcModeCurrent == BT_CRC_MODE .TWO_BYTE_CRC ) {
10531055 // + 2 as this is the location of the CRC's MSB
10541056 if (bufferTemp [getPacketSize () + 2 ] != crcCalc [1 ]) {
1055- discardFirstBufferByte ();
1057+ discardBufferBytesToNextPacket ();
10561058 return false ;
10571059 }
10581060 }
@@ -1294,16 +1296,38 @@ protected void clearBuffers() {
12941296 }
12951297
12961298 /**
1297- *
1299+ * Next packet start should begin with DATA_PACKET or ACK_COMMAND_PROCESSED byte so skip to that point
12981300 */
1299- protected void discardFirstBufferByte (){
1301+ protected void discardBufferBytesToNextPacket (){
13001302 byte [] bTemp = mByteArrayOutputStream .toByteArray ();
1303+
1304+ //Find index of first DATA_PACKET or ACK byte within the buffer
1305+ int offset = findOffsetOfNextZeroOrFF (bTemp );
1306+ //If not found, just skip one byte
1307+ offset = (offset == -1 ) ? 1 : offset ;
1308+
13011309 mByteArrayOutputStream .reset ();
1302- mByteArrayOutputStream .write (bTemp , 1 , bTemp .length -1 ); //this will throw the first byte away
1310+ mByteArrayOutputStream .write (bTemp , offset , bTemp .length -offset ); // discard first 'offset' bytes
13031311 if (mEnablePCTimeStamps ) {
1304- mListofPCTimeStamps .remove (0 );
1312+ // Remove first `offset` elements from the original list (destructive, modifies same list)
1313+ mListofPCTimeStamps .subList (0 , Math .min (offset , mListofPCTimeStamps .size ())).clear ();
1314+ }
1315+ consolePrintLn ("Throw Bytes " + UtilShimmer .bytesToHexStringWithSpacesFormatted (Arrays .copyOfRange (bTemp , 0 , offset )));
1316+ }
1317+
1318+ /**
1319+ * Finds the offset/index of the next DATA_PACKET (0x00) or ACK_COMMAND_PROCESSED (0xFF) byte.
1320+ *
1321+ * @param buffer a byte array to search within
1322+ * @return index of the first 0x00 or 0xFF byte found after position 0, or -1 if not found
1323+ */
1324+ private static int findOffsetOfNextZeroOrFF (byte [] buffer ) {
1325+ for (int i = 1 ; i < buffer .length ; i ++) {
1326+ byte b = buffer [i ];
1327+ if (b == 0 || b == (byte ) 0xFF )
1328+ return i ;
13051329 }
1306- consolePrintLn ( "Throw Byte" + UtilShimmer . bytesToHexStringWithSpacesFormatted ( bTemp )) ;
1330+ return - 1 ;
13071331 }
13081332
13091333 /**
0 commit comments