2026-03-24 20:40:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Modbus.Device;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MembranePoreTester.Communication
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ModbusTcpPlcService : IPlcService, IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly PlcConfiguration _config;
|
|
|
|
|
|
private TcpClient _tcpClient;
|
|
|
|
|
|
private IModbusMaster _master;
|
|
|
|
|
|
|
|
|
|
|
|
public ModbusTcpPlcService(PlcConfiguration config)
|
|
|
|
|
|
{
|
|
|
|
|
|
_config = config;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task EnsureConnectedAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_tcpClient == null || !_tcpClient.Connected)
|
|
|
|
|
|
{
|
|
|
|
|
|
_tcpClient = new TcpClient();
|
|
|
|
|
|
await _tcpClient.ConnectAsync(_config.IpAddress, _config.Port);
|
|
|
|
|
|
_master = ModbusIpMaster.CreateIp(_tcpClient);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 读取两个连续的保持寄存器,转换为32位浮点数(假设大端模式)
|
|
|
|
|
|
private async Task<float> ReadFloatAsync(ushort startAddress)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
|
|
|
|
|
|
// 读取两个寄存器(从站地址由配置指定)
|
|
|
|
|
|
ushort[] registers = await _master.ReadHoldingRegistersAsync(_config.SlaveId, startAddress, 2);
|
|
|
|
|
|
|
|
|
|
|
|
// 将两个16位寄存器合并为32位浮点数(大端)
|
|
|
|
|
|
byte[] bytes = new byte[4];
|
|
|
|
|
|
bytes[0] = (byte)(registers[0] >> 8);
|
|
|
|
|
|
bytes[1] = (byte)(registers[0] & 0xFF);
|
|
|
|
|
|
bytes[2] = (byte)(registers[1] >> 8);
|
|
|
|
|
|
bytes[3] = (byte)(registers[1] & 0xFF);
|
|
|
|
|
|
|
|
|
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
|
|
Array.Reverse(bytes); // 如果系统是小端,需要反转字节顺序
|
|
|
|
|
|
|
|
|
|
|
|
return BitConverter.ToSingle(bytes, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<float> ReadPressureAsync() =>
|
|
|
|
|
|
await ReadFloatAsync(_config.PressureRegister) * (float)_config.PressureFactor;
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<float> ReadWetFlowAsync() =>
|
|
|
|
|
|
await ReadFloatAsync(_config.WetFlowRegister) * (float)_config.WetFlowFactor;
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<float> ReadDryFlowAsync() =>
|
|
|
|
|
|
await ReadFloatAsync(_config.DryFlowRegister) * (float)_config.DryFlowFactor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task WriteCoilAsync(ushort coilAddress, bool value)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
await _master.WriteSingleCoilAsync(_config.SlaveId, coilAddress, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task WriteRegisterAsync(ushort registerAddress, ushort value)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
await _master.WriteSingleRegisterAsync(_config.SlaveId, registerAddress, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> ReadCoilAsync(ushort coilAddress)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
bool[] result = await _master.ReadCoilsAsync(_config.SlaveId, coilAddress, 1);
|
|
|
|
|
|
return result[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<ushort[]> ReadHoldingRegistersAsync(ushort startAddress, ushort count)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
return await _master.ReadHoldingRegistersAsync(_config.SlaveId, startAddress, count);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task WriteSingleRegisterAsync(ushort registerAddress, ushort value)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
await _master.WriteSingleRegisterAsync(_config.SlaveId, registerAddress, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 新增读取压力(根据工位)
|
|
|
|
|
|
public async Task<float> ReadPressureAsync(int stationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
ushort startAddress = stationId switch
|
|
|
|
|
|
{
|
|
|
|
|
|
1 => _config.PressureRegisterStation1,
|
|
|
|
|
|
2 => _config.PressureRegisterStation2,
|
|
|
|
|
|
3 => _config.PressureRegisterStation3,
|
|
|
|
|
|
_ => throw new ArgumentException("Invalid station")
|
|
|
|
|
|
};
|
|
|
|
|
|
return await ReadFloatAsync(startAddress);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-01 20:29:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ushort转为float类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="P1"></param>
|
|
|
|
|
|
/// <param name="P2"></param>
|
|
|
|
|
|
/// <returns>float型数据</returns>
|
|
|
|
|
|
public float UshortToFloat(ushort P1, ushort P2)
|
|
|
|
|
|
{
|
|
|
|
|
|
int intSign, intSignRest, intExponent, intExponentRest;
|
|
|
|
|
|
float faResult, faDigit;
|
|
|
|
|
|
intSign = P1 / 32768;
|
|
|
|
|
|
intSignRest = P1 % 32768;
|
|
|
|
|
|
intExponent = intSignRest / 128;
|
|
|
|
|
|
intExponentRest = intSignRest % 128;
|
|
|
|
|
|
faDigit = (float)(intExponentRest * 65536 + P2) / 8388608;
|
|
|
|
|
|
faResult = (float)Math.Pow(-1, intSign) * (float)Math.Pow(2, intExponent - 127) * (faDigit + 1);
|
|
|
|
|
|
return faResult;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task WriteMultipleRegistersAsync(ushort registerAddress, float value)
|
|
|
|
|
|
{
|
|
|
|
|
|
await EnsureConnectedAsync();
|
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
|
await _master.WriteMultipleRegistersAsync(_config.SlaveId, registerAddress, SplitFloatToUShortArray((float)value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Float转为Ushort数组发送
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns>返回ushort数组</returns>
|
|
|
|
|
|
public ushort[] SplitFloatToUShortArray(float value)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] floatBytes = BitConverter.GetBytes(value);
|
|
|
|
|
|
ushort[] ushortArray = new ushort[floatBytes.Length / 2];
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0, j = 0; i < floatBytes.Length; i += 2, j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
ushortArray[j] = BitConverter.ToUInt16(floatBytes, i);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ushortArray;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 20:40:26 +08:00
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
_master?.Dispose();
|
|
|
|
|
|
_tcpClient?.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|