CheckHelper.cs 6.7 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 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 SigMod mode;
  25. public int start;
  26. public int len;
  27. public SigFlag sigflag;
  28. public double ps;//码元速率
  29. };
  30. enum SigMod : int
  31. {
  32. bp96_ccow = 0,
  33. bp96_a8_10258 = 1,
  34. bp96_rccow = 2,
  35. bp96_b5_10169 = 3,
  36. bp96_b5_10171 = 4,
  37. bp192_c7_10241 = 5,
  38. bp192_c7_10242 = 6,
  39. bp192_c8_10274 = 7,
  40. qp160_a8_10257 = 8,
  41. qp160_b5_10166 = 9,
  42. qp160_b5_10167 = 10,
  43. qp160_b5_10168 = 11,
  44. qp160_b5_10170 = 12,
  45. qp160_b5_10172 = 13,
  46. qp160_c8_10273 = 14,
  47. qp160_c8_10275 = 15,
  48. qp160_c8_10276 = 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. pArguments = $"{dmcCmd} \"{fileName}\" -c true";//-c包含ccow
  73. break;
  74. case EnumSigCheckTypeDto.IBS:
  75. dmcCmd = "nd";
  76. pArguments = $"{dmcCmd} \"{fileName}\" -w {bandKHz}";// -f {fs}" -c --dmfirst";
  77. break;
  78. case EnumSigCheckTypeDto.Ky5758:
  79. pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "AddIns\\enc.exe");
  80. //enc.exe enc-test.dat 0.096 5 0全部文件
  81. pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
  82. break;
  83. default:
  84. break;
  85. }
  86. return await Task.Run(() =>
  87. {
  88. dmp.StartInfo.FileName = pFileName;
  89. dmp.StartInfo.Arguments = pArguments;
  90. dmp.StartInfo.CreateNoWindow = true;
  91. dmp.StartInfo.RedirectStandardError = true;
  92. dmp.StartInfo.RedirectStandardOutput = true;
  93. dmp.StartInfo.UseShellExecute = false;
  94. StringBuilder sb = new StringBuilder();
  95. dmp.OutputDataReceived += (sender, e) =>
  96. {
  97. sb.AppendLine(e.Data);
  98. };
  99. dmp.Start();
  100. dmp.BeginOutputReadLine();
  101. Stopwatch stopWatch = new Stopwatch();
  102. stopWatch.Start();
  103. dmp.WaitForExit();
  104. stopWatch.Stop();
  105. TimeSpan ts = stopWatch.Elapsed;
  106. var str = sb.ToString();
  107. return ConvertDmcResult(dmcType, str, ts.TotalMilliseconds);
  108. });
  109. }
  110. public static async Task<IEnumerable<DmcResult>> DAMACheckAsync(string fileName, double fsHz, EnumSigCheckTypeDto dmcType, double? bandKHz = null)
  111. {
  112. var val = await Task.Run(() =>
  113. {
  114. Stopwatch stopWatch = new Stopwatch();
  115. stopWatch.Start();
  116. IntPtr res = IntPtr.Zero;
  117. PSignalInit();
  118. int rcount = PSignalCheck(fileName, (long)fsHz, 18, ref res);
  119. List<slot> rcontainer = new List<slot>();
  120. for (int idx = 0; idx < rcount; ++idx)
  121. {
  122. IntPtr tmp = IntPtr.Add(res, idx * Marshal.SizeOf<slot>());
  123. slot sl = Marshal.PtrToStructure<slot>(tmp);
  124. rcontainer.Add(sl);
  125. }
  126. pSignalFree(res);
  127. stopWatch.Stop();
  128. foreach (var item in rcontainer)
  129. {
  130. var s= item.mode.ToString();
  131. }
  132. return rcontainer.Select(t => new DmcResult()
  133. {
  134. DmcType = dmcType.GetEnumDisplayName(),
  135. Length = t.len,
  136. Start = t.start,
  137. UserName = t.mode.ToString(),
  138. Times = (int)stopWatch.ElapsedMilliseconds,
  139. });
  140. });
  141. return val;
  142. }
  143. private static IEnumerable<DmcResult> ConvertDmcResult(EnumSigCheckTypeDto type, string res, double tm)
  144. {
  145. var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
  146. foreach (var line in lines)
  147. {
  148. var items = line.Split('\t');
  149. if (items.Length < 2) continue;
  150. int start = int.Parse(items[0]);
  151. int length = int.Parse(items[1]);
  152. string userName = "";
  153. if (items.Length >= 3)
  154. userName = items[2];
  155. var item = new DmcResult(start, length, userName, (int)tm);
  156. item.DmcType = type.GetEnumDisplayName();
  157. yield return item;
  158. }
  159. }
  160. }
  161. }