2017-12-27 Update 1 Tcp Connector and Siemens Tcp Update
This commit is contained in:
@@ -18,6 +18,7 @@ namespace Modbus.Net.Siemens
|
|||||||
public SiemensPpiProtocol(byte slaveAddress, byte masterAddress)
|
public SiemensPpiProtocol(byte slaveAddress, byte masterAddress)
|
||||||
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress)
|
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress)
|
||||||
{
|
{
|
||||||
|
ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -71,7 +72,8 @@ namespace Modbus.Net.Siemens
|
|||||||
/// <returns>是否连接成功</returns>
|
/// <returns>是否连接成功</returns>
|
||||||
public override async Task<bool> ConnectAsync()
|
public override async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress);
|
if (ProtocolLinker.IsConnected) return true;
|
||||||
|
if (!await ProtocolLinker.ConnectAsync()) return false;
|
||||||
var inputStruct = new ComCreateReferenceSiemensInputStruct(SlaveAddress, MasterAddress);
|
var inputStruct = new ComCreateReferenceSiemensInputStruct(SlaveAddress, MasterAddress);
|
||||||
var outputStruct =
|
var outputStruct =
|
||||||
(await (await
|
(await (await
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ namespace Modbus.Net.Siemens
|
|||||||
_ip = ip;
|
_ip = ip;
|
||||||
_port = port;
|
_port = port;
|
||||||
_connectTryCount = 0;
|
_connectTryCount = 0;
|
||||||
|
ProtocolLinker = new SiemensTcpProtocolLinker(_ip, _port);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -143,7 +144,7 @@ namespace Modbus.Net.Siemens
|
|||||||
public override async Task<bool> ConnectAsync()
|
public override async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
_connectTryCount++;
|
_connectTryCount++;
|
||||||
ProtocolLinker = new SiemensTcpProtocolLinker(_ip, _port);
|
if (ProtocolLinker.IsConnected) return true;
|
||||||
if (!await ProtocolLinker.ConnectAsync()) return false;
|
if (!await ProtocolLinker.ConnectAsync()) return false;
|
||||||
_connectTryCount = 0;
|
_connectTryCount = 0;
|
||||||
var inputStruct = new CreateReferenceSiemensInputStruct(_tdpuSize, _taspSrc, _tsapDst);
|
var inputStruct = new CreateReferenceSiemensInputStruct(_tdpuSize, _taspSrc, _tsapDst);
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ namespace Modbus.Net
|
|||||||
public abstract void SendStop();
|
public abstract void SendStop();
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void SendStart()
|
public virtual void SendStart()
|
||||||
{
|
{
|
||||||
if (SendingThread == null)
|
if (SendingThread == null)
|
||||||
{
|
{
|
||||||
@@ -80,7 +80,7 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
lock (WaitingMessages)
|
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);
|
WaitingMessages.Add(def);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -349,12 +349,13 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
lock (SerialPort)
|
lock (SerialPort)
|
||||||
{
|
{
|
||||||
|
_taskCancel = false;
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
ReceiveMsgThreadStart();
|
ReceiveMsgThreadStart();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Controller.SendStart();
|
Controller.SendStart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Log.Information("Com client {ConnectionToken} connect success", ConnectionToken);
|
Log.Information("Com client {ConnectionToken} connect success", ConnectionToken);
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace Modbus.Net
|
|||||||
int slaveAddress)
|
int slaveAddress)
|
||||||
: this(
|
: this(
|
||||||
com, baudRate, parity, stopBits, dataBits,
|
com, baudRate, parity, stopBits, dataBits,
|
||||||
int.Parse(ConfigurationManager.AppSettings["ComConnectionTimeout"] ?? "3000"), slaveAddress)
|
int.Parse(ConfigurationManager.AppSettings["ComConnectionTimeout"] ?? "-1"), slaveAddress)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,9 +50,17 @@ namespace Modbus.Net
|
|||||||
/// <param name="slaveAddress">从站地址</param>
|
/// <param name="slaveAddress">从站地址</param>
|
||||||
protected ComProtocolLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits,
|
protected ComProtocolLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits,
|
||||||
int connectionTimeout, int slaveAddress)
|
int connectionTimeout, int slaveAddress)
|
||||||
|
{
|
||||||
|
if (connectionTimeout == -1)
|
||||||
|
{
|
||||||
|
BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits,
|
BaseConnector = new ComConnector(com + ":" + slaveAddress, baudRate, parity, stopBits, dataBits,
|
||||||
connectionTimeout);
|
connectionTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,6 +44,14 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
lock (WaitingMessages)
|
lock (WaitingMessages)
|
||||||
{
|
{
|
||||||
|
if (_currentSendingPos == null)
|
||||||
|
{
|
||||||
|
if (WaitingMessages.Count > 0)
|
||||||
|
{
|
||||||
|
_currentSendingPos = WaitingMessages.First();
|
||||||
|
_currentSendingPos.SendMutex.Set();
|
||||||
|
}
|
||||||
|
}
|
||||||
if (_currentSendingPos != null)
|
if (_currentSendingPos != null)
|
||||||
{
|
{
|
||||||
if (WaitingMessages.Count <= 0)
|
if (WaitingMessages.Count <= 0)
|
||||||
@@ -70,6 +78,13 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void SendStart()
|
||||||
|
{
|
||||||
|
_taskCancel = false;
|
||||||
|
base.SendStart();
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void SendStop()
|
public override void SendStop()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -85,6 +85,13 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void SendStart()
|
||||||
|
{
|
||||||
|
_taskCancel = false;
|
||||||
|
base.SendStart();
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void SendStop()
|
public override void SendStop()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -138,9 +138,13 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override async Task<bool> ConnectAsync()
|
public override async Task<bool> ConnectAsync()
|
||||||
|
{
|
||||||
|
using (await Lock.LockAsync())
|
||||||
{
|
{
|
||||||
if (_socketClient != null)
|
if (_socketClient != null)
|
||||||
Disconnect();
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_socketClient = new TcpClient
|
_socketClient = new TcpClient
|
||||||
@@ -161,6 +165,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
if (_socketClient.Connected)
|
if (_socketClient.Connected)
|
||||||
{
|
{
|
||||||
|
_taskCancel = false;
|
||||||
Controller.SendStart();
|
Controller.SendStart();
|
||||||
ReceiveMsgThreadStart();
|
ReceiveMsgThreadStart();
|
||||||
Log.Information("Tcp client {ConnectionToken} connected", ConnectionToken);
|
Log.Information("Tcp client {ConnectionToken} connected", ConnectionToken);
|
||||||
@@ -175,6 +180,7 @@ namespace Modbus.Net
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool Disconnect()
|
public override bool Disconnect()
|
||||||
@@ -185,7 +191,6 @@ namespace Modbus.Net
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Dispose();
|
Dispose();
|
||||||
Controller.SendStop();
|
|
||||||
Log.Information("Tcp client {ConnectionToken} disconnected successfully", ConnectionToken);
|
Log.Information("Tcp client {ConnectionToken} disconnected successfully", ConnectionToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -276,7 +281,7 @@ namespace Modbus.Net
|
|||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
Log.Error(err, "Tcp client {ConnectionToken} receive exception", ConnectionToken);
|
Log.Error(err, "Tcp client {ConnectionToken} receive exception", ConnectionToken);
|
||||||
CloseClientSocket();
|
//CloseClientSocket();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,7 +325,10 @@ namespace Modbus.Net
|
|||||||
Controller.SendStop();
|
Controller.SendStop();
|
||||||
Controller.Clear();
|
Controller.Clear();
|
||||||
ReceiveMsgThreadStop();
|
ReceiveMsgThreadStop();
|
||||||
|
if (_socketClient.Connected)
|
||||||
|
{
|
||||||
_socketClient?.GetStream().Dispose();
|
_socketClient?.GetStream().Dispose();
|
||||||
|
}
|
||||||
_socketClient?.Close();
|
_socketClient?.Close();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Modbus.Net
|
|||||||
/// <param name="ip">Ip地址</param>
|
/// <param name="ip">Ip地址</param>
|
||||||
/// <param name="port">端口</param>
|
/// <param name="port">端口</param>
|
||||||
protected TcpProtocolLinker(string ip, int port)
|
protected TcpProtocolLinker(string ip, int port)
|
||||||
: this(ip, port, int.Parse(ConfigurationManager.AppSettings["IPConnectionTimeout"] ?? "5000"))
|
: this(ip, port, int.Parse(ConfigurationManager.AppSettings["IPConnectionTimeout"] ?? "-1"))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,9 +32,17 @@ namespace Modbus.Net
|
|||||||
/// <param name="port">端口</param>
|
/// <param name="port">端口</param>
|
||||||
/// <param name="connectionTimeout">超时时间</param>
|
/// <param name="connectionTimeout">超时时间</param>
|
||||||
protected TcpProtocolLinker(string ip, int port, int connectionTimeout)
|
protected TcpProtocolLinker(string ip, int port, int connectionTimeout)
|
||||||
|
{
|
||||||
|
if (connectionTimeout == -1)
|
||||||
|
{
|
||||||
|
//初始化连接对象
|
||||||
|
BaseConnector = new TcpConnector(ip, port);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
//初始化连接对象
|
//初始化连接对象
|
||||||
BaseConnector = new TcpConnector(ip, port, connectionTimeout);
|
BaseConnector = new TcpConnector(ip, port, connectionTimeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user