diff --git a/DriverTest/Program.cs b/DriverTest/Program.cs index 12486ba..8cb349b 100644 --- a/DriverTest/Program.cs +++ b/DriverTest/Program.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; +using System.Threading.Tasks; using S7CommPlusDriver; using S7CommPlusDriver.ClientApi; @@ -38,6 +39,7 @@ namespace DriverTest stopwatch1.Start(); res = conn.Connect(HostIp, Password); stopwatch1.Stop(); + byte[] var1_crc_bytes = { 0x88, 0xdd, 0xa4, 0x83, 0x34 }; Console.WriteLine($"PLCType: {conn.PLCInformation.PLCType} | MLFB: {conn.PLCInformation.MLFB} | Firmware: {conn.PLCInformation.Firmware}"); Console.WriteLine($"连接耗时{stopwatch1.ElapsedMilliseconds}ms."); if (res == 0) @@ -59,17 +61,13 @@ namespace DriverTest Console.WriteLine("Main - Lese Werte aller Variablen aus"); List taglist = new List(); - PlcTags tags = new PlcTags(); foreach (var v in vars_) { ItemAddress itemAddress = new ItemAddress(v.AccessSequence); + //S7p.DecodeUInt32Vlq(new MemoryStream(var1_crc_bytes), out itemAddress.SymbolCrc); taglist.Add(PlcTags.TagFactory(v.Name, itemAddress, v.Softdatatype)); } - foreach (var t in taglist) - { - tags.AddTag(t); - } if (res == 0) { Console.WriteLine("====================== VARIABLENHAUSHALT ======================"); @@ -85,13 +83,21 @@ namespace DriverTest } } #endregion - Console.WriteLine("按任意键开始读"); + Console.WriteLine("按任意键开始读或订阅"); Console.ReadKey(); + res = conn.SubscriptionCreate(taglist, 100); + if (res == 0) + { + Task.Run(() => + { + conn.TestWaitForVariableChangeNotifications(50000000000); + }); + } while (res == 0) { System.Diagnostics.Stopwatch stopwatch2 = new System.Diagnostics.Stopwatch(); stopwatch2.Start(); - res = tags.ReadTags(conn); + res = PlcTags.ReadTags(conn, taglist); stopwatch2.Stop(); long ms = stopwatch2.ElapsedMilliseconds; if (res == 0) @@ -99,7 +105,7 @@ namespace DriverTest string header = $"读取{vars_.Count}个变量耗时{ms}毫秒"; Console.WriteLine(header); } - System.Threading.Thread.Sleep(10); + //System.Threading.Thread.Sleep(50); } #endif @@ -196,4 +202,80 @@ namespace DriverTest Console.ReadKey(); } } + public static class S7SymbolCrc32 + { + private const uint Polynomial = 0xFA567993; // 多项式(x³² + x³¹ + x³⁰ + x²⁹ + x²⁸ + x²⁶ + x²³ + x²¹ + x¹⁹ + x¹⁸ + x¹⁵ + x¹⁴ + x¹³ + x¹² + x⁹ + x⁸ + x⁴ + x + 1) + private const uint InitialValue = 0xFFFFFFFF; + private static readonly uint[] Table; + + static S7SymbolCrc32() + { + // 预计算CRC表 + Table = new uint[256]; + for (uint i = 0; i < 256; i++) + { + uint crc = i << 24; + for (int j = 0; j < 8; j++) + { + if ((crc & 0x80000000) != 0) + { + crc = (crc << 1) ^ Polynomial; + } + else + { + crc <<= 1; + } + } + Table[i] = crc; + } + } + + /// + /// 计算S7CommPlus符号名的CRC32校验和 + /// + /// 符号名(如"DB1.TempBottom") + /// 数据类型字节 + /// CRC32校验和 + public static uint CalculateSymbolCrc(string symbolName) + { + // 1. 替换分隔符 '.' → 0x09 + string processedName = symbolName.Replace('.', '\t'); + + // 2. 转换为字节数组 + byte[] nameBytes = Encoding.ASCII.GetBytes(processedName); + + // 3. 添加数据类型字节 + byte[] data = new byte[nameBytes.Length]; + Array.Copy(nameBytes, 0, data, 0, nameBytes.Length); + + // 4. 计算CRC32 + uint crc = InitialValue; + foreach (byte b in data) + { + crc = (crc << 8) ^ Table[((crc >> 24) & 0xFF) ^ b]; + } + + // 5. 根据作者提示,可能需要再次计算(需要验证) + //crc = CalculateSecondPass(crc); + + return crc; + } + + // 如果需要二次计算的方法 + public static uint CalculateSecondPass(uint firstResult) + { + byte[] crcBytes = BitConverter.GetBytes(firstResult); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(crcBytes); // 确保大端序 + } + + uint crc = InitialValue; + foreach (byte b in crcBytes) + { + crc = (crc << 8) ^ Table[((crc >> 24) & 0xFF) ^ b]; + } + return crc; + } + } } diff --git a/DriverTest/bin/Debug/DriverTest.exe b/DriverTest/bin/Debug/DriverTest.exe index f37b407..037ffb0 100644 Binary files a/DriverTest/bin/Debug/DriverTest.exe and b/DriverTest/bin/Debug/DriverTest.exe differ diff --git a/DriverTest/bin/Debug/DriverTest.pdb b/DriverTest/bin/Debug/DriverTest.pdb index a0c6e83..88725b7 100644 Binary files a/DriverTest/bin/Debug/DriverTest.pdb and b/DriverTest/bin/Debug/DriverTest.pdb differ diff --git a/DriverTest/bin/Debug/S7CommPlusDriver.dll b/DriverTest/bin/Debug/S7CommPlusDriver.dll index 4e76d08..3ebfb1d 100644 Binary files a/DriverTest/bin/Debug/S7CommPlusDriver.dll and b/DriverTest/bin/Debug/S7CommPlusDriver.dll differ diff --git a/DriverTest/obj/Debug/DriverTest.csproj.CoreCompileInputs.cache b/DriverTest/obj/Debug/DriverTest.csproj.CoreCompileInputs.cache index 0c3cfa5..8c5d40f 100644 --- a/DriverTest/obj/Debug/DriverTest.csproj.CoreCompileInputs.cache +++ b/DriverTest/obj/Debug/DriverTest.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -dc7d04987a3dea6ec27cb7cd218eb8ff538ad9f0 +f536f4e9eec50d57b8ee892950e0066fa4996ec5 diff --git a/DriverTest/obj/Debug/DriverTest.csproj.FileListAbsolute.txt b/DriverTest/obj/Debug/DriverTest.csproj.FileListAbsolute.txt index 6c10012..09019c6 100644 --- a/DriverTest/obj/Debug/DriverTest.csproj.FileListAbsolute.txt +++ b/DriverTest/obj/Debug/DriverTest.csproj.FileListAbsolute.txt @@ -6,3 +6,11 @@ C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\DriverTest\bin\Debug\S C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.csproj.CopyComplete C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.exe C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.pdb +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.csproj.CoreCompileInputs.cache +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.exe +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.pdb +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\bin\Debug\DriverTest.exe.config +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\bin\Debug\DriverTest.exe +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\bin\Debug\DriverTest.pdb +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\bin\Debug\S7CommPlusDriver.dll +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.csproj.CopyComplete diff --git a/DriverTest/obj/Debug/DriverTest.exe b/DriverTest/obj/Debug/DriverTest.exe index f37b407..037ffb0 100644 Binary files a/DriverTest/obj/Debug/DriverTest.exe and b/DriverTest/obj/Debug/DriverTest.exe differ diff --git a/DriverTest/obj/Debug/DriverTest.pdb b/DriverTest/obj/Debug/DriverTest.pdb index a0c6e83..88725b7 100644 Binary files a/DriverTest/obj/Debug/DriverTest.pdb and b/DriverTest/obj/Debug/DriverTest.pdb differ diff --git a/Lib/S7CommPlusDriver.dll b/Lib/S7CommPlusDriver.dll index 4e76d08..3ebfb1d 100644 Binary files a/Lib/S7CommPlusDriver.dll and b/Lib/S7CommPlusDriver.dll differ diff --git a/S7CommPlusGUIBrowser/Form1.cs b/S7CommPlusGUIBrowser/Form1.cs index 83b146d..72d6747 100644 --- a/S7CommPlusGUIBrowser/Form1.cs +++ b/S7CommPlusGUIBrowser/Form1.cs @@ -39,8 +39,7 @@ namespace S7CommPlusGUIBrowser private void btnConnect_Click(object sender, EventArgs e) { setStatus("connecting..."); - - if (conn != null) conn.Disconnect(); + if (conn != null)conn.Disconnect(); conn = new S7CommPlusConnection(); conn.OnlySecurePGOrPCAndHMI = onlySecurePGOrPCAndHMI; int res = conn.Connect(tbIpAddress.Text, tbPassword.Text); @@ -106,12 +105,11 @@ namespace S7CommPlusGUIBrowser private void btnDisconnect_Click(object sender, EventArgs e) { setStatus("disconnecting..."); - if (conn != null) conn.Disconnect(); - conn = null; treeView1.Nodes.Clear(); txt_plcInfo.Text = "PLCType:"; setStatus("disconnected"); + conn = null; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) @@ -352,10 +350,8 @@ namespace S7CommPlusGUIBrowser if (tag == null) return; tbSymbolicAddress.Text = tag.Address.GetAccessString(); - - PlcTags tags = new PlcTags(); - tags.AddTag(tag); - if (tags.ReadTags(conn) != 0) return; + + if (PlcTags.ReadTags(conn,new List { tag}) != 0) return; tbValue.Text = tag.ToString(); } diff --git a/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusDriver.dll b/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusDriver.dll index 4e76d08..3ebfb1d 100644 Binary files a/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusDriver.dll and b/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusDriver.dll differ diff --git a/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.exe b/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.exe index 692b7a6..0139b7c 100644 Binary files a/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.exe and b/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.exe differ diff --git a/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.pdb b/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.pdb index 073c813..24fd8ba 100644 Binary files a/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.pdb and b/S7CommPlusGUIBrowser/bin/Debug/S7CommPlusGUIBrowser.pdb differ diff --git a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.CoreCompileInputs.cache b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.CoreCompileInputs.cache index 0009db7..db1786a 100644 --- a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.CoreCompileInputs.cache +++ b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -ec14fa67cfbca0656c6c62f93ec1446787c52f00 +a3f5da61a0c9535c5a7df7f53d9805aa7bd1a564 diff --git a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.FileListAbsolute.txt b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.FileListAbsolute.txt index 9184a94..6f99b05 100644 --- a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.FileListAbsolute.txt +++ b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.FileListAbsolute.txt @@ -10,3 +10,15 @@ C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\S7CommPlusGUIBrowser\o C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.csproj.CopyComplete C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.exe C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.pdb +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\bin\Debug\S7CommPlusGUIBrowser.exe.config +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\bin\Debug\S7CommPlusGUIBrowser.exe +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\bin\Debug\S7CommPlusGUIBrowser.pdb +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\bin\Debug\S7CommPlusDriver.dll +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.csprojAssemblyReference.cache +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.Form1.resources +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.Properties.Resources.resources +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.csproj.GenerateResource.cache +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.csproj.CoreCompileInputs.cache +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.csproj.CopyComplete +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.exe +C:\Users\ShenX\Desktop\S7CommPlusDriver_Secure\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.pdb diff --git a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.GenerateResource.cache b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.GenerateResource.cache index 1d7c634..3211d3d 100644 Binary files a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.GenerateResource.cache and b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csproj.GenerateResource.cache differ diff --git a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csprojAssemblyReference.cache b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csprojAssemblyReference.cache index 8b9d53e..1ec2210 100644 Binary files a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csprojAssemblyReference.cache and b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.csprojAssemblyReference.cache differ diff --git a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.exe b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.exe index 692b7a6..0139b7c 100644 Binary files a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.exe and b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.exe differ diff --git a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.pdb b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.pdb index 073c813..24fd8ba 100644 Binary files a/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.pdb and b/S7CommPlusGUIBrowser/obj/Debug/S7CommPlusGUIBrowser.pdb differ