/// <summary> /// /// </summary> /// <param name="br"></param> /// <param name="aLastUnixTime"></param> /// <returns></returns> public static double ReadUnixTimeStamp(ByteReader br, double aLastUnixTime) { double lastUnixTime = aLastUnixTime; //Console.WriteLine ("Read UNIX time stamp"); if (br == null) { return(0); } bool readValid = br.ReadByte(b); if (!readValid) { return(MITesData.NONE); } if (b[0] == ((int)255)) { Console.WriteLine("Marker: " + debugCount); // Marker, so read the whole timecode readValid = br.ReadBytes(b6); if (!readValid) { return(MITesData.NONE); } lastUnixTime = UnixTime.DecodeUnixTimeCodeBytesFixed(b6); // SSI Added test if (lastUnixTime == refTime) { // This is a the so-called sync byte. Need to read 5 more bytes and ignore for (int r = 0; r < 5; r++) { readValid = br.ReadByte(b); if (!readValid) { return(MITesData.NONE); } } //Console.WriteLine("SYNC byte: keep time as: " + lastUnixTime); //Now read the time byte and add to the existing time readValid = br.ReadByte(b); if (!readValid) { return(MITesData.NONE); } return(aLastUnixTime + (int)b[0]); } //Console.WriteLine ("UNIX Timecode: " + lastUnixTime); DateTime junk = new DateTime(); UnixTime.GetDateTime((long)lastUnixTime, out junk); //Console.WriteLine("UNIX Timecode date: " + junk); //junk = new DateTime(2007,9,9,8,16,5,609); //double junkme = UnixTime.GetUnixTime(junk); //UnixTime.GetDateTime((long)junkme,out junk); //Console.WriteLine("NEW DATE: " + junk); //byte[] somebytes = UnixTime.GetUnixTimeBytes(junkme); //double newUnixTime = UnixTime.DecodeUnixTimeCodeBytesFixed(somebytes); //UnixTime.GetDateTime((long) newUnixTime, out junk); //Console.WriteLine("NEW DATE: " + junk); if (!isRefDateSet) { aRefDate = junk.AddDays(-2); isRefDateSet = true; } //lastUnixTime = UnixTime.DecodeUnixTimeCodeBytesFixed(b6); //DateTime dt = new DateTime(); //UnixTime.GetDateTime((long) lastUnixTime, out dt); //Console.WriteLine("TEST: " + dt); } else { if (lastUnixTime == 0) { junkTimecodeData++; if ((junkTimecodeData % 5000) == 0) { Console.WriteLine("ERROR: Hit " + junkTimecodeData + " of junk timecode data."); //"Last unix time is zero for some reason!"); junkTimecodeData = 0; } } else { // Read only the difference between this and previous time (less than 255 ms) lastUnixTime += (int)b[0]; //Console.WriteLine ("Diff byte: " + b[0] + " modified UNIX Timecode: " + lastUnixTime); } } return(lastUnixTime); }
/// <summary> /// Decode multiple receiver data that has been saved by MITesLogger (always send multiple of 4 bytes). A MITesLogger saves /// MITesData. This reads it back in so it behaves exactly as data read from the /// serial port. Useful for "playing back" data that has been saved. /// </summary> /// <param name="someData">The array in which the resulting MITesData will be stored.</param> /// <param name="dataIndex">The current index of the array in which the MITesData will be stored. (This will append data onto the end of existing data if needed).</param> /// <param name="br1">A ByteReader object that has been opened to the proper file for receiver 1.</param> /// <param name="br2">A ByteReader object that has been opened to the proper file for receiver 2.</param> /// <returns>The new index for the someData array.</returns> public int DecodePLFormatMR(MITesData[] someData, int dataIndex, ByteReader br1, ByteReader br2) { bool isGoodPacketRead = false; isEndFile1 = false; isEndFile2 = false; int fileUsed = 0; // Determine if consumed next data point from each file. Value of 0 indicates yes and get next value. if (dTimeStamp1 == 0) { dTimeStamp1 = ReadUnixTimeStamp(br1, dLastTimeStamp1); } if (dTimeStamp2 == 0) { dTimeStamp2 = ReadUnixTimeStamp(br2, dLastTimeStamp2); } if ((dTimeStamp1 - dLastTimeStamp1) > (2 * 60 * 60 * 1000)) { Console.WriteLine("Skip of more than two hours in file 1"); } if ((dTimeStamp2 - dLastTimeStamp2) > (2 * 60 * 60 * 1000)) { Console.WriteLine("Skip of more than two hours in file 2"); } debugCount++; DateTime dt1 = new DateTime(); UnixTime.GetDateTime((long)dTimeStamp1, out dt1); DateTime dt2 = new DateTime(); UnixTime.GetDateTime((long)dTimeStamp2, out dt2); //if (((dTimeStamp1 != MITesData.NONE) && (dTimeStamp2 != MITesData.NONE)) && // ((dt1.Second != dt2.Second) || (dt1.Minute != dt2.Minute))) //{ // //isLastMatch = false; // Console.WriteLine("DATES: " + Environment.NewLine + dt1 + Environment.NewLine + dt2 + " " + debugCount); //} if ((dTimeStamp1 == (double)MITesData.NONE) || (br1 == null)) { //Console.WriteLine("End of file 1: " + GetDateTime(lastGoodTime1) + " " + GetDateTime(lastGoodTime2)); isEndFile1 = true; } if ((dTimeStamp2 == (double)MITesData.NONE) || (br2 == null)) { //Console.WriteLine("End of file 2: " + GetDateTime(lastGoodTime1) + " " + GetDateTime(lastGoodTime2)); isEndFile2 = true; } if (isEndFile1 && isEndFile2) { Console.WriteLine("End of both files."); return(0); } // If at this point, there is some data to read in one of the files #region Thread wait (do in the future) // Insert waiting code here in the future for graphing capability option //diffMS1 = (int)(dTimeStamp1 - dLastTimeStamp1); //if ((dLastTimeStamp1 != 0) && (dTimeStamp1 != 0)) // timeToWait1 = diffMS1; //else // timeToWait1 = 0; //diffMS2 = (int)(dTimeStamp1 - dLastTimeStamp1); //if ((dLastTimeStamp1 != 0) && (dTimeStamp1 != 0)) // timeToWait2 = diffMS1; //else // timeToWait2 = 0; //// Wait the right number of MS if needed from last time data grabbed //diffTime = Environment.TickCount - lastTimeStampTime; //if ((timeToWait - diffTime) > 0) //{ // Thread.Sleep(timeToWait - diffTime); // timeToWait = 0; //} #endregion if ((dTimeStamp1 != -1) && (dLastTimeStamp1 != -1) && (dTimeStamp1 < dLastTimeStamp1)) { Console.WriteLine("Jumpback1: " + debugCount); } if ((dTimeStamp2 != -1) && (dLastTimeStamp2 != -1) && (dTimeStamp2 < dLastTimeStamp2)) { Console.WriteLine("Jumpback2: " + debugCount); } dLastTimeStamp1 = dTimeStamp1; dLastTimeStamp2 = dTimeStamp2; lastTimeStampTime1 = Environment.TickCount; lastTimeStampTime2 = Environment.TickCount; //DateTime junkme = new DateTime(); //UnixTime.GetDateTime((long) dTimeStamp1, out junkme); //Console.WriteLine(" DTIMESTAMP1: " + junkme); //UnixTime.GetDateTime((long)dTimeStamp2, out junkme); //Console.WriteLine(" DTIMESTAMP2: " + junkme); // Read packet that is first in time from whichever file. Leave other be ByteReader brTemp; brTemp = null; if ((!isEndFile1) && (!isEndFile2)) // both active { if ((dTimeStamp1 <= dTimeStamp2) && (dTimeStamp1 != 0)) { lastGoodTime1 = dTimeStamp1; brTemp = br1; fileUsed = 1; } else if (dTimeStamp2 != 0) { lastGoodTime2 = dTimeStamp2; brTemp = br2; fileUsed = 2; } //else //{ // Console.WriteLine("ERROR1 -- Should not be here!! ----------------------------"); //} } else { if ((dTimeStamp1 != 0) && (!isEndFile1)) { lastGoodTime1 = dTimeStamp1; brTemp = br1; fileUsed = 1; } else if ((dTimeStamp2 != 0) && (!isEndFile2)) { lastGoodTime2 = dTimeStamp2; brTemp = br2; fileUsed = 2; } //else //{ // Console.WriteLine("ERROR2 -- Should not be here!! ----------------------------"); //} } // Check if need to ignore data because we have a bad timestamp // in either of the files. If so, read bytes and ignore. if (fileUsed == 0) { if (dTimeStamp1 == 0) { IgnorePacket(br1); } if (dTimeStamp2 == 0) { IgnorePacket(br2); } } else if ((fileUsed == 1) && (dTimeStamp2 == 0)) { IgnorePacket(br2); } else if ((fileUsed == 2) && (dTimeStamp1 == 0)) { IgnorePacket(br1); } if (fileUsed != 0) { isGoodPacketRead = brTemp.ReadByte(tempByte); aMITesDecoder.packet[0] = tempByte[0]; brTemp.ReadByte(tempByte); aMITesDecoder.packet[1] = tempByte[0]; brTemp.ReadByte(tempByte); aMITesDecoder.packet[2] = tempByte[0]; brTemp.ReadByte(tempByte); aMITesDecoder.packet[3] = tempByte[0]; brTemp.ReadByte(tempByte); aMITesDecoder.packet[4] = tempByte[0]; aMITesDecoder.DecodeLastPacket(someData[dataIndex], false); // Don't swap bytes if (!isGoodPacketRead) { someData[dataIndex].type = (int)MITesTypes.NOISE; } } //Console.WriteLine("FileUsed: " + fileUsed); if (fileUsed == 1) { someData[dataIndex].timeStamp = UnixTime.IntTimeFromUnixTime(dTimeStamp1, aRefDate); aMITesDecoder.SetUnixTime(someData[dataIndex], dTimeStamp1); // Set the time someData[dataIndex].fileID = 1; // If not a good timestamp (probably because haven't gotten marker yet) set to noise if (dTimeStamp1 == 0) { someData[dataIndex].type = (int)MITesTypes.NOISE; //Console.WriteLine("Losing data due to lack of timestamp sync."); } dTimeStamp1 = 0; // Reset so gets read next time from file } else if (fileUsed == 2) { someData[dataIndex].timeStamp = UnixTime.IntTimeFromUnixTime(dTimeStamp2, aRefDate); aMITesDecoder.SetUnixTime(someData[dataIndex], dTimeStamp2); // Set the time someData[dataIndex].fileID = 2; // If not a good timestamp (probably because haven't gotten marker yet) set to noise if (dTimeStamp2 == 0) { someData[dataIndex].type = (int)MITesTypes.NOISE; //Console.WriteLine("Losing data due to lack of timestamp sync."); } dTimeStamp2 = 0; // Reset so gets read next time from file } else { //Console.WriteLine("ERROR: no file used"); return(-1); } // dataIndex++; return(1); }
public int DecodeWocketsPLFormat(MITesData[] someData, int dataIndex, ByteReader br) { isEndFile = false; // Determine if consumed next data point from each file. Value of 0 indicates yes and get next value. if (dTimeStamp == 0) { dTimeStamp = ReadUnixTimeStamp(br, dLastTimeStamp); } debugCount++; DateTime dt = new DateTime(); UnixTime.GetDateTime((long)dTimeStamp, out dt); //if (((dTimeStamp1 != MITesData.NONE) && (dTimeStamp2 != MITesData.NONE)) && // ((dt1.Second != dt2.Second) || (dt1.Minute != dt2.Minute))) //{ // //isLastMatch = false; // Console.WriteLine("DATES: " + Environment.NewLine + dt1 + Environment.NewLine + dt2 + " " + debugCount); //} if (dTimeStamp == (double)MITesData.NONE) { //Console.WriteLine("End of file 1: " + GetDateTime(lastGoodTime1) + " " + GetDateTime(lastGoodTime2)); isEndFile = true; } if (isEndFile) { Console.WriteLine("End of both files."); return(0); } // If at this point, there is some data to read in one of the files #region Thread wait (do in the future) // Insert waiting code here in the future for graphing capability option //diffMS1 = (int)(dTimeStamp1 - dLastTimeStamp1); //if ((dLastTimeStamp1 != 0) && (dTimeStamp1 != 0)) // timeToWait1 = diffMS1; //else // timeToWait1 = 0; //diffMS2 = (int)(dTimeStamp1 - dLastTimeStamp1); //if ((dLastTimeStamp1 != 0) && (dTimeStamp1 != 0)) // timeToWait2 = diffMS1; //else // timeToWait2 = 0; //// Wait the right number of MS if needed from last time data grabbed //diffTime = Environment.TickCount - lastTimeStampTime; //if ((timeToWait - diffTime) > 0) //{ // Thread.Sleep(timeToWait - diffTime); // timeToWait = 0; //} #endregion if ((dTimeStamp != -1) && (dLastTimeStamp != -1) && (dTimeStamp < dLastTimeStamp)) { Console.WriteLine("Jumpback1: " + debugCount); } dLastTimeStamp = dTimeStamp; lastTimeStampTime = Environment.TickCount; //DateTime junkme = new DateTime(); //UnixTime.GetDateTime((long) dTimeStamp1, out junkme); //Console.WriteLine(" DTIMESTAMP1: " + junkme); //UnixTime.GetDateTime((long)dTimeStamp2, out junkme); //Console.WriteLine(" DTIMESTAMP2: " + junkme); // Read packet that is first in time from whichever file. Leave other be if ((dTimeStamp != 0) && (!isEndFile)) { lastGoodTime = dTimeStamp; } else { Console.WriteLine("ERROR2 -- Should not be here!! ----------------------------"); } br.ReadByte(tempByte); aMITesDecoder.packet2[0] = tempByte[0]; br.ReadByte(tempByte); aMITesDecoder.packet2[1] = tempByte[0]; br.ReadByte(tempByte); aMITesDecoder.packet2[2] = tempByte[0]; br.ReadByte(tempByte); aMITesDecoder.packet2[3] = tempByte[0]; br.ReadByte(tempByte); aMITesDecoder.packet2[4] = tempByte[0]; br.ReadByte(tempByte); aMITesDecoder.packet2[5] = tempByte[0]; br.ReadByte(tempByte); aMITesDecoder.packet2[6] = tempByte[0]; //if the packet is for an HTC phone (i.e. packet[1]>=50)... read 1 additional byte aMITesDecoder.DecodeWocketsFrame(someData[dataIndex], aMITesDecoder.packet2); //aMITesDecoder.DecodeLastPacket(someData[dataIndex], false); // Don't swap bytes //Console.WriteLine("FileUsed: " + fileUsed); someData[dataIndex].timeStamp = UnixTime.IntTimeFromUnixTime(dTimeStamp, aRefDate); aMITesDecoder.SetUnixTime(someData[dataIndex], dTimeStamp); // Set the time someData[dataIndex].fileID = 1; dTimeStamp = 0; // Reset so gets read next time from file // dataIndex++; return(1); }