BOB/DeviceCommand/Device/WS-68030-380T.cs

71 lines
2.0 KiB
C#

using Common.Attributes;
using DeviceCommand.Base;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace DeviceCommand.Device
{
[BOBCommand]
public class WS_68030_380T : ModbusTcp
{
public WS_68030_380T(string IpAddress, int port, int SendTimeout, int ReceiveTimeout)
{
ConfigureDevice(IpAddress, port, SendTimeout, ReceiveTimeout);
}
public WS_68030_380T() { }
private const byte SlaveAddress = 1;
public async Task _KW(float , CancellationToken ct = default)
{
var send = ConvertFromFloat();
await WriteMultipleRegistersAsync(1, 58, send).WaitAsync(ct);
}
public async Task (CancellationToken ct = default)
{
var tmp = new ushort();
ushort send = (ushort)(tmp | (1 << 8));
await WriteSingleRegisterAsync(1, 70, send).WaitAsync(ct);
}
public async Task (CancellationToken ct = default)
{
await WriteSingleRegisterAsync(1, 70, new()).WaitAsync(ct);
}
public async Task<float> _KW(CancellationToken ct = default)
{
var re = await ReadHoldingRegistersAsync(1, 58, 2).WaitAsync(ct);
return ConvertToFloat(re);
}
#region
protected virtual float ConvertToFloat(ushort[] values)
{
byte[] bytes = new byte[4];
bytes[3] = (byte)(values[0] >> 8);
bytes[2] = (byte)(values[0] & 0xFF);
bytes[1] = (byte)(values[1] >> 8);
bytes[0] = (byte)(values[1] & 0xFF);
return BitConverter.ToSingle(bytes, 0);
}
protected virtual ushort[] ConvertFromFloat(float value)
{
byte[] bytes = BitConverter.GetBytes(value);
return new ushort[]
{
(ushort)((bytes[3] << 8) | bytes[2]),
(ushort)((bytes[1] << 8) | bytes[0])
};
}
#endregion
}
}