From 8c9901b1378f5f5f8cddcaf8b4352b3b40011c3f Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Fri, 17 Jul 2015 16:20:33 +0800 Subject: [PATCH] 2015-07-17 update 1 --- Modbus.Net/ModBus.Net/AsyncHelper.cs | 12 ++++ Modbus.Net/ModBus.Net/BaseMachine.cs | 5 +- .../ModBus.Net/Properties/AssemblyInfo.cs | 6 +- Modbus.Net/ModBus.Net/TaskManager.cs | 31 ++++++---- Modbus.Net/ModBus.Net/TcpConnector.cs | 59 ++----------------- 5 files changed, 40 insertions(+), 73 deletions(-) diff --git a/Modbus.Net/ModBus.Net/AsyncHelper.cs b/Modbus.Net/ModBus.Net/AsyncHelper.cs index 9d93140..4235428 100644 --- a/Modbus.Net/ModBus.Net/AsyncHelper.cs +++ b/Modbus.Net/ModBus.Net/AsyncHelper.cs @@ -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 WithCancellation(this Task task, + CancellationToken token) + { + return task.ContinueWith(t => t.GetAwaiter().GetResult(), token); + } } } diff --git a/Modbus.Net/ModBus.Net/BaseMachine.cs b/Modbus.Net/ModBus.Net/BaseMachine.cs index 8506694..67d4868 100644 --- a/Modbus.Net/ModBus.Net/BaseMachine.cs +++ b/Modbus.Net/ModBus.Net/BaseMachine.cs @@ -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 /// 放缩比例 /// public double Zoom { get; set; } + public int DecimalPos { get; set; } /// /// 通讯标识名称 /// diff --git a/Modbus.Net/ModBus.Net/Properties/AssemblyInfo.cs b/Modbus.Net/ModBus.Net/Properties/AssemblyInfo.cs index 1e06f97..f32b189 100644 --- a/Modbus.Net/ModBus.Net/Properties/AssemblyInfo.cs +++ b/Modbus.Net/ModBus.Net/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file +[assembly: AssemblyVersion("0.2.5.0716")] +[assembly: AssemblyFileVersion("0.2.5.0716")] \ No newline at end of file diff --git a/Modbus.Net/ModBus.Net/TaskManager.cs b/Modbus.Net/ModBus.Net/TaskManager.cs index e264bc6..552792d 100644 --- a/Modbus.Net/ModBus.Net/TaskManager.cs +++ b/Modbus.Net/ModBus.Net/TaskManager.cs @@ -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> _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>(machine.Id, ans)); diff --git a/Modbus.Net/ModBus.Net/TcpConnector.cs b/Modbus.Net/ModBus.Net/TcpConnector.cs index ab345e2..40321b6 100644 --- a/Modbus.Net/ModBus.Net/TcpConnector.cs +++ b/Modbus.Net/ModBus.Net/TcpConnector.cs @@ -82,59 +82,7 @@ namespace ModBus.Net return AsyncHelper.RunSync(ConnectAsync); } - private ManualResetEvent _timeoutObject = new ManualResetEvent(false); - private bool _isConnectionSuccessful; public override async Task 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 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() {