/// <summary> /// Get the number of bytes in the data type. /// This is based off the number of NMEA strings in the data set. /// </summary> /// <returns>Number of bytes for the data type.</returns> public override int GetDataTypeSize() { // Each NMEA string has a 14 byte header int numBytes = 0; foreach (string nmea in NmeaStrings) { numBytes += Pd0NmeaData.GetDataTypeSize(nmea); } return(numBytes); }
/// <summary> /// Decode the given data into an object. /// </summary> /// <param name="data">Data to decode.</param> /// <param name="numDepthCells">NOT USED.</param> /// <param name="numBeams">NOT USED</param> public void Decode(byte[] data, int numDepthCells = 0, int numBeams = 4) { try { if (data.Length > HEADER_MIN_BYTE) { // Ensure the correct data type is given if (data[0] == ID_LSB && data[1] == ID_MSB) { // Number of bytes NumberOfBytes = MathHelper.LsbMsbInt(data[2], data[3]); //DataTypes.Clear(); // Clear anything currently stored int numDT = data[5]; // Number of Data types int dtOffset = HEADER_MIN_BYTE; // Start of the offsets RTI.PD0.CoordinateTransforms xform = RTI.PD0.CoordinateTransforms.Coord_Earth; // Determine the size of each data type. // This will start at the end of the header //int prevOffset = dtOffset + (numDT * SHORT_NUM_BYTE); // Collect each offset for (int x = 0; x < numDT; x++) { // Get the offset and add it to the list byte lsb = data[dtOffset]; byte msb = data[dtOffset + 1]; ushort offset = MathHelper.LsbMsbUShort(lsb, msb); if (data.Length > offset) { // Determine the data type byte lsbID = data[offset]; byte msbID = data[offset + 1]; Pd0ID id = Pd0ID.GetType(lsbID, msbID); // Set the XFORM // Create and add the data type to the ensemble switch (id.Type) { case Pd0ID.Pd0Types.FixedLeader: // Copy the buffer byte[] flBuffer = new byte[Pd0FixedLeader.DATATYPE_SIZE]; Buffer.BlockCopy(data, offset, flBuffer, 0, flBuffer.Length); if (DataTypes.ContainsKey(Pd0ID.Pd0Types.FixedLeader)) { GetFixedLeader().Decode(flBuffer); xform = GetFixedLeader().GetCoordinateTransform(); } else { AddDataType(new Pd0FixedLeader(flBuffer, offset)); } break; case Pd0ID.Pd0Types.VariableLeader: // Copy the buffer byte[] vlBuffer = new byte[Pd0VariableLeader.DATATYPE_SIZE]; Buffer.BlockCopy(data, offset, vlBuffer, 0, vlBuffer.Length); if (DataTypes.ContainsKey(Pd0ID.Pd0Types.VariableLeader)) { GetVariableLeader().Decode(vlBuffer); } else { AddDataType(new Pd0VariableLeader(vlBuffer, offset)); } break; case Pd0ID.Pd0Types.BottomTrack: // Copy the buffer byte[] btBuffer = new byte[Pd0BottomTrack.DATATYPE_SIZE]; Buffer.BlockCopy(data, offset, btBuffer, 0, btBuffer.Length); // Decode the data if (DataTypes.ContainsKey(Pd0ID.Pd0Types.BottomTrack)) { GetBottomTrack().Decode(btBuffer, GetFixedLeader().NumberOfBeams); } else { AddDataType(new Pd0BottomTrack(btBuffer, offset, GetFixedLeader().NumberOfBeams)); } break; case Pd0ID.Pd0Types.Velocity: // Copy the buffer byte[] velBuffer = new byte[Pd0Velocity.GetVelocitySize(GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)]; Buffer.BlockCopy(data, offset, velBuffer, 0, velBuffer.Length); // Decode the data if (DataTypes.ContainsKey(Pd0ID.Pd0Types.Velocity)) { GetVelocity().Decode(velBuffer, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams); } else { AddDataType(new Pd0Velocity(velBuffer, offset, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)); } break; case Pd0ID.Pd0Types.Correlation: // Copy the buffer byte[] corrBuffer = new byte[Pd0Correlation.GetCorrelationSize(GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)]; Buffer.BlockCopy(data, offset, corrBuffer, 0, corrBuffer.Length); // Decode the data if (DataTypes.ContainsKey(Pd0ID.Pd0Types.Correlation)) { GetCorrelation().Decode(corrBuffer, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams); } else { AddDataType(new Pd0Correlation(corrBuffer, offset, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)); } break; case Pd0ID.Pd0Types.EchoIntensity: // Copy the buffer byte[] eiBuffer = new byte[Pd0EchoIntensity.GetEchoIntensitySize(GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)]; Buffer.BlockCopy(data, offset, eiBuffer, 0, eiBuffer.Length); // Decode the data if (DataTypes.ContainsKey(Pd0ID.Pd0Types.EchoIntensity)) { GetEchoIntensity().Decode(eiBuffer, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams); } else { AddDataType(new Pd0EchoIntensity(eiBuffer, offset, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)); } break; case Pd0ID.Pd0Types.PercentGood: // Copy the buffer byte[] pgBuffer = new byte[Pd0PercentGood.GetPercentGoodSize(GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)]; Buffer.BlockCopy(data, offset, pgBuffer, 0, pgBuffer.Length); // Decode the data if (DataTypes.ContainsKey(Pd0ID.Pd0Types.PercentGood)) { GetPercentGood().Decode(pgBuffer, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams); } else { AddDataType(new Pd0PercentGood(pgBuffer, offset, GetFixedLeader().NumberOfCells, GetFixedLeader().NumberOfBeams)); } break; case Pd0ID.Pd0Types.NmeaData: // Get the Dataset Size // Get the first 6 bytes to determine the size // Number of bytes to read for NMEA size byte[] nmeaSize = new byte[6]; Buffer.BlockCopy(data, offset, nmeaSize, 0, 6); int nmeaDsSize = Pd0NmeaData.GetNmeaDataSize(nmeaSize); // Copy the buffer byte[] nmeaBuffer = new byte[nmeaDsSize]; Buffer.BlockCopy(data, offset, nmeaBuffer, 0, nmeaBuffer.Length); // Decode the data if (DataTypes.ContainsKey(Pd0ID.Pd0Types.NmeaData)) { GetNmeaData().Decode(nmeaBuffer); } else { AddDataType(new Pd0NmeaData(nmeaBuffer, offset)); } break; default: Debug.WriteLine(string.Format("Unknown PD0 ID {0}", id.Type)); break; } } // Move the offset to the next value dtOffset += 2; } } } } catch (Exception ex) { log.Error("Error Decoding PD0 Data. ", ex); } }
/// <summary> /// Convert the object to a byte array to have /// binary data. /// </summary> /// <returns>Binary Array for binary format.</returns> public override byte[] Encode() { // Number of bytes byte numByteLsb; byte numByteMsb; MathHelper.LsbMsb(NumberOfBytes, out numByteLsb, out numByteMsb); // Create the array byte[] data = new byte[GetDataTypeSize()]; data[0] = ID_LSB; // Header ID LSB data[1] = ID_MSB; // Header ID MSB data[2] = numByteLsb; // Number of Bytes LSB data[3] = numByteMsb; // Number of Bytes MSB data[4] = 0x0f; // Spare data[5] = (byte)DataTypes.Count; // Number of Data Types // Start location is based off the number of data types // Each data type will consume 2 bytes for the offset // The header requires 6 bytes int start = (DataTypes.Count * SHORT_NUM_BYTE) + HEADER_MIN_BYTE; int x = HEADER_MIN_BYTE; // Add all the offset for each data type foreach (var dt in DataTypes) { // Get the next offset location // and convert to 2 bytes // Then add it to the array byte lsb; byte msb; MathHelper.LsbMsb(start, out lsb, out msb); data[x++] = lsb; data[x++] = msb; // Set the offset value dt.Value.Offset = (ushort)start; if (dt.Key != Pd0ID.Pd0Types.NmeaData) { // Add in the size plus the last location start += GetDataTypeSize(dt.Value); } else { if (dt.Key == Pd0ID.Pd0Types.NmeaData) { // Set the offset value dt.Value.Offset = (ushort)start; // Loop through all the NMEA strings to create an offset for each Pd0NmeaData nmeaDS = (Pd0NmeaData)dt.Value; foreach (var nmea in nmeaDS.NmeaStrings) { // Add in the size plus the last location start += Pd0NmeaData.GetDataTypeSize(nmea); // Set each NMEA string offset MathHelper.LsbMsb(start, out lsb, out msb); data[x++] = lsb; data[x++] = msb; } // Move the start location to the end now //start += GetDataTypeSize(dt.Value); } } } return(data); }