using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Collections; using System.Collections.Generic; using XdCxRhDW.Dto; using System.Security.Policy; using System.Runtime.InteropServices; using System.Text; namespace CheckServer { public static class CheckHelper { enum SigFlag : int { BPSK = 1, QPSK = 2 } struct slot { public SigMod mode; public int start; public int len; public SigFlag sigflag; public double ps;//码元速率 }; enum SigMod : int { bp96_ccow = 0, bp96_a8_10258 = 1, bp96_rccow = 2, bp96_b5_10169 = 3, bp96_b5_10171 = 4, bp192_c7_10241 = 5, bp192_c7_10242 = 6, bp192_c8_10274 = 7, qp160_a8_10257 = 8, qp160_b5_10166 = 9, qp160_b5_10167 = 10, qp160_b5_10168 = 11, qp160_b5_10170 = 12, qp160_b5_10172 = 13, qp160_c8_10273 = 14, qp160_c8_10275 = 15, qp160_c8_10276 = 16, } private const string dll = @"AddIns\PSignalCheck.dll";//张志明写的DAMA检测算法,根据特征码识别 #region cpp dll Interop [DllImport(dll, EntryPoint = "SigalEst", CallingConvention = CallingConvention.Cdecl)] private extern static void SigalEst(string file, long fsHz, int[] smpStart, int[] smpCount, int[] modes, double[] rates, double[] ffcs, double[] snrs, int len); [DllImport(dll, EntryPoint = "PSignalInit", CallingConvention = CallingConvention.Cdecl)] private static extern bool PSignalInit(); [DllImport(dll, EntryPoint = "PSignalCheck", CallingConvention = CallingConvention.Cdecl)] private static extern int PSignalCheck(String ifile, Int64 fsample, double snr, ref IntPtr signalslots); [DllImport(dll, EntryPoint = "pSignalFree", CallingConvention = CallingConvention.Cdecl)] private static extern void pSignalFree(IntPtr ipt); #endregion public static async Task> DmcCheckAsync(string fileName, double fsHz, EnumSigCheckTypeDto dmcType, double? bandKHz = null) { if (bandKHz == null || bandKHz.Value <= 0) bandKHz = 25; string dmcCmd = "all"; var dmp = new Process(); string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "AddIns\\dmc.exe"); string pArguments = string.Empty; switch (dmcType) { case EnumSigCheckTypeDto.DAMA: dmcCmd = "dm"; pArguments = $"{dmcCmd} \"{fileName}\" -c true";//-c包含ccow break; case EnumSigCheckTypeDto.IBS: dmcCmd = "nd"; pArguments = $"{dmcCmd} \"{fileName}\" -w {bandKHz}";// -f {fs}" -c --dmfirst"; break; case EnumSigCheckTypeDto.Ky5758: pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "AddIns\\enc.exe"); //enc.exe enc-test.dat 0.096 5 0全部文件 pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}"; break; default: break; } return await Task.Run(() => { dmp.StartInfo.FileName = pFileName; dmp.StartInfo.Arguments = pArguments; dmp.StartInfo.CreateNoWindow = true; dmp.StartInfo.RedirectStandardError = true; dmp.StartInfo.RedirectStandardOutput = true; dmp.StartInfo.UseShellExecute = false; StringBuilder sb = new StringBuilder(); dmp.OutputDataReceived += (sender, e) => { sb.AppendLine(e.Data); }; dmp.Start(); dmp.BeginOutputReadLine(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); dmp.WaitForExit(); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; var str = sb.ToString(); return ConvertDmcResult(dmcType, str, ts.TotalMilliseconds); }); } public static async Task> DAMACheckAsync(string fileName, double fsHz, EnumSigCheckTypeDto dmcType, double? bandKHz = null) { var val = await Task.Run(() => { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); IntPtr res = IntPtr.Zero; PSignalInit(); int rcount = PSignalCheck(fileName, (long)fsHz, 18, ref res); List rcontainer = new List(); for (int idx = 0; idx < rcount; ++idx) { IntPtr tmp = IntPtr.Add(res, idx * Marshal.SizeOf()); slot sl = Marshal.PtrToStructure(tmp); rcontainer.Add(sl); } pSignalFree(res); stopWatch.Stop(); foreach (var item in rcontainer) { var s= item.mode.ToString(); } return rcontainer.Select(t => new DmcResult() { DmcType = dmcType.GetEnumDisplayName(), Length = t.len, Start = t.start, UserName = t.mode.ToString(), Times = (int)stopWatch.ElapsedMilliseconds, }); }); return val; } private static IEnumerable ConvertDmcResult(EnumSigCheckTypeDto type, string res, double tm) { var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var items = line.Split('\t'); if (items.Length < 2) continue; int start = int.Parse(items[0]); int length = int.Parse(items[1]); string userName = ""; if (items.Length >= 3) userName = items[2]; var item = new DmcResult(start, length, userName, (int)tm); item.DmcType = type.GetEnumDisplayName(); yield return item; } } } }