namespace APS7100TestTool.Models { public enum ModbusRegisterType { Holding, // 保持寄存器 (4x区, R/W) Input // 输入寄存器 (3x区, R) } public enum ModbusOperationType { WriteCommand, // 写入触发 (PLC写 -> APP发命令) ReadStatus // 读取状态 (APP查 -> PLC读) } public enum ModbusDataType { Int16, // 1个寄存器 UInt16, // 1个寄存器 Float, // 2个寄存器 (32-bit float) Bool // 1个寄存器 (0或1) } public class ModbusRegisterMapping { // Excel 列名匹配 public int Address { get; set; } public string RegisterType { get; set; } // Excel中填 "Holding" 或 "Input" public string OperationType { get; set; } // Excel中填 "Write" 或 "Read" public string DataType { get; set; } // "Int16", "UInt16", "Float", "Bool" public string ScpiCommand { get; set; } // 例如 "SOUR:VOLT {0}" 或 "MEAS:SCAL:VOLT?"(APS) / "MEAS:VOLT?"(PSW) public string DeviceTarget { get; set; } = "Universal"; // "Universal", "APS7100", "PSW250" public double? TriggerValue { get; set; } = null; // 触发值 (为 null 则为数值同步模式) public int? DataAddress { get; set; } = null; // 数据源地址 (为 null 则使用当前地址的值) public string DataAddressType { get; set; } = null; // 数据源地址的数据类型 (为 null 则默认 Float) public double ScaleFactor { get; set; } = 1.0; // 缩放因子 public int? ResponseAddress { get; set; } = null; // 执行确认地址 (成功写入触发值,失败写入-1) public string Description { get; set; } // 辅助转换方法 public ModbusRegisterType GetRegType() => RegisterType?.ToLower() == "input" ? ModbusRegisterType.Input : ModbusRegisterType.Holding; public ModbusOperationType GetOpType() => OperationType?.ToLower() == "read" ? ModbusOperationType.ReadStatus : ModbusOperationType.WriteCommand; public ModbusDataType GetDataType() { switch(DataType?.ToLower()) { case "float": return ModbusDataType.Float; case "uint16": return ModbusDataType.UInt16; case "bool": return ModbusDataType.Bool; default: return ModbusDataType.Int16; } } /// /// 获取数据源地址的数据类型(默认为 Float,因为数据通常是浮点数) /// public ModbusDataType GetDataAddressType() { if (string.IsNullOrEmpty(DataAddressType)) return ModbusDataType.Float; // 默认 Float switch(DataAddressType.ToLower()) { case "int16": return ModbusDataType.Int16; case "uint16": return ModbusDataType.UInt16; case "bool": return ModbusDataType.Bool; case "float": default: return ModbusDataType.Float; } } } }