Program.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // // ******************************************************************
  2. // // /\ /| @File Program.cs
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-1-17
  6. // // / \\ @Modified 2024-5-21
  7. // // *(__\_\
  8. // // ******************************************************************
  9. // // *********************** USB 解码测试 **************************
  10. // // ******************************************************************
  11. using System.Diagnostics;
  12. using System.Runtime.InteropServices;
  13. using ComModel;
  14. using TekWaveformSampling;
  15. using TekWaveformSampling.Model;
  16. using static ComModel.ProtocolBase;
  17. using UInt64 = System.UInt64;
  18. namespace ConsoleAppCsvRead;
  19. abstract internal class Program
  20. {
  21. // 导入C++ DLL中的函数
  22. [DllImport("ProtocolDecoder.dll", CallingConvention = CallingConvention.Cdecl)]
  23. public static extern void setCancellationSignal([MarshalAs(UnmanagedType.I1)] bool value);
  24. [DllImport("ProtocolDecoder.dll", CallingConvention = CallingConvention.Cdecl)]
  25. public static extern bool Parse_USB(Protocol_USB_Options options, ProtocolBase.TwoLevelEdgePulseData edgePulseData1,
  26. ProtocolBase.TwoLevelEdgePulseData edgePulseData2, out UsbDecodeResultStruct decodeResult);
  27. const bool ENABLE_DIFF_SIGN = false;
  28. static void Main()
  29. {
  30. // CSV文件路径
  31. //string filePath1 = "u1_000.csv";
  32. //string filePath2 = "u2_000.csv";
  33. string filePath1 = "991.csv";
  34. string filePath2 = "992.csv";
  35. Console.WriteLine("读取数据......");
  36. // 调用读取CSV文件的方法
  37. TekWaveformData tekWaveformData1 = Helper.ReadTekCsvFile(filePath1);
  38. TekWaveformData tekWaveformData2 = Helper.ReadTekCsvFile(filePath2);
  39. // 打印读取到的数据和相关信息
  40. Console.WriteLine($"Model: {tekWaveformData2.Model}");
  41. Console.WriteLine($"Waveform Type: {tekWaveformData2.WaveformType}");
  42. Console.WriteLine($"Sample Interval: {tekWaveformData2.SampleInterval} => Sample Rate:{tekWaveformData2.SampleRateMHz} MHz");
  43. Console.WriteLine("Time, Voltage");
  44. if (tekWaveformData2.WaveformDatas == null)
  45. {
  46. return;
  47. }
  48. Console.WriteLine(@$"Data Len:{tekWaveformData2.WaveformDatas.Count}");
  49. var testDiffData = new List<TwoLevelEdgePulse>();
  50. //测试阈值电平
  51. double thresholdHigh = 2.6;
  52. double thresholdlow = 1.0;
  53. //double thresholdHigh = 1.8;
  54. //double thresholdlow = 1;
  55. //double thresholdHigh = 2.6;
  56. //double thresholdlow = 1.1;
  57. // 采样得到Tektronix波形数据的类
  58. bool result;
  59. result = WaveFormSampling.Sampling(tekWaveformData1, thresholdHigh, thresholdlow, out List<ProtocolBase.TwoLevelEdgePulse> edgePulses1);
  60. if (!result)
  61. {
  62. Console.WriteLine(@$"采样失败");
  63. }
  64. result = WaveFormSampling.Sampling(tekWaveformData2, thresholdHigh, thresholdlow, out List<ProtocolBase.TwoLevelEdgePulse> edgePulses2);
  65. if (!result)
  66. {
  67. Console.WriteLine(@$"采样失败");
  68. }
  69. var edgePulsesArray1 = edgePulses1.ToArray();
  70. var edgePulsesCount1 = (UInt64)edgePulses1.Count();
  71. ProtocolBase.TwoLevelEdgePulseData edgePulseData1 = new(edgePulsesArray1, edgePulsesCount1, (UInt64)tekWaveformData1.WaveformDatas.Count,
  72. tekWaveformData2.SampleRateMHz * 1e6);
  73. var edgePulsesArray2 = edgePulses2.ToArray();
  74. var edgePulsesCount2 = (UInt64)edgePulses2.Count();
  75. ProtocolBase.TwoLevelEdgePulseData edgePulseData2 = new(edgePulsesArray2, edgePulsesCount2, (UInt64)tekWaveformData2.WaveformDatas.Count,
  76. tekWaveformData1.SampleRateMHz * 1e6);
  77. UInt64 testDiffDataCount = edgePulsesCount1 > edgePulsesCount2 ? edgePulsesCount2 : edgePulsesCount1;
  78. for (UInt64 i = 0; i < testDiffDataCount; i++)
  79. {
  80. if (edgePulsesArray1[i].StartIndex != edgePulsesArray2[i].StartIndex)
  81. {
  82. testDiffData.Add(edgePulsesArray1[i]);
  83. }
  84. }
  85. //模拟用户参数
  86. Protocol_USB_Options options = new()
  87. {
  88. SignalType = ENABLE_DIFF_SIGN ? CommonBase.SignalType.Difference : CommonBase.SignalType.Single,
  89. SamplingFrequency = tekWaveformData2.SampleRateMHz,
  90. USBSpeed = Enums.USBSpeed.FullSpeed,
  91. AutoClock = true
  92. //AutoClock = false
  93. };
  94. //开始解码
  95. ProtocolBase.TwoLevelEdgePulse[]? edgePulsesArrary = edgePulses1.ToArray();
  96. GCHandle.Alloc(edgePulsesArrary, GCHandleType.Pinned);
  97. IntPtr edgePulsesPtr = Marshal.UnsafeAddrOfPinnedArrayElement(edgePulsesArrary, 0);
  98. Console.WriteLine($"edgePulsesPtr={edgePulsesPtr:x16}");
  99. Console.WriteLine($"edgePulses1 Count:{edgePulses1.Count}");
  100. Console.WriteLine($"edgePulses2 Count:{edgePulses2.Count}");
  101. Stopwatch timer = new();
  102. timer.Start();
  103. result = Parse_USB(options, edgePulseData1, edgePulseData2, out UsbDecodeResultStruct decodeResultStruct);
  104. timer.Stop();
  105. if (!result)
  106. {
  107. Console.WriteLine(@$"解码失败");
  108. return;
  109. }
  110. result = UsbDecodeResult.ConvertData(decodeResultStruct, out UsbDecodeResult decodeResult);
  111. if (!result)
  112. {
  113. Console.WriteLine(@$"转换失败");
  114. return;
  115. }
  116. //输出结果
  117. int showCount = 16;
  118. //Console.WriteLine("=============== Result =============");
  119. //Console.WriteLine($"DecodeResult Count:{decodeResult.DecodeResultCount}");
  120. Console.WriteLine("=============== Event =============");
  121. Console.WriteLine($"DecodeEvent Count:{decodeResult.DecodeEventCount}\n\n");
  122. if (decodeResult.DecodeEventCount > 0)
  123. {
  124. if (decodeResult.DecodeEventCount < showCount)
  125. {
  126. showCount = decodeResult.DecodeEventCount;
  127. }
  128. Console.WriteLine($"展示前{showCount}个事件:\n");
  129. for (int i = 0; i < showCount; i++)
  130. {
  131. var eventData = decodeResult.DecodeEvents[i];
  132. Console.WriteLine($" 事件{eventData.EventIndex}: StartIndex: {eventData.StartIndex},EndIndex:{eventData.EndIndex}");
  133. Console.Write($" SYNC => {eventData.EventTitle} ");
  134. if (eventData.EventTitle == Enums.EventInfoTitles.DATA0 || eventData.EventTitle == Enums.EventInfoTitles.DATA1)
  135. {
  136. Console.Write($" => Data:");
  137. for (UInt64 x = 0; x < eventData.DataCount; x++)
  138. {
  139. Console.Write($" 0x{Marshal.ReadByte(eventData.DecodeDataPtr + (int)x):X2}");
  140. }
  141. }
  142. if (eventData.EventTitle == Enums.EventInfoTitles.TOUT)
  143. {
  144. Console.Write($" => Addr:{eventData.Address} => EndPoint:{eventData.EndPoint}");
  145. }
  146. Console.WriteLine("");
  147. if (eventData.CrcSignNum >0)
  148. {
  149. if (eventData.CrcSignNum == 5)
  150. {
  151. Console.WriteLine($" CRC5: 0x{eventData.CrcData:X2} ");
  152. }
  153. else
  154. {
  155. Console.WriteLine($" CRC16: 0x{eventData.CrcData:X4}");
  156. }
  157. }
  158. Console.WriteLine($" -------------------------------- ");
  159. }
  160. }
  161. Console.WriteLine($"\nTime:{timer.ElapsedMilliseconds} ms");
  162. Console.ReadLine();
  163. }
  164. }