diff --git a/Modbus.Net/Modbus.Net/BaseConnector.cs b/Modbus.Net/Modbus.Net/BaseConnector.cs
index cefc94e..3335596 100644
--- a/Modbus.Net/Modbus.Net/BaseConnector.cs
+++ b/Modbus.Net/Modbus.Net/BaseConnector.cs
@@ -38,33 +38,11 @@ namespace Modbus.Net
///
public override async Task SendMsgAsync(byte[] message)
{
- var ans = await SendMsgCtrl(message);
+ var ans = await SendMsgInner(message);
if (ans == null) return new byte[0];
return ans.ReceiveMessage;
}
- ///
- /// 发送主控
- ///
- /// 发送的信息
- /// 等待信息的定义
- protected async Task SendMsgCtrl(byte[] message)
- {
- MessageWaitingDef ans;
- if (!IsFullDuplex)
- {
- using (await Lock.LockAsync())
- {
- ans = await SendMsgInner(message);
- }
- }
- else
- {
- ans = await SendMsgInner(message);
- }
- return ans;
- }
-
///
/// 发送内部
///
@@ -72,11 +50,16 @@ namespace Modbus.Net
/// 发送信息的定义
protected async Task SendMsgInner(byte[] message)
{
+ IDisposable asyncLock = null;
try
- {
+ {
var messageSendingdef = Controller.AddMessage(message);
if (messageSendingdef != null)
{
+ if (!IsFullDuplex)
+ {
+ asyncLock = await Lock.LockAsync();
+ }
var success = messageSendingdef.SendMutex.WaitOne(TimeoutTime);
if (success)
{
@@ -89,6 +72,7 @@ namespace Modbus.Net
}
Controller.ForceRemoveWaitingMessage(messageSendingdef);
}
+ Log.Information("Message is waiting in {0}. Cancel!", ConnectionToken);
return null;
}
catch (Exception e)
@@ -96,7 +80,10 @@ namespace Modbus.Net
Log.Error(e, "Connector {0} Send Error.", ConnectionToken);
return null;
}
-
+ finally
+ {
+ asyncLock?.Dispose();
+ }
}
}
diff --git a/Modbus.Net/Modbus.Net/TaskManager.cs b/Modbus.Net/Modbus.Net/TaskManager.cs
index ef0c7cf..aeceeee 100644
--- a/Modbus.Net/Modbus.Net/TaskManager.cs
+++ b/Modbus.Net/Modbus.Net/TaskManager.cs
@@ -409,16 +409,26 @@ namespace Modbus.Net
{
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
- var ans =
- await tasks.StartNew(
- async () => await machine.GetMachineMethods()
- .GetDatasAsync(
- getDataType).WithCancellation(cts.Token)).Unwrap();
- return new DataReturnDef
+ try
{
- MachineId = machine.GetMachineIdString(),
- ReturnValues = ans
- };
+ var ans =
+ await tasks.StartNew(
+ async () => await machine.GetMachineMethods()
+ .GetDatasAsync(
+ getDataType).WithCancellation(cts.Token)).Unwrap();
+ return new DataReturnDef
+ {
+ MachineId = machine.GetMachineIdString(),
+ ReturnValues = ans
+ };
+ }
+ catch (Exception e)
+ {
+ Log.Error(e, "GetData task has been canceled.");
+ machine.Disconnect();
+ }
+ return null;
+
};
Params = null;
Return = returnFunc;
@@ -460,12 +470,21 @@ namespace Modbus.Net
{
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
- var ans =
- await tasks.StartNew(
- async () => await machine.GetMachineMethods().
- SetDatasAsync(setDataType, (Dictionary)parameters[0]
- ).WithCancellation(cts.Token)).Unwrap();
- return ans;
+ try
+ {
+ var ans =
+ await tasks.StartNew(
+ async () => await machine.GetMachineMethods().
+ SetDatasAsync(setDataType, (Dictionary)parameters[0]
+ ).WithCancellation(cts.Token)).Unwrap();
+ return ans;
+ }
+ catch (Exception e)
+ {
+ Log.Error(e, "SetData task has been canceled.");
+ machine.Disconnect();
+ }
+ return false;
};
Params = () => new object[] {values()};
Return = returnFunc;