Fix
This commit is contained in:
@@ -112,7 +112,7 @@ namespace Modbus.Net
|
|||||||
IDisposable asyncLock = null;
|
IDisposable asyncLock = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Controller.IsSending != true)
|
if (!Controller.IsSending)
|
||||||
{
|
{
|
||||||
Controller.SendStart();
|
Controller.SendStart();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,10 +82,6 @@ namespace Modbus.Net
|
|||||||
/// 获取线程
|
/// 获取线程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Task _receiveThread;
|
private Task _receiveThread;
|
||||||
/// <summary>
|
|
||||||
/// 获取线程关闭
|
|
||||||
/// </summary>
|
|
||||||
private bool _taskCancel = false;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 缓冲的字节流
|
/// 缓冲的字节流
|
||||||
@@ -276,23 +272,23 @@ namespace Modbus.Net
|
|||||||
// Release managed resources
|
// Release managed resources
|
||||||
}
|
}
|
||||||
// Release unmanaged resources
|
// Release unmanaged resources
|
||||||
if (SerialPort != null)
|
Linkers?.Remove((_slave, _com));
|
||||||
{
|
|
||||||
Linkers.Remove((_slave, _com));
|
|
||||||
logger.LogInformation("Com connector {ConnectionToken} Removed", _com);
|
logger.LogInformation("Com connector {ConnectionToken} Removed", _com);
|
||||||
if (Linkers.Count(p => p.Item2 == _com) == 0)
|
if (Linkers?.Count(p => p.Item2 == _com) == 0)
|
||||||
{
|
{
|
||||||
if (SerialPort.IsOpen)
|
if (SerialPort?.IsOpen == true)
|
||||||
{
|
{
|
||||||
SerialPort.Close();
|
SerialPort?.Close();
|
||||||
}
|
}
|
||||||
SerialPort.Dispose();
|
SerialPort?.Dispose();
|
||||||
logger.LogInformation("Com interface {Com} Disposed", _com);
|
logger.LogInformation("Com interface {Com} Disposed", _com);
|
||||||
Controller.SendStop();
|
Controller?.SendStop();
|
||||||
|
if (Connectors.ContainsKey(_com))
|
||||||
|
{
|
||||||
Connectors[_com] = null;
|
Connectors[_com] = null;
|
||||||
Connectors.Remove(_com);
|
Connectors.Remove(_com);
|
||||||
ReceiveMsgThreadStop();
|
|
||||||
}
|
}
|
||||||
|
ReceiveMsgThreadStop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +361,7 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
lock (SerialPort)
|
lock (SerialPort)
|
||||||
{
|
{
|
||||||
_taskCancel = false;
|
ReceiveMsgThreadStop();
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
ReceiveMsgThreadStart();
|
ReceiveMsgThreadStart();
|
||||||
Controller.SendStart();
|
Controller.SendStart();
|
||||||
@@ -495,19 +491,36 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void ReceiveMsgThreadStart()
|
protected override void ReceiveMsgThreadStart()
|
||||||
|
{
|
||||||
|
if (_receiveThread == null)
|
||||||
{
|
{
|
||||||
_receiveThread = Task.Run(ReceiveMessage);
|
_receiveThread = Task.Run(ReceiveMessage);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void ReceiveMsgThreadStop()
|
protected override void ReceiveMsgThreadStop()
|
||||||
{
|
{
|
||||||
_taskCancel = true;
|
_receiveThread?.Dispose();
|
||||||
|
_receiveThread = null;
|
||||||
|
CacheClear();
|
||||||
|
Controller?.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CacheClear()
|
||||||
|
{
|
||||||
|
if (CacheBytes != null)
|
||||||
|
{
|
||||||
|
lock (CacheBytes)
|
||||||
|
{
|
||||||
|
CacheBytes.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ReceiveMessage()
|
private async Task ReceiveMessage()
|
||||||
{
|
{
|
||||||
while (!_taskCancel)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -532,10 +545,7 @@ namespace Modbus.Net
|
|||||||
CacheBytes.Count);
|
CacheBytes.Count);
|
||||||
logger.LogError(
|
logger.LogError(
|
||||||
$"Com client {ConnectionToken} cached msg: {string.Concat(CacheBytes.Select(p => " " + p.ToString("X2")))}");
|
$"Com client {ConnectionToken} cached msg: {string.Concat(CacheBytes.Select(p => " " + p.ToString("X2")))}");
|
||||||
lock (CacheBytes)
|
CacheClear();
|
||||||
{
|
|
||||||
CacheBytes.Clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -569,6 +579,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
CacheClear();
|
||||||
logger.LogError(e, "Com client {ConnectionToken} read msg error", ConnectionToken);
|
logger.LogError(e, "Com client {ConnectionToken} read msg error", ConnectionToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息维护线程是否在运行
|
/// 消息维护线程是否在运行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual bool? IsSending => SendingThread?.Status == TaskStatus.Running || SendingThread?.Status == TaskStatus.WaitingToRun || SendingThread?.Status == TaskStatus.Created;
|
public virtual bool IsSending => SendingThread != null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 包切分位置函数
|
/// 包切分位置函数
|
||||||
@@ -73,7 +73,8 @@ namespace Modbus.Net
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual void SendStop()
|
public virtual void SendStop()
|
||||||
{
|
{
|
||||||
SendingThread.Dispose();
|
Clear();
|
||||||
|
SendingThread?.Dispose();
|
||||||
SendingThread = null;
|
SendingThread = null;
|
||||||
Clear();
|
Clear();
|
||||||
}
|
}
|
||||||
@@ -81,7 +82,7 @@ namespace Modbus.Net
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual void SendStart()
|
public virtual void SendStart()
|
||||||
{
|
{
|
||||||
if (IsSending != true)
|
if (!IsSending)
|
||||||
{
|
{
|
||||||
SendingThread = Task.Run(() => SendingMessageControlInner());
|
SendingThread = Task.Run(() => SendingMessageControlInner());
|
||||||
}
|
}
|
||||||
@@ -89,12 +90,15 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Clear()
|
public void Clear()
|
||||||
|
{
|
||||||
|
if (WaitingMessages != null)
|
||||||
{
|
{
|
||||||
lock (WaitingMessages)
|
lock (WaitingMessages)
|
||||||
{
|
{
|
||||||
WaitingMessages.Clear();
|
WaitingMessages.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将信息添加到队列
|
/// 将信息添加到队列
|
||||||
@@ -196,13 +200,7 @@ namespace Modbus.Net
|
|||||||
if (def != null)
|
if (def != null)
|
||||||
{
|
{
|
||||||
def.ReceiveMessage = message.Item1;
|
def.ReceiveMessage = message.Item1;
|
||||||
lock (WaitingMessages)
|
ForceRemoveWaitingMessage(def);
|
||||||
{
|
|
||||||
if (WaitingMessages.IndexOf(def) >= 0)
|
|
||||||
{
|
|
||||||
WaitingMessages.Remove(def);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
def.ReceiveMutex.Set();
|
def.ReceiveMutex.Set();
|
||||||
ans.Add((message.Item1, true));
|
ans.Add((message.Item1, true));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
private MessageWaitingDef _currentSendingPos;
|
private MessageWaitingDef _currentSendingPos;
|
||||||
|
|
||||||
private bool _taskCancel = false;
|
|
||||||
|
|
||||||
private int _waitingListMaxCount;
|
private int _waitingListMaxCount;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -40,7 +38,7 @@ namespace Modbus.Net
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void SendingMessageControlInner()
|
protected override void SendingMessageControlInner()
|
||||||
{
|
{
|
||||||
while (!_taskCancel)
|
while (true)
|
||||||
{
|
{
|
||||||
if (AcquireTime > 0)
|
if (AcquireTime > 0)
|
||||||
{
|
{
|
||||||
@@ -79,25 +77,10 @@ namespace Modbus.Net
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
logger.LogError(e, "Controller throws exception");
|
logger.LogError(e, "Controller throws exception");
|
||||||
_taskCancel = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SendStop();
|
SendStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void SendStart()
|
|
||||||
{
|
|
||||||
_taskCancel = false;
|
|
||||||
base.SendStart();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/// <inheritdoc />
|
|
||||||
public override void SendStop()
|
|
||||||
{
|
|
||||||
_taskCancel = true;
|
|
||||||
base.SendStop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -124,7 +107,7 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (_taskCancel)
|
if (!IsSending)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息维护线程是否在运行
|
/// 消息维护线程是否在运行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override bool? IsSending => true;
|
public override bool IsSending => true;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 消息维护线程是否在运行
|
/// 消息维护线程是否在运行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool? IsSending { get; }
|
bool IsSending { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 增加信息
|
/// 增加信息
|
||||||
|
|||||||
Reference in New Issue
Block a user