Pārlūkot izejas kodu

USB 静态初版

lijinwen 3 mēneši atpakaļ
vecāks
revīzija
742c8966f1

+ 1 - 1
ComModel/ProtocolBase.cs

@@ -55,7 +55,7 @@ public class ProtocolBase
         IntPtr edgePulsesPtr;
 
         public TwoLevelEdgePulseData(TwoLevelEdgePulse[] edgePulses, UInt64 edgePulsesCount, UInt64 waveformDataCount
-            , CommonBase.ChannelId channelId, double sampleRateByHz)
+            , double sampleRateByHz)
         {
             WaveformDataCount = waveformDataCount;
             edgePulsesPtr = Marshal.UnsafeAddrOfPinnedArrayElement(edgePulses, 0);

+ 180 - 95
ConsoleAppCsvRead/Program.cs

@@ -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();
+
+
+
+    }
 
 }

+ 40 - 0
TekWaveformSampling/Enums.cs

@@ -10,6 +10,17 @@ namespace TekWaveformSampling;
 
 public class Enums
 {
+    public enum USBSpeed
+    {
+        LowSpeed = 0,
+        FullSpeed = 1,
+        HighSpeed = 2,
+    };
+    public enum SignType
+    {
+        SingleEnded,
+        Diff
+    };
     public enum DataBitWidth
     {
         DataBitWidth_5Bit = 5,
@@ -30,4 +41,33 @@ public class Enums
         Data,
         Parity
     }
+    public enum EventInfoTitles
+    {
+        UNKNOW,
+        INDEX,
+        START_TIME,
+        SYNC,
+        SOF,
+        SETUP,
+        PID,
+        DATA0,
+        DATA1,
+        DATA2,
+        MDATA,
+        ADDR,
+        ACK,
+        //IN,关键字
+        TIN,
+        //OUT,关键字
+        TOUT,
+        NAK,
+        STALL,
+        NYET,
+        FNUM,
+        CRC5,
+        CRC16,
+        EOP,
+        //ERROR, 关键字
+        ERR,
+    };
 }

+ 4 - 2
TekWaveformSampling/Helper.cs

@@ -16,9 +16,10 @@ namespace TekWaveformSampling;
 
 public class Helper
 {
-  
+    const int READ_LENGTH_MAX = 10 * 1024 * 1024;
     public static TekWaveformData ReadTekCsvFile(string filePath)
     {
+        //filePath = "232_000.csv";
         using (StreamReader? reader = new(filePath))
         {
             int i = 0, m = 0;
@@ -27,6 +28,7 @@ public class Helper
             TekWaveformData tekWaveformData = new();
             do
             {
+
                 dataStr = reader.ReadLine()?.Trim();
                 if (string.IsNullOrWhiteSpace(dataStr))
                 {
@@ -46,7 +48,7 @@ public class Helper
                 }
             }
 
-            while (reader.Peek() > 0 && dataStr != "TIME,REF1");
+            while (reader.Peek() > 0 && dataStr != "TIME,REF1" && dataStr != "TIME,CH1" && dataStr != "TIME,CH2");
 
             using (CsvReader? csv = new(reader, CultureInfo.InvariantCulture))
             {

+ 3 - 3
TekWaveformSampling/Model/struct RS232DecodeResultStruct.cs

@@ -36,7 +36,7 @@ internal struct RS232DecodeEventUintStruct
     [FieldOffset(16)] public byte Data;
 
     [FieldOffset(24)] public Enums.RS232DecodeEventType EventType;
- 
+
 }
 [StructLayout(LayoutKind.Explicit, Size = 56)]
 internal struct RS232DecodeEventStruct
@@ -100,7 +100,7 @@ internal struct RS232DecodeEventStruct
 //    [FieldOffset(24)] public IntPtr DecodeEvents;
 //    [FieldOffset(32)] public UInt64 DecodeResultCount;
 //    [FieldOffset(40)] public UInt64 DecodeEventCount;
-  
+
 
 //}
 
@@ -115,4 +115,4 @@ public struct RS232DecodeResultStruct
     public UInt64 DecodeResultCount;
     public UInt64 DecodeEventCount;
 
-}
+}

+ 17 - 4
TekWaveformSampling/WaveFormSampling.cs

@@ -7,12 +7,13 @@
 // //    *(__\_\
 // // ******************************************************************
 using ComModel;
+using static ComModel.ProtocolBase;
 
 namespace TekWaveformSampling;
 
 public class WaveFormSampling
 {
-    public static bool Sampling(TekWaveformData? waveformData, double threshold, out List<ProtocolBase.TwoLevelEdgePulse> edgePulses)
+    public static bool Sampling(TekWaveformData? waveformData, double thresholdHigh, double thresholdLow, out List<ProtocolBase.TwoLevelEdgePulse> edgePulses)
     {
         edgePulses = new List<ProtocolBase.TwoLevelEdgePulse>();
         if (waveformData == null || waveformData.SampleInterval == 0 || waveformData.WaveformDatas == null || !waveformData.WaveformDatas.Any())
@@ -25,7 +26,15 @@ public class WaveFormSampling
         for (int i = 0; i < waveformData.WaveformDatas.Count; i++)
         {
             WaveformData? data = waveformData.WaveformDatas[i];
-            nowEdge = data.Voltage >= threshold ? CommonBase.Edge.Rise : CommonBase.Edge.Falling;
+            if (data.Voltage >= thresholdLow && data.Voltage <= thresholdHigh && edgePulses.Count() > 0)
+            {
+                //延申
+                //var changeEdgePulse = edgePulses[edgePulses.Count - 1];
+                //changeEdgePulse.EndIndex = i;
+                //edgePulses[edgePulses.Count - 1] = changeEdgePulse;
+                continue;
+            }
+            nowEdge = data.Voltage > thresholdHigh ? CommonBase.Edge.Rise : CommonBase.Edge.Falling;
             if (i == 0)
             {
                 lastEdgePulse = new ProtocolBase.EdgePulse(0, 0, nowEdge);
@@ -35,12 +44,16 @@ public class WaveFormSampling
             if (lastEdgePulse != null && nowEdge != lastEdgePulse.Value.Edge)
             {
                 lastEdgePulse = lastEdgePulse.Value with { EndIndex = i };
-
+             
                 edgePulses.Add(new ProtocolBase.TwoLevelEdgePulse((ProtocolBase.EdgePulse)lastEdgePulse));
+                //lastEdgePulse = new ProtocolBase.EdgePulse(i, 0, nowEdge);
                 lastEdgePulse = new ProtocolBase.EdgePulse(i, 0, nowEdge);
-
             }
+            //test point
+            //if (i >= 117704)
+            //{
 
+            //}
         }
         return true;
     }