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