io板卡兼容优化

This commit is contained in:
hsc
2026-07-20 15:45:36 +08:00
parent 48492973c3
commit 7f2721ab11
6 changed files with 224 additions and 36 deletions

View File

@@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace DeviceCommand.Device
{
[ADPCommand]
//[ADPCommand]
public class IOBoard : ModbusTcp
{

View File

@@ -0,0 +1,160 @@
using Common.Attributes;
using Model.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using DeviceCommand.Base;
using DeviceCommand.Device;
using System.ComponentModel;
namespace DeviceCommand.Devices
{
/// <summary>
/// 系统控制功能类型枚举 (中文)
/// </summary>
public enum : byte
{
,
,
,
}
/// <summary>
/// 精简后的 IO 端口信息结构体
/// </summary>
public struct IoPortInfo
{
public int ModuleIndex { get; set; } // IO板卡/模块序号 (1 或 2)
public ushort Address { get; set; } // 转换为 Modbus 的线圈相对偏移地址 (Y1->0, Y2->1)
public IoPortInfo(int moduleIndex, ushort address)
{
ModuleIndex = moduleIndex;
Address = address;
}
}
[ADPCommand]
public class IOBoardGroup
{
// 声明底层的 2 个 IO 板卡实例
public IOBoard Board1 { get; private set; }
public IOBoard Board2 { get; private set; }
private readonly byte _slaveAddress = 1; // 默认 Modbus 从站号为 1
// 核心:严格对齐图片数据的双层嵌套配置字典 (2个模块1-8台架)
private static readonly Dictionary<int, Dictionary<, IoPortInfo>> IoMapping
= new Dictionary<int, Dictionary<, IoPortInfo>>()
{
{ 1, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 5) }, // Y6 -> 5
{ ., new IoPortInfo(1, 0) }, // Y1 -> 0
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 8) } // Y9 -> 8
}},
{ 2, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 6) }, // Y7 -> 6
{ ., new IoPortInfo(1, 2) }, // Y3 -> 2
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 9) } // Y10 -> 9
}},
{ 3, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 7) }, // Y8 -> 7
{ ., new IoPortInfo(1, 3) }, // Y4 -> 3
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 10) } // Y11 -> 10
}},
{ 4, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 8) }, // Y9 -> 8
{ ., new IoPortInfo(1, 4) }, // Y5 -> 4
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 11) } // Y12 -> 11
}},
{ 5, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 4) }, // Y5 -> 4
{ ., new IoPortInfo(2, 0) }, // Y1 -> 0
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 12) } // Y13 -> 12
}},
{ 6, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 5) }, // Y6 -> 5
{ ., new IoPortInfo(2, 1) }, // Y2 -> 1
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 13) } // Y14 -> 13
}},
{ 7, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 6) }, // Y7 -> 6
{ ., new IoPortInfo(2, 2) }, // Y3 -> 2
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 14) } // Y15 -> 14
}},
{ 8, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 7) }, // Y8 -> 7
{ ., new IoPortInfo(2, 3) }, // Y4 -> 3
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 15) } // Y16 -> 15
}}
};
public IOBoardGroup(IOBoard board1, IOBoard board2)
{
Board1 = board1 ?? throw new ArgumentNullException(nameof(board1), "IOBoard Board1 实例不能为空。");
Board2 = board2 ?? throw new ArgumentNullException(nameof(board2), "IOBoard Board2 实例不能为空。");
}
/// <summary>
/// 执行指定台架的功能动作控制(带高低电平开关及底层安全互锁)
/// </summary>
/// <param name="台架序号">台架/工位序号 (1 - 8)</param>
/// <param name="动作">控制功能类型 (单相充电, 抛负载, 短路测试, 蜂鸣器报警)</param>
/// <param name="开关状态">开关状态 (true: 开启吸合, false: 关闭断开)</param>
/// <param name="取消令牌">异步取消令牌</param>
public async Task Async(int , , bool , CancellationToken = default)
{
// 1. 根据传入的台架序号和动作检索字典映射关系
if (!IoMapping.TryGetValue(, out var ) || !.TryGetValue(, out var io信息))
{
throw new ArgumentException($"未找到台架 [{台架序号}] 对应功能 [{动作}] 的 IO 点位映射配置。");
}
// 2. 软件防错高危互锁逻辑
if ()
{
if ( == .)
{
var = IoMapping[][.];
await GetBoardInstance(.ModuleIndex).(_slaveAddress, .Address, false);
}
else if ( == .)
{
var = IoMapping[][.];
await GetBoardInstance(.ModuleIndex).(_slaveAddress, .Address, false);
}
}
// 3. 动态获取目标板卡实例并直接驱动开关端口
IOBoard = GetBoardInstance(io信息.ModuleIndex);
await .(_slaveAddress, io信息.Address, );
}
/// <summary>
/// 内部路由辅助方法:根据板卡索引安全获取当前连接的实例对象
/// </summary>
private IOBoard GetBoardInstance(int moduleIndex)
{
IOBoard board = moduleIndex switch
{
1 => Board1,
2 => Board2,
_ => throw new ArgumentOutOfRangeException(nameof(moduleIndex), "非法的板卡序号,系统仅支持 1# 和 2# 模块。")
};
return board ?? throw new InvalidOperationException($"IO板卡模块 [{moduleIndex}#] 尚未完成初始化,无法建立总线通信。");
}
}
}

View File

@@ -2,6 +2,7 @@
using System.IO;
using System.Windows.Input;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Logger;
using Prism.Ioc;
using TestingModule.ViewModels;
@@ -84,18 +85,22 @@ namespace MainModule.ViewModels
private async Task SilenceBuzzer(string scope)
{
if (_deviceManager.DeviceMap.TryGetValue("IO板卡(蜂鸣器)", out var lazyIoBoard))
if (_deviceManager.IOGroup == null) return;
if (scope == "default")
{
if (lazyIoBoard is IOBoard io)
for(int i = 1; i <= 8; i++)
{
if (scope == "default")
await io.(1, 0, [false, false, false, false, false, false, false, false]);
else if (scope != TestStatus) return;
else
{
int index = _systemConfig.SharedParameterList.Where(x => x.ParameterName == "蜂鸣器").FirstOrDefault().Value;
await io.(1, (ushort)index, false);
}
await _deviceManager.IOGroup.Async(i, ., false);
}
}
else if (scope == TestStatus)
{
int = _systemConfig.SharedParameterList
.FirstOrDefault(x => x.ParameterName == "台架序号")?.Value ?? 0;
if ( >= 1 && <= 8)
{
await _deviceManager.IOGroup.Async(, ., false);
}
}
}

View File

@@ -1,4 +1,6 @@
using DeviceCommand.Base;
using DeviceCommand.Device;
using DeviceCommand.Devices;
using Logger;
using Model.Models;
using Prism.Events;
@@ -34,6 +36,7 @@ namespace UIShare.GlobalVariable
/// <summary>类名 → Type 的反射缓存(仅扫描一次)。</summary>
private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap();
public ZLGCANFD CANFD { get; set; }
public IOBoardGroup IOGroup { get; set; }
public DeviceManager(SystemConfig systemConfig, GlobalInfo globalInfo, IEventAggregator eventAggregator)
{
@@ -207,6 +210,25 @@ namespace UIShare.GlobalVariable
LoggerHelper.ErrorWithNotify(_scopeName, $"设备 [{config.DeviceName}] 实例化失败:{inner}");
}
}
// IOGroup 初始化:从已实例化的设备中取出前两个 IOBoard 实例组建 IOBoardGroup
try
{
var ioBoards = DeviceMap.Values.OfType<IOBoard>().Take(2).ToList();
if (ioBoards.Count == 2)
{
IOGroup = new IOBoardGroup(ioBoards[0], ioBoards[1]);
LoggerHelper.Info($"IOBoardGroup 已初始化Board1={ioBoards[0].GetType().Name}, Board2={ioBoards[1].GetType().Name}。");
}
else
{
LoggerHelper.Warn($"IOBoardGroup 初始化跳过:需要 2 个 IOBoard 实例,当前仅找到 {ioBoards.Count} 个。");
}
}
catch (Exception ex)
{
LoggerHelper.ErrorWithNotify(_scopeName, $"IOBoardGroup 初始化失败:{ex.Message}");
}
}
public async Task ConnectAllDevices(CancellationToken ct = default)
@@ -407,12 +429,16 @@ namespace UIShare.GlobalVariable
private static async Task<bool> ConnectTcpAsync(string name, IBaseInterface device, CancellationToken ct)
{
if (device is not ITcp tcp)
if (device is ITcp tcp)
{
LoggerHelper.Warn($"设备 [{name}] 配置为 Tcp 但未实现 ITcp实际类型为 {device.GetType().Name}。");
return false;
return await tcp.ConnectAsync(ct);
}
return await tcp.ConnectAsync(ct);
if (device is IModbusDevice modbusTCP)
{
return await modbusTCP.ConnectAsync(ct);
}
LoggerHelper.Warn($"设备 [{name}] 配置为 Tcp 但未实现 ITcp实际类型为 {device.GetType().Name}。");
return false;
}
private static async Task<bool> ConnectSerialAsync(string name, IBaseInterface device, CancellationToken ct)

View File

@@ -568,6 +568,10 @@ namespace UIShare
{
instance = _deviceManager.CANFD;
}
else if (targetType.Name == "IOBoardGroup")
{
instance = _deviceManager.IOGroup;
}
else instance = _deviceManager.DeviceMap[targetType.Name];
}
catch (Exception ex)

View File

@@ -47,15 +47,7 @@ namespace UIShare.GlobalVariable
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "单相充电",
Value = 0,
IsEditable=false
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "抛负载",
Name = "台架序号",
Value = 1,
IsEditable=false
},
@@ -63,22 +55,15 @@ namespace UIShare.GlobalVariable
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "短路测试",
Value = 0,
IsEditable=false
}, new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "初始化供电",
Value = 0,
Name = "直流负载通道",
Value = 1,
IsEditable=false
},
new ParameterVM
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "蜂鸣器",
Name = "交流电源通道",
Value = 1,
IsEditable=false
},
@@ -94,7 +79,15 @@ namespace UIShare.GlobalVariable
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "示波器通道",
Name = "示波器通道1",
Value = 0,
IsEditable=false
},
new ParameterVM
{
Category = ParameterCategory.Input,
Type = typeof(int),
Name = "示波器通道2",
Value = 0,
IsEditable=false
},