Add GetData Key Rules
This commit is contained in:
@@ -5,6 +5,21 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取设备值的方式
|
||||
/// </summary>
|
||||
public enum MachineGetDataType
|
||||
{
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
Address,
|
||||
/// <summary>
|
||||
/// 通讯标识
|
||||
/// </summary>
|
||||
CommunicationTag
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向设备设置值的方式
|
||||
/// </summary>
|
||||
@@ -113,16 +128,18 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
public Dictionary<string, ReturnUnit> GetDatas()
|
||||
public Dictionary<string, ReturnUnit> GetDatas(MachineGetDataType getDataType)
|
||||
{
|
||||
return AsyncHelper.RunSync(GetDatasAsync);
|
||||
return AsyncHelper.RunSync(()=>GetDatasAsync(getDataType));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
public async Task<Dictionary<string,ReturnUnit>> GetDatasAsync()
|
||||
public async Task<Dictionary<string,ReturnUnit>> GetDatasAsync(MachineGetDataType getDataType)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -164,8 +181,28 @@ namespace Modbus.Net
|
||||
p => p.Area == communicateAddress.Area && p.Address == pos + communicateAddress.Address);
|
||||
if (address != null)
|
||||
{
|
||||
string key;
|
||||
switch (getDataType)
|
||||
{
|
||||
case MachineGetDataType.CommunicationTag:
|
||||
{
|
||||
key = address.CommunicationTag;
|
||||
break;
|
||||
}
|
||||
case MachineGetDataType.Address:
|
||||
{
|
||||
key = AddressFormater.FormatAddress(address.Area, address.Address);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
key = address.CommunicationTag;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//将获取的数据和对应的通讯标识对应
|
||||
ans.Add(address.CommunicationTag,
|
||||
ans.Add(key,
|
||||
new ReturnUnit
|
||||
{
|
||||
PlcValue =
|
||||
|
||||
@@ -279,6 +279,8 @@ namespace Modbus.Net
|
||||
}
|
||||
}
|
||||
|
||||
public MachineGetDataType GetDataType { get; set; }
|
||||
|
||||
/*public int MaxRunningTasks
|
||||
{
|
||||
get { return _scheduler.MaximumConcurrencyLevel; }
|
||||
@@ -295,13 +297,14 @@ namespace Modbus.Net
|
||||
/// <param name="maxRunningTask">同时可以运行的任务数</param>
|
||||
/// <param name="getCycle">读取数据的时间间隔(秒)</param>
|
||||
/// <param name="keepConnect">读取数据后是否保持连接</param>
|
||||
public TaskManager(/*int maxRunningTask,*/ int getCycle, bool keepConnect)
|
||||
public TaskManager(/*int maxRunningTask,*/ int getCycle, bool keepConnect, MachineGetDataType getDataType = MachineGetDataType.CommunicationTag)
|
||||
{
|
||||
//_scheduler = new LimitedConcurrencyLevelTaskScheduler(maxRunningTask);
|
||||
_machines = new HashSet<BaseMachine>(new BaseMachineEqualityComparer());
|
||||
_unlinkedMachines = new HashSet<BaseMachine>(new BaseMachineEqualityComparer());
|
||||
_getCycle = getCycle;
|
||||
KeepConnect = keepConnect;
|
||||
GetDataType = getDataType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -556,7 +559,7 @@ namespace Modbus.Net
|
||||
//超时后取消任务
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(_getCycle));
|
||||
//读取数据
|
||||
var ans = await machine.GetDatasAsync().WithCancellation(cts.Token);
|
||||
var ans = await machine.GetDatasAsync(GetDataType).WithCancellation(cts.Token);
|
||||
if (!machine.IsConnected)
|
||||
{
|
||||
MoveMachineToUnlinked(machine.Id);
|
||||
|
||||
Reference in New Issue
Block a user