diff --git a/Modbus.Net/CrossLampControl.WebApi/Controllers/CrossLampController.cs b/Modbus.Net/CrossLampControl.WebApi/Controllers/CrossLampController.cs index bb78bba..da3e203 100644 --- a/Modbus.Net/CrossLampControl.WebApi/Controllers/CrossLampController.cs +++ b/Modbus.Net/CrossLampControl.WebApi/Controllers/CrossLampController.cs @@ -16,7 +16,7 @@ namespace CrossLampControl.WebApi.Controllers public CrossLampController() { - _utility = new SimenseUtility(SimenseType.Tcp, "192.168.3.241,200"); + _utility = new SimenseUtility(SimenseType.Tcp, "192.168.3.241", SimenseMachineModel.S7_200); _utility.AddressTranslator = new AddressTranslatorSimense(); } diff --git a/Modbus.Net/ModBus.Net/BaseMachine.cs b/Modbus.Net/ModBus.Net/BaseMachine.cs index 1265cc7..f2a182c 100644 --- a/Modbus.Net/ModBus.Net/BaseMachine.cs +++ b/Modbus.Net/ModBus.Net/BaseMachine.cs @@ -43,9 +43,9 @@ namespace ModBus.Net KeepConnect = keepConnect; } - public Dictionary GetDatas() + public Dictionary GetDatas() { - Dictionary ans = new Dictionary(); + Dictionary ans = new Dictionary(); if (!BaseUtility.IsConnected) { BaseUtility.Connect(); @@ -61,7 +61,7 @@ namespace ModBus.Net p => p.Area == communicateAddress.Area && p.Address == pos + communicateAddress.Address); if (address != null) { - ans.Add(address.CommunicationTag, (String.Format("{0:#0.#}", Math.Round(Double.Parse(ValueHelper.Instance.GetValue(datas, ref pos, address.DataType).ToString()) * address.Zoom, 3)))); + ans.Add(address.CommunicationTag, new ReturnUnit{PlcValue = String.Format("{0:#0.#}", Math.Round(Single.Parse(ValueHelper.Instance.GetValue(datas, ref pos, address.DataType).ToString()) * address.Zoom, 3)),ExtendUnit = address.ExtendUnit}); } else { @@ -100,7 +100,7 @@ namespace ModBus.Net } } - public struct CommunicationUnit + public class CommunicationUnit { public string Area { get; set; } public int Address { get; set; } @@ -108,6 +108,17 @@ namespace ModBus.Net public Type DataType { get; set; } } + public class ExtendUnit + { + + } + + public class ReturnUnit + { + public string PlcValue { get; set; } + public ExtendUnit ExtendUnit { get; set; } + } + public class AddressUnit { public int Id { get; set; } @@ -127,6 +138,8 @@ namespace ModBus.Net public string CommunicationTag { get; set; } public string Name { get; set; } public string Unit { get; set; } + + public ExtendUnit ExtendUnit { get; set; } } public struct AddressUnitEqualityComparer : IEqualityComparer diff --git a/Modbus.Net/ModBus.Net/BaseProtocal.cs b/Modbus.Net/ModBus.Net/BaseProtocal.cs index dba1459..6728642 100644 --- a/Modbus.Net/ModBus.Net/BaseProtocal.cs +++ b/Modbus.Net/ModBus.Net/BaseProtocal.cs @@ -71,15 +71,25 @@ namespace ModBus.Net public virtual OutputStruct SendReceive(ProtocalUnit unit, InputStruct content) { int t = 0; - //如果为特别处理协议的话,跳过协议扩展收缩 - if (unit is SpecialProtocalUnit) + //如果为特别处理协议的话,跳过协议扩展收缩 + var formatContent = unit.Format(content); + if (formatContent != null) { - return unit.Unformat(ProtocalLinker.SendReceiveWithoutExtAndDec(unit.Format(content)), ref t); - } - else - { - return unit.Unformat(ProtocalLinker.SendReceive(unit.Format(content)), ref t); + byte[] receiveContent; + if (unit is SpecialProtocalUnit) + { + receiveContent = ProtocalLinker.SendReceiveWithoutExtAndDec(formatContent); + } + else + { + receiveContent = ProtocalLinker.SendReceive(formatContent); + } + if (receiveContent != null) + { + return unit.Unformat(receiveContent, ref t); + } } + return null; } /// diff --git a/Modbus.Net/ModBus.Net/TaskManager.cs b/Modbus.Net/ModBus.Net/TaskManager.cs index 559fbee..a173cad 100644 --- a/Modbus.Net/ModBus.Net/TaskManager.cs +++ b/Modbus.Net/ModBus.Net/TaskManager.cs @@ -167,7 +167,7 @@ namespace ModBus.Net public class TaskManager { private HashSet _machines; - private TaskFactory> _tasks; + private TaskFactory> _tasks; private TaskScheduler _scheduler; private CancellationTokenSource _cts; private Timer _timer; @@ -191,7 +191,7 @@ namespace ModBus.Net } } - public delegate void ReturnValuesDelegate(KeyValuePair> returnValue); + public delegate void ReturnValuesDelegate(KeyValuePair> returnValue); public event ReturnValuesDelegate ReturnValues; @@ -304,7 +304,7 @@ namespace ModBus.Net { TaskStop(); _cts = new CancellationTokenSource(); - _tasks = new TaskFactory>(_cts.Token, TaskCreationOptions.None, TaskContinuationOptions.None, _scheduler); + _tasks = new TaskFactory>(_cts.Token, TaskCreationOptions.None, TaskContinuationOptions.None, _scheduler); GetCycle = TimeRestore.Restore; } @@ -335,7 +335,7 @@ namespace ModBus.Net var ans = _tasks.StartNew(machine.GetDatas).Result; if (ReturnValues != null) { - ReturnValues(new KeyValuePair>(machine.Id, ans)); + ReturnValues(new KeyValuePair>(machine.Id, ans)); } } catch (Exception e) @@ -343,7 +343,7 @@ namespace ModBus.Net if (ReturnValues != null) { - ReturnValues(new KeyValuePair>(machine.Id, null)); + ReturnValues(new KeyValuePair>(machine.Id, null)); } } }