diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs index 307ac8e..b25bb07 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs @@ -18,7 +18,6 @@ namespace Modbus.Net.Siemens public SiemensPpiProtocol(byte slaveAddress, byte masterAddress) : this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress) { - ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress); } /// @@ -31,6 +30,7 @@ namespace Modbus.Net.Siemens : base(slaveAddress, masterAddress) { _com = com; + ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress); } /// @@ -79,10 +79,14 @@ namespace Modbus.Net.Siemens (await (await ForceSendReceiveAsync(this[typeof(ComCreateReferenceSiemensProtocol)], inputStruct)). - SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocol)], answer => - - new ComConfirmMessageSiemensInputStruct(SlaveAddress, MasterAddress) - )).Unwrap(); + SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocol)], answer => + answer != null + ? new ComConfirmMessageSiemensInputStruct(SlaveAddress, MasterAddress) + : null)).Unwrap(); + if (outputStruct == null && ProtocolLinker.IsConnected) + { + ProtocolLinker.Disconnect(); + } return outputStruct != null; } } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs index 1146d23..f9db58a 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs @@ -18,7 +18,7 @@ namespace Modbus.Net.Siemens public SiemensPpiProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.Even, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[] { new List<(int,int)> { (4,5) }, new List<(int,int)> {(5,4) }, new List<(int,int)> { (11 ,11), (12,12) } }, 0)); + ((BaseConnector)BaseConnector).AddController(new FifoController(0)); } /// @@ -37,6 +37,7 @@ namespace Modbus.Net.Siemens new ComConfirmMessageSiemensProtocol().Format(inputStruct2)); } var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes); + if (receiveBytes == null) return null; if (content.Length > 6 && receiveBytes.Length == 1 && receiveBytes[0] == 0xe5) { var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]); @@ -56,7 +57,7 @@ namespace Modbus.Net.Siemens public override async Task SendReceiveWithoutExtAndDecAsync(byte[] content) { var ans = await base.SendReceiveWithoutExtAndDecAsync(content); - while (ans.Length == 1 && ans[0] == 0xf9) + while (ans?.Length == 1 && ans[0] == 0xf9) { Thread.Sleep(500); if (content.Length <= 6) diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs index 15fd6ac..ed24709 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs @@ -152,10 +152,15 @@ namespace Modbus.Net.Siemens //先建立连接,然后建立设备的引用 (await (await ForceSendReceiveAsync(this[typeof(CreateReferenceSiemensProtocol)], inputStruct)).SendReceiveAsync( - this[typeof(EstablishAssociationSiemensProtocol)], answer => + this[typeof(EstablishAssociationSiemensProtocol)], answer => + answer != null ? new EstablishAssociationSiemensInputStruct(0x0101, _maxCalling, _maxCalled, - _maxPdu))).Unwrap(); + _maxPdu) : null)).Unwrap(); + if (outputStruct == null && ProtocolLinker.IsConnected) + { + ProtocolLinker.Disconnect(); + } return outputStruct != null; } } diff --git a/Modbus.Net/Modbus.Net/ProtocalLinker.cs b/Modbus.Net/Modbus.Net/ProtocalLinker.cs index 91f77d3..e9fd246 100644 --- a/Modbus.Net/Modbus.Net/ProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/ProtocalLinker.cs @@ -66,6 +66,11 @@ namespace Modbus.Net return bytesExtend?.BytesDecact(content); } + /// + /// 检查接收的数据是否正确 + /// + /// 接收协议的内容 + /// 协议是否是正确的 public override bool? CheckRight(byte[] content) { if (content == null)