public static string DecodeAsterixData(string FileName) { long Position = 0; // get total byte length of the file long TotalBytes = new System.IO.FileInfo(FileName).Length; try { // Open file for reading System.IO.FileStream FileStream = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); // attach filestream to binary reader System.IO.BinaryReader BinaryReader = new System.IO.BinaryReader(FileStream); while (Position < TotalBytes) { // First determine the size of the message // octet data // 0 ASTERIX CATEGORY // 1 .. 2 LENGTH OF MESSAGE BLOCK byte[] Data_Block_Buffer = BinaryReader.ReadBytes((Int32)3); int LengthOfMessageBlock = ASTERIX.ExtractLengthOfDataBlockInBytes_Int(Data_Block_Buffer); BinaryReader.BaseStream.Position = BinaryReader.BaseStream.Position - 3; // Now read the message and pass it to the parser // for decoding Data_Block_Buffer = BinaryReader.ReadBytes((Int32)LengthOfMessageBlock); ExtractAndDecodeASTERIX_CAT_DataBlock(Data_Block_Buffer, false); Position = BinaryReader.BaseStream.Position; } // close file reader FileStream.Close(); FileStream.Dispose(); BinaryReader.Close(); } catch (Exception Exception) { // Error } return("Read " + Position.ToString() + " of total " + TotalBytes.ToString() + " bytes"); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// // The method that gets invoked as a thread when a Connect button is // pressed. It will listen on a given multicast address and store messages in the // list box above. /////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void ListenForData() { bool ThereWasAnException = false; // File stream // Stream RecordingStream_debug = new FileStream(@"C:\ASTERIX\Italy", FileMode.Create); //BinaryWriter RecordingBinaryWriter_debug = new BinaryWriter(RecordingStream_debug); // Loop forever while (!_shouldStop) { // Do something only if user has requested so if (SharedData.bool_Listen_for_Data) { ThereWasAnException = false; try { // Lets receive data in an array of bytes // (an octet, of course composed of 8bits) UDPBuffer = sock.Receive(ref iep); LastDataBlockDateTimeForStalleData = DateTime.Now; } catch { ThereWasAnException = true; } if (ThereWasAnException == false) { int DataBufferIndexForThisExtraction = 0; // Check if this is non-standard 6 byte RMCDE header ASTERIX. // Now write the data block //RecordingBinaryWriter_debug.Write(UDPBuffer); // Extract lenghts int LengthOfASTERIX_CAT = ASTERIX.ExtractLengthOfDataBlockInBytes_Int(UDPBuffer); int LenghtOfDataBuffer = UDPBuffer.Length; // Loop through the buffer and pass on each ASTERIX category block to // the category extractor. The extractor itslef will then extract individual // data records. try { while (DataBufferIndexForThisExtraction < LenghtOfDataBuffer) { byte[] LocalSingle_ASTERIX_CAT_Buffer = new byte[LengthOfASTERIX_CAT]; Array.Copy(UDPBuffer, DataBufferIndexForThisExtraction, LocalSingle_ASTERIX_CAT_Buffer, 0, LengthOfASTERIX_CAT); ExtractAndDecodeASTERIX_CAT_DataBlock(LocalSingle_ASTERIX_CAT_Buffer, true); DataBufferIndexForThisExtraction = DataBufferIndexForThisExtraction + LengthOfASTERIX_CAT; if (DataBufferIndexForThisExtraction < LenghtOfDataBuffer) { Array.Copy(UDPBuffer, DataBufferIndexForThisExtraction, LocalSingle_ASTERIX_CAT_Buffer, 0, 3); LengthOfASTERIX_CAT = ASTERIX.ExtractLengthOfDataBlockInBytes_Int(LocalSingle_ASTERIX_CAT_Buffer); } } } catch { } // Check if recording of the currently live connection is requested if (SharedData.DataRecordingClass.DataRecordingRequested == true) { if (RecordingJustStarted == true) { RecordingJustStarted = false; RecordingStream = new FileStream(SharedData.DataRecordingClass.FilePathandName, FileMode.Create); RecordingBinaryWriter = new BinaryWriter(RecordingStream); LastDataBlockDateTimeForRecording = DateTime.Now; } } else if (RecordingJustStarted == false) { RecordingJustStarted = true; // Close the data stream if (RecordingBinaryWriter != null) { RecordingBinaryWriter.Close(); } if (RecordingStream != null) { RecordingStream.Close(); } } } } } _shouldStop = false; }