// // ****************************************************************** // // /\ /| @File Program.cs // // \ V/ @Brief // // | "") @Author lijinwen, ghz005@uni-trend.com.cn // // / | @Creation 2024-1-17 // // / \\ @Modified 2024-5-21 // // *(__\_\ // // ****************************************************************** // // *********************** USB 解码测试 ************************** // // ****************************************************************** using System.Diagnostics; using System.Runtime.InteropServices; using ComModel; using TekWaveformSampling; using TekWaveformSampling.Model; using static ComModel.ProtocolBase; using UInt64 = System.UInt64; namespace ConsoleAppCsvRead; abstract internal class Program { // 导入C++ DLL中的函数 [DllImport("ProtocolDecoder.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void setCancellationSignal([MarshalAs(UnmanagedType.I1)] bool value); [DllImport("ProtocolDecoder.dll", CallingConvention = CallingConvention.Cdecl)] public static extern bool Parse_USB(Protocol_USB_Options options, ProtocolBase.TwoLevelEdgePulseData edgePulseData1, ProtocolBase.TwoLevelEdgePulseData edgePulseData2, out UsbDecodeResultStruct decodeResult); const bool ENABLE_DIFF_SIGN = false; static void Main() { // CSV文件路径 //string filePath1 = "u1_000.csv"; //string filePath2 = "u2_000.csv"; string filePath1 = "991.csv"; string filePath2 = "992.csv"; Console.WriteLine("读取数据......"); // 调用读取CSV文件的方法 TekWaveformData tekWaveformData1 = Helper.ReadTekCsvFile(filePath1); TekWaveformData tekWaveformData2 = Helper.ReadTekCsvFile(filePath2); // 打印读取到的数据和相关信息 Console.WriteLine($"Model: {tekWaveformData2.Model}"); Console.WriteLine($"Waveform Type: {tekWaveformData2.WaveformType}"); Console.WriteLine($"Sample Interval: {tekWaveformData2.SampleInterval} => Sample Rate:{tekWaveformData2.SampleRateMHz} MHz"); Console.WriteLine("Time, Voltage"); if (tekWaveformData2.WaveformDatas == null) { return; } Console.WriteLine(@$"Data Len:{tekWaveformData2.WaveformDatas.Count}"); var testDiffData = new List(); //测试阈值电平 double thresholdHigh = 2.6; double thresholdlow = 1.0; //double thresholdHigh = 1.8; //double thresholdlow = 1; //double thresholdHigh = 2.6; //double thresholdlow = 1.1; // 采样得到Tektronix波形数据的类 bool result; result = WaveFormSampling.Sampling(tekWaveformData1, thresholdHigh, thresholdlow, out List edgePulses1); if (!result) { Console.WriteLine(@$"采样失败"); } result = WaveFormSampling.Sampling(tekWaveformData2, thresholdHigh, thresholdlow, out List edgePulses2); if (!result) { Console.WriteLine(@$"采样失败"); } var edgePulsesArray1 = edgePulses1.ToArray(); var edgePulsesCount1 = (UInt64)edgePulses1.Count(); ProtocolBase.TwoLevelEdgePulseData edgePulseData1 = new(edgePulsesArray1, edgePulsesCount1, (UInt64)tekWaveformData1.WaveformDatas.Count, tekWaveformData2.SampleRateMHz * 1e6); var edgePulsesArray2 = edgePulses2.ToArray(); var edgePulsesCount2 = (UInt64)edgePulses2.Count(); ProtocolBase.TwoLevelEdgePulseData edgePulseData2 = new(edgePulsesArray2, edgePulsesCount2, (UInt64)tekWaveformData2.WaveformDatas.Count, tekWaveformData1.SampleRateMHz * 1e6); UInt64 testDiffDataCount = edgePulsesCount1 > edgePulsesCount2 ? edgePulsesCount2 : edgePulsesCount1; for (UInt64 i = 0; i < testDiffDataCount; i++) { if (edgePulsesArray1[i].StartIndex != edgePulsesArray2[i].StartIndex) { testDiffData.Add(edgePulsesArray1[i]); } } //模拟用户参数 Protocol_USB_Options options = new() { SignalType = ENABLE_DIFF_SIGN ? CommonBase.SignalType.Difference : CommonBase.SignalType.Single, SamplingFrequency = tekWaveformData2.SampleRateMHz, USBSpeed = Enums.USBSpeed.FullSpeed, AutoClock = true //AutoClock = false }; //开始解码 ProtocolBase.TwoLevelEdgePulse[]? edgePulsesArrary = edgePulses1.ToArray(); GCHandle.Alloc(edgePulsesArrary, GCHandleType.Pinned); IntPtr edgePulsesPtr = Marshal.UnsafeAddrOfPinnedArrayElement(edgePulsesArrary, 0); Console.WriteLine($"edgePulsesPtr={edgePulsesPtr:x16}"); Console.WriteLine($"edgePulses1 Count:{edgePulses1.Count}"); Console.WriteLine($"edgePulses2 Count:{edgePulses2.Count}"); Stopwatch timer = new(); timer.Start(); result = Parse_USB(options, edgePulseData1, edgePulseData2, out UsbDecodeResultStruct decodeResultStruct); timer.Stop(); if (!result) { Console.WriteLine(@$"解码失败"); return; } result = UsbDecodeResult.ConvertData(decodeResultStruct, out UsbDecodeResult decodeResult); if (!result) { Console.WriteLine(@$"转换失败"); return; } //输出结果 int showCount = 16; //Console.WriteLine("=============== Result ============="); //Console.WriteLine($"DecodeResult Count:{decodeResult.DecodeResultCount}"); Console.WriteLine("=============== Event ============="); Console.WriteLine($"DecodeEvent Count:{decodeResult.DecodeEventCount}\n\n"); if (decodeResult.DecodeEventCount > 0) { if (decodeResult.DecodeEventCount < showCount) { showCount = decodeResult.DecodeEventCount; } Console.WriteLine($"展示前{showCount}个事件:\n"); for (int i = 0; i < showCount; i++) { var eventData = decodeResult.DecodeEvents[i]; Console.WriteLine($" 事件{eventData.EventIndex}: StartIndex: {eventData.StartIndex},EndIndex:{eventData.EndIndex}"); Console.Write($" SYNC => {eventData.EventTitle} "); if (eventData.EventTitle == Enums.EventInfoTitles.DATA0 || eventData.EventTitle == Enums.EventInfoTitles.DATA1) { Console.Write($" => Data:"); for (UInt64 x = 0; x < eventData.DataCount; x++) { Console.Write($" 0x{Marshal.ReadByte(eventData.DecodeDataPtr + (int)x):X2}"); } } if (eventData.EventTitle == Enums.EventInfoTitles.TOUT) { Console.Write($" => Addr:{eventData.Address} => EndPoint:{eventData.EndPoint}"); } Console.WriteLine(""); if (eventData.CrcSignNum >0) { if (eventData.CrcSignNum == 5) { Console.WriteLine($" CRC5: 0x{eventData.CrcData:X2} "); } else { Console.WriteLine($" CRC16: 0x{eventData.CrcData:X4}"); } } Console.WriteLine($" -------------------------------- "); } } Console.WriteLine($"\nTime:{timer.ElapsedMilliseconds} ms"); Console.ReadLine(); } }