1. Resolved the hourly connection termination problem in non-TLS firmware communication.

2. Enhanced socket-level data packet reception throughput.
This commit is contained in:
CoderShen
2025-11-24 16:49:05 +08:00
committed by lircy
parent b7d6993fa3
commit ada79f2073
19 changed files with 116 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks;
using S7CommPlusDriver; using S7CommPlusDriver;
using S7CommPlusDriver.ClientApi; using S7CommPlusDriver.ClientApi;
@@ -38,6 +39,7 @@ namespace DriverTest
stopwatch1.Start(); stopwatch1.Start();
res = conn.Connect(HostIp, Password); res = conn.Connect(HostIp, Password);
stopwatch1.Stop(); 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($"PLCType: {conn.PLCInformation.PLCType} | MLFB: {conn.PLCInformation.MLFB} | Firmware: {conn.PLCInformation.Firmware}");
Console.WriteLine($"连接耗时{stopwatch1.ElapsedMilliseconds}ms."); Console.WriteLine($"连接耗时{stopwatch1.ElapsedMilliseconds}ms.");
if (res == 0) if (res == 0)
@@ -59,17 +61,13 @@ namespace DriverTest
Console.WriteLine("Main - Lese Werte aller Variablen aus"); Console.WriteLine("Main - Lese Werte aller Variablen aus");
List<PlcTag> taglist = new List<PlcTag>(); List<PlcTag> taglist = new List<PlcTag>();
PlcTags tags = new PlcTags();
foreach (var v in vars_) foreach (var v in vars_)
{ {
ItemAddress itemAddress = new ItemAddress(v.AccessSequence); 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)); taglist.Add(PlcTags.TagFactory(v.Name, itemAddress, v.Softdatatype));
} }
foreach (var t in taglist)
{
tags.AddTag(t);
}
if (res == 0) if (res == 0)
{ {
Console.WriteLine("====================== VARIABLENHAUSHALT ======================"); Console.WriteLine("====================== VARIABLENHAUSHALT ======================");
@@ -85,13 +83,21 @@ namespace DriverTest
} }
} }
#endregion #endregion
Console.WriteLine("按任意键开始读"); Console.WriteLine("按任意键开始读或订阅");
Console.ReadKey(); Console.ReadKey();
res = conn.SubscriptionCreate(taglist, 100);
if (res == 0)
{
Task.Run(() =>
{
conn.TestWaitForVariableChangeNotifications(50000000000);
});
}
while (res == 0) while (res == 0)
{ {
System.Diagnostics.Stopwatch stopwatch2 = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch stopwatch2 = new System.Diagnostics.Stopwatch();
stopwatch2.Start(); stopwatch2.Start();
res = tags.ReadTags(conn); res = PlcTags.ReadTags(conn, taglist);
stopwatch2.Stop(); stopwatch2.Stop();
long ms = stopwatch2.ElapsedMilliseconds; long ms = stopwatch2.ElapsedMilliseconds;
if (res == 0) if (res == 0)
@@ -99,7 +105,7 @@ namespace DriverTest
string header = $"读取{vars_.Count}个变量耗时{ms}毫秒"; string header = $"读取{vars_.Count}个变量耗时{ms}毫秒";
Console.WriteLine(header); Console.WriteLine(header);
} }
System.Threading.Thread.Sleep(10); //System.Threading.Thread.Sleep(50);
} }
#endif #endif
@@ -196,4 +202,80 @@ namespace DriverTest
Console.ReadKey(); 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;
}
}
/// <summary>
/// 计算S7CommPlus符号名的CRC32校验和
/// </summary>
/// <param name="symbolName">符号名(如"DB1.TempBottom"</param>
/// <param name="dataType">数据类型字节</param>
/// <returns>CRC32校验和</returns>
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;
}
}
} }

Binary file not shown.

Binary file not shown.

View File

@@ -1 +1 @@
dc7d04987a3dea6ec27cb7cd218eb8ff538ad9f0 f536f4e9eec50d57b8ee892950e0066fa4996ec5

View File

@@ -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.csproj.CopyComplete
C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\DriverTest\obj\Debug\DriverTest.exe 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\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

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -39,8 +39,7 @@ namespace S7CommPlusGUIBrowser
private void btnConnect_Click(object sender, EventArgs e) private void btnConnect_Click(object sender, EventArgs e)
{ {
setStatus("connecting..."); setStatus("connecting...");
if (conn != null)conn.Disconnect();
if (conn != null) conn.Disconnect();
conn = new S7CommPlusConnection(); conn = new S7CommPlusConnection();
conn.OnlySecurePGOrPCAndHMI = onlySecurePGOrPCAndHMI; conn.OnlySecurePGOrPCAndHMI = onlySecurePGOrPCAndHMI;
int res = conn.Connect(tbIpAddress.Text, tbPassword.Text); int res = conn.Connect(tbIpAddress.Text, tbPassword.Text);
@@ -106,12 +105,11 @@ namespace S7CommPlusGUIBrowser
private void btnDisconnect_Click(object sender, EventArgs e) private void btnDisconnect_Click(object sender, EventArgs e)
{ {
setStatus("disconnecting..."); setStatus("disconnecting...");
if (conn != null) conn.Disconnect(); if (conn != null) conn.Disconnect();
conn = null;
treeView1.Nodes.Clear(); treeView1.Nodes.Clear();
txt_plcInfo.Text = "PLCType:"; txt_plcInfo.Text = "PLCType:";
setStatus("disconnected"); setStatus("disconnected");
conn = null;
} }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) private void Form1_FormClosed(object sender, FormClosedEventArgs e)
@@ -353,9 +351,7 @@ namespace S7CommPlusGUIBrowser
tbSymbolicAddress.Text = tag.Address.GetAccessString(); tbSymbolicAddress.Text = tag.Address.GetAccessString();
PlcTags tags = new PlcTags(); if (PlcTags.ReadTags(conn,new List<PlcTag> { tag}) != 0) return;
tags.AddTag(tag);
if (tags.ReadTags(conn) != 0) return;
tbValue.Text = tag.ToString(); tbValue.Text = tag.ToString();
} }

View File

@@ -1 +1 @@
ec14fa67cfbca0656c6c62f93ec1446787c52f00 a3f5da61a0c9535c5a7df7f53d9805aa7bd1a564

View File

@@ -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.csproj.CopyComplete
C:\Users\ShenX\Desktop\DiscoverDevices\S7CommPlusV3Driver\S7CommPlusGUIBrowser\obj\Debug\S7CommPlusGUIBrowser.exe 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\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