diff --git a/Modbus.Net/ModBus.Net/BaseProtocal.cs b/Modbus.Net/ModBus.Net/BaseProtocal.cs index ab85f4c..52b092e 100644 --- a/Modbus.Net/ModBus.Net/BaseProtocal.cs +++ b/Modbus.Net/ModBus.Net/BaseProtocal.cs @@ -60,7 +60,7 @@ namespace ModBus.Net /// public virtual byte[] SendReceive(params object[] content) { - return ProtocalLinker.SendReceive(ProtocalUnit.TranslateContent(content)); + return AsyncHelper.RunSync(() => SendReceiveAsync(content)); } /// @@ -70,6 +70,10 @@ namespace ModBus.Net /// public virtual async Task SendReceiveAsync(params object[] content) { + if (ProtocalLinker == null || !ProtocalLinker.IsConnected) + { + await ConnectAsync(); + } return await ProtocalLinker.SendReceiveAsync(ProtocalUnit.TranslateContent(content)); } diff --git a/Modbus.Net/ModBus.Net/Siemens/SiemensTcpProtocal.cs b/Modbus.Net/ModBus.Net/Siemens/SiemensTcpProtocal.cs index 05b8cc5..ec91abd 100644 --- a/Modbus.Net/ModBus.Net/Siemens/SiemensTcpProtocal.cs +++ b/Modbus.Net/ModBus.Net/Siemens/SiemensTcpProtocal.cs @@ -37,7 +37,7 @@ namespace ModBus.Net.Siemens public override async Task SendReceiveAsync(params object[] content) { - while (!ProtocalLinker.IsConnected) + if (ProtocalLinker == null || !ProtocalLinker.IsConnected) { await ConnectAsync(); } @@ -51,7 +51,7 @@ namespace ModBus.Net.Siemens public override async Task SendReceiveAsync(ProtocalUnit unit, InputStruct content) { - if (!ProtocalLinker.IsConnected) + if (ProtocalLinker == null || !ProtocalLinker.IsConnected) { if (connectTryCount > 10) return null; return await await ConnectAsync().ContinueWith(answer => answer.Result ? base.SendReceiveAsync(unit, content) : null);