/// <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"); 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) { Console.WriteLine("ERROR: Last unix time is zero for some reason!"); } // 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; }
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; }
/// <summary> /// Decode 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="numPackets">The number of packets to read.</param> /// <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="br">A ByteReader object that has been opened to the proper file.</param> /// <returns>The new index for the someData array.</returns> public int DecodeMITesLoggerDataOld(int numPackets, MITesData[] someData, int dataIndex, ByteReader br) { int valuesGrabbed = 0; for (int i = 0; i < numPackets; i++) { if (timeCount == 0) { timeStamp = MITesLoggerReaderNew.ReadTimeStamp(br); Console.WriteLine("TIME: " + timeStamp); } timeCount++; if (timeCount == MITesLoggerNew.TIMESTAMP_AFTER_SAMPLES) { timeCount = 0; } br.ReadByte(tempByte); packet[0] = tempByte[0]; br.ReadByte(tempByte); packet[1] = tempByte[0]; br.ReadByte(tempByte); packet[2] = tempByte[0]; br.ReadByte(tempByte); packet[3] = tempByte[0]; br.ReadByte(tempByte); packet[4] = tempByte[0]; br.ReadByte(tempByte); diffMS = (int)tempByte[0]; Thread.Sleep(diffMS); timeStamp += diffMS; // MAYBE WORKS? SSI if (dataIndex >= someData.Length) Warning("MAX_MITES_DATA too small! Losing info. Size:" + someData.Length); else { DecodeLastPacket(someData[dataIndex], false); // Don't swap bytes someData[dataIndex].timeStamp = timeStamp; // Set the time dataIndex++; } valuesGrabbed++; } if (valuesGrabbed != numPackets) Console.WriteLine("ERROR: something wrong!!!"); if (valuesGrabbed < someData.Length) return valuesGrabbed; else return someData.Length; }
/// <summary> /// Decode 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="numPackets">The number of packets to read.</param> /// <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="br">A ByteReader object that has been opened to the proper file.</param> /// <returns>The new index for the someData array.</returns> public int DecodeMITesLoggerDataVariable(int numPackets, MITesData[] someData, int dataIndex, ByteReader br) { if (isWaitTime) Console.WriteLine("Waiting: " + timeToWait); if (!isWaitTime) { //Get time to wait timeStamp = MITesLoggerReaderNew.ReadTimeStamp(br); if (timeStamp == MITesData.NONE) { Console.WriteLine("End of file."); return 0; } diffMS = timeStamp - lastTimeStamp; if ((lastTimeStamp != 0) && (timeStamp != 0)) timeToWait = diffMS; else timeToWait = 0; lastTimeStamp = timeStamp; lastTimeStampTime = Environment.TickCount; isWaitTime = true; return 0; } else { if ((Environment.TickCount - lastTimeStampTime) >= timeToWait) { br.ReadByte(tempByte); packet[0] = tempByte[0]; br.ReadByte(tempByte); packet[1] = tempByte[0]; br.ReadByte(tempByte); packet[2] = tempByte[0]; br.ReadByte(tempByte); packet[3] = tempByte[0]; br.ReadByte(tempByte); packet[4] = tempByte[0]; DecodeLastPacket(someData[dataIndex], false); // Don't swap bytes someData[dataIndex].timeStamp = timeStamp; // Set the time dataIndex++; isWaitTime = false; return 1; } else return 0; } }
/// <summary> /// Decode 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="numPackets">The number of packets to read.</param> /// <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="br">A ByteReader object that has been opened to the proper file.</param> /// <param name="isPLFormat">True if PLFormat, false if older version.</param> /// <returns>The new index for the someData array.</returns> public int DecodeMITesLoggerData(int numPackets, MITesData[] someData, int dataIndex, ByteReader br, bool isPLFormat) { if (isPLFormat) return DecodeMITesLoggerDataPLFormat(numPackets, someData, dataIndex, br); if (numPackets == 0) return DecodeMITesLoggerDataVariable(numPackets, someData, dataIndex, br); int valuesGrabbed = 0; for (int i = 0; i < numPackets; i++) { timeStamp = MITesLoggerReaderNew.ReadTimeStamp(br); //Console.WriteLine ("TIME: " + timeStamp); diffMS = timeStamp - lastTimeStamp; if ((lastTimeStamp != 0) && (timeStamp != 0)) { //Console.WriteLine ("Sleep: " + diffMS); Thread.Sleep(diffMS); } lastTimeStamp = timeStamp; br.ReadByte(tempByte); packet[0] = tempByte[0]; br.ReadByte(tempByte); packet[1] = tempByte[0]; br.ReadByte(tempByte); packet[2] = tempByte[0]; br.ReadByte(tempByte); packet[3] = tempByte[0]; br.ReadByte(tempByte); packet[4] = tempByte[0]; if (dataIndex >= someData.Length) Warning("MAX_MITES_DATA too small! Losing info. Size:" + someData.Length); else { DecodeLastPacket(someData[dataIndex], false); // Don't swap bytes someData[dataIndex].timeStamp = timeStamp; // Set the time dataIndex++; } valuesGrabbed++; } if (valuesGrabbed != numPackets) Console.WriteLine("ERROR: something wrong!!!"); if (valuesGrabbed < someData.Length) return valuesGrabbed; else return someData.Length; }
/// <summary> /// Decode 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="numPackets">The number of packets to read.</param> /// <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="br">A ByteReader object that has been opened to the proper file.</param> /// <returns>The new index for the someData array.</returns> public int DecodeMITesLoggerDataPLFormat(int numPackets, MITesData[] someData, int dataIndex, ByteReader br) { //Get new time to wait dTimeStamp = MITesLoggerReaderNew.ReadUnixTimeStamp(br); if (dTimeStamp == (double)MITesData.NONE) { Console.WriteLine("End of file."); return 0; } diffMS = (int)(dTimeStamp - dLastTimeStamp); if ((dLastTimeStamp != 0) && (dTimeStamp != 0)) timeToWait = diffMS; else timeToWait = 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; } dLastTimeStamp = dTimeStamp; lastTimeStampTime = Environment.TickCount; br.ReadByte(tempByte); packet[0] = tempByte[0]; br.ReadByte(tempByte); packet[1] = tempByte[0]; br.ReadByte(tempByte); packet[2] = tempByte[0]; br.ReadByte(tempByte); packet[3] = tempByte[0]; br.ReadByte(tempByte); packet[4] = tempByte[0]; DecodeLastPacket(someData[dataIndex], false); // Don't swap bytes SetUnixTime(someData[dataIndex], dTimeStamp);// Set the time dataIndex++; return 1; }
private void IgnorePacket(ByteReader brTemp) { if (brTemp != null) { brTemp.ReadByte(tempByte); brTemp.ReadByte(tempByte); brTemp.ReadByte(tempByte); brTemp.ReadByte(tempByte); brTemp.ReadByte(tempByte); } }