This commit is contained in:
parallelbgls
2018-01-03 16:23:46 +08:00
parent cb2d552cdf
commit 5c156e8150
2 changed files with 46 additions and 40 deletions

View File

@@ -38,33 +38,11 @@ namespace Modbus.Net
/// <inheritdoc /> /// <inheritdoc />
public override async Task<byte[]> SendMsgAsync(byte[] message) public override async Task<byte[]> SendMsgAsync(byte[] message)
{ {
var ans = await SendMsgCtrl(message); var ans = await SendMsgInner(message);
if (ans == null) return new byte[0]; if (ans == null) return new byte[0];
return ans.ReceiveMessage; return ans.ReceiveMessage;
} }
/// <summary>
/// 发送主控
/// </summary>
/// <param name="message">发送的信息</param>
/// <returns>等待信息的定义</returns>
protected async Task<MessageWaitingDef> SendMsgCtrl(byte[] message)
{
MessageWaitingDef ans;
if (!IsFullDuplex)
{
using (await Lock.LockAsync())
{
ans = await SendMsgInner(message);
}
}
else
{
ans = await SendMsgInner(message);
}
return ans;
}
/// <summary> /// <summary>
/// 发送内部 /// 发送内部
/// </summary> /// </summary>
@@ -72,11 +50,16 @@ namespace Modbus.Net
/// <returns>发送信息的定义</returns> /// <returns>发送信息的定义</returns>
protected async Task<MessageWaitingDef> SendMsgInner(byte[] message) protected async Task<MessageWaitingDef> SendMsgInner(byte[] message)
{ {
IDisposable asyncLock = null;
try try
{ {
var messageSendingdef = Controller.AddMessage(message); var messageSendingdef = Controller.AddMessage(message);
if (messageSendingdef != null) if (messageSendingdef != null)
{ {
if (!IsFullDuplex)
{
asyncLock = await Lock.LockAsync();
}
var success = messageSendingdef.SendMutex.WaitOne(TimeoutTime); var success = messageSendingdef.SendMutex.WaitOne(TimeoutTime);
if (success) if (success)
{ {
@@ -89,6 +72,7 @@ namespace Modbus.Net
} }
Controller.ForceRemoveWaitingMessage(messageSendingdef); Controller.ForceRemoveWaitingMessage(messageSendingdef);
} }
Log.Information("Message is waiting in {0}. Cancel!", ConnectionToken);
return null; return null;
} }
catch (Exception e) catch (Exception e)
@@ -96,7 +80,10 @@ namespace Modbus.Net
Log.Error(e, "Connector {0} Send Error.", ConnectionToken); Log.Error(e, "Connector {0} Send Error.", ConnectionToken);
return null; return null;
} }
finally
{
asyncLock?.Dispose();
}
} }
} }

View File

@@ -409,6 +409,8 @@ namespace Modbus.Net
{ {
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime)); cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
try
{
var ans = var ans =
await tasks.StartNew( await tasks.StartNew(
async () => await machine.GetMachineMethods<IMachineMethodData>() async () => await machine.GetMachineMethods<IMachineMethodData>()
@@ -419,6 +421,14 @@ namespace Modbus.Net
MachineId = machine.GetMachineIdString(), MachineId = machine.GetMachineIdString(),
ReturnValues = ans ReturnValues = ans
}; };
}
catch (Exception e)
{
Log.Error(e, "GetData task has been canceled.");
machine.Disconnect();
}
return null;
}; };
Params = null; Params = null;
Return = returnFunc; Return = returnFunc;
@@ -460,12 +470,21 @@ namespace Modbus.Net
{ {
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime)); cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
try
{
var ans = var ans =
await tasks.StartNew( await tasks.StartNew(
async () => await machine.GetMachineMethods<IMachineMethodData>(). async () => await machine.GetMachineMethods<IMachineMethodData>().
SetDatasAsync(setDataType, (Dictionary<string, double>)parameters[0] SetDatasAsync(setDataType, (Dictionary<string, double>)parameters[0]
).WithCancellation(cts.Token)).Unwrap(); ).WithCancellation(cts.Token)).Unwrap();
return ans; return ans;
}
catch (Exception e)
{
Log.Error(e, "SetData task has been canceled.");
machine.Disconnect();
}
return false;
}; };
Params = () => new object[] {values()}; Params = () => new object[] {values()};
Return = returnFunc; Return = returnFunc;