Job System and DuplicateWithCount bug fix.
This commit is contained in:
@@ -25,7 +25,7 @@ namespace Modbus.Net.Modbus
|
||||
public ModbusAsciiInTcpProtocolLinker(string ip, int port)
|
||||
: base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "100")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Modbus.Net.Modbus
|
||||
public ModbusAsciiInUdpProtocolLinker(string ip, int port)
|
||||
: base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO.Ports;
|
||||
using System.Text;
|
||||
|
||||
@@ -17,7 +18,7 @@ namespace Modbus.Net.Modbus
|
||||
public ModbusAsciiProtocolLinker(string com, int slaveAddress)
|
||||
: base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[] { new List<(int,int)> { (1,1), (2,2) }, new List<(int, int)> { (3, 3), (4, 4) }}, 0));
|
||||
((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[] { new List<(int,int)> { (1,1), (2,2) }, new List<(int, int)> { (3, 3), (4, 4) }}, int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Modbus.Net.Modbus
|
||||
public ModbusRtuInTcpProtocolLinker(string ip, int port)
|
||||
: base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Modbus.Net.Modbus
|
||||
public ModbusRtuInUdpProtocolLinker(string ip, int port)
|
||||
: base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
@@ -16,7 +17,7 @@ namespace Modbus.Net.Modbus
|
||||
public ModbusRtuProtocolLinker(string com, int slaveAddress)
|
||||
: base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[]{new List<(int,int)>{(0,0)}, new List<(int, int)>{(1,1)}}, 0));
|
||||
((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[]{new List<(int,int)>{(0,0)}, new List<(int, int)>{(1,1)}}, int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Modbus.Net.Modbus
|
||||
/// <param name="port">端口</param>
|
||||
public ModbusTcpProtocolLinker(string ip, int port) : base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0, true, DuplicateWithCount.GetDuplcateFunc(new List<int>{4,5})));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0"), true, DuplicateWithCount.GetDuplcateFunc(new List<int>{4,5}, 6)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Modbus.Net.Modbus
|
||||
/// <param name="port">端口</param>
|
||||
public ModbusUdpProtocolLinker(string ip, int port) : base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0, true, DuplicateWithCount.GetDuplcateFunc(new List<int> { 4, 5 })));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0"), true, DuplicateWithCount.GetDuplcateFunc(new List<int> { 4, 5 }, 6)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO.Ports;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Modbus.Net.Siemens
|
||||
{
|
||||
/// <summary>
|
||||
@@ -18,7 +19,7 @@ namespace Modbus.Net.Siemens
|
||||
public SiemensPpiProtocolLinker(string com, int slaveAddress)
|
||||
: base(com, 9600, Parity.Even, StopBits.One, 8, slaveAddress)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0));
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Modbus.Net.Siemens
|
||||
public SiemensTcpProtocolLinker(string ip, int port)
|
||||
: base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new MatchDirectlySendController(new ICollection<(int,int)>[] { new List<(int,int)> { (11,11), (12,12) } }, DuplicateWithCount.GetDuplcateFunc(new List<int>{2, 3})));
|
||||
((BaseConnector)BaseConnector).AddController(new MatchDirectlySendController(new ICollection<(int,int)>[] { new List<(int,int)> { (11,11), (12,12) } }, int.Parse(ConfigurationManager.AppSettings["FetchSleepTime"] ?? "0"), DuplicateWithCount.GetDuplcateFunc(new List<int>{2, 3}, 0)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -11,5 +11,6 @@
|
||||
<add key="OpcDaHost" value="opcda://localhost/..." />
|
||||
<add key="OpcUaHost" value="opc.tcp://localhost/..." />
|
||||
<add key="SiemensPort" value="102" />
|
||||
<add key="FetchSleepTime" value="100" />
|
||||
</appSettings>
|
||||
</configuration>
|
||||
@@ -17,7 +17,7 @@ namespace Modbus.Net
|
||||
/// <param name="receiveMessage">收到的报文信息</param>
|
||||
/// <param name="packageCountPositions">收到的断包长度查询位置</param>
|
||||
/// <returns>切分后的报文信息</returns>
|
||||
private static ICollection<byte[]> DuplicateMessages(byte[] receiveMessage, ICollection<int> packageCountPositions)
|
||||
private static ICollection<byte[]> DuplicateMessages(byte[] receiveMessage, ICollection<int> packageCountPositions, int otherCount)
|
||||
{
|
||||
if (packageCountPositions == null)
|
||||
return new List<byte[]> { receiveMessage };
|
||||
@@ -32,6 +32,7 @@ namespace Modbus.Net
|
||||
{
|
||||
length = length * 256 + receiveMessage[pos + countPos];
|
||||
}
|
||||
length += otherCount;
|
||||
if (pos + length > receiveMessage.Length) break;
|
||||
byte[] currentPackage = new byte[length];
|
||||
Array.Copy(receiveMessage, pos, currentPackage, 0, length);
|
||||
@@ -51,9 +52,9 @@ namespace Modbus.Net
|
||||
/// </summary>
|
||||
/// <param name="packageCountPositions">断包长度的位置信息</param>
|
||||
/// <returns>断包函数</returns>
|
||||
public static Func<byte[], ICollection<byte[]>> GetDuplcateFunc(ICollection<int> packageCountPositions)
|
||||
public static Func<byte[], ICollection<byte[]>> GetDuplcateFunc(ICollection<int> packageCountPositions, int otherCount)
|
||||
{
|
||||
return receiveMessage => DuplicateMessages(receiveMessage, packageCountPositions);
|
||||
return receiveMessage => DuplicateMessages(receiveMessage, packageCountPositions, otherCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ namespace Modbus.Net
|
||||
public class MatchDirectlySendController : MatchController
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
||||
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches, int acquireTime,
|
||||
Func<byte[], ICollection<byte[]>> duplicateFunc = null) : base(keyMatches,
|
||||
0, false, duplicateFunc)
|
||||
acquireTime, false, duplicateFunc)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -19,13 +19,13 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
Dictionary<string, ReturnUnit> GetDatas(MachineGetDataType getDataType);
|
||||
Dictionary<string, ReturnUnit> GetDatas(MachineDataType getDataType);
|
||||
|
||||
/// <summary>
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
Task<Dictionary<string, ReturnUnit>> GetDatasAsync(MachineGetDataType getDataType);
|
||||
Task<Dictionary<string, ReturnUnit>> GetDatasAsync(MachineDataType getDataType);
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据
|
||||
@@ -33,7 +33,7 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
bool SetDatas(MachineSetDataType setDataType, Dictionary<string, double> values);
|
||||
bool SetDatas(MachineDataType setDataType, Dictionary<string, double> values);
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据
|
||||
@@ -41,6 +41,6 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
Task<bool> SetDatasAsync(MachineSetDataType setDataType, Dictionary<string, double> values);
|
||||
Task<bool> SetDatasAsync(MachineDataType setDataType, Dictionary<string, double> values);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Quartz.Listener;
|
||||
using Quartz;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Job
|
||||
{
|
||||
public class JobChainingJobListenerWithDataMap : JobListenerSupport
|
||||
{
|
||||
public JobChainingJobListenerWithDataMap(string name, bool overwrite)
|
||||
{
|
||||
Name = name;
|
||||
OverWrite = overwrite;
|
||||
chainLinks = new Dictionary<JobKey, JobKey>();
|
||||
}
|
||||
|
||||
private readonly Dictionary<JobKey, JobKey> chainLinks;
|
||||
|
||||
public override string Name { get; }
|
||||
|
||||
public bool OverWrite { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Add a chain mapping - when the Job identified by the first key completes
|
||||
/// the job identified by the second key will be triggered.
|
||||
/// </summary>
|
||||
/// <param name="firstJob">a JobKey with the name and group of the first job</param>
|
||||
/// <param name="secondJob">a JobKey with the name and group of the follow-up job</param>
|
||||
public new void AddJobChainLink(JobKey firstJob, JobKey secondJob)
|
||||
{
|
||||
chainLinks.Add(firstJob, secondJob);
|
||||
}
|
||||
|
||||
public override async Task JobWasExecuted(IJobExecutionContext context,
|
||||
JobExecutionException? jobException,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
chainLinks.TryGetValue(context.JobDetail.Key, out var sj);
|
||||
|
||||
if (sj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Information("Job '{JobKey}' will now chain to Job '{Job}'", context.JobDetail.Key, sj);
|
||||
|
||||
try
|
||||
{
|
||||
var sjJobDetail = await context.Scheduler.GetJobDetail(sj);
|
||||
foreach (var entry in context.JobDetail.JobDataMap)
|
||||
{
|
||||
if (!OverWrite && !sjJobDetail.JobDataMap.ContainsKey(entry.Key))
|
||||
{
|
||||
sjJobDetail.JobDataMap.Put(entry.Key, entry.Value);
|
||||
await context.Scheduler.AddJob(sjJobDetail, true, false);
|
||||
}
|
||||
}
|
||||
await context.Scheduler.TriggerJob(sj, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (SchedulerException se)
|
||||
{
|
||||
Log.Error(se, "Error encountered during chaining to Job '{Job}'", sj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
197
Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs
Normal file
197
Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
using Modbus.Net.Interface;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Quartz.Impl.Matchers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Job
|
||||
{
|
||||
public sealed class MachineJobSchedulerCreator
|
||||
{
|
||||
public static async Task<MachineGetJobScheduler> CreateScheduler(int count, int interval)
|
||||
{
|
||||
IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
|
||||
|
||||
ITrigger trigger;
|
||||
if (count >= 0)
|
||||
trigger = TriggerBuilder.Create()
|
||||
.WithIdentity("Modbus.Net.DataQuery.Trigger", "Modbus.Net.DataQuery.Group")
|
||||
.StartNow()
|
||||
.WithSimpleSchedule(b => b.WithIntervalInSeconds(interval).WithRepeatCount(count))
|
||||
.Build();
|
||||
else
|
||||
trigger = TriggerBuilder.Create()
|
||||
.WithIdentity("Modbus.Net.DataQuery.Trigger", "Modbus.Net.DataQuery.Group")
|
||||
.StartNow()
|
||||
.WithSimpleSchedule(b => b.WithIntervalInSeconds(interval).RepeatForever())
|
||||
.Build();
|
||||
|
||||
return new MachineGetJobScheduler(scheduler, trigger);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MachineGetJobScheduler
|
||||
{
|
||||
IScheduler _scheduler;
|
||||
|
||||
ITrigger _trigger;
|
||||
|
||||
public MachineGetJobScheduler(IScheduler scheduler, ITrigger trigger)
|
||||
{
|
||||
_scheduler = scheduler;
|
||||
_trigger = trigger;
|
||||
}
|
||||
|
||||
public async Task<MachineQueryJobScheduler> From(string queryId, IMachineMethodData machine, MachineDataType machineDataType)
|
||||
{
|
||||
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group");
|
||||
|
||||
IJobDetail job = JobBuilder.Create<MachineGetDataJob>()
|
||||
.WithIdentity(jobKey)
|
||||
.Build();
|
||||
|
||||
job.JobDataMap.Put("DataType", machineDataType);
|
||||
job.JobDataMap.Put("Machine", machine);
|
||||
|
||||
await _scheduler.ScheduleJob(job, _trigger);
|
||||
|
||||
return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey);
|
||||
}
|
||||
}
|
||||
|
||||
public class MachineQueryJobScheduler
|
||||
{
|
||||
IScheduler _scheduler;
|
||||
|
||||
ITrigger _trigger;
|
||||
|
||||
JobKey _parentJobKey;
|
||||
|
||||
public MachineQueryJobScheduler(IScheduler scheduler, ITrigger trigger, JobKey parentJobKey)
|
||||
{
|
||||
_scheduler = scheduler;
|
||||
_trigger = trigger;
|
||||
_parentJobKey = parentJobKey;
|
||||
}
|
||||
|
||||
public async Task<MachineSetJobScheduler> Query(string queryId = null, Func<Dictionary<string, ReturnUnit>, Dictionary<string, ReturnUnit>> QueryDataFunc = null)
|
||||
{
|
||||
JobChainingJobListenerWithDataMap listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain", false);
|
||||
_scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group"));
|
||||
|
||||
if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, listener, _parentJobKey);
|
||||
|
||||
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group");
|
||||
|
||||
IJobDetail job = JobBuilder.Create<MachineQueryDataJob>()
|
||||
.WithIdentity(jobKey)
|
||||
.StoreDurably(true)
|
||||
.Build();
|
||||
|
||||
job.JobDataMap.Put("QueryMethod", QueryDataFunc);
|
||||
|
||||
listener.AddJobChainLink(_parentJobKey, jobKey);
|
||||
|
||||
await _scheduler.AddJob(job, true);
|
||||
|
||||
return new MachineSetJobScheduler(_scheduler, _trigger, listener, jobKey);
|
||||
}
|
||||
}
|
||||
|
||||
public class MachineSetJobScheduler
|
||||
{
|
||||
IScheduler _scheduler;
|
||||
|
||||
ITrigger _trigger;
|
||||
|
||||
JobChainingJobListenerWithDataMap _listener;
|
||||
|
||||
JobKey _parentJobKey;
|
||||
|
||||
public MachineSetJobScheduler(IScheduler scheduler, ITrigger trigger, JobChainingJobListenerWithDataMap listener, JobKey parentJobKey)
|
||||
{
|
||||
_scheduler = scheduler;
|
||||
|
||||
_trigger = trigger;
|
||||
|
||||
_listener = listener;
|
||||
|
||||
_parentJobKey = parentJobKey;
|
||||
}
|
||||
|
||||
public async Task<MachineSetJobScheduler> To(string queryId, IMachineMethodData machine)
|
||||
{
|
||||
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group");
|
||||
|
||||
IJobDetail job = JobBuilder.Create<MachineSetDataJob>()
|
||||
.WithIdentity(jobKey)
|
||||
.StoreDurably(true)
|
||||
.Build();
|
||||
|
||||
job.JobDataMap.Put("Machine", machine);
|
||||
|
||||
_listener.AddJobChainLink(_parentJobKey, jobKey);
|
||||
|
||||
await _scheduler.AddJob(job, true);
|
||||
|
||||
return new MachineSetJobScheduler(_scheduler, _trigger, _listener, jobKey);
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
await _scheduler.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public class MachineGetDataJob : IJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
object machine;
|
||||
object machineDataType;
|
||||
context.MergedJobDataMap.TryGetValue("Machine", out machine);
|
||||
context.MergedJobDataMap.TryGetValue("DataType", out machineDataType);
|
||||
var values = await (machine as IMachineMethodData)!.GetDatasAsync((MachineDataType)machineDataType);
|
||||
|
||||
context.JobDetail.JobDataMap.Put("Value", values);
|
||||
await context.Scheduler.AddJob(context.JobDetail, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
public class MachineQueryDataJob : IJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
object values;
|
||||
object QueryMethod;
|
||||
|
||||
context.MergedJobDataMap.TryGetValue("Value", out values);
|
||||
context.MergedJobDataMap.TryGetValue("QueryMethod", out QueryMethod);
|
||||
Func<Dictionary<string, ReturnUnit>, Dictionary<string, ReturnUnit>> QueryMethodDispatch = (Func<Dictionary<string, ReturnUnit>, Dictionary<string, ReturnUnit>>)QueryMethod;
|
||||
|
||||
if (QueryMethod != null)
|
||||
{
|
||||
context.JobDetail.JobDataMap.Put("Value", QueryMethodDispatch((Dictionary<string, ReturnUnit>)values));
|
||||
await context.Scheduler.AddJob(context.JobDetail, true, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MachineSetDataJob : IJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
object machine;
|
||||
object machineDataType;
|
||||
object values;
|
||||
context.MergedJobDataMap.TryGetValue("Machine", out machine);
|
||||
context.MergedJobDataMap.TryGetValue("DataType", out machineDataType);
|
||||
context.MergedJobDataMap.TryGetValue("Value", out values);
|
||||
Dictionary<string, double> valuesSet = ((Dictionary<string, ReturnUnit>)values).MapGetValuesToSetValues();
|
||||
|
||||
var success = await (machine as IMachine<string>)!.SetDatasAsync((MachineDataType)machineDataType, valuesSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,58 +33,6 @@ namespace Modbus.Net
|
||||
Id
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设备值的方式
|
||||
/// </summary>
|
||||
public enum MachineGetDataType
|
||||
{
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
Address,
|
||||
|
||||
/// <summary>
|
||||
/// 通讯标识
|
||||
/// </summary>
|
||||
CommunicationTag,
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
Name,
|
||||
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
Id
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向设备设置值的方式
|
||||
/// </summary>
|
||||
public enum MachineSetDataType
|
||||
{
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
Address,
|
||||
|
||||
/// <summary>
|
||||
/// 通讯标识
|
||||
/// </summary>
|
||||
CommunicationTag,
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
Name,
|
||||
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
Id
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备
|
||||
/// </summary>
|
||||
@@ -244,7 +192,7 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
public Dictionary<string, ReturnUnit> GetDatas(MachineGetDataType getDataType)
|
||||
public Dictionary<string, ReturnUnit> GetDatas(MachineDataType getDataType)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => GetDatasAsync(getDataType));
|
||||
}
|
||||
@@ -254,7 +202,7 @@ namespace Modbus.Net
|
||||
/// 读取数据
|
||||
/// </summary>
|
||||
/// <returns>从设备读取的数据</returns>
|
||||
public async Task<Dictionary<string, ReturnUnit>> GetDatasAsync(MachineGetDataType getDataType)
|
||||
public async Task<Dictionary<string, ReturnUnit>> GetDatasAsync(MachineDataType getDataType)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -304,22 +252,22 @@ namespace Modbus.Net
|
||||
string key;
|
||||
switch (getDataType)
|
||||
{
|
||||
case MachineGetDataType.CommunicationTag:
|
||||
case MachineDataType.CommunicationTag:
|
||||
{
|
||||
key = address.CommunicationTag;
|
||||
break;
|
||||
}
|
||||
case MachineGetDataType.Address:
|
||||
case MachineDataType.Address:
|
||||
{
|
||||
key = AddressFormater.FormatAddress(address.Area, address.Address, address.SubAddress);
|
||||
break;
|
||||
}
|
||||
case MachineGetDataType.Name:
|
||||
case MachineDataType.Name:
|
||||
{
|
||||
key = address.Name;
|
||||
break;
|
||||
}
|
||||
case MachineGetDataType.Id:
|
||||
case MachineDataType.Id:
|
||||
{
|
||||
key = address.Id.ToString();
|
||||
break;
|
||||
@@ -337,14 +285,14 @@ namespace Modbus.Net
|
||||
if (datas.Length == 0)
|
||||
ans.Add(key, new ReturnUnit
|
||||
{
|
||||
PlcValue = null,
|
||||
DeviceValue = null,
|
||||
UnitExtend = address.UnitExtend
|
||||
});
|
||||
else
|
||||
ans.Add(key,
|
||||
new ReturnUnit
|
||||
{
|
||||
PlcValue =
|
||||
DeviceValue =
|
||||
Convert.ToDouble(
|
||||
ValueHelper.GetInstance(BaseUtility.Endian)
|
||||
.GetValue(datas, ref localMainPos, ref localSubPos,
|
||||
@@ -367,7 +315,7 @@ namespace Modbus.Net
|
||||
if (!KeepConnect)
|
||||
BaseUtility.Disconnect();
|
||||
//返回数据
|
||||
if (ans.All(p => p.Value.PlcValue == null)) ans = null;
|
||||
if (ans.All(p => p.Value.DeviceValue == null)) ans = null;
|
||||
ErrorCount = 0;
|
||||
return ans;
|
||||
}
|
||||
@@ -388,7 +336,7 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public bool SetDatas(MachineSetDataType setDataType, Dictionary<string, double> values)
|
||||
public bool SetDatas(MachineDataType setDataType, Dictionary<string, double> values)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SetDatasAsync(setDataType, values));
|
||||
}
|
||||
@@ -399,7 +347,7 @@ namespace Modbus.Net
|
||||
/// <param name="setDataType">写入类型</param>
|
||||
/// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public async Task<bool> SetDatasAsync(MachineSetDataType setDataType, Dictionary<string, double> values)
|
||||
public async Task<bool> SetDatasAsync(MachineDataType setDataType, Dictionary<string, double> values)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -416,7 +364,7 @@ namespace Modbus.Net
|
||||
AddressUnit<TUnitKey> address = null;
|
||||
switch (setDataType)
|
||||
{
|
||||
case MachineSetDataType.Address:
|
||||
case MachineDataType.Address:
|
||||
{
|
||||
address =
|
||||
GetAddresses.SingleOrDefault(
|
||||
@@ -426,18 +374,18 @@ namespace Modbus.Net
|
||||
AddressFormater.FormatAddress(p.Area, p.Address) == value.Key);
|
||||
break;
|
||||
}
|
||||
case MachineSetDataType.CommunicationTag:
|
||||
case MachineDataType.CommunicationTag:
|
||||
{
|
||||
address =
|
||||
GetAddresses.SingleOrDefault(p => p.CommunicationTag == value.Key);
|
||||
break;
|
||||
}
|
||||
case MachineSetDataType.Name:
|
||||
case MachineDataType.Name:
|
||||
{
|
||||
address = GetAddresses.SingleOrDefault(p => p.Name == value.Key);
|
||||
break;
|
||||
}
|
||||
case MachineSetDataType.Id:
|
||||
case MachineDataType.Id:
|
||||
{
|
||||
address = GetAddresses.SingleOrDefault(p => p.Id.ToString() == value.Key);
|
||||
break;
|
||||
@@ -526,7 +474,7 @@ namespace Modbus.Net
|
||||
KeyValuePair<string, double> value;
|
||||
switch (setDataType)
|
||||
{
|
||||
case MachineSetDataType.Address:
|
||||
case MachineDataType.Address:
|
||||
{
|
||||
//获取要写入的值
|
||||
value =
|
||||
@@ -534,17 +482,17 @@ namespace Modbus.Net
|
||||
p => p.Key == address || address2 != null && p.Key == address2);
|
||||
break;
|
||||
}
|
||||
case MachineSetDataType.CommunicationTag:
|
||||
case MachineDataType.CommunicationTag:
|
||||
{
|
||||
value = values.SingleOrDefault(p => p.Key == addressUnit.CommunicationTag);
|
||||
break;
|
||||
}
|
||||
case MachineSetDataType.Name:
|
||||
case MachineDataType.Name:
|
||||
{
|
||||
value = values.SingleOrDefault(p => p.Key == addressUnit.Name);
|
||||
break;
|
||||
}
|
||||
case MachineSetDataType.Id:
|
||||
case MachineDataType.Id:
|
||||
{
|
||||
value = values.SingleOrDefault(p => p.Key == addressUnit.Id.ToString());
|
||||
break;
|
||||
@@ -760,7 +708,7 @@ namespace Modbus.Net
|
||||
/// <summary>
|
||||
/// 返回的数据
|
||||
/// </summary>
|
||||
public double? PlcValue { get; set; }
|
||||
public double? DeviceValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据的扩展
|
||||
|
||||
@@ -18,8 +18,8 @@ namespace Modbus.Net
|
||||
{
|
||||
if (getValues == null) return null;
|
||||
return (from getValue in getValues
|
||||
where getValue.Value.PlcValue != null
|
||||
select new KeyValuePair<string, double>(getValue.Key, getValue.Value.PlcValue.Value)).ToDictionary(
|
||||
where getValue.Value.DeviceValue != null
|
||||
select new KeyValuePair<string, double>(getValue.Key, getValue.Value.DeviceValue.Value)).ToDictionary(
|
||||
p => p.Key, p => p.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
|
||||
<PackageReference Include="Quartz" Version="3.6.0" />
|
||||
<PackageReference Include="Serilog" Version="2.12.0" />
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
|
||||
<PackageReference Include="System.IO.Ports" Version="7.0.0" />
|
||||
|
||||
@@ -156,10 +156,10 @@ machine = new ModbusMachine(ModbusType.Rtu, "COM3", new List<AddressUnit>()
|
||||
machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator);
|
||||
machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator);
|
||||
machine.AddressCombiner = new AddressCombinerPercentageJump(20.0);
|
||||
var result = machine.InvokeMachineMethods<IMachineMethodData>?.GetDatas(MachineGetDataType.CommunicationTag);
|
||||
var add1 = result["Add1"].PlcValue;
|
||||
var result = machine.InvokeMachineMethods<IMachineMethodData>?.GetDatas(MachineDataType.CommunicationTag);
|
||||
var add1 = result["Add1"].DeviceValue;
|
||||
var resultFormat = result.MapGetValuesToSetValues();
|
||||
machine.InvokeMachineMethods<IMachineMethodData>?.SetDatas(MachineSetDataType.CommunicationTag, resultFormat);
|
||||
machine.InvokeMachineMethods<IMachineMethodData>?.SetDatas(MachineDataType.CommunicationTag, resultFormat);
|
||||
```
|
||||
|
||||
To use BaseMachine, follow these steps.
|
||||
@@ -196,13 +196,13 @@ There are 4 AddressCombiners implemented in the platform.
|
||||
|
||||
3.Use GetDatas Api.
|
||||
```C#
|
||||
var result = machine.InvokeMachineMethods<IMachineMethodData>?.GetDatas(MachineGetDataType.CommunicationTag);
|
||||
//var result = await machine.InvokeMachineMethods<IMachineMethodData>?.GetDatasAsync(MachineGetDataType.CommunicationTag);
|
||||
var result = machine.InvokeMachineMethods<IMachineMethodData>?.GetDatas(MachineDataType.CommunicationTag);
|
||||
//var result = await machine.InvokeMachineMethods<IMachineMethodData>?.GetDatasAsync(MachineDataType.CommunicationTag);
|
||||
```
|
||||
|
||||
4.Retrive data from result.
|
||||
```C#
|
||||
var add1 = result["Add1"].PlcValue;
|
||||
var add1 = result["Add1"].DeviceValue;
|
||||
```
|
||||
|
||||
5.Format result to SetData parameter.
|
||||
@@ -212,15 +212,15 @@ var resultFormat = result.MapGetValuesToSetValues();
|
||||
|
||||
6.SetData to machine or another machine.
|
||||
```C#
|
||||
machine.InvokeMachineMethods<IMachineMethodData>?.SetDatas(MachineSetDataType.CommunicationTag, resultFormat);
|
||||
machine.InvokeMachineMethods<IMachineMethodData>?.SetDatas(MachineDataType.CommunicationTag, resultFormat);
|
||||
```
|
||||
There is also a SetDatasAsync Api.
|
||||
machine.SetDatas has four types. It is referenced as the first parameter.
|
||||
|
||||
1. MachineSetDataType.Address: the key of the dictionary of the second parameter is address.
|
||||
2. MachineSetDataType.CommunicationTag: the key of the dictionary of the second parameter is communication tag.
|
||||
3. MachineSetDataType.Id: the key of the dictionary of the second paramenter is ID.
|
||||
4. MachineSetDataType.Name: the key of the dictionary of the second paramenter is name.
|
||||
1. MachineDataType.Address: the key of the dictionary of the second parameter is address.
|
||||
2. MachineDataType.CommunicationTag: the key of the dictionary of the second parameter is communication tag.
|
||||
3. MachineDataType.Id: the key of the dictionary of the second paramenter is ID.
|
||||
4. MachineDataType.Name: the key of the dictionary of the second paramenter is name.
|
||||
|
||||
##<a name="implement"></a> Implementing Your Own Protocol
|
||||
The main target of Modbus.Net is building a high extensable hardware communication protocol, so we allow everyone to extend the protocol.
|
||||
|
||||
Reference in New Issue
Block a user