141 lines
4.9 KiB
C#
141 lines
4.9 KiB
C#
using DeviceCommand.Base;
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DeviceCommand.Device
|
|
{
|
|
public class LQ7500_D : ModbusRtu
|
|
{
|
|
public byte SlaveAddress { get; set; } = 1; // default slave address
|
|
|
|
private static readonly int[] Pow10Table =
|
|
{ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
|
|
|
private async Task EnsureConnectionAsync()
|
|
{
|
|
if (SerialPort == null || !SerialPort.IsOpen)
|
|
await ConnectAsync();
|
|
}
|
|
|
|
#region Read Methods
|
|
|
|
public async Task<ushort> ReadTemperatureResolutionAsync()
|
|
{
|
|
await EnsureConnectionAsync();
|
|
return (await ReadHoldingRegistersAsync(SlaveAddress, 35, 1))[0];
|
|
}
|
|
|
|
public async Task<double> ReadInternalSensorTemperatureAsync(ushort resolution = 0)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
if (resolution == 0) resolution = await ReadTemperatureResolutionAsync();
|
|
var data = await ReadHoldingRegistersAsync(SlaveAddress, 0, 1);
|
|
return Math.Round(data[0] / (double)Pow10Table[resolution], resolution + 1);
|
|
}
|
|
|
|
public async Task<double> ReadExternalSensorTemperatureAsync(ushort resolution = 0)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
if (resolution == 0) resolution = await ReadTemperatureResolutionAsync();
|
|
var data = await ReadHoldingRegistersAsync(SlaveAddress, 1, 1);
|
|
return data[0] / (double)Pow10Table[resolution];
|
|
}
|
|
|
|
public async Task<ushort> ReadFaultCodeAsync()
|
|
{
|
|
await EnsureConnectionAsync();
|
|
return (await ReadHoldingRegistersAsync(SlaveAddress, 1, 1))[0];
|
|
}
|
|
|
|
public async Task<ushort> ReadDeviceStatusAsync()
|
|
{
|
|
await EnsureConnectionAsync();
|
|
return (await ReadHoldingRegistersAsync(SlaveAddress, 3, 1))[0];
|
|
}
|
|
|
|
public async Task<ushort> ReadPumpStatusAsync()
|
|
{
|
|
await EnsureConnectionAsync();
|
|
return (await ReadHoldingRegistersAsync(SlaveAddress, 4, 1))[0];
|
|
}
|
|
|
|
public async Task<double> ReadFlowAsync()
|
|
{
|
|
await EnsureConnectionAsync();
|
|
var data = await ReadHoldingRegistersAsync(SlaveAddress, 17, 1);
|
|
return Math.Round(data[0] / 10.0, 1);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Write Methods
|
|
|
|
public async Task WriteFlowSettingAsync(double value)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
short temp = (short)(value * 10);
|
|
ushort result = Unsafe.As<short, ushort>(ref temp);
|
|
await WriteSingleRegisterAsync(SlaveAddress, 24, result);
|
|
}
|
|
|
|
public async Task WriteDeviceStatusAsync(bool on)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
await WriteSingleRegisterAsync(SlaveAddress, 3, (ushort)(on ? 1 : 0));
|
|
}
|
|
|
|
public async Task WritePumpStatusAsync(bool on)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
await WriteSingleRegisterAsync(SlaveAddress, 4, (ushort)(on ? 1 : 0));
|
|
}
|
|
|
|
public async Task SetTemperatureAsync(double temperature)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
short temp = (short)(temperature * 100);
|
|
ushort result = Unsafe.As<short, ushort>(ref temp);
|
|
await WriteSingleRegisterAsync(SlaveAddress, 2, result);
|
|
}
|
|
|
|
public async Task SetTemperatureUpperLimitAsync(double temperature)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
short temp = (short)(temperature * 100);
|
|
ushort result = Unsafe.As<short, ushort>(ref temp);
|
|
await WriteSingleRegisterAsync(SlaveAddress, 5, result);
|
|
}
|
|
|
|
public async Task SetTemperatureLowerLimitAsync(double temperature)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
short temp = (short)(temperature * 100);
|
|
ushort result = Unsafe.As<short, ushort>(ref temp);
|
|
await WriteSingleRegisterAsync(SlaveAddress, 6, result);
|
|
}
|
|
|
|
public async Task SetSoftwareOverTemperatureAsync(double upper, double lower)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
short v1 = (short)(upper * 100);
|
|
short v2 = (short)(lower * 100);
|
|
ushort[] data = MemoryMarshal.Cast<short, ushort>(new short[] { v1, v2 }).ToArray();
|
|
await WriteSingleRegisterAsync(SlaveAddress, 30, data[0]);
|
|
await Task.Delay(5);
|
|
await WriteSingleRegisterAsync(SlaveAddress, 31, data[1]);
|
|
}
|
|
|
|
public async Task SetHardwareOverTemperatureAsync(double upper)
|
|
{
|
|
await EnsureConnectionAsync();
|
|
short v1 = (short)(upper * 100);
|
|
ushort result = Unsafe.As<short, ushort>(ref v1);
|
|
await WriteSingleRegisterAsync(SlaveAddress, 32, result);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|