This commit is contained in:
@@ -1,79 +1,105 @@
|
||||
namespace MembranePoreTester.Communication
|
||||
using System;
|
||||
|
||||
namespace MembranePoreTester.Communication
|
||||
{
|
||||
/// <summary>
|
||||
/// PLC 配置类,用于存储从 appsettings.json 读取的 Modbus 参数和寄存器地址。
|
||||
/// </summary>
|
||||
public class PlcConfiguration
|
||||
{
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
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 double WetFlowFactor { get; set; } = 1.0;
|
||||
public double DryFlowFactor { get; set; } = 1.0;
|
||||
// ========== 网络连接参数 ==========
|
||||
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 double WetFlowFactor { get; set; } = 1.0; // 湿膜流量系数
|
||||
public double DryFlowFactor { get; set; } = 1.0; // 干膜流量系数
|
||||
|
||||
// ========== 工位专用寄存器 ==========
|
||||
public ushort PressureRegisterStation1 { get; set; } // 工位1 压力寄存器起始地址
|
||||
public ushort PressureRegisterStation2 { get; set; } // 工位2
|
||||
public ushort PressureRegisterStation3 { get; set; } // 工位3
|
||||
|
||||
public ushort PressureRegisterStation1 { get; set; }
|
||||
public ushort PressureRegisterStation2 { get; set; }
|
||||
public ushort PressureRegisterStation3 { get; set; }
|
||||
public ushort PressureModeRegister { get; set; }
|
||||
public ushort PressCoil { get; set; }
|
||||
public ushort StartCoil { get; set; }
|
||||
public ushort EnableCoil { get; set; }
|
||||
public ushort StopCoil { get; set; }
|
||||
// ========== 控制线圈 ==========
|
||||
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 PressureCoeff { get; set; } = 282; // 加压系数 D282
|
||||
|
||||
|
||||
|
||||
|
||||
public ushort PressureUpperLimit { get; set; } = 300;
|
||||
public ushort PressureRate { get; set; } = 280;
|
||||
public ushort PressureCoeff { get; set; } = 282;
|
||||
public ushort HPCoeff1 { get; set; } = 3120;
|
||||
public ushort LPCoeff1 { get; set; } = 3122;
|
||||
public ushort HPCoeff2 { get; set; } = 3124;
|
||||
// 高压/低压系数(每个工位独立)
|
||||
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;
|
||||
public ushort HPCoeff3 { get; set; } = 3128; // 工位3
|
||||
public ushort LPCoeff3 { get; set; } = 3130;
|
||||
public ushort LargeFlowCoeff1 { get; set; } = 3048;
|
||||
public ushort SmallFlowCoeff1 { get; set; } = 380;
|
||||
public ushort LargeFlowCoeff2 { get; set; } = 1218;
|
||||
|
||||
// 大/小流量系数(每个工位独立)
|
||||
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;
|
||||
public ushort LargeFlowCoeff3 { get; set; } = 1418; // 工位3
|
||||
public ushort SmallFlowCoeff3 { get; set; } = 1468;
|
||||
public ushort FlowModeRegister1 { get; set; } = 5; // 工位1流量模式 (0=大,1=小)
|
||||
|
||||
// 流量模式选择寄存器(0=大流量,1=小流量)
|
||||
|
||||
public ushort FlowModeRegister { get; set; } = 4; // 工位1 流量模式
|
||||
public ushort FlowModeRegister1 { get; set; } = 5; // 工位1 流量模式
|
||||
public ushort FlowModeRegister2 { get; set; } = 6;
|
||||
public ushort FlowModeRegister3 { get; set; } = 7;
|
||||
// 校准系数地址(需与PLC约定)
|
||||
public ushort PressureCalibZero { get; set; } = 3200;
|
||||
public ushort PressureCalibSpan { get; set; } = 3202;
|
||||
public ushort FlowCalibZero { get; set; } = 3204;
|
||||
public ushort FlowCalibSpan { get; set; } = 3206;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 校准系数(零点/量程)寄存器地址
|
||||
public ushort PressureCalibZero { get; set; } = 3200; // 压力零点系数
|
||||
public ushort PressureCalibSpan { get; set; } = 3202; // 压力量程系数
|
||||
public ushort FlowCalibZero { get; set; } = 3204; // 流量零点系数
|
||||
public ushort FlowCalibSpan { get; set; } = 3206; // 流量量程系数
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// PLC 服务接口,定义与 Modbus 设备通信的方法。
|
||||
/// </summary>
|
||||
public interface IPlcService
|
||||
{
|
||||
Task<float> ReadPressureAsync(int stationId); // 按工位读取压力
|
||||
/// <summary> 读取指定工位的压力(浮点数) </summary>
|
||||
/// <param name="stationId">工位号 1~3</param>
|
||||
Task<float> ReadPressureAsync(int stationId);
|
||||
|
||||
/// <summary> 读取湿膜流量(浮点数) </summary>
|
||||
Task<float> ReadWetFlowAsync();
|
||||
|
||||
/// <summary> 读取干膜流量(浮点数) </summary>
|
||||
Task<float> ReadDryFlowAsync();
|
||||
Task WriteCoilAsync(ushort coilAddress, bool value); // 写线圈
|
||||
Task WriteRegisterAsync(ushort registerAddress, ushort value); // 写寄存器
|
||||
|
||||
Task<bool> ReadCoilAsync(ushort coilAddress); // 新增:读取线圈状态
|
||||
/// <summary> 写入线圈(如 M 元件) </summary>
|
||||
Task WriteCoilAsync(ushort coilAddress, bool value);
|
||||
|
||||
/// <summary> 写入单个寄存器(16位) </summary>
|
||||
Task WriteRegisterAsync(ushort registerAddress, ushort value);
|
||||
|
||||
/// <summary> 读取线圈状态(如 M 元件的 ON/OFF) </summary>
|
||||
Task<bool> ReadCoilAsync(ushort coilAddress);
|
||||
|
||||
/// <summary> 读取连续多个保持寄存器(16位) </summary>
|
||||
Task<ushort[]> ReadHoldingRegistersAsync(ushort startAddress, ushort count);
|
||||
Task WriteSingleRegisterAsync(ushort registerAddress, ushort value);
|
||||
}
|
||||
|
||||
/// <summary> 写入单个保持寄存器(16位) </summary>
|
||||
Task WriteSingleRegisterAsync(ushort registerAddress, ushort value);
|
||||
|
||||
|
||||
Task WriteMultipleRegistersAsync(ushort registerAddress, float value);
|
||||
|
||||
float UshortToFloat(ushort P1, ushort P2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user