Files
Z173/Models/PlcConfiguration.cs
2026-06-17 23:08:06 +08:00

124 lines
4.8 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AciTester.Models
{
/// <summary>
/// PLC 配置类,用于存储从 appsettings.json 读取的 Modbus 参数和寄存器地址。
/// </summary>
public class PlcConfiguration
{
// ========== 网络连接参数 ==========
public string IpAddress { get; set; } = "192.168.1.10"; // PLC IP 地址
public int Port { get; set; } = 502; // Modbus TCP 端口
public byte SlaveId { get; set; } = 1; // 从站地址默认1
// 以下属性用于与上位机交互(但实际按工位读取,此处保留兼容)
public ushort PressureRegister { get; set; } // 不再使用,保留兼容
public ushort WetFlowRegister { get; set; } // 湿膜流量寄存器起始地址
public ushort WetFlowRegister2 { get; set; } // 湿膜流量寄存器起始地址
public ushort WetFlowRegister3 { get; set; } // 湿膜流量寄存器起始地址
// ========== 工位专用寄存器 ==========
public ushort PressureRegisterStation1 { get; set; } // 工位1 压力寄存器起始地址
public ushort PressureRegisterStation2 { get; set; } // 工位2
public ushort PressureRegisterStation3 { get; set; } // 工位3
public ushort ValveCoil { get; set; } = 5; // 假设 M5 对应线圈地址 5
public ushort PumpCoil { get; set; } = 6; // 高压超限 M180
public ushort FlowRegister { get; set; } = 1330; // 高压超限 M180
// 新增地址
public ushort FlowCalibrationReg { get; set; } = 1328;
public ushort FlowProtectReg { get; set; } = 1332;
public ushort TemperatureReg { get; set; } = 1082;//
public ushort TempCalibrationReg { get; set; } = 1080;
public ushort PumpPressureReg { get; set; } = 1430;
public ushort PumpPressureCalibReg { get; set; } = 1428;
public ushort ImpactorPressureReg { get; set; } = 1480;
public ushort ImpactorPressureCalibReg { get; set; } = 1478;
public ushort ImpactorPressureCalibCoil { get; set; } = 1303; // M1303
public ushort AcLowPressureAlarmCoil { get; set; } = 1001; // M1001
public ushort AcHighPressureAlarmCoil { get; set; } = 1002; // M1002
public ushort AcStartupCountdownReg { get; set; } = 50; // D50 (双整数)
public ushort DefrostTempSetReg { get; set; } = 302; // D302 (浮点)
public ushort DefrostTimeSetReg { get; set; } = 310; // D310 (双整数)
public ushort DefrostMinuteReg { get; set; } = 42; // D42 (双整数)
public ushort DefrostSecondReg { get; set; } = 40; // D40 (双整数)
public ushort TempProtectReg { get; set; } = 1084; // D1084 (浮点)
public ushort ConstantTempStartCoil { get; set; } = 4; // M4
public ushort DefrostStartCoil { get; set; } = 19; // M19
public ushort FlowCalibrationCoil { get; set; } = 1300; // M1300
public ushort PumpPressureCalibCoil { get; set; } = 1302; // M1302
}
/// <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(int stationId);
/// <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);
Task EnsureConnectedAsync(int retryCount = 3);
void Dispose();
bool IsConnected { get; } // 新增
Task<int> ReadInt32Async(ushort startAddress);
Task WriteInt32Async(ushort startAddress, int value);
}
}