diff --git a/framework/Demo/Directory.Build.props b/framework/Demo/Directory.Build.props index 24af98084..b2da6ea73 100644 --- a/framework/Demo/Directory.Build.props +++ b/framework/Demo/Directory.Build.props @@ -1,6 +1,6 @@ - 3.0.0.21 + 3.0.0.22 latest net6.0;net7.0 Diego diff --git a/framework/Foundation/Directory.Build.props b/framework/Foundation/Directory.Build.props index 86e729ce5..47d24e80d 100644 --- a/framework/Foundation/Directory.Build.props +++ b/framework/Foundation/Directory.Build.props @@ -1,6 +1,6 @@ - 3.0.0.21 + 3.0.0.22 True latest net45;netstandard2.0;net6.0;net7.0 diff --git a/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusSerialServer/ModbusSerialServer.cs b/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusSerialServer/ModbusSerialServer.cs index 0e9098e52..8150f09c0 100644 --- a/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusSerialServer/ModbusSerialServer.cs +++ b/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusSerialServer/ModbusSerialServer.cs @@ -101,62 +101,76 @@ public class ModbusSerialServer : ReadWriteDevicesSerialSessionBase return PackHelper.LoadSourceRead(this, deviceVariables, maxPack); } + + EasyLock easyLock = new(); + + /// public override OperResult Read(string address, int length, CancellationToken cancellationToken = default) { - ModbusAddress mAddress; try { - mAddress = ModbusAddress.ParseFrom(address, Station); - } - catch (Exception ex) - { - return new OperResult(ex); - } - if (MulStation) - { - Init(mAddress); - } - else - { - if (Station != mAddress.Station) + easyLock.Wait(); + + ModbusAddress mAddress; + try { - return new OperResult("地址错误"); + mAddress = ModbusAddress.ParseFrom(address, Station); } - Init(mAddress); + catch (Exception ex) + { + return new OperResult(ex); + } + if (MulStation) + { + Init(mAddress); + } + else + { + if (Station != mAddress.Station) + { + return new OperResult("地址错误"); + } + Init(mAddress); + } + + var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; + var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; + var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; + var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; + int len = mAddress.ReadFunction == 2 || mAddress.ReadFunction == 1 ? length : length * RegisterByteLength; + switch (mAddress.ReadFunction) + { + case 1: + byte[] bytes0 = new byte[len]; + ModbusServer01ByteBlock.Pos = mAddress.AddressStart; + ModbusServer01ByteBlock.Read(bytes0); + return OperResult.CreateSuccessResult(bytes0); + case 2: + byte[] bytes1 = new byte[len]; + ModbusServer02ByteBlock.Pos = mAddress.AddressStart; + ModbusServer02ByteBlock.Read(bytes1); + return OperResult.CreateSuccessResult(bytes1); + case 3: + + byte[] bytes3 = new byte[len]; + ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer03ByteBlock.Read(bytes3); + return OperResult.CreateSuccessResult(bytes3); + case 4: + byte[] bytes4 = new byte[len]; + ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer04ByteBlock.Read(bytes4); + return OperResult.CreateSuccessResult(bytes4); + } + return new OperResult("功能码错误"); } - - var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; - var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; - var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; - var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; - int len = mAddress.ReadFunction == 2 || mAddress.ReadFunction == 1 ? length : length * RegisterByteLength; - switch (mAddress.ReadFunction) + finally { - case 1: - byte[] bytes0 = new byte[len]; - ModbusServer01ByteBlock.Pos = mAddress.AddressStart; - ModbusServer01ByteBlock.Read(bytes0); - return OperResult.CreateSuccessResult(bytes0); - case 2: - byte[] bytes1 = new byte[len]; - ModbusServer02ByteBlock.Pos = mAddress.AddressStart; - ModbusServer02ByteBlock.Read(bytes1); - return OperResult.CreateSuccessResult(bytes1); - case 3: - byte[] bytes3 = new byte[len]; - ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer03ByteBlock.Read(bytes3); - return OperResult.CreateSuccessResult(bytes3); - case 4: - byte[] bytes4 = new byte[len]; - ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer04ByteBlock.Read(bytes4); - return OperResult.CreateSuccessResult(bytes4); + easyLock.Release(); } - return new OperResult("功能码错误"); } /// @@ -173,87 +187,108 @@ public class ModbusSerialServer : ReadWriteDevicesSerialSessionBase SerialSession.SetDataHandlingAdapter(dataHandleAdapter); } + /// public override OperResult Write(string address, byte[] value, CancellationToken cancellationToken = default) { - ModbusAddress mAddress; try { - mAddress = ModbusAddress.ParseFrom(address, Station); - } - catch (Exception ex) - { - return new OperResult(ex); - } - if (MulStation) - { - Init(mAddress); - } - else - { - if (Station != mAddress.Station) + easyLock.Wait(); + ModbusAddress mAddress; + try { - return new OperResult("地址错误"); + mAddress = ModbusAddress.ParseFrom(address, Station); } - Init(mAddress); + catch (Exception ex) + { + return new OperResult(ex); + } + if (MulStation) + { + Init(mAddress); + } + else + { + if (Station != mAddress.Station) + { + return new OperResult("地址错误"); + } + Init(mAddress); + } + var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; + var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; + switch (mAddress.ReadFunction) + { + case 3: + ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer03ByteBlock.Write(value); + return OperResult.CreateSuccessResult(); + case 4: + ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer04ByteBlock.Write(value); + return OperResult.CreateSuccessResult(); + } + return new OperResult("功能码错误"); + } - var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; - var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; - switch (mAddress.ReadFunction) + finally { - case 3: - ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer03ByteBlock.Write(value); - return OperResult.CreateSuccessResult(); - case 4: - ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer04ByteBlock.Write(value); - return OperResult.CreateSuccessResult(); + + easyLock.Release(); } - return new OperResult("功能码错误"); } /// public override OperResult Write(string address, bool[] value, CancellationToken cancellationToken = default) { - ModbusAddress mAddress; try { - mAddress = ModbusAddress.ParseFrom(address, Station); - } - catch (Exception ex) - { - return (new OperResult(ex)); - } - if (MulStation) - { - Init(mAddress); - - } - else - { - if (Station != mAddress.Station) + easyLock.Wait(); + ModbusAddress mAddress; + try { - return (new OperResult("地址错误")); + mAddress = ModbusAddress.ParseFrom(address, Station); } - Init(mAddress); + catch (Exception ex) + { + return (new OperResult(ex)); + } + if (MulStation) + { + Init(mAddress); + + } + else + { + if (Station != mAddress.Station) + { + return (new OperResult("地址错误")); + } + Init(mAddress); + + } + + var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; + var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; + switch (mAddress.ReadFunction) + { + case 1: + ModbusServer01ByteBlock.Pos = mAddress.AddressStart; + ModbusServer01ByteBlock.Write(value.BoolArrayToByte()); + return (OperResult.CreateSuccessResult()); + case 2: + ModbusServer02ByteBlock.Pos = mAddress.AddressStart; + ModbusServer02ByteBlock.Write(value.BoolArrayToByte()); + return (OperResult.CreateSuccessResult()); + } + return new OperResult("功能码错误"); } - - var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; - var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; - switch (mAddress.ReadFunction) + finally { - case 1: - ModbusServer01ByteBlock.Pos = mAddress.AddressStart; - ModbusServer01ByteBlock.Write(value.BoolArrayToByte()); - return (OperResult.CreateSuccessResult()); - case 2: - ModbusServer02ByteBlock.Pos = mAddress.AddressStart; - ModbusServer02ByteBlock.Write(value.BoolArrayToByte()); - return (OperResult.CreateSuccessResult()); + + easyLock.Release(); } - return new OperResult("功能码错误"); } /// diff --git a/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusTcpServer/ModbusTcpServer.cs b/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusTcpServer/ModbusTcpServer.cs index ba6cadf96..60046e9d3 100644 --- a/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusTcpServer/ModbusTcpServer.cs +++ b/framework/Foundation/ThingsGateway.Foundation.Adapter.Modbus/Modbus/ModbusTcpServer/ModbusTcpServer.cs @@ -104,62 +104,75 @@ public class ModbusTcpServer : ReadWriteDevicesTcpServerBase return PackHelper.LoadSourceRead(this, deviceVariables, maxPack); } + + EasyLock easyLock = new(); + /// public override OperResult Read(string address, int length, CancellationToken cancellationToken = default) { - ModbusAddress mAddress; try { - mAddress = ModbusAddress.ParseFrom(address, Station); - } - catch (Exception ex) - { - return new OperResult(ex); - } - if (MulStation) - { - Init(mAddress); - } - else - { - if (Station != mAddress.Station) + easyLock.Wait(); + + ModbusAddress mAddress; + try { - return new OperResult("地址错误"); + mAddress = ModbusAddress.ParseFrom(address, Station); } - Init(mAddress); + catch (Exception ex) + { + return new OperResult(ex); + } + if (MulStation) + { + Init(mAddress); + } + else + { + if (Station != mAddress.Station) + { + return new OperResult("地址错误"); + } + Init(mAddress); + } + + var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; + var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; + var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; + var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; + int len = mAddress.ReadFunction == 2 || mAddress.ReadFunction == 1 ? length : length * RegisterByteLength; + switch (mAddress.ReadFunction) + { + case 1: + byte[] bytes0 = new byte[len]; + ModbusServer01ByteBlock.Pos = mAddress.AddressStart; + ModbusServer01ByteBlock.Read(bytes0); + return OperResult.CreateSuccessResult(bytes0); + case 2: + byte[] bytes1 = new byte[len]; + ModbusServer02ByteBlock.Pos = mAddress.AddressStart; + ModbusServer02ByteBlock.Read(bytes1); + return OperResult.CreateSuccessResult(bytes1); + case 3: + + byte[] bytes3 = new byte[len]; + ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer03ByteBlock.Read(bytes3); + return OperResult.CreateSuccessResult(bytes3); + case 4: + byte[] bytes4 = new byte[len]; + ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer04ByteBlock.Read(bytes4); + return OperResult.CreateSuccessResult(bytes4); + } + return new OperResult("功能码错误"); } - - var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; - var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; - var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; - var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; - int len = mAddress.ReadFunction == 2 || mAddress.ReadFunction == 1 ? length : length * RegisterByteLength; - switch (mAddress.ReadFunction) + finally { - case 1: - byte[] bytes0 = new byte[len]; - ModbusServer01ByteBlock.Pos = mAddress.AddressStart; - ModbusServer01ByteBlock.Read(bytes0); - return OperResult.CreateSuccessResult(bytes0); - case 2: - byte[] bytes1 = new byte[len]; - ModbusServer02ByteBlock.Pos = mAddress.AddressStart; - ModbusServer02ByteBlock.Read(bytes1); - return OperResult.CreateSuccessResult(bytes1); - case 3: - byte[] bytes3 = new byte[len]; - ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer03ByteBlock.Read(bytes3); - return OperResult.CreateSuccessResult(bytes3); - case 4: - byte[] bytes4 = new byte[len]; - ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer04ByteBlock.Read(bytes4); - return OperResult.CreateSuccessResult(bytes4); + easyLock.Release(); } - return new OperResult("功能码错误"); } /// @@ -193,84 +206,104 @@ public class ModbusTcpServer : ReadWriteDevicesTcpServerBase /// public override OperResult Write(string address, byte[] value, CancellationToken cancellationToken = default) { - ModbusAddress mAddress; try { - mAddress = ModbusAddress.ParseFrom(address, Station); - } - catch (Exception ex) - { - return new OperResult(ex); - } - if (MulStation) - { - Init(mAddress); - } - else - { - if (Station != mAddress.Station) + easyLock.Wait(); + ModbusAddress mAddress; + try { - return new OperResult("地址错误"); + mAddress = ModbusAddress.ParseFrom(address, Station); } - Init(mAddress); + catch (Exception ex) + { + return new OperResult(ex); + } + if (MulStation) + { + Init(mAddress); + } + else + { + if (Station != mAddress.Station) + { + return new OperResult("地址错误"); + } + Init(mAddress); + } + var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; + var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; + switch (mAddress.ReadFunction) + { + case 3: + ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer03ByteBlock.Write(value); + return OperResult.CreateSuccessResult(); + case 4: + ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; + ModbusServer04ByteBlock.Write(value); + return OperResult.CreateSuccessResult(); + } + return new OperResult("功能码错误"); + } - var ModbusServer03ByteBlock = ModbusServer03ByteBlocks[mAddress.Station]; - var ModbusServer04ByteBlock = ModbusServer04ByteBlocks[mAddress.Station]; - switch (mAddress.ReadFunction) + finally { - case 3: - ModbusServer03ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer03ByteBlock.Write(value); - return OperResult.CreateSuccessResult(); - case 4: - ModbusServer04ByteBlock.Pos = mAddress.AddressStart * RegisterByteLength; - ModbusServer04ByteBlock.Write(value); - return OperResult.CreateSuccessResult(); + + easyLock.Release(); } - return new OperResult("功能码错误"); } /// public override OperResult Write(string address, bool[] value, CancellationToken cancellationToken = default) { - ModbusAddress mAddress; try { - mAddress = ModbusAddress.ParseFrom(address, Station); - } - catch (Exception ex) - { - return (new OperResult(ex)); - } - if (MulStation) - { - Init(mAddress); - - } - else - { - if (Station != mAddress.Station) + easyLock.Wait(); + ModbusAddress mAddress; + try { - return (new OperResult("地址错误")); + mAddress = ModbusAddress.ParseFrom(address, Station); } - Init(mAddress); + catch (Exception ex) + { + return (new OperResult(ex)); + } + if (MulStation) + { + Init(mAddress); + + } + else + { + if (Station != mAddress.Station) + { + return (new OperResult("地址错误")); + } + Init(mAddress); + + } + + var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; + var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; + switch (mAddress.ReadFunction) + { + case 1: + ModbusServer01ByteBlock.Pos = mAddress.AddressStart; + ModbusServer01ByteBlock.Write(value.BoolArrayToByte()); + return (OperResult.CreateSuccessResult()); + case 2: + ModbusServer02ByteBlock.Pos = mAddress.AddressStart; + ModbusServer02ByteBlock.Write(value.BoolArrayToByte()); + return (OperResult.CreateSuccessResult()); + } + return new OperResult("功能码错误"); } - - var ModbusServer01ByteBlock = ModbusServer01ByteBlocks[mAddress.Station]; - var ModbusServer02ByteBlock = ModbusServer02ByteBlocks[mAddress.Station]; - switch (mAddress.ReadFunction) + finally { - case 1: - ModbusServer01ByteBlock.Pos = mAddress.AddressStart; - ModbusServer01ByteBlock.Write(value.BoolArrayToByte()); - return (OperResult.CreateSuccessResult()); - case 2: - ModbusServer02ByteBlock.Pos = mAddress.AddressStart; - ModbusServer02ByteBlock.Write(value.BoolArrayToByte()); - return (OperResult.CreateSuccessResult()); + + easyLock.Release(); } - return new OperResult("功能码错误"); } /// diff --git a/framework/Plugin/Directory.Build.props b/framework/Plugin/Directory.Build.props index 7c887c15d..0a64bbf4e 100644 --- a/framework/Plugin/Directory.Build.props +++ b/framework/Plugin/Directory.Build.props @@ -1,6 +1,6 @@ - 3.0.0.21 + 3.0.0.22 latest enable net6.0;net7.0 diff --git a/framework/UpgradeManger/Directory.Build.props b/framework/UpgradeManger/Directory.Build.props index f47930a9f..e43e21213 100644 --- a/framework/UpgradeManger/Directory.Build.props +++ b/framework/UpgradeManger/Directory.Build.props @@ -1,6 +1,6 @@ - 3.0.0.21 + 3.0.0.22 latest net6.0;net7.0 Diego diff --git a/framework/Web/Directory.Build.props b/framework/Web/Directory.Build.props index 0eec2b050..c6b7e4717 100644 --- a/framework/Web/Directory.Build.props +++ b/framework/Web/Directory.Build.props @@ -1,6 +1,6 @@ - 3.0.0.21 + 3.0.0.22 latest enable net6.0;net7.0