diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs index 52d45b3..307ac8e 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs @@ -18,6 +18,7 @@ namespace Modbus.Net.Siemens public SiemensPpiProtocol(byte slaveAddress, byte masterAddress) : this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress) { + ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress); } /// @@ -71,7 +72,8 @@ namespace Modbus.Net.Siemens /// 是否连接成功 public override async Task ConnectAsync() { - ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress); + if (ProtocolLinker.IsConnected) return true; + if (!await ProtocolLinker.ConnectAsync()) return false; var inputStruct = new ComCreateReferenceSiemensInputStruct(SlaveAddress, MasterAddress); var outputStruct = (await (await diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs index 3e8ce00..15fd6ac 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs @@ -74,6 +74,7 @@ namespace Modbus.Net.Siemens _ip = ip; _port = port; _connectTryCount = 0; + ProtocolLinker = new SiemensTcpProtocolLinker(_ip, _port); } /// @@ -143,7 +144,7 @@ namespace Modbus.Net.Siemens public override async Task ConnectAsync() { _connectTryCount++; - ProtocolLinker = new SiemensTcpProtocolLinker(_ip, _port); + if (ProtocolLinker.IsConnected) return true; if (!await ProtocolLinker.ConnectAsync()) return false; _connectTryCount = 0; var inputStruct = new CreateReferenceSiemensInputStruct(_tdpuSize, _taspSrc, _tsapDst); diff --git a/Modbus.Net/Modbus.Net/BaseController.cs b/Modbus.Net/Modbus.Net/BaseController.cs index 6e4f2a0..2358b71 100644 --- a/Modbus.Net/Modbus.Net/BaseController.cs +++ b/Modbus.Net/Modbus.Net/BaseController.cs @@ -55,7 +55,7 @@ namespace Modbus.Net public abstract void SendStop(); /// - public void SendStart() + public virtual void SendStart() { if (SendingThread == null) { @@ -80,7 +80,7 @@ namespace Modbus.Net { lock (WaitingMessages) { - if (WaitingMessages.FirstOrDefault(p => p.Key == def.Key) == null) + if (WaitingMessages.FirstOrDefault(p => p.Key == def.Key) == null || def.Key == null) { WaitingMessages.Add(def); return true; diff --git a/Modbus.Net/Modbus.Net/ComConnector.cs b/Modbus.Net/Modbus.Net/ComConnector.cs index 0da1861..5492a4c 100644 --- a/Modbus.Net/Modbus.Net/ComConnector.cs +++ b/Modbus.Net/Modbus.Net/ComConnector.cs @@ -349,13 +349,14 @@ namespace Modbus.Net { lock (SerialPort) { + _taskCancel = false; SerialPort.Open(); ReceiveMsgThreadStart(); + Controller.SendStart(); } } } - Controller.SendStart(); - + Log.Information("Com client {ConnectionToken} connect success", ConnectionToken); return true; diff --git a/Modbus.Net/Modbus.Net/ComProtocalLinker.cs b/Modbus.Net/Modbus.Net/ComProtocalLinker.cs index 2c266c3..a845e37 100644 --- a/Modbus.Net/Modbus.Net/ComProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/ComProtocalLinker.cs @@ -34,7 +34,7 @@ namespace Modbus.Net int slaveAddress) : this( com, baudRate, parity, stopBits, dataBits, - int.Parse(ConfigurationManager.AppSettings["ComConnectionTimeout"] ?? "3000"), slaveAddress) + int.Parse(ConfigurationManager.AppSettings["ComConnectionTimeout"] ?? "-1"), slaveAddress) { } @@ -51,8 +51,16 @@ namespace Modbus.Net protected ComProtocolLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits, int connectionTimeout, int slaveAddress) { - BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits, - connectionTimeout); + if (connectionTimeout == -1) + { + BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits); + } + else + { + BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits, + connectionTimeout); + } + } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/FifoController.cs b/Modbus.Net/Modbus.Net/FifoController.cs index 0b8267b..58134fb 100644 --- a/Modbus.Net/Modbus.Net/FifoController.cs +++ b/Modbus.Net/Modbus.Net/FifoController.cs @@ -44,6 +44,14 @@ namespace Modbus.Net } lock (WaitingMessages) { + if (_currentSendingPos == null) + { + if (WaitingMessages.Count > 0) + { + _currentSendingPos = WaitingMessages.First(); + _currentSendingPos.SendMutex.Set(); + } + } if (_currentSendingPos != null) { if (WaitingMessages.Count <= 0) @@ -70,6 +78,13 @@ namespace Modbus.Net } + /// + public override void SendStart() + { + _taskCancel = false; + base.SendStart(); + } + /// public override void SendStop() { diff --git a/Modbus.Net/Modbus.Net/MatchController.cs b/Modbus.Net/Modbus.Net/MatchController.cs index a4f00f5..252f811 100644 --- a/Modbus.Net/Modbus.Net/MatchController.cs +++ b/Modbus.Net/Modbus.Net/MatchController.cs @@ -85,6 +85,13 @@ namespace Modbus.Net } + /// + public override void SendStart() + { + _taskCancel = false; + base.SendStart(); + } + /// public override void SendStop() { diff --git a/Modbus.Net/Modbus.Net/TcpConnector.cs b/Modbus.Net/Modbus.Net/TcpConnector.cs index bdcbe9b..174968a 100644 --- a/Modbus.Net/Modbus.Net/TcpConnector.cs +++ b/Modbus.Net/Modbus.Net/TcpConnector.cs @@ -139,40 +139,46 @@ namespace Modbus.Net /// public override async Task ConnectAsync() { - if (_socketClient != null) - Disconnect(); - try + using (await Lock.LockAsync()) { - _socketClient = new TcpClient + if (_socketClient != null) { - SendTimeout = TimeoutTime, - ReceiveTimeout = TimeoutTime - }; - - try - { - var cts = new CancellationTokenSource(); - cts.CancelAfter(TimeoutTime); - await _socketClient.ConnectAsync(_host, _port).WithCancellation(cts.Token); - } - catch (Exception e) - { - Log.Error(e, "Tcp client {ConnectionToken} connect error", ConnectionToken); - } - if (_socketClient.Connected) - { - Controller.SendStart(); - ReceiveMsgThreadStart(); - Log.Information("Tcp client {ConnectionToken} connected", ConnectionToken); return true; } - Log.Error("Tcp client {ConnectionToken} connect failed.", ConnectionToken); - return false; - } - catch (Exception err) - { - Log.Error(err, "Tcp client {ConnectionToken} connect exception", ConnectionToken); - return false; + try + { + _socketClient = new TcpClient + { + SendTimeout = TimeoutTime, + ReceiveTimeout = TimeoutTime + }; + + try + { + var cts = new CancellationTokenSource(); + cts.CancelAfter(TimeoutTime); + await _socketClient.ConnectAsync(_host, _port).WithCancellation(cts.Token); + } + catch (Exception e) + { + Log.Error(e, "Tcp client {ConnectionToken} connect error", ConnectionToken); + } + if (_socketClient.Connected) + { + _taskCancel = false; + Controller.SendStart(); + ReceiveMsgThreadStart(); + Log.Information("Tcp client {ConnectionToken} connected", ConnectionToken); + return true; + } + Log.Error("Tcp client {ConnectionToken} connect failed.", ConnectionToken); + return false; + } + catch (Exception err) + { + Log.Error(err, "Tcp client {ConnectionToken} connect exception", ConnectionToken); + return false; + } } } @@ -185,7 +191,6 @@ namespace Modbus.Net try { Dispose(); - Controller.SendStop(); Log.Information("Tcp client {ConnectionToken} disconnected successfully", ConnectionToken); return true; } @@ -276,7 +281,7 @@ namespace Modbus.Net catch (Exception err) { Log.Error(err, "Tcp client {ConnectionToken} receive exception", ConnectionToken); - CloseClientSocket(); + //CloseClientSocket(); } } @@ -320,7 +325,10 @@ namespace Modbus.Net Controller.SendStop(); Controller.Clear(); ReceiveMsgThreadStop(); - _socketClient?.GetStream().Dispose(); + if (_socketClient.Connected) + { + _socketClient?.GetStream().Dispose(); + } _socketClient?.Close(); } catch (Exception ex) diff --git a/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs b/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs index 34eb76c..d3f155f 100644 --- a/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/TcpProtocalLinker.cs @@ -21,7 +21,7 @@ namespace Modbus.Net /// Ip地址 /// 端口 protected TcpProtocolLinker(string ip, int port) - : this(ip, port, int.Parse(ConfigurationManager.AppSettings["IPConnectionTimeout"] ?? "5000")) + : this(ip, port, int.Parse(ConfigurationManager.AppSettings["IPConnectionTimeout"] ?? "-1")) { } @@ -33,8 +33,16 @@ namespace Modbus.Net /// 超时时间 protected TcpProtocolLinker(string ip, int port, int connectionTimeout) { - //初始化连接对象 - BaseConnector = new TcpConnector(ip, port, connectionTimeout); + if (connectionTimeout == -1) + { + //初始化连接对象 + BaseConnector = new TcpConnector(ip, port); + } + else + { + //初始化连接对象 + BaseConnector = new TcpConnector(ip, port, connectionTimeout); + } } } } \ No newline at end of file