Files
VacuumPressureMembranePoreS…/Helpers/PlcConfiguration.cs
2026-04-02 10:13:01 +08:00

119 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
namespace MembranePoreTester.Communication
{
/// <summary>
/// PLC 配置类,用于存储从 appsettings.json 读取的 Modbus 参数和寄存器地址。
/// </summary>
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
}
/// <summary>
/// PLC 服务接口,定义与 Modbus 设备通信的方法。
/// </summary>
public interface IPlcService
{
/// <summary> 读取指定工位的压力(浮点数) </summary>
/// <param name="stationId">工位号 1~3</param>
Task<float> ReadPressureAsync(int stationId);
/// <summary> 读取湿膜流量(浮点数) </summary>
Task<float> ReadWetFlowAsync();
/// <summary> 读取干膜流量(浮点数) </summary>
Task<float> ReadDryFlowAsync();
/// <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);
/// <summary> 写入单个保持寄存器16位 </summary>
Task WriteSingleRegisterAsync(ushort registerAddress, ushort value);
Task WriteMultipleRegistersAsync(ushort registerAddress, float value);
float UshortToFloat(ushort P1, ushort P2);
Task<float> ReadFloatAsync(ushort startAddress);
}
}