添加项目文件。
This commit is contained in:
320
OrpaonEMS.App/Services/KEMeterService.cs
Normal file
320
OrpaonEMS.App/Services/KEMeterService.cs
Normal file
@@ -0,0 +1,320 @@
|
||||
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;
|
||||
|
||||
namespace OrpaonEMS.App.Services
|
||||
{
|
||||
public class KEMeterService
|
||||
{
|
||||
//创建日志记录对象
|
||||
Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
public KEMeterService()
|
||||
{
|
||||
//MeterMdDrive = MdDrive;
|
||||
|
||||
MeterMdDrive?.Close();
|
||||
MeterMdDrive = new ModbusRtu();
|
||||
MeterMdDrive.AddressStartWithZero = true;//
|
||||
MeterMdDrive.IsStringReverse = false;
|
||||
MeterMdDrive.DataFormat = HslCommunication.Core.DataFormat.ABCD;
|
||||
try
|
||||
{
|
||||
MeterMdDrive.SerialPortInni(sp =>
|
||||
{
|
||||
sp.PortName = "COM8";
|
||||
//sp.PortName = "COM10";
|
||||
sp.BaudRate = 9600;
|
||||
sp.DataBits = 8;
|
||||
sp.StopBits = System.IO.Ports.StopBits.One;
|
||||
sp.Parity = System.IO.Ports.Parity.Even;
|
||||
});
|
||||
var result = MeterMdDrive.Open();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
MessageBox.Show("总负载仪表连接失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
|
||||
ScanDeviceThread = new Thread(new ThreadStart(ScanDeviceThreadMethod));
|
||||
ScanDeviceThread.Start();
|
||||
}
|
||||
|
||||
/// <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; } = 83;
|
||||
|
||||
private void ScanDeviceThreadMethod()
|
||||
{
|
||||
while (ThreadEnable)
|
||||
{
|
||||
//DiagnosticsTime.Reset();
|
||||
//DiagnosticsTime.Start();
|
||||
|
||||
GetRtPwMeter();
|
||||
|
||||
//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 void GetRtPwMeter()
|
||||
{
|
||||
try
|
||||
{
|
||||
OperateResultBytes1 = MeterMdDrive.Read($"s={MdStation};x=3;30", 10);
|
||||
if (OperateResultBytes1.IsSuccess)
|
||||
{
|
||||
Psum = MeterMdDrive.ByteTransform.TransSingle(OperateResultBytes1.Content, 0) * 0.4;//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}");
|
||||
|
||||
//通信正常
|
||||
MeterLinkState = true;
|
||||
LinkFaultCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
LinkFaultCount = LinkFaultCount + 1;
|
||||
if (LinkFaultCount > 5)
|
||||
{
|
||||
//通信正常
|
||||
MeterLinkState = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Info($"时间:{DateTime.Now.ToString()}-【ExitPeakValley】-{ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 连接失败
|
||||
/// </summary>
|
||||
public int LinkFaultCount { 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>
|
||||
/// 扫描设备线程数据
|
||||
/// </summary>
|
||||
public Thread ScanDeviceThread { set; get; }
|
||||
|
||||
/// <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; }
|
||||
|
||||
/// <summary>
|
||||
/// 总有功功率
|
||||
/// 总实时负载
|
||||
/// 总市电的电表
|
||||
/// 维持为0是目标
|
||||
/// 单位Kw
|
||||
/// </summary>
|
||||
public double Psum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 总无功功率
|
||||
/// </summary>
|
||||
public double Qsum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 功率因数
|
||||
/// </summary>
|
||||
public double cosQ { get; set; }
|
||||
|
||||
///// <summary>
|
||||
///// 频率
|
||||
///// </summary>
|
||||
//public double F { get; set; }
|
||||
|
||||
///// <summary>
|
||||
///// 输入总有功电度
|
||||
///// </summary>
|
||||
//public UInt32 TotalPWh { get; set; }
|
||||
|
||||
///// <summary>
|
||||
///// 输入总无功电度
|
||||
///// </summary>
|
||||
//public UInt32 TotalQWh { get; set; }
|
||||
|
||||
///// <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; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user