using System; namespace MembranePoreTester.Communication { /// /// PLC 配置类,用于存储从 appsettings.json 读取的 Modbus 参数和寄存器地址。 /// public class PlcConfiguration { // ========== 网络连接参数 ========== public string IpAddress { get; set; } // PLC IP 地址 public int Port { get; set; } // Modbus TCP 端口 public byte SlaveId { get; set; } = 1; // 从站地址(默认1) // 以下属性用于与上位机交互(但实际按工位读取,此处保留兼容) public ushort PressureRegister { get; set; } // 不再使用,保留兼容 public ushort WetFlowRegister { get; set; } // 湿膜流量寄存器起始地址 public ushort DryFlowRegister { get; set; } // 干膜流量寄存器起始地址 public double PressureFactor { get; set; } = 1.0; // 压力系数(单位换算) // ========== 工位专用寄存器 ========== public ushort PressureRegisterStation1 { get; set; } // 工位1 压力寄存器起始地址 public ushort PressureRegisterStation2 { get; set; } // 工位2 public ushort PressureRegisterStation3 { get; set; } // 工位3 // ========== 控制线圈 ========== public ushort PressureModeRegister { get; set; } // 高压/低压模式寄存器 public ushort PressCoil { get; set; } // 加压线圈(M100) public ushort StartCoil { get; set; } // 启动线圈(M20) public ushort EnableCoil { get; set; } // 使能线圈(M21,只读状态) public ushort StopCoil { get; set; } // 停止线圈(M7) // ========== 运维参数(用户可设置) ========== public ushort PressureUpperLimit { get; set; } = 300; // 加压上限 D300 public ushort PressureRate { get; set; } = 280; // 加压速率 D280 // 高压/低压系数(每个工位独立) public ushort HPCoeff1 { get; set; } = 3120; // 工位1 高压系数 public ushort LPCoeff1 { get; set; } = 3122; // 工位1 低压系数 public ushort HPCoeff2 { get; set; } = 3124; // 工位2 public ushort LPCoeff2 { get; set; } = 3126; public ushort HPCoeff3 { get; set; } = 3128; // 工位3 public ushort LPCoeff3 { get; set; } = 3130; // 大/小流量系数(每个工位独立) public ushort LargeFlowCoeff1 { get; set; } = 3048; // 工位1 大流量系数 public ushort SmallFlowCoeff1 { get; set; } = 380; // 工位1 小流量系数 public ushort LargeFlowCoeff2 { get; set; } = 1218; // 工位2 public ushort SmallFlowCoeff2 { get; set; } = 1318; public ushort LargeFlowCoeff3 { get; set; } = 1418; // 工位3 public ushort SmallFlowCoeff3 { get; set; } = 1468; // 流量模式选择寄存器(0=大流量,1=小流量) public ushort FlowModeRegister { get; set; } = 4; // 工位1 流量模式 public ushort High1 { get; set; } // 工位1 public ushort High2 { get; set; } // 工位1 public ushort High3 { get; set; } // 工位1 public ushort Low1 { get; set; } // 工位1 public ushort Low2 { get; set; } // 工位1 public ushort Low3 { get; set; } // 工位1 public ushort SmallFlow1 { get; set; } // 工位1 public ushort SmallFlow2 { get; set; } // 工位1 public ushort SmallFlow3 { get; set; } // 工位1 public ushort BigFlow1 { get; set; } // 工位1 public ushort BigFlow2 { get; set; } // 工位1 public ushort BigFlow3 { get; set; } // 工位1 } /// /// PLC 服务接口,定义与 Modbus 设备通信的方法。 /// public interface IPlcService { /// 读取指定工位的压力(浮点数) /// 工位号 1~3 Task ReadPressureAsync(int stationId); /// 读取湿膜流量(浮点数) Task ReadWetFlowAsync(); /// 读取干膜流量(浮点数) Task ReadDryFlowAsync(); /// 写入线圈(如 M 元件) Task WriteCoilAsync(ushort coilAddress, bool value); /// 写入单个寄存器(16位) Task WriteRegisterAsync(ushort registerAddress, ushort value); /// 读取线圈状态(如 M 元件的 ON/OFF) Task ReadCoilAsync(ushort coilAddress); /// 读取连续多个保持寄存器(16位) Task ReadHoldingRegistersAsync(ushort startAddress, ushort count); /// 写入单个保持寄存器(16位) Task WriteSingleRegisterAsync(ushort registerAddress, ushort value); Task WriteMultipleRegistersAsync(ushort registerAddress, float value); float UshortToFloat(ushort P1, ushort P2); } }