2015-07-17 update 1
This commit is contained in:
@@ -29,5 +29,17 @@ namespace ModBus.Net
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
public static Task WithCancellation(this Task task,
|
||||
CancellationToken token)
|
||||
{
|
||||
return task.ContinueWith(t => t.GetAwaiter().GetResult(), token);
|
||||
}
|
||||
|
||||
public static Task<T> WithCancellation<T>(this Task<T> task,
|
||||
CancellationToken token)
|
||||
{
|
||||
return task.ContinueWith(t => t.GetAwaiter().GetResult(), token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace ModBus.Net
|
||||
p => p.Area == communicateAddress.Area && p.Address == pos + communicateAddress.Address);
|
||||
if (address != null)
|
||||
{
|
||||
ans.Add(address.CommunicationTag, new ReturnUnit{PlcValue = Math.Round(Single.Parse(ValueHelper.Instance.GetValue(datas, ref pos, address.DataType).ToString()) * address.Zoom, address.DataType == typeof(System.Single) || address.DataType == typeof(System.Double) ? 3 : (Math.Log10(address.Zoom) > 0 ? 0 : (int)Math.Ceiling(-Math.Log10(address.Zoom)))).ToString(),UnitExtend = address.UnitExtend});
|
||||
ans.Add(address.CommunicationTag, new ReturnUnit{PlcValue = Double.Parse(ValueHelper.Instance.GetValue(datas, ref pos, address.DataType).ToString()) * address.Zoom,UnitExtend = address.UnitExtend});
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -123,7 +123,7 @@ namespace ModBus.Net
|
||||
|
||||
public class ReturnUnit
|
||||
{
|
||||
public string PlcValue { get; set; }
|
||||
public double PlcValue { get; set; }
|
||||
public UnitExtend UnitExtend { get; set; }
|
||||
}
|
||||
|
||||
@@ -140,6 +140,7 @@ namespace ModBus.Net
|
||||
/// 放缩比例
|
||||
/// </summary>
|
||||
public double Zoom { get; set; }
|
||||
public int DecimalPos { get; set; }
|
||||
/// <summary>
|
||||
/// 通讯标识名称
|
||||
/// </summary>
|
||||
|
||||
@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("杭州德联科技股份有限公司")]
|
||||
[assembly: AssemblyProduct("ModBus.Net")]
|
||||
[assembly: AssemblyCopyright("Copyright © Chris L. 2014 HangZhou Delin Technology")]
|
||||
[assembly: AssemblyCopyright("Copyright © Chris L. 2014 HangZhou Delian Technology")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
@@ -35,5 +35,5 @@ using System.Runtime.InteropServices;
|
||||
// 方法是按如下所示使用“*”:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
[assembly: AssemblyVersion("0.2.2.0415")]
|
||||
[assembly: AssemblyFileVersion("0.2.2.0415")]
|
||||
[assembly: AssemblyVersion("0.2.5.0716")]
|
||||
[assembly: AssemblyFileVersion("0.2.5.0716")]
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
|
||||
namespace ModBus.Net
|
||||
@@ -170,6 +169,7 @@ namespace ModBus.Net
|
||||
private TaskFactory<Dictionary<string,ReturnUnit>> _tasks;
|
||||
private TaskScheduler _scheduler;
|
||||
private CancellationTokenSource _cts;
|
||||
|
||||
private Timer _timer;
|
||||
|
||||
private bool _keepConnect;
|
||||
@@ -205,27 +205,30 @@ namespace ModBus.Net
|
||||
get { return _getCycle; }
|
||||
set
|
||||
{
|
||||
if (value == _getCycle) return;
|
||||
|
||||
if (value == Timeout.Infinite)
|
||||
{
|
||||
if (_timer != null)
|
||||
{
|
||||
_timer.Stop();
|
||||
_timer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
else if (value < 0) return;
|
||||
else
|
||||
{
|
||||
if (_timer != null)
|
||||
{
|
||||
_timer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
_timer.Dispose();
|
||||
_timer = null;
|
||||
}
|
||||
if (value > 0)
|
||||
{
|
||||
_getCycle = value;
|
||||
}
|
||||
if (_timer != null) _timer.Interval = _getCycle*1000;
|
||||
else
|
||||
{
|
||||
_timer = new Timer(_getCycle*1000);
|
||||
_timer.Elapsed += MaintainTasks;
|
||||
}
|
||||
_timer.Start();
|
||||
//MaintainTasks(null,null);
|
||||
_timer = new Timer(MaintainTasks, null, 0, _getCycle * 1000);
|
||||
//MaintainTasks(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -293,7 +296,7 @@ namespace ModBus.Net
|
||||
}
|
||||
|
||||
|
||||
private void MaintainTasks(object sender, System.Timers.ElapsedEventArgs e)
|
||||
private void MaintainTasks(object sender)
|
||||
{
|
||||
AsyncHelper.RunSync(MaintainTasksAsync);
|
||||
}
|
||||
@@ -343,7 +346,9 @@ namespace ModBus.Net
|
||||
try
|
||||
{
|
||||
//var ans = machine.GetDatas();
|
||||
var ans = await _tasks.StartNew(machine.GetDatas);
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(_getCycle * 2));
|
||||
var ans = await _tasks.StartNew(machine.GetDatas, cts.Token);
|
||||
if (ReturnValues != null)
|
||||
{
|
||||
ReturnValues(new KeyValuePair<int, Dictionary<string,ReturnUnit>>(machine.Id, ans));
|
||||
|
||||
@@ -82,59 +82,7 @@ namespace ModBus.Net
|
||||
return AsyncHelper.RunSync(ConnectAsync);
|
||||
}
|
||||
|
||||
private ManualResetEvent _timeoutObject = new ManualResetEvent(false);
|
||||
private bool _isConnectionSuccessful;
|
||||
public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
_timeoutObject.Reset();
|
||||
_socketClient = new TcpClient();
|
||||
_socketClient.BeginConnect(_host, _port, CallBackMethod, _socketClient);
|
||||
if (_timeoutObject.WaitOne(TimeoutTime, false))
|
||||
{
|
||||
if (_isConnectionSuccessful)
|
||||
{
|
||||
AddInfo("client connected.");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
AddInfo("connect failed.");
|
||||
_socketClient = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_socketClient.Close();
|
||||
AddInfo("connect failed.");
|
||||
_socketClient = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void CallBackMethod(IAsyncResult asyncresult)
|
||||
{
|
||||
try
|
||||
{
|
||||
_isConnectionSuccessful = false;
|
||||
TcpClient tcpclient = asyncresult.AsyncState as TcpClient;
|
||||
if (tcpclient != null && tcpclient.Client != null)
|
||||
{
|
||||
_isConnectionSuccessful = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddInfo("client connected exception: " + ex.Message);
|
||||
_isConnectionSuccessful = false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_timeoutObject.Set();
|
||||
}
|
||||
}
|
||||
|
||||
/*public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
if (_socketClient != null)
|
||||
{
|
||||
@@ -150,7 +98,9 @@ namespace ModBus.Net
|
||||
|
||||
try
|
||||
{
|
||||
await _socketClient.ConnectAsync(_host, _port);
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeoutTime);
|
||||
await _socketClient.ConnectAsync(_host, _port).WithCancellation(cts.Token);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -158,7 +108,6 @@ namespace ModBus.Net
|
||||
}
|
||||
if (_socketClient.Connected)
|
||||
{
|
||||
_stream = _socketClient.GetStream();
|
||||
AddInfo("client connected.");
|
||||
return true;
|
||||
}
|
||||
@@ -170,7 +119,7 @@ namespace ModBus.Net
|
||||
AddInfo("client connect exception: " + err.Message);
|
||||
return false;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
public override bool Disconnect()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user