CheckHelper.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using XdCxRhDW.Dto;
  10. using System.Security.Policy;
  11. using System.Runtime.InteropServices;
  12. using DevExpress.Utils.Text.Internal;
  13. using System.Text;
  14. namespace CheckServer
  15. {
  16. public static class CheckHelper
  17. {
  18. enum SigFlag : int
  19. {
  20. BPSK = 1,
  21. QPSK = 2
  22. }
  23. struct slot
  24. {
  25. public SigFlag sigflag;
  26. public int start;
  27. public int len;
  28. public SigMod mode;
  29. public double ps;//码元速率
  30. };
  31. enum SigMod : int
  32. {
  33. bp96_ccow = 0,
  34. bp96_01 = 1,
  35. bp96_02 = 2,
  36. bp96_03 = 3,
  37. bp96_04 = 4,
  38. bp192_01 = 5,
  39. bp192_02 = 6,
  40. bp192_03 = 7,
  41. qp16_01 = 8,
  42. qp16_02 = 9,
  43. qp16_03 = 10,
  44. qp16_04 = 11,
  45. qp16_05 = 12,
  46. qp16_06 = 13,
  47. qp16_07 = 14,
  48. qp16_08 = 15,
  49. qp16_09 = 16,
  50. }
  51. private const string dll = @"AddIns\PSignalCheck.dll";//张志明写的DAMA检测算法,根据特征码识别
  52. #region cpp dll Interop
  53. [DllImport(dll, EntryPoint = "SigalEst", CallingConvention = CallingConvention.Cdecl)]
  54. private extern static void SigalEst(string file, long fsHz, int[] smpStart, int[] smpCount, int[] modes, double[] rates, double[] ffcs, double[] snrs, int len);
  55. [DllImport(dll, EntryPoint = "PSignalInit", CallingConvention = CallingConvention.Cdecl)]
  56. private static extern bool PSignalInit();
  57. [DllImport(dll, EntryPoint = "PSignalCheck", CallingConvention = CallingConvention.Cdecl)]
  58. private static extern int PSignalCheck(String ifile, Int64 fsample, double snr, ref IntPtr signalslots);
  59. [DllImport(dll, EntryPoint = "pSignalFree", CallingConvention = CallingConvention.Cdecl)]
  60. private static extern void pSignalFree(IntPtr ipt);
  61. #endregion
  62. public static async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, EnumSigCheckTypeDto dmcType, double? bandKHz = null)
  63. {
  64. if (bandKHz == null || bandKHz.Value <= 0) bandKHz = 25;
  65. string dmcCmd = "all";
  66. var dmp = new Process();
  67. string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "AddIns\\dmc.exe");
  68. string pArguments = string.Empty;
  69. switch (dmcType)
  70. {
  71. case EnumSigCheckTypeDto.DAMA:
  72. dmcCmd = "dm";
  73. if (fsHz != 96000)
  74. throw new Exception("DAMA只支持96K采样率");
  75. pArguments = $"{dmcCmd} \"{fileName}\" -c true";//-c包含ccow
  76. break;
  77. case EnumSigCheckTypeDto.IBS:
  78. dmcCmd = "nd";
  79. pArguments = $"{dmcCmd} \"{fileName}\" -w {bandKHz}";// -f {fs}" -c --dmfirst";
  80. break;
  81. case EnumSigCheckTypeDto.Ky5758:
  82. pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "AddIns\\enc.exe");
  83. //enc.exe enc-test.dat 0.096 5 0全部文件
  84. pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
  85. break;
  86. default:
  87. break;
  88. }
  89. return await Task.Run(() =>
  90. {
  91. dmp.StartInfo.FileName = pFileName;
  92. dmp.StartInfo.Arguments = pArguments;
  93. dmp.StartInfo.CreateNoWindow = true;
  94. dmp.StartInfo.RedirectStandardError = true;
  95. dmp.StartInfo.RedirectStandardOutput = true;
  96. dmp.StartInfo.UseShellExecute = false;
  97. StringBuilder sb = new StringBuilder();
  98. dmp.OutputDataReceived += (sender, e) =>
  99. {
  100. sb.AppendLine(e.Data);
  101. };
  102. dmp.Start();
  103. dmp.BeginOutputReadLine();
  104. Stopwatch stopWatch = new Stopwatch();
  105. stopWatch.Start();
  106. dmp.WaitForExit();
  107. stopWatch.Stop();
  108. TimeSpan ts = stopWatch.Elapsed;
  109. var str = sb.ToString();
  110. return ConvertDmcResult(dmcType, str, ts.TotalMilliseconds);
  111. });
  112. }
  113. public static async Task<IEnumerable<DmcResult>> DAMACheckAsync(string fileName, double fsHz, EnumSigCheckTypeDto dmcType, double? bandKHz = null)
  114. {
  115. var val = await Task.Run(() =>
  116. {
  117. Stopwatch stopWatch = new Stopwatch();
  118. stopWatch.Start();
  119. IntPtr res = IntPtr.Zero;
  120. PSignalInit();
  121. int rcount = PSignalCheck(fileName, (long)fsHz, 18, ref res);
  122. List<slot> rcontainer = new List<slot>();
  123. for (int idx = 0; idx < rcount; ++idx)
  124. {
  125. IntPtr tmp = IntPtr.Add(res, idx * Marshal.SizeOf<slot>());
  126. slot sl = Marshal.PtrToStructure<slot>(tmp);
  127. rcontainer.Add(sl);
  128. }
  129. pSignalFree(res);
  130. stopWatch.Stop();
  131. return rcontainer.Select(t => new DmcResult()
  132. {
  133. DmcType = dmcType.GetEnumDisplayName(),
  134. Length = t.len,
  135. Start = t.start,
  136. //UserName = t.mode.ToString(),
  137. Times = (int)stopWatch.ElapsedMilliseconds,
  138. });
  139. });
  140. return val;
  141. }
  142. private static IEnumerable<DmcResult> ConvertDmcResult(EnumSigCheckTypeDto type, string res, double tm)
  143. {
  144. var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
  145. foreach (var line in lines)
  146. {
  147. var items = line.Split('\t');
  148. if (items.Length < 2) continue;
  149. int start = int.Parse(items[0]);
  150. int length = int.Parse(items[1]);
  151. string userName = "";
  152. if (items.Length >= 3)
  153. userName = items[2];
  154. var item = new DmcResult(start, length, userName, (int)tm);
  155. item.DmcType = type.GetEnumDisplayName();
  156. yield return item;
  157. }
  158. }
  159. }
  160. }