Files
VacuumPressureMembranePoreS…/Helpers/PlcConfiguration.cs

119 lines
5.3 KiB
C#
Raw Normal View History

2026-03-26 19:43:52 +08:00
using System;
namespace MembranePoreTester.Communication
2026-02-27 16:58:02 +08:00
{
2026-03-26 19:43:52 +08:00
/// <summary>
/// PLC 配置类,用于存储从 appsettings.json 读取的 Modbus 参数和寄存器地址。
/// </summary>
2026-02-27 16:58:02 +08:00
public class PlcConfiguration
{
2026-03-26 19:43:52 +08:00
// ========== 网络连接参数 ==========
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; // 压力系数(单位换算)
2026-03-28 16:48:41 +08:00
2026-03-26 19:43:52 +08:00
// ========== 工位专用寄存器 ==========
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
2026-03-24 19:33:35 +08:00
public ushort LPCoeff2 { get; set; } = 3126;
2026-03-26 19:43:52 +08:00
public ushort HPCoeff3 { get; set; } = 3128; // 工位3
2026-03-24 19:33:35 +08:00
public ushort LPCoeff3 { get; set; } = 3130;
2026-03-26 19:43:52 +08:00
// 大/小流量系数(每个工位独立)
public ushort LargeFlowCoeff1 { get; set; } = 3048; // 工位1 大流量系数
public ushort SmallFlowCoeff1 { get; set; } = 380; // 工位1 小流量系数
public ushort LargeFlowCoeff2 { get; set; } = 1218; // 工位2
2026-03-24 19:33:35 +08:00
public ushort SmallFlowCoeff2 { get; set; } = 1318;
2026-03-26 19:43:52 +08:00
public ushort LargeFlowCoeff3 { get; set; } = 1418; // 工位3
2026-03-24 19:33:35 +08:00
public ushort SmallFlowCoeff3 { get; set; } = 1468;
2026-03-26 19:43:52 +08:00
// 流量模式选择寄存器0=大流量1=小流量)
2026-03-24 19:33:35 +08:00
2026-03-26 19:43:52 +08:00
public ushort FlowModeRegister { get; set; } = 4; // 工位1 流量模式
2026-03-28 16:48:41 +08:00
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
2026-02-27 16:58:02 +08:00
}
2026-03-26 19:43:52 +08:00
/// <summary>
/// PLC 服务接口,定义与 Modbus 设备通信的方法。
/// </summary>
2026-02-27 16:58:02 +08:00
public interface IPlcService
{
2026-03-26 19:43:52 +08:00
/// <summary> 读取指定工位的压力(浮点数) </summary>
/// <param name="stationId">工位号 1~3</param>
Task<float> ReadPressureAsync(int stationId);
/// <summary> 读取湿膜流量(浮点数) </summary>
2026-02-27 16:58:02 +08:00
Task<float> ReadWetFlowAsync();
2026-03-26 19:43:52 +08:00
/// <summary> 读取干膜流量(浮点数) </summary>
2026-02-27 16:58:02 +08:00
Task<float> ReadDryFlowAsync();
2026-03-19 20:40:54 +08:00
2026-03-26 19:43:52 +08:00
/// <summary> 写入线圈(如 M 元件) </summary>
Task WriteCoilAsync(ushort coilAddress, bool value);
/// <summary> 写入单个寄存器16位 </summary>
Task WriteRegisterAsync(ushort registerAddress, ushort value);
2026-03-24 19:33:35 +08:00
2026-03-26 19:43:52 +08:00
/// <summary> 读取线圈状态(如 M 元件的 ON/OFF </summary>
Task<bool> ReadCoilAsync(ushort coilAddress);
2026-03-24 19:33:35 +08:00
2026-03-26 19:43:52 +08:00
/// <summary> 读取连续多个保持寄存器16位 </summary>
2026-03-24 19:33:35 +08:00
Task<ushort[]> ReadHoldingRegistersAsync(ushort startAddress, ushort count);
2026-03-26 19:43:52 +08:00
/// <summary> 写入单个保持寄存器16位 </summary>
2026-03-24 19:33:35 +08:00
Task WriteSingleRegisterAsync(ushort registerAddress, ushort value);
2026-02-27 16:58:02 +08:00
2026-03-26 19:43:52 +08:00
Task WriteMultipleRegistersAsync(ushort registerAddress, float value);
float UshortToFloat(ushort P1, ushort P2);
2026-04-02 10:13:01 +08:00
Task<float> ReadFloatAsync(ushort startAddress);
2026-03-26 19:43:52 +08:00
}
2026-02-27 16:58:02 +08:00
}