@@ -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,8 +1002,8 @@ 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 }
@@ -1012,7 +1012,7 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
10121012 else {
10131013 printLogDataForDebugging ("Packet syncing problem:\t Expected: " + (getPacketSizeWithCrc ()+2 ) + "bytes. Buffer contains " + mByteArrayOutputStream .size () + "bytes"
10141014 + "\n Buffer = " + UtilShimmer .bytesToHexStringWithSpacesFormatted (mByteArrayOutputStream .toByteArray ()));
1015- discardFirstBufferByte (); //throw the first byte away
1015+ discardBufferBytesToNextPacket (); // Skip to start of next packet
10161016 }
10171017 }
10181018
@@ -1052,7 +1052,7 @@ public boolean checkCrc(byte[] bufferTemp, int length) {
10521052 if (mBtCommsCrcModeCurrent == BT_CRC_MODE .TWO_BYTE_CRC ) {
10531053 // + 2 as this is the location of the CRC's MSB
10541054 if (bufferTemp [getPacketSize () + 2 ] != crcCalc [1 ]) {
1055- discardFirstBufferByte ();
1055+ discardBufferBytesToNextPacket ();
10561056 return false ;
10571057 }
10581058 }
@@ -1190,6 +1190,10 @@ private void buildAndSendMsg(byte[] packet, COMMUNICATION_TYPE fwType, boolean t
11901190 ObjectCluster objectCluster = null ;
11911191 try {
11921192 objectCluster = buildMsg (packet , fwType , timeSync , pcTimeStamp );
1193+ if (objectCluster .successfullyParsed ==false ){
1194+ printLogDataForDebugging ("Packet timestamp is not contiguous - skipping:\t \t " + UtilShimmer .bytesToHexStringWithSpacesFormatted (packet ));
1195+ return ;
1196+ }
11931197 objectCluster = systemTimestampPlot .processSystemTimestampPlot (objectCluster );
11941198 } catch (Exception e ) {
11951199 e .printStackTrace ();
@@ -1294,16 +1298,34 @@ protected void clearBuffers() {
12941298 }
12951299
12961300 /**
1297- *
1301+ * Next packet start should begin with DATA_PACKET or ACK_COMMAND_PROCESSED byte so skip to that point
12981302 */
1299- protected void discardFirstBufferByte (){
1303+ protected void discardBufferBytesToNextPacket (){
13001304 byte [] bTemp = mByteArrayOutputStream .toByteArray ();
1305+
1306+ //Find index of first DATA_PACKET or ACK byte within the buffer
1307+ int offset = findOffsetOfNextZeroOrFF (bTemp );
1308+ //If not found, just skip one byte
1309+ offset = (offset == -1 ) ? 1 : offset ;
1310+
13011311 mByteArrayOutputStream .reset ();
1302- mByteArrayOutputStream .write (bTemp , 1 , bTemp .length -1 ); //this will throw the first byte away
1312+ mByteArrayOutputStream .write (bTemp , offset , bTemp .length -offset ); //this will throw the first byte away
13031313 if (mEnablePCTimeStamps ) {
1304- mListofPCTimeStamps .remove (0 );
1314+ for (int i =0 ;i <offset ;i ++)
1315+ {
1316+ mListofPCTimeStamps .remove (0 );
1317+ }
1318+ }
1319+ consolePrintLn ("Throw Bytes " + UtilShimmer .bytesToHexStringWithSpacesFormatted (Arrays .copyOfRange (bTemp , 0 , offset )));
1320+ }
1321+
1322+ private static int findOffsetOfNextZeroOrFF (byte [] a ) {
1323+ for (int i = 1 ; i < a .length ; i ++) {
1324+ byte b = a [i ];
1325+ if (b == 0 || b == (byte ) 0xFF )
1326+ return i ;
13051327 }
1306- consolePrintLn ( "Throw Byte" + UtilShimmer . bytesToHexStringWithSpacesFormatted ( bTemp )) ;
1328+ return - 1 ;
13071329 }
13081330
13091331 /**
0 commit comments