using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using XdCxRhDW.Dto; namespace GpuCgServer { /// /// GPU参数估计帮助类 /// public static class GpuCgHelper { private static string exePath = "AddIns"; private const string exeName = "XcorrGpu.exe"; /// /// 设置【ReSample.exe】文件所在路径,支持相对路径 /// public static void SetExePath(string path) { if (string.IsNullOrWhiteSpace(path)) return; if (path.StartsWith("\\"))//相对路径要么开头不带\,要么是 .\ path = path.Remove(0, 1); exePath = path; } /// /// GPU参估计算 /// /// 输入文件1 /// 输入文件2 /// 采样率Hz /// 样点数,0-1之间的小数表示样点百分比 /// 时差中心(us) /// 时差范围(us) /// 频差范围(Hz) /// 信噪比门限 /// 超时时间(s) /// public static List Calc(string file1, string file2, double fsHz, double smpCount, double dtoCenterus, double dtoRangeus, double dfoRange, double snr0, int timeoutSeconds = 120) { if (timeoutSeconds <= 0) timeoutSeconds = 120; FileInfo f1 = new FileInfo(file1); FileInfo f2 = new FileInfo(file2); long len = f1.Length <= f2.Length ? f1.Length / 4 : f2.Length / 4; if (smpCount <= 0 || smpCount > len) smpCount = len; if (smpCount > 0 && smpCount < 1) { smpCount = (long)(len * smpCount); } if (string.IsNullOrWhiteSpace(exePath)) throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径"); if (!Directory.Exists(exePath)) throw new Exception($"路径[{exePath}]不存在"); var exeFile = Path.Combine(exePath, exeName); if (!File.Exists(exeFile)) throw new Exception($"文件[{exeFile}]不存在"); Process p = new Process(); p.StartInfo.WorkingDirectory = exePath; p.StartInfo.FileName = exeFile; p.StartInfo.Arguments = $"-m \"{file1}\" -a \"{file2}\" -s {smpCount} -f {fsHz} -c {dtoCenterus} -r {dtoRangeus} -j {dfoRange} -t {snr0} -p {0}"; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; Stopwatch sw = new Stopwatch(); sw.Start(); p.Start(); var succeed = p.WaitForExit(timeoutSeconds * 1000); if (!succeed) { throw new Exception($"进程[{exeName}]超时未完成!"); } var res = p.StandardOutput.ReadToEnd(); sw.Stop(); if (sw.ElapsedMilliseconds < 800) { throw new Exception("没有GPU"); } List list = new List(); //3:-0.000*-0.00054.553+72.917*0.000*37.756+-72.917*0.000*37.671+ if (res.Length < 3) { list.Add(new GpuCgResponseDto() { Smplen=(long)smpCount, TimeMs=(int)sw.ElapsedMilliseconds, }); return list; } else { var xgfArr = res.Substring(2).Split("+".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var item in xgfArr) { var dtdfArr = item.Split("*".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); double.TryParse(dtdfArr[0], out double dt); double.TryParse(dtdfArr[1], out double df); double.TryParse(dtdfArr[2], out double snr); dt = Math.Round(dt, 4); df = Math.Round(df, 3); snr = Math.Round(snr, 1); GpuCgResponseDto dto = new GpuCgResponseDto() { Dt = dt, Df = df, Smplen = (long)smpCount, Snr = snr, TimeMs = (int)sw.ElapsedMilliseconds }; list.Add(dto); } return list; } } } }