20240301_JSEQ_upperpc/JiangsuEarthquakeJM/JiangsuEarthquake/Models/VolCurCollectorModel.cs
春风过客 d7f51483a7 新增功能:
1 将地震仪数据读取功能单独成一个页面;
2024-05-13 18:51:05 +08:00

382 lines
22 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using JiangsuEarthquake.Common;
using JiangsuEarthquake.DataAccess;
using JiangsuEarthquake.ViewModels;
using JiangsuEarthquake.Views.UserControls;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace JiangsuEarthquake.Models
{
public class VolCurCollectorModel
{
private byte fromDeviceAddress { get; set; } //从设备地址
private byte functionCode { get; set; } //功能码
private byte[] startingRegisterAddress { get; set; } = new byte[2]; //起始寄存器地址
private byte[] registerNumber { get; set; } = new byte[2]; //寄存器个数
private byte[] checkCodeCRC { get; set; } = new byte[2]; //校验码
private List<byte> checkByte { get; set; } = new List<byte>(); //校验码Byte
private byte dataAreaBytes { get; set; } //数据区字节数
private List<byte> dataArea { get; set; } = new List<byte>(); //数据区
private float voltage { get; set; }
private float current { get; set; }
private float power { get; set; }
private float posEleDegree { get; set; }
private float revEleDegree { get; set; }
private float uVoltage { get; set; }
private float uCurrent { get; set; }
private int relayStatus { get; set; }
private float highResVoltage { get; set; }
private float highResCurrent { get; set; }
private float forAmpereHour { get; set; }
private float revAmpereHour { get; set; }
public void ParsingData(List<byte> byteList, int id)
{
if (byteList.Count == 0)
return;
//原始数据存储
string sql = $"insert into boosterstationoriginaldata(StationID,RecordTime,OriginalData) values('{id}','{DateTime.Now}','{BitConverter.ToString(byteList.ToArray())}');";
DBHelper.ExecuteNonQuery(sql, 1);
checkByte.Clear();
//CRC校验
for (int i = 0; i < byteList.Count - 2; i++)
checkByte.Add(byteList[i]);
checkCodeCRC = Tools.CRCCalc(checkByte.ToArray());
//CRC校验失败
if (!Tools.CompareByte(checkCodeCRC, new byte[2] { byteList[byteList.Count - 2], byteList[byteList.Count - 1] }))
return;
if (byteList[1] == 0x03) //查询从设备寄存器内容
{
//目前为读取寄存器 0000H-000FH即所有数据
if (byteList.Count < 37)
return;
//长度校验
dataAreaBytes = byteList[2];
if (dataAreaBytes + 5 != byteList.Count)
return;
//checkByte.Clear();
////CRC校验
//for (int i = 0; i < byteList.Count - 2; i++)
// checkByte.Add(byteList[i]);
//checkCodeCRC = Tools.CRCCalc(checkByte.ToArray());
////CRC校验失败
//if (!Tools.CompareByte(checkCodeCRC, new byte[2] { byteList[byteList.Count - 2], byteList[byteList.Count - 1] }))
// return;
//获取电压电流的量程;
int VoltageRange = int.Parse(Tools.GetAppSetting("SYZVoltageRange"));
int CurrentRange = int.Parse(Tools.GetAppSetting("SYZCurrentRange"));
voltage = BitConverter.ToInt16(new byte[2] { byteList[3], byteList[4] }) / 10000f * VoltageRange;
current = BitConverter.ToInt16(new byte[2] { byteList[5], byteList[6] }) / 10000f * CurrentRange;
power = BitConverter.ToInt16(new byte[2] { byteList[7], byteList[8] }) / 10000f * VoltageRange * CurrentRange;
posEleDegree = BitConverter.ToUInt32(new byte[4] { byteList[9], byteList[10], byteList[11], byteList[12] }) * VoltageRange * CurrentRange / (1000 * 3600);
revEleDegree = BitConverter.ToUInt32(new byte[4] { byteList[13], byteList[14], byteList[15], byteList[16] }) * VoltageRange * CurrentRange / (1000 * 3600);
uVoltage = BitConverter.ToUInt16(new byte[2] { byteList[17], byteList[18] }) / 10000f * VoltageRange;
uCurrent = BitConverter.ToUInt16(new byte[2] { byteList[19], byteList[20] }) / 10000f * VoltageRange;
relayStatus = BitConverter.ToUInt16(new byte[2] { byteList[21], byteList[22] });
highResVoltage = BitConverter.ToInt16(new byte[2] { byteList[23], byteList[24] }) / 50000f * VoltageRange;
highResCurrent = BitConverter.ToInt16(new byte[2] { byteList[25], byteList[26] }) / 50000f * CurrentRange;
forAmpereHour = BitConverter.ToUInt32(new byte[4] { byteList[27], byteList[28], byteList[29], byteList[30] }) * CurrentRange / 3600;
revAmpereHour = BitConverter.ToUInt32(new byte[4] { byteList[31], byteList[32], byteList[33], byteList[34] }) * CurrentRange / 3600;
sql = $"insert into boosterstationstate(StationID,RecordTime,In_Vol,In_Cur,Power,PosEleDegree," +
$"RevEleDegree,uVoltage,uCurrent,RelayStatus,HighResVoltage,HighResCurrent,ForAmpereHour," +
$"RevAmpereHour) values('{id}','{DateTime.Now}','{voltage}','{current}','{power}','{posEleDegree}'," +
$"'{revEleDegree}','{uVoltage}','{uCurrent}','{relayStatus}','{highResVoltage}'," +
$"'{highResCurrent}','{forAmpereHour}','{revAmpereHour}');";
DBHelper.ExecuteNonQuery(sql, 1);
App.Current.Dispatcher.Invoke(() =>
{
if (id == 1)
{
MainViewModel.boosterStationStateDataViewModel1.AskDataBtnIsReceived = true;
MainViewModel.boosterStationStateDataViewModel1.AskDataBtnIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel1.timerAskData.Stop();
MainViewModel.boosterStationStateDataViewModel1.AskDataMsgVisibility = Visibility.Visible;
MainViewModel.boosterStationStateDataViewModel1.AskDataMsg = "请求数据成功!";
MainViewModel.boosterStationStateDataViewModel1.timerAskDataMsgCollapse.Start();
MainViewModel.boosterStationStateDataViewModel1.AskDataMsgForeground = new SolidColorBrush(Colors.Green);
}
else
{
MainViewModel.boosterStationStateDataViewModel2.AskDataBtnIsReceived = true;
MainViewModel.boosterStationStateDataViewModel2.AskDataBtnIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel2.timerAskData.Stop();
MainViewModel.boosterStationStateDataViewModel2.AskDataMsgVisibility = Visibility.Visible;
MainViewModel.boosterStationStateDataViewModel2.AskDataMsg = "请求数据成功!";
MainViewModel.boosterStationStateDataViewModel2.timerAskDataMsgCollapse.Start();
MainViewModel.boosterStationStateDataViewModel2.AskDataMsgForeground = new SolidColorBrush(Colors.Green);
}
});
}
else if (byteList[1] == 0x06) //对从设备单个寄存器置数
{
//长度校验
if (byteList.Count != 8)
return;
//checkByte.Clear();
////CRC校验
//for (int i = 0; i < byteList.Count - 2; i++)
// checkByte.Add(byteList[i]);
//checkCodeCRC = Tools.CRCCalc(checkByte.ToArray());
//byte[] code = new byte[2] { byteList[byteList.Count - 2], byteList[byteList.Count - 1] };
////CRC校验失败
//if (!Tools.CompareByte(checkCodeCRC, code))
// return;
//需要判断发送内容和接收内容是否相同,相同则设置成功
//根据寄存器地址判断接收的到信息是对什么进行设置的
if (byteList[2] == 0x01 && byteList[3] == 0x2C) //继电器报警参数
{
if (id == 1)
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel1.AlarmParaRecv))
{
MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived1 = true;
}
}
else
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel2.AlarmParaRecv))
{
MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived1 = true;
}
}
}
else if (byteList[2] == 0x01 && byteList[3] == 0x2D) //继电器报警阀值 1
{
//下限报警、上限报警
if (id == 1)
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel1.AlarmThresholdRecv))
{
MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived3 = true;
//若均为true则说明报警设置成功
if (MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived1 && MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived2 && MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived3)
{
App.Current.Dispatcher.Invoke(() =>
{
MainViewModel.boosterStationStateDataViewModel1.AlarmSetButtonIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel1.timerAlarmSet.Stop();
MainViewModel.boosterStationStateDataViewModel1.AlarmSetMessage = "报警设置成功!";
MainViewModel.boosterStationStateDataViewModel2.timerAlarmSetMsgCollapse.Start();
MainViewModel.boosterStationStateDataViewModel1.AlarmSetMessageForeground = new SolidColorBrush(Colors.Green);
});
}
}
}
else
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel2.AlarmThresholdRecv))
{
MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived3 = true;
//若均为true则说明报警设置成功
if (MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived1 && MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived2 && MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived3)
{
App.Current.Dispatcher.Invoke(() =>
{
MainViewModel.boosterStationStateDataViewModel2.AlarmSetButtonIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel2.timerAlarmSet.Stop();
MainViewModel.boosterStationStateDataViewModel2.AlarmSetMessage = "报警设置成功!";
MainViewModel.boosterStationStateDataViewModel2.timerAlarmSetMsgCollapse.Start();
MainViewModel.boosterStationStateDataViewModel2.AlarmSetMessageForeground = new SolidColorBrush(Colors.Green);
});
}
}
}
}
else if (byteList[2] == 0x01 && byteList[3] == 0x2E) //继电器报警阀值 2
{
}
else if (byteList[2] == 0x01 && byteList[3] == 0x2F) //继电器报警功能
{
if (id == 1)
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel1.AlarmFuncRecv))
{
MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived2 = true;
}
}
else
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel2.AlarmFuncRecv))
{
MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived2 = true;
}
}
}
else if (byteList[2] == 0x01 && byteList[3] == 0x30) //继电器输出
{
if (id == 1)
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel1.RelayActivationRecv))
{
App.Current.Dispatcher.Invoke(() =>
{
// 更新UI的代码
MainViewModel.boosterStationStateDataViewModel1.RelayActivationBtnIsReceived = true;
MainViewModel.boosterStationStateDataViewModel1.RelayActivationBtnIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel1.timerRelayActivation.Stop();
MainViewModel.boosterStationStateDataViewModel1.RelayControlMsgVisibility = Visibility.Visible;
MainViewModel.boosterStationStateDataViewModel1.RelayControlMsg = "继电开启成功!";
MainViewModel.boosterStationStateDataViewModel1.timerRelayControl.Start();
MainViewModel.boosterStationStateDataViewModel1.RelayControlMsgForeground = new SolidColorBrush(Colors.Green);
});
}
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel1.RelayShutdownRecv))
{
App.Current.Dispatcher.Invoke(() =>
{
// 更新UI的代码
MainViewModel.boosterStationStateDataViewModel1.RelayShutdownBtnIsReceived = true;
MainViewModel.boosterStationStateDataViewModel1.RelayShutdownBtnIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel1.timerRelayShutdown.Stop();
MainViewModel.boosterStationStateDataViewModel1.RelayControlMsgVisibility = Visibility.Visible;
MainViewModel.boosterStationStateDataViewModel1.RelayControlMsg = "继电关闭成功!";
MainViewModel.boosterStationStateDataViewModel1.timerRelayControl.Start();
MainViewModel.boosterStationStateDataViewModel1.RelayControlMsgForeground = new SolidColorBrush(Colors.Green);
});
}
}
else
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel2.RelayActivationRecv))
{
App.Current.Dispatcher.Invoke(() =>
{
// 更新UI的代码
MainViewModel.boosterStationStateDataViewModel2.RelayActivationBtnIsReceived = true;
MainViewModel.boosterStationStateDataViewModel2.RelayActivationBtnIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel2.timerRelayActivation.Stop();
MainViewModel.boosterStationStateDataViewModel2.RelayControlMsgVisibility = Visibility.Visible;
MainViewModel.boosterStationStateDataViewModel2.RelayControlMsg = "继电开启成功!";
MainViewModel.boosterStationStateDataViewModel2.timerRelayControl.Start();
MainViewModel.boosterStationStateDataViewModel2.RelayControlMsgForeground = new SolidColorBrush(Colors.Green);
});
}
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel2.RelayShutdownRecv))
{
App.Current.Dispatcher.Invoke(() =>
{
// 更新UI的代码
MainViewModel.boosterStationStateDataViewModel2.RelayShutdownBtnIsReceived = true;
MainViewModel.boosterStationStateDataViewModel2.RelayShutdownBtnIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel2.timerRelayShutdown.Stop();
MainViewModel.boosterStationStateDataViewModel2.RelayControlMsgVisibility = Visibility.Visible;
MainViewModel.boosterStationStateDataViewModel2.RelayControlMsg = "继电关闭成功!";
MainViewModel.boosterStationStateDataViewModel2.timerRelayControl.Start();
MainViewModel.boosterStationStateDataViewModel2.RelayControlMsgForeground = new SolidColorBrush(Colors.Green);
});
}
}
}
}
else if (byteList[1] == 0x10) //对从设备多个寄存器置数
{
//长度校验
if (byteList.Count != 8)
return;
//checkByte.Clear();
////CRC校验
//for (int i = 0; i < byteList.Count - 2; i++)
// checkByte.Add(byteList[i]);
//checkCodeCRC = Tools.CRCCalc(checkByte.ToArray());
////CRC校验失败
//if (!Tools.CompareByte(checkCodeCRC, new byte[2] { byteList[byteList.Count - 2], byteList[byteList.Count - 1] }))
// return;
if (byteList[2] == 0x01 && byteList[3] == 0x2D) //继电器报警阀值 1
{
//上下限报警、区间报警
if (id == 1)
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel1.AlarmThresholdRecv))
{
MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived3 = true;
//若均为true则说明报警设置成功
if (MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived1 && MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived2 && MainViewModel.boosterStationStateDataViewModel1.AlarmSetBtnIsReceived3)
{
App.Current.Dispatcher.Invoke(() =>
{
// 更新UI的代码
MainViewModel.boosterStationStateDataViewModel1.AlarmSetButtonIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel1.timerAlarmSet.Stop();
MainViewModel.boosterStationStateDataViewModel1.AlarmSetMessage = "报警设置成功!";
MainViewModel.boosterStationStateDataViewModel1.AlarmSetMessageForeground = new SolidColorBrush(Colors.Green);
MainViewModel.boosterStationStateDataViewModel2.timerAlarmSetMsgCollapse.Start();
});
}
}
}
else
{
if (Tools.CompareByte(byteList.ToArray(), MainViewModel.boosterStationStateDataViewModel2.AlarmThresholdRecv))
{
MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived3 = true;
//若均为true则说明报警设置成功
if (MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived1 && MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived2 && MainViewModel.boosterStationStateDataViewModel2.AlarmSetBtnIsReceived3)
{
App.Current.Dispatcher.Invoke(() =>
{
// 更新UI的代码
MainViewModel.boosterStationStateDataViewModel2.AlarmSetButtonIsEnabled = true;
MainViewModel.boosterStationStateDataViewModel2.timerAlarmSet.Stop();
MainViewModel.boosterStationStateDataViewModel2.AlarmSetMessage = "报警设置成功!";
MainViewModel.boosterStationStateDataViewModel2.AlarmSetMessageForeground = new SolidColorBrush(Colors.Green);
MainViewModel.boosterStationStateDataViewModel2.timerAlarmSetMsgCollapse.Start();
});
}
}
}
}
}
}
}
}