Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        private bool SetupNextFiles(int index)
        {
            dTimeStamp = 0;

            if (br != null)
                br.CloseFile();

            br = null;

            string f = ((string) someBinaryFiles[index]);

            if (f != "")
            {
                br = new ByteReader(f);
                br.OpenFile();
                Console.WriteLine("Opening file for read: " + f);
            }

            if (br == null)
                return false;
            else
                return true;
        }
Exemplo n.º 3
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>
        /// <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;
        }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
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>
        /// <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;
            }
        }
Exemplo n.º 7
0
//		private String[] SplitString(String aString)
//		{
//			return (new Regex(" ")).Split(aString);
//		}
    
		/// <summary>
		/// Test method
		/// </summary>
		static void Main()
		{
			String inFile = "\\My Documents\\testBytes.txt";        
			Console.WriteLine("Infile: " + inFile);  
			ByteReader br = new ByteReader(inFile);
			br.OpenFile();
			bool gotByte = false;
			int[] i = new int[1];
			//byte[] b = new byte[1]; 
				       
			do
			{
				//gotByte = br.ReadByte(b);
				gotByte = br.ReadInt(i);
				if (gotByte)
				{
					Console.Write(i);
				}
				else
					Console.WriteLine("Could not read an int as bytes");
			} while (gotByte);
				        
			br.CloseFile();
		}
Exemplo n.º 8
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>
        /// <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;
        }
Exemplo n.º 9
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 DecodePLFormatMR(MITesData[] someData, int dataIndex, ByteReader br1, ByteReader br2)
        {
            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);

            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)
            {
                //Console.WriteLine("End of file 1: " + GetDateTime(lastGoodTime1) + " " + GetDateTime(lastGoodTime2));
                isEndFile1 = true; 
            }

            if (dTimeStamp2 == (double)MITesData.NONE)
            {
                //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 = br1; 

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

            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

            //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; 
                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
                dTimeStamp2 = 0; // Reset so gets read next time from file 
                someData[dataIndex].fileID = 2;
            }
            else
            {
                Console.WriteLine("ERROR: no file used");
            }

//            dataIndex++;
            return 1;
        }
Exemplo n.º 10
0
	    private bool SetupNextFiles(int index)
		{
	        dTimeStamp1 = 0;
	        dTimeStamp2 = 0; 

            if (br1 != null)
                br1.CloseFile();
	        if (br2 != null)
                br2.CloseFile();

	        br1 = null;
	        br2 = null; 
	       
	        string f1 = ((BFileInfo) someBinaryFiles[index]).fileRec1;  
	        string f2 = ((BFileInfo) someBinaryFiles[index]).fileRec2;  
            if (f1 != "")
            {
                br1 = new ByteReader(f1);
                br1.OpenFile();              
                Console.WriteLine("Opening file for read: " + f1);
            }
            if (f2 != "")
            {
                br2 = new ByteReader(f2);
                br2.OpenFile();
                Console.WriteLine("Opening file for read: " + f2);
            }

            if ((br1 == null) || (br2 == null))
                return false;
            else
            {
                return true;                 
            }
		}
        /// <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;
        }
 private void IgnorePacket(ByteReader brTemp)
 {
     if (brTemp != null)
     {
         brTemp.ReadByte(tempByte);
         brTemp.ReadByte(tempByte);
         brTemp.ReadByte(tempByte);
         brTemp.ReadByte(tempByte);
         brTemp.ReadByte(tempByte);
     }
 }
	    private bool SetupNextFiles(int index)
		{
	        dTimeStamp1 = 0;
	        dTimeStamp2 = 0; 

            if (br != null)
                br.CloseFile();

	        br = null;
	       
	        string f1 = ((BFileInfo) someBinaryFiles[index]).fileRec1;  
            if (f1 != "")
            {
                br = new ByteReader(f1);
                br.OpenFile();              
                Console.WriteLine("Opening file for read: " + f1);
            }

            if (br == null)
                return false;
            else
                return true;                 
		}