178 lines
6.6 KiB
C#
178 lines
6.6 KiB
C#
using HslCommunication.ModBus;
|
|
using NLog;
|
|
using OrpaonEMS.App.Com;
|
|
using Prism.Mvvm;
|
|
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 GridDataService:BindableBase
|
|
{
|
|
/// <summary>
|
|
/// 网络的开关和功率仪表数据服务
|
|
/// 当前的逻辑定制的,放到一个模块里面,方便后期摘除
|
|
/// </summary>
|
|
public GridDataService()
|
|
{
|
|
//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>
|
|
/// 扫描线程使能
|
|
/// </summary>
|
|
public bool ThreadEnable { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// 扫描设备线程数据
|
|
/// </summary>
|
|
public Thread ScanDeviceThread { set; get; }
|
|
|
|
/// <summary>
|
|
/// 仪表数据驱动
|
|
/// </summary>
|
|
public ModbusRtu MeterMdDrive { set; get; }
|
|
|
|
/// <summary>
|
|
/// 电表和开关1
|
|
/// </summary>
|
|
public MeterSwitchCell MeterSwitch1 { get; set; } = new MeterSwitchCell();
|
|
|
|
/// <summary>
|
|
/// 电表和开关2
|
|
/// </summary>
|
|
public MeterSwitchCell MeterSwitch2 { get; set; } = new MeterSwitchCell();
|
|
|
|
/// <summary>
|
|
/// 电表和开关3
|
|
/// </summary>
|
|
public MeterSwitchCell MeterSwitch3 { get; set; } = new MeterSwitchCell();
|
|
|
|
/// <summary>
|
|
/// 电表和开关4
|
|
/// </summary>
|
|
public MeterSwitchCell MeterSwitch4 { get; set; } = new MeterSwitchCell();
|
|
|
|
|
|
private void ScanDeviceThreadMethod()
|
|
{
|
|
while (ThreadEnable)
|
|
{
|
|
//DiagnosticsTime.Reset();
|
|
//DiagnosticsTime.Start();
|
|
|
|
//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}");
|
|
//}
|
|
|
|
//DiagnosticsTime.Stop();
|
|
|
|
//ScanRtTimeinfo = $"电表:{DiagnosticsTime.Elapsed.TotalMilliseconds.ToString()}";
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 断开2号开关
|
|
/// </summary>
|
|
public bool SetSolarByMeter2Off()
|
|
{
|
|
if (MeterSwitch2.SwtichState==Model.Enums.SwitchEm.On)
|
|
{
|
|
var result = MeterMdDrive.Write("", false);
|
|
if (result.IsSuccess)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 闭合2号开关
|
|
/// </summary>
|
|
public bool SetSolarByMeter2On()
|
|
{
|
|
if (MeterSwitch2.SwtichState == Model.Enums.SwitchEm.Off)
|
|
{
|
|
var result = MeterMdDrive.Write("", true);
|
|
if (result.IsSuccess)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|