HJ212 and Documents

This commit is contained in:
OpenClaw
2026-04-04 17:25:15 +08:00
parent e7cad88bcf
commit 3b223bc440
125 changed files with 18829 additions and 9380 deletions

View File

@@ -1,23 +1,37 @@
// AnyType - ASP.NET Core Web 应用入口
// 任何类型的数据都可以处理的 Web 应用示例
// 创建 Web 应用构建器
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// 添加服务到容器
// 添加控制器和视图支持
builder.Services.AddControllersWithViews();
// 构建应用
var app = builder.Build();
// Configure the HTTP request pipeline.
// 配置 HTTP 请求管道
// 如果不是开发环境,使用异常处理中间件
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
// 启用静态文件服务
app.UseStaticFiles();
// 启用路由
app.UseRouting();
// 启用授权
app.UseAuthorization();
// 映射控制器路由
// 默认路由模式:{controller=Home}/{action=Index}/{id?}
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// 运行应用
app.Run();

View File

@@ -1,23 +1,37 @@
// CrossLamp - ASP.NET Core Web 应用入口
// 交叉灯控制 Web 应用示例
// 创建 Web 应用构建器
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// 添加服务到容器
// 添加控制器和视图支持
builder.Services.AddControllersWithViews();
// 构建应用
var app = builder.Build();
// Configure the HTTP request pipeline.
// 配置 HTTP 请求管道
// 如果不是开发环境,使用异常处理中间件
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
// 启用静态文件服务
app.UseStaticFiles();
// 启用路由
app.UseRouting();
// 启用授权
app.UseAuthorization();
// 映射控制器路由
// 默认路由模式:{controller=Home}/{action=Index}/{id?}
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// 运行应用
app.Run();

View File

@@ -1,18 +1,29 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis;
namespace Modbus.Net.CodeGenerator
{
/// <summary>
/// 数据库写入实体代码生成器
/// 使用 Roslyn 源生成器自动生成 DatabaseWriteEntity 类的属性
/// </summary>
[Generator]
public class DatabaseWriteEntityCodeGenerator : ISourceGenerator
{
/// <summary>
/// 执行代码生成
/// 生成 10 个 Value1 到 Value10 的双精度属性
/// </summary>
public void Execute(GeneratorExecutionContext context)
{
var content = "";
// 生成 10 个属性Value1 到 Value10
for (int i = 1; i <= 10; i++)
{
content += $@"public double? Value{i} {{ get; set; }}
";
}
// 生成完整的类代码
var source = $@"
namespace MachineJob
@@ -22,12 +33,15 @@ namespace MachineJob
{content}
}}
}}";
// 添加生成的源代码到编译
context.AddSource("DatabaseWriteContent.g.cs", source);
}
/// <summary>
/// 初始化生成器
/// </summary>
public void Initialize(GeneratorInitializationContext context)
{
}
}
}

View File

@@ -1,33 +1,53 @@
using Quartz.Logging;
using Quartz.Logging;
namespace MachineJob
{
// simple log provider to get something to the console
/// <summary>
/// 控制台日志提供者
/// 为 Quartz 调度器提供简单的控制台日志输出
/// </summary>
public class ConsoleLogProvider : ILogProvider
{
// 配置根对象
private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
// 设置配置文件的基础路径
.SetBasePath(Directory.GetCurrentDirectory())
// 添加 appsettings.json 配置文件
.AddJsonFile("appsettings.json")
// 根据环境变量添加对应的配置文件
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
.Build();
/// <summary>
/// 获取日志记录器
/// </summary>
/// <param name="name">日志记录器名称</param>
/// <returns>日志记录器委托</returns>
public Logger GetLogger(string name)
{
return (level, func, exception, parameters) =>
{
// 如果日志级别大于等于配置的级别且有消息函数
if (level >= configuration.GetSection("Quartz").GetValue<Quartz.Logging.LogLevel>("LogLevel") && func != null)
{
// 输出到控制台,格式:[时间] [级别] 消息
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters);
}
return true;
};
}
/// <summary>
/// 打开嵌套上下文(未实现)
/// </summary>
public IDisposable OpenNestedContext(string message)
{
throw new NotImplementedException();
}
/// <summary>
/// 打开映射上下文(未实现)
/// </summary>
public IDisposable OpenMappedContext(string key, object value, bool destructure = false)
{
throw new NotImplementedException();

View File

@@ -1,34 +1,68 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MachineJob
{
/// <summary>
/// 数据库写入上下文
/// 用于 Entity Framework Core 访问 MySQL 数据库
/// </summary>
public class DatabaseWriteContext : DbContext
{
// 配置根对象
private static readonly IConfigurationRoot configuration = new ConfigurationBuilder()
// 设置配置文件的基础路径
.SetBasePath(Directory.GetCurrentDirectory())
// 添加 appsettings.default.json 配置文件(必需)
.AddJsonFile("appsettings.default.json", optional: false, reloadOnChange: true)
// 添加 appsettings.json 配置文件(必需)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
// 根据环境变量添加对应的配置文件(可选)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
.Build();
// 从配置读取数据库连接字符串
private static readonly string connectionString = configuration.GetConnectionString("DatabaseWriteConnectionString")!;
/// <summary>
/// 数据库写入实体集合
/// </summary>
public DbSet<DatabaseWriteEntity>? DatabaseWrites { get; set; }
/// <summary>
/// 配置数据库上下文
/// 使用 MySQL 数据库
/// </summary>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// 使用 MySQL自动检测服务器版本
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
/// <summary>
/// 数据库写入实体类
/// 映射到 databasewrites 表
/// </summary>
[Table(name: "databasewrites")]
public partial class DatabaseWriteEntity
{
/// <summary>
/// 主键 ID
/// </summary>
[Key]
public int Id { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdateTime { get; set; }
// 注意Value1 到 Value10 属性由 DatabaseWriteEntityCodeGenerator 代码生成器自动生成
// public double? Value1 { get; set; }
// public double? Value2 { get; set; }
// ...
// public double? Value10 { get; set; }
}
}

View File

@@ -2,38 +2,59 @@ using MachineJob;
using MachineJob.Service;
using Serilog;
// MachineJob - 多设备定时任务调度示例
// 作为 Windows 服务运行
// 配置主机服务
IHost host = Host.CreateDefaultBuilder(args).UseWindowsService()
// 配置应用程序配置
.ConfigureAppConfiguration((hostingContext, config) =>
{
// 构建配置
var configuration = config
// 设置配置文件的基础路径
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
// 添加 appsettings.json 配置文件
.AddJsonFile("appsettings.json")
// 根据环境变量添加对应的配置文件
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
// 添加环境变量配置
.AddEnvironmentVariables()
.Build();
// 设置当前工作目录
Directory.SetCurrentDirectory(hostingContext.HostingEnvironment.ContentRootPath);
// 配置 Serilog 日志
Log.Logger = new LoggerConfiguration()
// 从配置读取日志设置
.ReadFrom.Configuration(configuration)
// 从日志上下文丰富日志信息
.Enrich.FromLogContext()
// 输出到控制台
.WriteTo.Console()
// 输出到文件(错误级别,按天滚动)
.WriteTo.File("Log\\log..txt", Serilog.Events.LogEventLevel.Error, shared: true, rollingInterval: RollingInterval.Day)
.CreateLogger();
// 创建日志工厂并添加 Serilog
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
// 设置 Quartz 日志提供者
Quartz.Logging.LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
// 设置 Modbus.Net 日志提供者
Modbus.Net.LogProvider.SetLogProvider(loggerFactory);
}
)
})
// 配置服务
.ConfigureServices(services =>
{
// 添加后台服务 Worker
services.AddHostedService<Worker>();
// 添加日志服务
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(Log.Logger, true));
})
.Build();
// 运行主机
await host.RunAsync();

View File

@@ -4,6 +4,10 @@ using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Mod
namespace MachineJob.Service
{
/// <summary>
/// MachineJob 后台工作服务
/// 使用 Quartz 调度器定期从多个设备读取数据并写入
/// </summary>
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
@@ -15,44 +19,32 @@ namespace MachineJob.Service
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
//1. 直接Coding
// 1. 编码方式(已注释)
// 手动配置地址单元列表
//List<AddressUnit> _addresses = new List<AddressUnit>
//{
// new AddressUnit() { Area = "4X", Address = 1, DataType = typeof(short), Id = "1", Name = "Test1" },
// new AddressUnit() { Area = "4X", Address = 2, DataType = typeof(short), Id = "2", Name = "Test2" },
// new AddressUnit() { Area = "4X", Address = 3, DataType = typeof(short), Id = "3", Name = "Test3" },
// new AddressUnit() { Area = "4X", Address = 4, DataType = typeof(short), Id = "4", Name = "Test4" },
// new AddressUnit() { Area = "4X", Address = 5, DataType = typeof(short), Id = "5", Name = "Test5" },
// new AddressUnit() { Area = "4X", Address = 6, DataType = typeof(short), Id = "6", Name = "Test6" },
// new AddressUnit() { Area = "4X", Address = 7, DataType = typeof(short), Id = "7", Name = "Test7" },
// new AddressUnit() { Area = "4X", Address = 8, DataType = typeof(short), Id = "8", Name = "Test8" },
// new AddressUnit() { Area = "4X", Address = 9, DataType = typeof(short), Id = "9", Name = "Test9" },
// new AddressUnit() { Area = "4X", Address = 10, DataType = typeof(short), Id = "10", Name = "Test10" }
// ...
//};
//List<AddressUnit> _addresses2 = new List<AddressUnit>
//{
// new AddressUnit() { Area = "DB1", Address = 0, DataType = typeof(short), Id = "1", Name = "Test1" },
// new AddressUnit() { Area = "DB1", Address = 2, DataType = typeof(short), Id = "2", Name = "Test2" },
// new AddressUnit() { Area = "DB1", Address = 4, DataType = typeof(short), Id = "3", Name = "Test3" },
// new AddressUnit() { Area = "DB1", Address = 6, DataType = typeof(short), Id = "4", Name = "Test4" },
// new AddressUnit() { Area = "DB1", Address = 8, DataType = typeof(short), Id = "5", Name = "Test5" },
// new AddressUnit() { Area = "DB1", Address = 10, DataType = typeof(short), Id = "6", Name = "Test6" },
// new AddressUnit() { Area = "DB1", Address = 12, DataType = typeof(short), Id = "7", Name = "Test7" },
// new AddressUnit() { Area = "DB1", Address = 14, DataType = typeof(short), Id = "8", Name = "Test8" },
// new AddressUnit() { Area = "DB1", Address = 16, DataType = typeof(short), Id = "9", Name = "Test9" },
// new AddressUnit() { Area = "DB1", Address = 18, DataType = typeof(short), Id = "10", Name = "Test10" }
// ...
//};
// 创建 Modbus 和 Siemens 机器实例
//IMachine<string> machine = new ModbusMachine<string, string>("ModbusMachine1", ModbusType.Tcp, null, _addresses, true, 1, 2, Endian.BigEndianLsb);
//IMachine<string> machine2 = new SiemensMachine<string, string>("SiemensMachine1", SiemensType.Tcp, null, SiemensMachineModel.S7_1200, _addresses2, true, 1, 2);
//var machines = new List<IMachine<string>> { machine, machine2 };
//2. 从参数表读取参数
// 2. 从配置文件读取机器配置
// 使用 MachineReader 从 appsettings.json 读取机器配置
var machines = MachineReader.ReadMachines();
//3.使用MachineJobScheduler
// 3. 使用 MachineJobScheduler(已注释)
// 为每个机器创建独立的调度器
//foreach (var machine in machines)
//{
// var scheduler = await MachineJobSchedulerCreator<IMachineMethodDatas, string, double>.CreateScheduler(machine.Id, -1, 10);
@@ -60,47 +52,71 @@ namespace MachineJob.Service
// await job.Run();
//}
//4. 使用MultipleMachinesJobScheduler
// 4. 使用 MultipleMachinesJobScheduler(已注释)
// 使用多机器调度器,带时间间隔
//return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
//{
// /await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
// await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
//}, -1, 10));
//5. 不设置固定时间,连续触发Job
// 5. 使用固定时间,立即调度 Job
// 当前使用的方式:立即执行,无时间间隔
return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
{
// 配置作业链From -> Query -> To -> Deal
// From: 从机器读取数据
// Query: 查询处理QueryConsole
// To: 写入数据到机器
// Deal: 处理结果OnSuccess/OnFailure
await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
}, -1, 0));
}
public override Task StopAsync(CancellationToken cancellationToken)
{
// 停止所有作业调度
return Task.Run(() => MultipleMachinesJobScheduler.CancelJob());
}
/// <summary>
/// 成功回调
/// 记录机器设置成功的日志
/// </summary>
public Task OnSuccess(string machineId)
{
_logger.LogInformation("Machine {0} set success", machineId);
return Task.CompletedTask;
}
/// <summary>
/// 失败回调
/// 记录机器设置失败的日志
/// </summary>
public Task OnFailure(string machineId, int errorCode, string errorMsg)
{
_logger.LogError("Machine {0} set failure: {1}", machineId, errorMsg);
return Task.CompletedTask;
}
/// <summary>
/// 查询处理函数
/// 处理从设备读取的数据,可以写入数据库或生成随机数据
/// </summary>
private Dictionary<string, double>? QueryConsole(DataReturnDef dataReturnDef)
{
var values = dataReturnDef.ReturnValues.Datas;
// 如果读取成功
if (dataReturnDef.ReturnValues.IsSuccess == true)
{
// 记录日志
foreach (var value in values)
{
_logger.LogDebug(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue);
}
/*
// 写入数据库(已注释)
try
{
using (var context = new DatabaseWriteContext())
@@ -124,19 +140,24 @@ namespace MachineJob.Service
}
catch
{
//ignore
// 忽略异常
}
*/
// 生成随机数据用于写入
Random r = new Random();
foreach (var value in values)
{
value.Value.DeviceValue = r.Next(65536) - 32768;
}
// 将读取的值转换为写入格式
return values.MapGetValuesToSetValues();
}
// 如果读取结果为 null未知状态
else if (dataReturnDef.ReturnValues.IsSuccess == null)
{
// 生成随机数据
Random r = new Random();
Dictionary<string, double> ans = new Dictionary<string, double>();
@@ -148,12 +169,3 @@ namespace MachineJob.Service
return ans;
}
else
{
_logger.LogError(dataReturnDef.MachineId + " Return Error.");
return null;
}
}
}
}

View File

@@ -1,38 +1,56 @@
using ModbusTcpToRtu;
using Serilog;
// 配置主机服务,作为 Windows 服务运行
IHost host = Host.CreateDefaultBuilder(args).UseWindowsService()
// 配置应用程序配置
.ConfigureAppConfiguration((hostingContext, config) =>
{
// 构建配置
var configuration = config
// 设置配置文件的基础路径
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
// 添加 appsettings.json 配置文件
.AddJsonFile("appsettings.json")
// 根据环境变量添加对应的配置文件(开发/生产等)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
// 添加环境变量配置
.AddEnvironmentVariables()
.Build();
// 设置当前工作目录
Directory.SetCurrentDirectory(hostingContext.HostingEnvironment.ContentRootPath);
// 配置 Serilog 日志
Log.Logger = new LoggerConfiguration()
// 从配置读取日志设置
.ReadFrom.Configuration(configuration)
// 从日志上下文丰富日志信息
.Enrich.FromLogContext()
// 输出到控制台
.WriteTo.Console()
// 输出到文件(错误级别,按天滚动)
.WriteTo.File("Log\\log..txt", Serilog.Events.LogEventLevel.Error, shared: true, rollingInterval: RollingInterval.Day)
.CreateLogger();
// 创建日志工厂并添加 Serilog
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
// 设置 Quartz 日志提供者
Quartz.Logging.LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
// 设置 Modbus.Net 日志提供者
Modbus.Net.LogProvider.SetLogProvider(loggerFactory);
}
)
})
// 配置服务
.ConfigureServices(services =>
{
// 添加后台服务 Worker
services.AddHostedService<Worker>();
// 添加日志服务
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(Log.Logger, true));
})
.Build();
// 运行主机
await host.RunAsync();

View File

@@ -8,12 +8,18 @@ using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Mod
namespace ModbusTcpToRtu
{
/// <summary>
/// Modbus TCP 转 RTU 后台工作服务
/// 定期从 Modbus TCP 设备读取数据并写入到 Modbus RTU 设备
/// </summary>
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
// Modbus 读取工具TCP
private BaseUtility readUtility;
// Modbus 写入工具RTU
private BaseUtility writeUtility;
public Worker(ILogger<Worker> logger)
@@ -23,92 +29,120 @@ namespace ModbusTcpToRtu
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// 配置 Quartz 调度器的触发器和作业键
var triggerKey = "Modbus.Net.Job.Utility.SchedulerTrigger";
var jobKey = "Modbus.Net.Job.Utility.JobKey";
// 从配置读取调度间隔(秒转毫秒)
var intervalMilliSecond = int.Parse(ConfigurationReader.GetValue("Utility", "interval")) * 1000;
// 读取重复次数(-1 表示无限重复)
var count = int.Parse(ConfigurationReader.GetValue("Utility", "count"));
// 读取读写配置组
var readWriteGroup = ConfigurationReader.GetContent<List<ReadWriteGroup>>("Utility", "readwrite");
// 读取 Modbus TCP 配置
var readType = Enum.Parse<ModbusType>(ConfigurationReader.GetValue("Utility:read", "type"));
var readAddress = ConfigurationReader.GetValue("Utility:read", "address");
var readSlaveAddress = byte.Parse(ConfigurationReader.GetValue("Utility:read", "slaveAddress"));
var readMasterAddress = byte.Parse(ConfigurationReader.GetValue("Utility:read", "masterAddress"));
// 读取 Modbus RTU 配置
var writeType = Enum.Parse<ModbusType>(ConfigurationReader.GetValue("Utility:write", "type"));
var writeAddress = ConfigurationReader.GetValue("Utility:write", "address");
var writeSlaveAddress = byte.Parse(ConfigurationReader.GetValue("Utility:write", "slaveAddress"));
var writeMasterAddress = byte.Parse(ConfigurationReader.GetValue("Utility:write", "masterAddress"));
// 创建 Modbus 工具实例
readUtility = new ModbusUtility(readType, readAddress, readSlaveAddress, readMasterAddress, Endian.BigEndianLsb);
writeUtility = new ModbusUtility(writeType, writeAddress, writeSlaveAddress, writeMasterAddress, Endian.BigEndianLsb);
// 获取 Quartz 调度器
IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
// 启动调度器
await scheduler.Start();
// 创建触发器
ITrigger trigger;
if (intervalMilliSecond <= 0)
{
// 间隔<=0立即执行一次
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey, "Modbus.Net.DataQuery.Group." + triggerKey)
.StartNow()
.Build();
}
else if (count >= 0)
// 指定重复次数
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey, "Modbus.Net.DataQuery.Group." + triggerKey)
.StartNow()
.WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromMilliseconds(intervalMilliSecond)).WithRepeatCount(count))
.Build();
else
// 无限重复
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey, "Modbus.Net.DataQuery.Group." + triggerKey)
.StartNow()
.WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromMilliseconds(intervalMilliSecond)).RepeatForever())
.Build();
// 创建作业监听器
IJobListener listener;
if (intervalMilliSecond <= 0)
{
// 重复指定次数的监听器
listener = new JobChainingJobLIstenerWithDataMapRepeated("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" }, count);
}
else
{
// 无限重复的监听器
listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" });
}
// 添加作业监听器到调度器
scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey));
// 如果触发器已存在,先删除
if (await scheduler.GetTrigger(new TriggerKey(triggerKey)) != null)
{
await scheduler.UnscheduleJob(new TriggerKey(triggerKey, "Modbus.Net.DataQuery.Group." + triggerKey));
}
// 删除已存在的作业
var jobKeys = await scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey));
await scheduler.DeleteJobs(jobKeys);
// 创建数据传递作业
var job = JobBuilder.Create<UtilityPassDataJob>()
.WithIdentity(jobKey)
.StoreDurably(true)
.Build();
// 将工具实例放入作业数据映射
job.JobDataMap.Put("UtilityRead", readUtility);
job.JobDataMap.Put("UtilityReadWriteGroup", readWriteGroup);
job.JobDataMap.Put("UtilityWrite", writeUtility);
// 调度作业
await scheduler.ScheduleJob(job, trigger);
}
public override Task StopAsync(CancellationToken cancellationToken)
{
// 停止所有作业调度
return Task.Run(() => MultipleMachinesJobScheduler.CancelJob());
}
}
/// <summary>
/// Modbus 数据传递作业
/// 从读取工具读取数据并写入到写入工具
/// </summary>
public class UtilityPassDataJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
// 从作业数据映射获取工具实例
object utilityReadObject;
object utilityWriteObject;
object utilityReadWriteGroupObject;
@@ -121,15 +155,22 @@ namespace ModbusTcpToRtu
var writeUtility = (BaseUtility)utilityWriteObject;
var utilityReadWriteGroup = (List<ReadWriteGroup>)utilityReadWriteGroupObject;
// 连接读取工具
if (readUtility.IsConnected != true)
await readUtility.ConnectAsync();
// 连接写入工具
if (writeUtility.IsConnected != true)
await writeUtility.ConnectAsync();
// 遍历每个读写组
foreach (var rwGroup in utilityReadWriteGroup)
{
// 从 Modbus TCP 读取数据
// 地址格式:"4X 1" = 区域 + 地址
var datas = await readUtility.GetDatasAsync(rwGroup.ReadStart / 10000 + "X " + rwGroup.ReadStart % 10000, rwGroup.ReadCount * 2, rwGroup.ReadCount);
if (datas.IsSuccess == true)
{
// 将字节数组转换为对象数组并写入 Modbus RTU
var ans = await writeUtility.SetDatasAsync(rwGroup.WriteStart / 10000 + "X " + rwGroup.WriteStart % 10000, ByteArrayToObjectArray(datas.Datas), rwGroup.ReadCount);
if (ans.Datas)
{
@@ -143,6 +184,10 @@ namespace ModbusTcpToRtu
}
}
/// <summary>
/// 字节数组转对象数组
/// 将每个字节转换为独立的对象
/// </summary>
public static object[] ByteArrayToObjectArray(byte[] arrBytes)
{
List<object> objArray = new List<object>();
@@ -154,10 +199,27 @@ namespace ModbusTcpToRtu
}
}
/// <summary>
/// 读写配置组
/// 定义从哪个地址读取多少个数据,写入到哪个地址
/// </summary>
public class ReadWriteGroup
{
/// <summary>
/// 读取起始地址
/// 格式:区域*10000 + 地址,如 40001 表示 4X 区地址 1
/// </summary>
public int ReadStart { get; set; }
/// <summary>
/// 读取数量
/// </summary>
public int ReadCount { get; set; }
/// <summary>
/// 写入起始地址
/// 格式:区域*10000 + 地址
/// </summary>
public int WriteStart { get; set; }
}
}
}

View File

@@ -2,38 +2,56 @@ using SampleModbusRtuServer;
using SampleModbusRtuServer.Service;
using Serilog;
// 配置主机服务,作为 Windows 服务运行
IHost host = Host.CreateDefaultBuilder(args).UseWindowsService()
// 配置应用程序配置
.ConfigureAppConfiguration((hostingContext, config) =>
{
// 构建配置
var configuration = config
// 设置配置文件的基础路径
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
// 添加 appsettings.json 配置文件
.AddJsonFile("appsettings.json")
// 根据环境变量添加对应的配置文件(开发/生产等)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
// 添加环境变量配置
.AddEnvironmentVariables()
.Build();
// 设置当前工作目录
Directory.SetCurrentDirectory(hostingContext.HostingEnvironment.ContentRootPath);
// 配置 Serilog 日志
Log.Logger = new LoggerConfiguration()
// 从配置读取日志设置
.ReadFrom.Configuration(configuration)
// 从日志上下文丰富日志信息
.Enrich.FromLogContext()
// 输出到控制台
.WriteTo.Console()
// 输出到文件(错误级别,按天滚动)
.WriteTo.File("Log\\log..txt", Serilog.Events.LogEventLevel.Error, shared: true, rollingInterval: RollingInterval.Day)
.CreateLogger();
// 创建日志工厂并添加 Serilog
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
// 设置 Quartz 日志提供者
Quartz.Logging.LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
// 设置 Modbus.Net 日志提供者
Modbus.Net.LogProvider.SetLogProvider(loggerFactory);
}
)
})
// 配置服务
.ConfigureServices(services =>
{
// 添加后台服务 Worker
services.AddHostedService<Worker>();
// 添加日志服务
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(Log.Logger, true));
})
.Build();
// 运行主机
await host.RunAsync();

View File

@@ -4,15 +4,24 @@ using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Mod
namespace SampleModbusRtuServer.Service
{
/// <summary>
/// Modbus RTU 服务器后台工作服务
/// 监听串口,响应 Modbus RTU 读写请求
/// </summary>
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
// 线圈数据数组0X 区10000 点)
private bool[] zerox = new bool[10000];
// 保持寄存器数据数组4X 区20000 字节=10000 个寄存器)
private byte[] threex = new byte[20000];
// 数据更新标志
private bool _isUpdate = false;
// 最后更新时间
private DateTime _updateTime = DateTime.MinValue;
public Worker(ILogger<Worker> logger)
@@ -22,18 +31,28 @@ namespace SampleModbusRtuServer.Service
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// 创建 Modbus RTU 协议接收器,监听 COM2 端口,从站地址 1
ModbusRtuProtocolReceiver receiver = new ModbusRtuProtocolReceiver("COM2", 1);
// 设置数据处理回调
receiver.DataProcess = receiveContent =>
{
byte[]? returnBytes = null;
// 读取内容缓冲区(每个寄存器 2 字节)
var readContent = new byte[receiveContent.Count * 2];
// 写入内容
var values = receiveContent.WriteContent;
// 值字典(未使用)
var valueDic = new Dictionary<string, double>();
// Redis 值字典(未使用)
var redisValues = new Dictionary<string, ReturnUnit<double>>();
// 处理写操作
if (values != null)
{
try
{
// 如果处于更新模式且距离上次更新超过 9.5 秒,记录日志
if (_isUpdate && DateTime.Now - _updateTime > TimeSpan.FromSeconds(9.5))
{
_logger.LogDebug($"receive content { String.Concat(receiveContent.WriteContent.Select(p => " " + p.ToString("X2")))}");
@@ -43,17 +62,24 @@ namespace SampleModbusRtuServer.Service
{
_logger.LogError(ex, "Error");
}
// 根据功能码处理不同的写操作
switch (receiveContent.FunctionCode)
{
case (byte)ModbusProtocolFunctionCode.WriteMultiRegister:
{
// 写多个寄存器(功能码 16
// 将写入的数据复制到保持寄存器数组
Array.Copy(receiveContent.WriteContent, 0, threex, receiveContent.StartAddress * 2, receiveContent.WriteContent.Length);
// 生成响应帧
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count);
_isUpdate = true;
break;
}
case (byte)ModbusProtocolFunctionCode.WriteSingleCoil:
{
// 写单个线圈(功能码 5
// 255=ON, 0=OFF
if (receiveContent.WriteContent[0] == 255)
{
zerox[receiveContent.StartAddress] = true;
@@ -62,80 +88,100 @@ namespace SampleModbusRtuServer.Service
{
zerox[receiveContent.StartAddress] = false;
}
// 生成响应帧
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.WriteContent);
_isUpdate = true;
break;
}
case (byte)ModbusProtocolFunctionCode.WriteMultiCoil:
{
// 写多个线圈(功能码 15
var pos = 0;
List<bool> bitList = new List<bool>();
// 将字节数组转换为位列表
for (int i = 0; i < receiveContent.WriteByteCount; i++)
{
var bitArray = BigEndianLsbValueHelper.Instance.GetBits(receiveContent.WriteContent, ref pos);
bitList.AddRange(bitArray.ToList());
}
// 将位列表复制到线圈数组
Array.Copy(bitList.ToArray(), 0, zerox, receiveContent.StartAddress, bitList.Count);
// 生成响应帧
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count);
_isUpdate = true;
break;
}
case (byte)ModbusProtocolFunctionCode.WriteSingleRegister:
{
// 写单个寄存器(功能码 6
// 将写入的数据复制到保持寄存器数组
Array.Copy(receiveContent.WriteContent, 0, threex, receiveContent.StartAddress * 2, receiveContent.Count * 2);
// 生成响应帧
returnBytes = new WriteDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.StartAddress, receiveContent.Count);
_isUpdate = true;
break;
}
}
}
// 处理读操作
else
{
switch (receiveContent.FunctionCode)
{
case (byte)ModbusProtocolFunctionCode.ReadHoldRegister:
{
// 读保持寄存器(功能码 3
// 从保持寄存器数组复制数据到读取缓冲区
Array.Copy(threex, receiveContent.StartAddress, readContent, 0, readContent.Length);
// 生成响应帧
returnBytes = new ReadDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, (byte)receiveContent.Count, readContent);
break;
}
case (byte)ModbusProtocolFunctionCode.ReadCoilStatus:
{
// 读线圈状态(功能码 1
// 计算需要读取的位数
var bitCount = receiveContent.WriteByteCount * 8;
var boolContent = new bool[bitCount];
// 从线圈数组复制数据
Array.Copy(zerox, receiveContent.StartAddress, boolContent, 0, bitCount);
// 将位数组打包为字节数组
var byteList = new List<byte>();
for (int i = 0; i < receiveContent.WriteByteCount; i++)
{
byte result = 0;
for (int j = i; j < i + 8; j++)
{
// 将布尔值转换为对应的
// 将布尔值转换为位1 或 0
byte bit = boolContent[j] ? (byte)1 : (byte)0;
// 使用左移位运算将位合并到结果字节
// 使用位运算将位合并字节
result = (byte)((result << 1) | bit);
}
byteList.Add(result);
}
readContent = byteList.ToArray();
// 生成响应帧
returnBytes = new ReadDataModbusProtocol().Format(receiveContent.SlaveAddress, receiveContent.FunctionCode, receiveContent.WriteByteCount, readContent);
break;
}
}
}
// 返回响应帧,如果无法处理则返回 null
if (returnBytes != null) return returnBytes;
else return null;
};
// 连接接收器(打开串口)
await receiver.ConnectAsync();
}
public override Task StopAsync(CancellationToken cancellationToken)
{
// 停止所有作业调度
return Task.Run(() => MultipleMachinesJobScheduler.CancelJob());
}
}
}
}

View File

@@ -1,23 +1,37 @@
// TripleAdd - ASP.NET Core Web 应用入口
// 三数相加 Web 应用示例
// 创建 Web 应用构建器
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// 添加服务到容器
// 添加控制器和视图支持
builder.Services.AddControllersWithViews();
// 构建应用
var app = builder.Build();
// Configure the HTTP request pipeline.
// 配置 HTTP 请求管道
// 如果不是开发环境,使用异常处理中间件
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
// 启用静态文件服务
app.UseStaticFiles();
// 启用路由
app.UseRouting();
// 启用授权
app.UseAuthorization();
// 映射控制器路由
// 默认路由模式:{controller=Home}/{action=Index}/{id?}
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// 运行应用
app.Run();