2015-05-18 update 1
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,9 +43,9 @@ namespace ModBus.Net
|
||||
KeepConnect = keepConnect;
|
||||
}
|
||||
|
||||
public Dictionary<string,string> GetDatas()
|
||||
public Dictionary<string,ReturnUnit> GetDatas()
|
||||
{
|
||||
Dictionary<string, string> ans = new Dictionary<string, string>();
|
||||
Dictionary<string, ReturnUnit> ans = new Dictionary<string, ReturnUnit>();
|
||||
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<AddressUnit>
|
||||
|
||||
@@ -72,14 +72,24 @@ namespace ModBus.Net
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -167,7 +167,7 @@ namespace ModBus.Net
|
||||
public class TaskManager
|
||||
{
|
||||
private HashSet<BaseMachine> _machines;
|
||||
private TaskFactory<Dictionary<string,string>> _tasks;
|
||||
private TaskFactory<Dictionary<string,ReturnUnit>> _tasks;
|
||||
private TaskScheduler _scheduler;
|
||||
private CancellationTokenSource _cts;
|
||||
private Timer _timer;
|
||||
@@ -191,7 +191,7 @@ namespace ModBus.Net
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void ReturnValuesDelegate(KeyValuePair<string, Dictionary<string,string>> returnValue);
|
||||
public delegate void ReturnValuesDelegate(KeyValuePair<string, Dictionary<string,ReturnUnit>> returnValue);
|
||||
|
||||
public event ReturnValuesDelegate ReturnValues;
|
||||
|
||||
@@ -304,7 +304,7 @@ namespace ModBus.Net
|
||||
{
|
||||
TaskStop();
|
||||
_cts = new CancellationTokenSource();
|
||||
_tasks = new TaskFactory<Dictionary<string,string>>(_cts.Token, TaskCreationOptions.None, TaskContinuationOptions.None, _scheduler);
|
||||
_tasks = new TaskFactory<Dictionary<string,ReturnUnit>>(_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<string, Dictionary<string,string>>(machine.Id, ans));
|
||||
ReturnValues(new KeyValuePair<string, Dictionary<string,ReturnUnit>>(machine.Id, ans));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -343,7 +343,7 @@ namespace ModBus.Net
|
||||
|
||||
if (ReturnValues != null)
|
||||
{
|
||||
ReturnValues(new KeyValuePair<string, Dictionary<string,string>>(machine.Id, null));
|
||||
ReturnValues(new KeyValuePair<string, Dictionary<string,ReturnUnit>>(machine.Id, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user