CheckHelper.cs 6.6 KB

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