|
@@ -3,113 +3,198 @@
|
|
|
// // \ V/ @Brief
|
|
|
// // | "") @Author lijinwen, ghz005@uni-trend.com.cn
|
|
|
// // / | @Creation 2024-1-17
|
|
|
-// // / \\ @Modified 2024-2-19
|
|
|
+// // / \\ @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("Protocol.RS232.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
- // static extern bool ParsingData(Protocol_RS232_Options options, IntPtr twoLvEdgePulses, UInt64 dataCount,
|
|
|
- // double sampleRate, CommonBase.ChannelId channelId, IntPtr decodeResultPtr);
|
|
|
- [DllImport("ProtocolDecoder.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
- public static extern bool decodeRS232(Protocol_RS232_Options options, ProtocolBase.TwoLevelEdgePulseData edgePulseData,
|
|
|
- out RS232DecodeResultStruct decodeResult);
|
|
|
-
|
|
|
- static void Main()
|
|
|
- {
|
|
|
- // 请替换为你的CSV文件路径
|
|
|
- string filePath = "232_000.csv";
|
|
|
-
|
|
|
- // 调用读取CSV文件的方法
|
|
|
- TekWaveformData tekWaveformData = Helper.ReadTekCsvFile(filePath);
|
|
|
-
|
|
|
- // 打印读取到的数据和相关信息
|
|
|
- Console.WriteLine($"Model: {tekWaveformData.Model}");
|
|
|
- Console.WriteLine($"Waveform Type: {tekWaveformData.WaveformType}");
|
|
|
- Console.WriteLine($"Sample Interval: {tekWaveformData.SampleInterval} => Sample Rate:{tekWaveformData.SampleRateMHz} MHz");
|
|
|
- Console.WriteLine("Time, Voltage");
|
|
|
- if (tekWaveformData.WaveformDatas == null)
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- Console.WriteLine(@$"Data Len:{tekWaveformData.WaveformDatas.Count}");
|
|
|
-
|
|
|
- //测试阈值电平 0V
|
|
|
- int threshold = 0;
|
|
|
- // 采样得到Tektronix波形数据的类
|
|
|
- bool result = WaveFormSampling.Sampling(tekWaveformData, threshold, out List<ProtocolBase.TwoLevelEdgePulse> edgePulses);
|
|
|
- if (!result)
|
|
|
- {
|
|
|
- Console.WriteLine(@$"采样失败");
|
|
|
- }
|
|
|
-
|
|
|
- var edgePulsesArray = edgePulses.ToArray();
|
|
|
- var edgePulsesCount = (UInt64)edgePulses.Count();
|
|
|
- ProtocolBase.TwoLevelEdgePulseData edgePulseData = new(edgePulsesArray, edgePulsesCount, (UInt64)tekWaveformData.WaveformDatas.Count,
|
|
|
- CommonBase.ChannelId.C1, tekWaveformData.SampleRateMHz * 1e6);
|
|
|
-
|
|
|
- //模拟用户参数
|
|
|
- Protocol_RS232_Options options = new()
|
|
|
- {
|
|
|
- BaudRate = 9600,
|
|
|
- DataBitWidth = Enums.DataBitWidth.DataBitWidth_8Bit,
|
|
|
- MSBOrLSB = CommonBase.MSBOrLSB.LSB,
|
|
|
-
|
|
|
- Polarity = CommonBase.Polarity.Pos, ///////////////////////////////////////////
|
|
|
- StopBit = Enums.StopBit.StopBit_1bit,
|
|
|
- Threshold = threshold
|
|
|
- };
|
|
|
-
|
|
|
- //开始解码
|
|
|
- ProtocolBase.TwoLevelEdgePulse[]? edgePulsesArrary = edgePulses.ToArray();
|
|
|
- GCHandle.Alloc(edgePulsesArrary, GCHandleType.Pinned);
|
|
|
- IntPtr edgePulsesPtr = Marshal.UnsafeAddrOfPinnedArrayElement(edgePulsesArrary, 0);
|
|
|
- Console.WriteLine($"edgePulsesPtr={edgePulsesPtr:x16}");
|
|
|
- //RS232DecodeResult[] rs232DecodeResult = { new() };
|
|
|
-
|
|
|
- //IntPtr decodeResultPtr = Marshal.UnsafeAddrOfPinnedArrayElement(rs232DecodeResult, 0);
|
|
|
- Console.WriteLine($"edgePulses.Count:{edgePulses.Count}");
|
|
|
-
|
|
|
- Stopwatch timer = new();
|
|
|
- timer.Start();
|
|
|
- //result = ParsingData(options, ptr, (UInt64)edgePulses.Count, edgePulseData.SampleRate, edgePulseData.channelId,IntPtr.Zero);
|
|
|
- //result = ParsingData(options, edgePulsesPtr, (UInt64)edgePulses.Count, (UInt64)tekWaveformData.WaveformDatas.Count, edgePulseData.SampleRate * 1E6, edgePulseData.ChannelId, out RS232DecodeResultStruct decodeResultStruct);
|
|
|
- result = decodeRS232(options, edgePulseData, out RS232DecodeResultStruct decodeResultStruct);
|
|
|
- timer.Stop();
|
|
|
- if (!result)
|
|
|
- {
|
|
|
- Console.WriteLine(@$"解码失败");
|
|
|
- return;
|
|
|
- }
|
|
|
- result = RS232DecodeResult.ConvertData(decodeResultStruct, out RS232DecodeResult decodeResult);
|
|
|
- if (!result)
|
|
|
- {
|
|
|
- Console.WriteLine(@$"转换失败");
|
|
|
- return;
|
|
|
- }
|
|
|
- //输出结果
|
|
|
- Console.WriteLine($"DecodeResult Count:{decodeResult.DecodeResultCount}");
|
|
|
- Console.WriteLine($"DecodeEvent Count:{decodeResult.DecodeEventCount}");
|
|
|
- Console.WriteLine($"Time:{timer.ElapsedMilliseconds} ms");
|
|
|
- Console.ReadLine();
|
|
|
-
|
|
|
- // Console.WriteLine($"DecodeResult Count:{rs232DecodeResult[0].DecodeResultCount}");
|
|
|
- // Console.WriteLine($"DecodeResult Count:{rs232DecodeResult[0].DecodeEventCount}");
|
|
|
-
|
|
|
- }
|
|
|
+ // 导入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<TwoLevelEdgePulse>();
|
|
|
+ //测试阈值电平
|
|
|
+ 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<ProtocolBase.TwoLevelEdgePulse> edgePulses1);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ Console.WriteLine(@$"采样失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ result = WaveFormSampling.Sampling(tekWaveformData2, thresholdHigh, thresholdlow, out List<ProtocolBase.TwoLevelEdgePulse> 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();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|