using Infrastructure.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; namespace ZR.Service.Utils.MyAlarmLigth { /// /// 三色灯帮助类 /// public class LightUp { /// /// 读警灯设备站号 /// /// 0 没有解析出来 /// 其他 解析出来设备站号 public static ushort ReadAlarmlightNumber(ThreeColorLightModbus _modbusClient) { // 2. 定义要读取的寄存器信息(根据您的表格) const byte UNIT_ID = 0x00; // 从站地址,根据您的指令示例是 01 ushort startAddress = 0x0fa1; // 起始地址,对应 ushort numberOfRegisters = 1; // 要读取的寄存器数量 (40001 到 40012) // 3. PLC 下发读指令并获取原始寄存器值列表 Console.WriteLine($"正在读取从站{UNIT_ID}广播的寄存器 {startAddress} 开始的 {numberOfRegisters} 个寄存器..."); List registerValues = _modbusClient.ReadHoldingRegisters(UNIT_ID, startAddress, numberOfRegisters); if (registerValues.Count == numberOfRegisters) { Console.WriteLine("读取成功,解析数据:"); return registerValues[0]; } return 0; } /// /// 向指定的警灯设备写入指令,控制警灯的状态为红色闪烁 且蜂鸣器响 /// /// /// 警灯站号 /// /// public static void WriteAlarmLightCommand_Red(ThreeColorLightModbus _modbusClient, byte unitId ) { ushort startAddress = 0x0000; // 起始地址,对应 ushort numberOfRegisters = 0x000B; // 要读取的寄存器数量 (40001 到 40012) //List commandValues= new List { 0x00001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,0x0000, 0x0000,0x000A }; // 红色闪烁 且蜂鸣器响 List commandValues = new List { 0x00001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; // 红色闪烁 且蜂鸣器关闭 // 发送写指令到警灯设备 _modbusClient.WriteMultipleRegisters(unitId, startAddress, commandValues); Console.WriteLine($"已向从站 {unitId} 的寄存器 {startAddress} 写入指令: {string.Join(", ", commandValues)}"); } /// /// 向指定的警灯设备写入指令,控制警灯的状态为黄色常亮 且蜂鸣器响 /// /// /// 警灯站号 /// /// public static void WriteAlarmLightCommand_Yellow(ThreeColorLightModbus _modbusClient, byte unitId) { ushort startAddress = 0x0000; // 起始地址,对应 ushort numberOfRegisters = 0x000B; // 要读取的寄存器数量 (40001 到 40012) //List commandValues = new List { 0x0000, 0x0000, 0x0063, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x000A }; // 黄色常亮 且蜂鸣器响 List commandValues = new List { 0x0000, 0x0000, 0x0063, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; // 黄色常亮 且蜂鸣器关闭 // 发送写指令到警灯设备 _modbusClient.WriteMultipleRegisters(unitId, startAddress, commandValues); Console.WriteLine($"已向从站 {unitId} 的寄存器 {startAddress} 写入指令: {string.Join(", ", commandValues)}"); } /// /// 向指定的警灯设备写入指令,控制警灯的状态为绿灯常亮 且蜂鸣器关闭 /// /// /// 警灯站号 /// /// public static void WriteAlarmLightCommand_Normal(ThreeColorLightModbus _modbusClient, byte unitId) { ushort startAddress = 0x0000; // 起始地址,对应 ushort numberOfRegisters = 0x000B; // 要读取的寄存器数量 (40001 到 40012) List commandValues = new List { 0x0000, 0x0063, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; // 绿灯常亮 且蜂鸣器关闭 // 发送写指令到警灯设备 _modbusClient.WriteMultipleRegisters(unitId, startAddress, commandValues); Console.WriteLine($"已向从站 {unitId} 的寄存器 {startAddress} 写入指令: {string.Join(", ", commandValues)}"); } /// /// 核心方法:检测通讯 → 判断灯是否亮 → 未亮则点亮(默认绿灯) /// /// Modbus客户端 /// 警灯站号(不传则自动读取) public static void CheckLightOnStatusAndTurnOn(ThreeColorLightModbus _modbusClient, byte unitId) { ushort startAddress = 0x0000; // 起始地址,对应 ushort numberOfRegisters = 0x000B; // 要读取的寄存器数量 (40001 到 40012) // 1. 检测通讯并判断灯是否亮(任意颜色亮则视为亮) bool isLightOn = false; try { List currentValues = _modbusClient.ReadHoldingRegisters(unitId, startAddress, numberOfRegisters); // 核心判断:只要红/黄/绿对应寄存器非0,就视为灯亮 isLightOn = currentValues[0] != 0 || // 红灯寄存器 currentValues[1] != 0 || // 绿灯寄存器 currentValues[2] != 0; // 黄灯寄存器 } catch (Exception ex) { Console.WriteLine($"警灯[{unitId}]通讯失败:{ex.Message}"); return; } // 3. 灯已亮则跳过,未亮则点亮指定颜色 if (!isLightOn) { WriteAlarmLightCommand_Normal(_modbusClient, unitId); } } } }