/// <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>
        /// <returns>The new index for the someData array.</returns>
        public int DecodePLFormatMR(MITesData[] someData, int dataIndex, ByteReader br1)
        {
            bool isGoodPacketRead = false;

            isEndFile1 = 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 ((dTimeStamp1 - dLastTimeStamp1) > (2 * 60 * 60 * 1000))
            {
                Console.WriteLine("Skip of more than two hours in file 1");
            }

            debugCount++;

            DateTime dt1 = new DateTime();

            UnixTime.GetDateTime((long)dTimeStamp1, out dt1);

            //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 (isEndFile1)
            {
                Console.WriteLine("End of file.");
                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("Jumpback: " + debugCount + " " + (dLastTimeStamp1 - dTimeStamp1));
            }

            dLastTimeStamp1    = dTimeStamp1;
            lastTimeStampTime1 = Environment.TickCount;

            // Read packet that is first in time from whichever file. Leave other be

            ByteReader brTemp;

            brTemp = null;

            if (!isEndFile1)
            {
                lastGoodTime1 = dTimeStamp1;
                brTemp        = br1;
                fileUsed      = 1;
            }

            // 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 (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
            {
                //Console.WriteLine("ERROR: no file used");
                return(-1);
            }

//            dataIndex++;
            return(1);
        }
Beispiel #2
0
        /// <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 DecodePLFormat(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.packet[0] = tempByte[0];
            br.ReadByte(tempByte);
            aMITesDecoder.packet[1] = tempByte[0];
            br.ReadByte(tempByte);
            aMITesDecoder.packet[2] = tempByte[0];
            br.ReadByte(tempByte);
            aMITesDecoder.packet[3] = tempByte[0];
            br.ReadByte(tempByte);
            aMITesDecoder.packet[4] = tempByte[0];

            if (aMITesDecoder.packet[0] == MITesDecoder.MAX_CHANNEL)
            {
                br.ReadByte(tempByte);
                aMITesDecoder.packet[5] = tempByte[0];
            }
            else
            {
                aMITesDecoder.packet[5] = 0;
            }

            //if the packet is for an HTC phone (i.e. packet[1]>=50)... read 1 additional byte

            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);
        }