public void Crc32ModeS_Timing_Test() { var crcCalculator = new Crc32ModeS(); var count = 1000000; var fastShortTiming = DoTimingRun(crcCalculator, true, false, count); var fastLongTiming = DoTimingRun(crcCalculator, true, true, count); var slowShortTiming = DoTimingRun(crcCalculator, false, false, count); var slowLongTiming = DoTimingRun(crcCalculator, false, true, count); Assert.Inconclusive("Timings on {0:N0} messages: 'Fast' and short: {1}, 'Fast' and long: {2}, 'Slow' and short: {3}, 'Slow' and long: {4}", count, fastShortTiming, fastLongTiming, slowShortTiming, slowLongTiming); }
private TimeSpan DoTimingRun(Crc32ModeS calculator, bool useFastMethod, bool useLongMessage, int count) { var message = new byte[useLongMessage ? 11 : 4]; var random = new Random(); DateTime start, finish; if (useFastMethod) { start = DateTime.UtcNow; for (var i = 0; i < count; ++i) { random.NextBytes(message); calculator.ComputeChecksumBytes(message, 0, message.Length); } finish = DateTime.UtcNow; } else if (useLongMessage) { start = DateTime.UtcNow; for (var i = 0; i < count; ++i) { random.NextBytes(message); calculator.ComputeChecksumBytesTraditional88(message); } finish = DateTime.UtcNow; } else { start = DateTime.UtcNow; for (var i = 0; i < count; ++i) { random.NextBytes(message); calculator.ComputeChecksumBytesTraditional32(message); } finish = DateTime.UtcNow; } return(finish - start); }
/// <summary> /// AVR ASCii Format Input /// </summary> /// <param name="avrline"></param> /// <returns></returns> public static Plane ReadMessage(string avrline) { if (!avrline.StartsWith("*")) return null; log.Debug(avrline); avrline = avrline.Trim().TrimEnd(';'); ModeSMessage adsbmess = new ModeSMessage(); byte[] data = ConvertHexStringToByteArray(avrline.TrimStart('*')); if (data.Length < 13) return null; adsbmess.DF = (byte)(data[0] >> 3); // 5 adsbmess.CA = (byte)(data[0] & 7); // 3 // aircraft icao adsbmess.AA = (uint)((data[1] << 16) + (data[2] << 8) +(data[3])); // 24 Array.Copy(data, 4, adsbmess.adsbdata, 0, 7); // 56 bytes // paraity adsbmess.PI = (uint)((data[11] << 16) + (data[12] << 8) + (data[13])); // 24 adsbmess.recvtime = DateTime.Now; // check parity Crc32ModeS crc = new Crc32ModeS(); byte[] pidata = crc.ComputeChecksumBytes(data, 0, data.Length - 3, false); if (adsbmess.PI != ((pidata[0] << 16) + (pidata[1] << 8) + pidata[2])) { Console.WriteLine("Parity Fail"); return null; } // create the plane id string planeid = adsbmess.AA.ToString("X5"); if (!Planes.ContainsKey(planeid)) Planes[planeid] = new Plane() { ID = planeid }; if (adsbmess.DF == 17 && (adsbmess.TypeCode >= 9 && adsbmess.TypeCode <= 18) || (adsbmess.TypeCode >= 0x14 && adsbmess.TypeCode <= 0x16) ) // airbornepos { // odd if (adsbmess.Fcprformat) { ((Plane)Planes[adsbmess.AA.ToString("X5")]).llaodd = adsbmess; //Console.WriteLine("adsb " + planeid + " type " + adsbmess.DF + " odd"); } else // even { ((Plane)Planes[adsbmess.AA.ToString("X5")]).llaeven = adsbmess; //Console.WriteLine("adsb " + planeid + " type " + adsbmess.DF + " even"); } } else if (adsbmess.DF == 17 && adsbmess.TypeCode >= 1 && adsbmess.TypeCode <= 4) // ident { StringBuilder builder = new StringBuilder(); int count = 8; for (int i = 0; i < count; i++) { char ch = '\0'; byte[] char1 = adsbmess.getbits(adsbmess.adsbdata, 7 + i * 6, 6); byte num2 = char1[0]; if ((num2 > 0) && (num2 < 0x1b)) { ch = (char)(0x41 + (num2 - 1)); } else if (num2 == 0x20) { ch = ' '; } else if ((num2 > 0x2f) && (num2 < 0x3a)) { ch = (char)(0x30 + (num2 - 0x30)); } if (ch != '\0') { builder.Append(ch); } } adsbmess.Ident = builder.ToString(); //Console.WriteLine("Ident " + builder.ToString()); } else if (adsbmess.DF == 17 && adsbmess.TypeCode == 0x13) // velocity { int subtype = adsbmess.adsbdata[0] & 7; int accuracy = (adsbmess.adsbdata[1] >> 3) & 15; switch (subtype) { case 3: case 4: bool headingstatus = ((adsbmess.adsbdata[1] >> 2) & 1) > 0; if (headingstatus) { ushort head = (ushort)(((adsbmess.adsbdata[1] & 3) << 8) + adsbmess.adsbdata[2]); double heading = head * 0.3515625; ((Plane)Planes[adsbmess.AA.ToString("X5")]).heading = heading; } break; case 1: case 2: default: bool westvel = ((adsbmess.adsbdata[1] >> 2) & 1) > 0; int ewvel = (int)(((adsbmess.adsbdata[1] & 3) << 8) + adsbmess.adsbdata[2]); bool southvel = ((adsbmess.adsbdata[3] >> 7) & 1) > 0; int nsvel = (int)(((adsbmess.adsbdata[3] & 127) << 3) + (adsbmess.adsbdata[4] >> 5)); if (westvel) ewvel *= -1; if (southvel) nsvel *= -1; double cog = (Math.Atan2(ewvel, nsvel) * (180 / Math.PI)); Console.WriteLine("vel " + ewvel + " " + nsvel + " " + cog); ((Plane)Planes[adsbmess.AA.ToString("X5")]).heading = (cog + 360) % 360; break; } } else { Console.WriteLine("No processing type 0x" + adsbmess.TypeCode.ToString("X2") + " DF " + adsbmess.DF); } return ((Plane)Planes[adsbmess.AA.ToString("X5")]); }
/// <summary> /// AVR ASCii Format Input /// </summary> /// <param name="avrline"></param> /// <returns></returns> public static Plane ReadMessage(string avrline) { if (!avrline.StartsWith("*")) { return(null); } log.Debug(avrline); avrline = avrline.Trim().TrimEnd(';'); ModeSMessage adsbmess = new ModeSMessage(); byte[] data = ConvertHexStringToByteArray(avrline.TrimStart('*')); if (data.Length < 13) { return(null); } adsbmess.DF = (byte)(data[0] >> 3); // 5 adsbmess.CA = (byte)(data[0] & 7); // 3 // aircraft icao adsbmess.AA = (uint)((data[1] << 16) + (data[2] << 8) + (data[3])); // 24 Array.Copy(data, 4, adsbmess.adsbdata, 0, 7); // 56 bytes // paraity adsbmess.PI = (uint)((data[11] << 16) + (data[12] << 8) + (data[13])); // 24 adsbmess.recvtime = DateTime.Now; // check parity Crc32ModeS crc = new Crc32ModeS(); byte[] pidata = crc.ComputeChecksumBytes(data, 0, data.Length - 3, false); if (adsbmess.PI != ((pidata[0] << 16) + (pidata[1] << 8) + pidata[2])) { Console.WriteLine("Parity Fail"); return(null); } // create the plane id string planeid = adsbmess.AA.ToString("X5"); if (!Planes.ContainsKey(planeid)) { Planes[planeid] = new Plane() { ID = planeid } } ; if (adsbmess.DF == 17 && (adsbmess.TypeCode >= 9 && adsbmess.TypeCode <= 18) || (adsbmess.TypeCode >= 0x14 && adsbmess.TypeCode <= 0x16) ) // airbornepos { // odd if (adsbmess.Fcprformat) { ((Plane)Planes[adsbmess.AA.ToString("X5")]).llaodd = adsbmess; //Console.WriteLine("adsb " + planeid + " type " + adsbmess.DF + " odd"); } else // even { ((Plane)Planes[adsbmess.AA.ToString("X5")]).llaeven = adsbmess; //Console.WriteLine("adsb " + planeid + " type " + adsbmess.DF + " even"); } } else if (adsbmess.DF == 17 && adsbmess.TypeCode >= 1 && adsbmess.TypeCode <= 4) // ident { StringBuilder builder = new StringBuilder(); int count = 8; for (int i = 0; i < count; i++) { char ch = '\0'; byte[] char1 = adsbmess.getbits(adsbmess.adsbdata, 7 + i * 6, 6); byte num2 = char1[0]; if ((num2 > 0) && (num2 < 0x1b)) { ch = (char)(0x41 + (num2 - 1)); } else if (num2 == 0x20) { ch = ' '; } else if ((num2 > 0x2f) && (num2 < 0x3a)) { ch = (char)(0x30 + (num2 - 0x30)); } if (ch != '\0') { builder.Append(ch); } } adsbmess.Ident = builder.ToString(); //Console.WriteLine("Ident " + builder.ToString()); } else if (adsbmess.DF == 17 && adsbmess.TypeCode == 0x13) // velocity { int subtype = adsbmess.adsbdata[0] & 7; int accuracy = (adsbmess.adsbdata[1] >> 3) & 15; switch (subtype) { case 3: case 4: bool headingstatus = ((adsbmess.adsbdata[1] >> 2) & 1) > 0; if (headingstatus) { ushort head = (ushort)(((adsbmess.adsbdata[1] & 3) << 8) + adsbmess.adsbdata[2]); double heading = head * 0.3515625; ((Plane)Planes[adsbmess.AA.ToString("X5")]).heading = heading; } break; case 1: case 2: default: bool westvel = ((adsbmess.adsbdata[1] >> 2) & 1) > 0; int ewvel = (int)(((adsbmess.adsbdata[1] & 3) << 8) + adsbmess.adsbdata[2]); bool southvel = ((adsbmess.adsbdata[3] >> 7) & 1) > 0; int nsvel = (int)(((adsbmess.adsbdata[3] & 127) << 3) + (adsbmess.adsbdata[4] >> 5)); if (westvel) { ewvel *= -1; } if (southvel) { nsvel *= -1; } double cog = (Math.Atan2(ewvel, nsvel) * (180 / Math.PI)); Console.WriteLine("vel " + ewvel + " " + nsvel + " " + cog); ((Plane)Planes[adsbmess.AA.ToString("X5")]).heading = (cog + 360) % 360; break; } } else { Console.WriteLine("No processing type 0x" + adsbmess.TypeCode.ToString("X2") + " DF " + adsbmess.DF); } return((Plane)Planes[adsbmess.AA.ToString("X5")]); }