添加项目文件。
This commit is contained in:
396
OrpaonEMS.App/Services/AcrelMeterDataService.cs
Normal file
396
OrpaonEMS.App/Services/AcrelMeterDataService.cs
Normal file
@@ -0,0 +1,396 @@
|
||||
using HslCommunication.ModBus;
|
||||
using HslCommunication;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using Prism.Mvvm;
|
||||
|
||||
namespace OrpaonEMS.App.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 客户提供的仪表
|
||||
/// Demo时是工厂的总负载
|
||||
/// </summary>
|
||||
public class AcrelMeterDataService : BindableBase
|
||||
{
|
||||
private readonly ILogService Logger;
|
||||
|
||||
public AcrelMeterDataService(ILogService logService, ConfigDataService configDataService)
|
||||
{
|
||||
//MeterMdDrive = MdDrive;
|
||||
|
||||
this.Logger = logService;
|
||||
ConfigDataService = configDataService;
|
||||
|
||||
MeterMdDrive?.Close();
|
||||
MeterMdDrive = new ModbusRtu();
|
||||
MeterMdDrive.AddressStartWithZero = true;//华为光伏逆变器从0开始的
|
||||
MeterMdDrive.IsStringReverse = false;
|
||||
MeterMdDrive.DataFormat = HslCommunication.Core.DataFormat.ABCD;
|
||||
try
|
||||
{
|
||||
if (ConfigDataService.IsDemo)
|
||||
{
|
||||
MdStation = 4;
|
||||
MeterMdDrive.SerialPortInni(sp =>
|
||||
{
|
||||
sp.PortName = "COM11";
|
||||
//sp.PortName = "COM10";
|
||||
sp.BaudRate = 19200;
|
||||
sp.DataBits = 8;
|
||||
sp.StopBits = System.IO.Ports.StopBits.One;
|
||||
sp.Parity = System.IO.Ports.Parity.None;
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
MeterMdDrive.SerialPortInni(sp =>
|
||||
{
|
||||
sp.PortName = "COM1";
|
||||
//sp.PortName = "COM10";
|
||||
sp.BaudRate = 9600;
|
||||
sp.DataBits = 8;
|
||||
sp.StopBits = System.IO.Ports.StopBits.One;
|
||||
sp.Parity = System.IO.Ports.Parity.None;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
var result = MeterMdDrive.Open();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
MessageBox.Show("AcrelMeter仪表连接失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
|
||||
ScanDeviceStart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Meter 电表通信连接状态
|
||||
/// </summary>
|
||||
public bool MeterLinkState { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 扫描线程使能
|
||||
/// </summary>
|
||||
public bool ThreadEnable { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Modbus 从站的站号
|
||||
/// </summary>
|
||||
private ushort MdStation { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 扫描线程
|
||||
/// </summary>
|
||||
private void ScanDeviceStart()
|
||||
{
|
||||
ScanDeviceTask = Task.Run(async () =>
|
||||
{
|
||||
while (ThreadEnable)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
|
||||
//DiagnosticsTime.Reset();
|
||||
//DiagnosticsTime.Start();
|
||||
try
|
||||
{
|
||||
//2048地址块
|
||||
OperateResultBytes1 = MeterMdDrive.Read($"s={MdStation};x=3;2048", 52);
|
||||
if (OperateResultBytes1.IsSuccess)
|
||||
{
|
||||
Psum = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 52);//BaseIndex1 + 0x81A
|
||||
Qsum = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 68);//BaseIndex1 + 0x822
|
||||
Ua = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 0);//BaseIndex1 + 0x800
|
||||
Ub = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 4);//BaseIndex1 + 0x802
|
||||
Uc = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 8);//BaseIndex1 + 0x804
|
||||
Ia = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 24);//BaseIndex1 + 0x80C
|
||||
Ib = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 28);//BaseIndex1 + 0x80E
|
||||
Ic = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 32);//BaseIndex1 + 0x810
|
||||
|
||||
cosQ = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 100);//BaseIndex1 + 0x832
|
||||
|
||||
//Console.WriteLine($"datetime:{DateTime.Now.ToString()} 3.1--{Psum}");
|
||||
}
|
||||
|
||||
//1501地址块
|
||||
OperateResultBytes2 = MeterMdDrive.Read($"s={MdStation};x=3;1501", 10);
|
||||
if (OperateResultBytes2.IsSuccess)
|
||||
{
|
||||
ThdVolA = MeterMdDrive.ByteTransform.TransUInt16(OperateResultBytes2.Content, 0);//BaseIndex1 + 0x5DD
|
||||
ThdVolB = MeterMdDrive.ByteTransform.TransUInt16(OperateResultBytes2.Content, 2);//BaseIndex1 + 0x5DE
|
||||
ThdVolC = MeterMdDrive.ByteTransform.TransUInt16(OperateResultBytes2.Content, 4);//BaseIndex1 + 0x5DF
|
||||
ThdCurA = MeterMdDrive.ByteTransform.TransUInt16(OperateResultBytes2.Content, 6);//BaseIndex1 + 0x5E0
|
||||
ThdCurB = MeterMdDrive.ByteTransform.TransUInt16(OperateResultBytes2.Content, 8);//BaseIndex1 + 0x5E1
|
||||
ThdCurC = MeterMdDrive.ByteTransform.TransUInt16(OperateResultBytes2.Content, 10);//BaseIndex1 + 0x5E2
|
||||
|
||||
//F = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes2.Content, 28);//
|
||||
|
||||
//Console.WriteLine($"datetime:{DateTime.Now.ToString()} 电表 3.2--负载功率: {Psum}");
|
||||
|
||||
//通信正常
|
||||
MeterLinkState = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//通信正常
|
||||
//MeterLinkState = false;
|
||||
}
|
||||
|
||||
//58130地址块
|
||||
OperateResultBytes3 = MeterMdDrive.Read($"s={MdStation};x=3;58130", 130);
|
||||
if (OperateResultBytes3.IsSuccess)
|
||||
{
|
||||
TipInPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 128);//BaseIndex1 + 0xE314
|
||||
PeakInPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 136);//BaseIndex1 + 0xE316
|
||||
FlatInPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 144);//BaseIndex1 + 0xE318
|
||||
VellayInPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 152);//BaseIndex1 + 0xE31A
|
||||
TipOutPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 200);//BaseIndex1 + 0xE326
|
||||
PeakOutPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 208);//BaseIndex1 + 0xE328
|
||||
FlatOutPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 216);//BaseIndex1 + 0xE32A
|
||||
VellayOutPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 224);//BaseIndex1 + 0xE32C
|
||||
|
||||
TotalInPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 120);//BaseIndex1 + 0xE312
|
||||
TotalOutPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 192);//BaseIndex1 + 0xE324
|
||||
TotalPWh = MeterMdDrive.ByteTransform.TransUInt32(OperateResultBytes3.Content, 48);//BaseIndex1 + 0xE324
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Info($"时间:{DateTime.Now.ToString()}-【ExitPeakValley】-{ex.Message}");
|
||||
}
|
||||
|
||||
|
||||
//DiagnosticsTime.Stop();
|
||||
//ScanRtTimeinfo = $"电表:{DiagnosticsTime.Elapsed.TotalMilliseconds.ToString()}";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭驱动连接
|
||||
/// </summary>
|
||||
public void CloseDrive()
|
||||
{
|
||||
MeterMdDrive.Close();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基础的Index
|
||||
/// </summary>
|
||||
private int BaseIndex1 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实际扫描时间
|
||||
/// </summary>
|
||||
public string ScanRtTimeinfo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 读取的字节数据集合
|
||||
/// </summary>
|
||||
public OperateResult<byte[]> OperateResultBytes1 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 读取的字节数据集合
|
||||
/// </summary>
|
||||
public OperateResult<byte[]> OperateResultBytes2 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 读取的字节数据集合
|
||||
/// </summary>
|
||||
public OperateResult<byte[]> OperateResultBytes3 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 仪表数据驱动
|
||||
/// </summary>
|
||||
public ModbusRtu MeterMdDrive { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// ScanDevice扫描Task
|
||||
/// </summary>
|
||||
static Task ScanDeviceTask { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 时间诊断
|
||||
/// </summary>
|
||||
System.Diagnostics.Stopwatch DiagnosticsTime = new System.Diagnostics.Stopwatch();
|
||||
|
||||
/// <summary>
|
||||
/// A相电压
|
||||
/// </summary>
|
||||
public double Ua { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// B相电压
|
||||
/// </summary>
|
||||
public double Ub { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// C相电压
|
||||
/// </summary>
|
||||
public double Uc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A相电流
|
||||
/// </summary>
|
||||
public double Ia { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// B相电流
|
||||
/// </summary>
|
||||
public double Ib { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// C相电流
|
||||
/// </summary>
|
||||
public double Ic { get; set; }
|
||||
|
||||
private double _Psum;
|
||||
/// <summary>
|
||||
/// 总有功功率
|
||||
/// 总实时负载
|
||||
/// 总市电的电表
|
||||
/// 维持为0是目标
|
||||
/// </summary>
|
||||
public double Psum
|
||||
{
|
||||
get { return _Psum; }
|
||||
set { _Psum = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 总无功功率
|
||||
/// </summary>
|
||||
public double Qsum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 功率因数
|
||||
/// </summary>
|
||||
public double cosQ { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 频率
|
||||
/// </summary>
|
||||
public double F { get; set; }
|
||||
|
||||
private UInt32 _TotalInPWh;
|
||||
/// <summary>
|
||||
/// 输入总有功电度
|
||||
/// </summary>
|
||||
public UInt32 TotalInPWh
|
||||
{
|
||||
get { return _TotalInPWh; }
|
||||
set { _TotalInPWh = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
private UInt32 _TotalOutPWh;
|
||||
/// <summary>
|
||||
/// 输出总有功电度
|
||||
/// </summary>
|
||||
public UInt32 TotalOutPWh
|
||||
{
|
||||
get { return _TotalOutPWh; }
|
||||
set { _TotalOutPWh = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
private UInt32 _TotalPWh;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 总有功总电能
|
||||
/// </summary>
|
||||
public UInt32 TotalPWh
|
||||
{
|
||||
get { return _TotalPWh; }
|
||||
set { _TotalPWh = value; RaisePropertyChanged(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A相电压总畸变
|
||||
/// </summary>
|
||||
public ushort ThdVolA { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// B相电压总畸变
|
||||
/// </summary>
|
||||
public ushort ThdVolB { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// C相电压总畸变
|
||||
/// </summary>
|
||||
public ushort ThdVolC { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A相电流总畸变
|
||||
/// </summary>
|
||||
public ushort ThdCurA { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// B相电流总畸变
|
||||
/// </summary>
|
||||
public ushort ThdCurB { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// C相电流总畸变
|
||||
/// </summary>
|
||||
public ushort ThdCurC { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 尖时段输入有功电度
|
||||
/// </summary>
|
||||
public UInt32 TipInPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 峰时段输入有功电度
|
||||
/// </summary>
|
||||
public UInt32 PeakInPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 平时段输入有功电度
|
||||
/// </summary>
|
||||
public UInt32 FlatInPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 谷时段输入有功电度
|
||||
/// </summary>
|
||||
public UInt32 VellayInPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 尖时段输出有功电度
|
||||
/// </summary>
|
||||
public UInt32 TipOutPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 峰时段输出有功电度
|
||||
/// </summary>
|
||||
public UInt32 PeakOutPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 平时段输出有功电度
|
||||
/// </summary>
|
||||
public UInt32 FlatOutPWh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 谷时段输出有功电度
|
||||
/// </summary>
|
||||
public UInt32 VellayOutPWh { get; set; }
|
||||
public ConfigDataService ConfigDataService { get; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user