ModbusRegisterMapping.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. namespace APS7100TestTool.Models
  2. {
  3. public enum ModbusRegisterType
  4. {
  5. Holding, // 保持寄存器 (4x区, R/W)
  6. Input // 输入寄存器 (3x区, R)
  7. }
  8. public enum ModbusOperationType
  9. {
  10. WriteCommand, // 写入触发 (PLC写 -> APP发命令)
  11. ReadStatus // 读取状态 (APP查 -> PLC读)
  12. }
  13. public enum ModbusDataType
  14. {
  15. Int16, // 1个寄存器
  16. UInt16, // 1个寄存器
  17. Float, // 2个寄存器 (32-bit float)
  18. Bool // 1个寄存器 (0或1)
  19. }
  20. public class ModbusRegisterMapping
  21. {
  22. // Excel 列名匹配
  23. public int Address { get; set; }
  24. public string RegisterType { get; set; } // Excel中填 "Holding" 或 "Input"
  25. public string OperationType { get; set; } // Excel中填 "Write" 或 "Read"
  26. public string DataType { get; set; } // "Int16", "UInt16", "Float", "Bool"
  27. public string ScpiCommand { get; set; } // 例如 "SOUR:VOLT {0}" 或 "MEAS:SCAL:VOLT?"(APS) / "MEAS:VOLT?"(PSW)
  28. public string DeviceTarget { get; set; } = "Universal"; // "Universal", "APS7100", "PSW250"
  29. public double? TriggerValue { get; set; } = null; // 触发值 (为 null 则为数值同步模式)
  30. public int? DataAddress { get; set; } = null; // 数据源地址 (为 null 则使用当前地址的值)
  31. public string DataAddressType { get; set; } = null; // 数据源地址的数据类型 (为 null 则默认 Float)
  32. public double ScaleFactor { get; set; } = 1.0; // 缩放因子
  33. public int? ResponseAddress { get; set; } = null; // 执行确认地址 (成功写入触发值,失败写入-1)
  34. public string Description { get; set; }
  35. // 辅助转换方法
  36. public ModbusRegisterType GetRegType() => RegisterType?.ToLower() == "input" ? ModbusRegisterType.Input : ModbusRegisterType.Holding;
  37. public ModbusOperationType GetOpType() => OperationType?.ToLower() == "read" ? ModbusOperationType.ReadStatus : ModbusOperationType.WriteCommand;
  38. public ModbusDataType GetDataType()
  39. {
  40. switch(DataType?.ToLower())
  41. {
  42. case "float": return ModbusDataType.Float;
  43. case "uint16": return ModbusDataType.UInt16;
  44. case "bool": return ModbusDataType.Bool;
  45. default: return ModbusDataType.Int16;
  46. }
  47. }
  48. /// <summary>
  49. /// 获取数据源地址的数据类型(默认为 Float,因为数据通常是浮点数)
  50. /// </summary>
  51. public ModbusDataType GetDataAddressType()
  52. {
  53. if (string.IsNullOrEmpty(DataAddressType))
  54. return ModbusDataType.Float; // 默认 Float
  55. switch(DataAddressType.ToLower())
  56. {
  57. case "int16": return ModbusDataType.Int16;
  58. case "uint16": return ModbusDataType.UInt16;
  59. case "bool": return ModbusDataType.Bool;
  60. case "float":
  61. default: return ModbusDataType.Float;
  62. }
  63. }
  64. }
  65. }