| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 | 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 SigFlag sigflag;            public int start;            public int len;            public SigMod mode;            public double ps;//码元速率        };        enum SigMod : int        {            bp96_ccow = 0,            bp96_01 = 1,            bp96_02 = 2,            bp96_03 = 3,            bp96_04 = 4,            bp192_01 = 5,            bp192_02 = 6,            bp192_03 = 7,            qp16_01 = 8,            qp16_02 = 9,            qp16_03 = 10,            qp16_04 = 11,            qp16_05 = 12,            qp16_06 = 13,            qp16_07 = 14,            qp16_08 = 15,            qp16_09 = 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<IEnumerable<DmcResult>> 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";                    if (fsHz != 96000)                        throw new Exception("DAMA只支持96K采样率");                    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<IEnumerable<DmcResult>> 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<slot> rcontainer = new List<slot>();                       for (int idx = 0; idx < rcount; ++idx)                       {                           IntPtr tmp = IntPtr.Add(res, idx * Marshal.SizeOf<slot>());                           slot sl = Marshal.PtrToStructure<slot>(tmp);                           rcontainer.Add(sl);                       }                       pSignalFree(res);                       stopWatch.Stop();                       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<DmcResult> 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;            }        }    }}
 |