20240301_JSEQ_upperpc/JiangsuEarthquake_test/JiangsuEarthquake/Models/VolCurCollectorModel2.cs

258 lines
13 KiB
C#
Raw Normal View History

using JiangsuEarthquake.Common;
using JiangsuEarthquake.DataAccess;
using JiangsuEarthquake.ViewModels;
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 VolCurCollectorModel2
{
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 boosterstation_state_originaldata(StationID,RecordTime,OriginalData) values('{id}','{DateTime.Now}','{BitConverter.ToString(byteList.ToArray())}');";
DBHelper.ExecuteNonQuery(sql, 1);
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.CRCCalcRev(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 boosterstation_state(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.CRCCalcRev(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 (Tools.CompareByte(byteList.ToArray(), MainWindow.mainViewModel.AlarmParaRecv))
//{
// MainWindow.mainViewModel.AlarmSetBtnIsReceived1 = true;
//}
}
else if (byteList[2] == 0x01 && byteList[3] == 0x2D) //继电器报警阀值 1
{
////下限报警、上限报警
//if (Tools.CompareByte(byteList.ToArray(), MainWindow.mainViewModel.AlarmThresholdRecv))
//{
// MainWindow.mainViewModel.AlarmSetBtnIsReceived3 = true;
// //若均为true则说明报警设置成功
// if (MainWindow.mainViewModel.AlarmSetBtnIsReceived1 && MainWindow.mainViewModel.AlarmSetBtnIsReceived2 && MainWindow.mainViewModel.AlarmSetBtnIsReceived3)
// {
// App.Current.Dispatcher.Invoke(() =>
// {
// MainWindow.mainViewModel.AlarmSetButtonIsEnabled = true;
// MainWindow.mainViewModel.timerAlarmSet.Stop();
// MainWindow.mainViewModel.AlarmSetMessage = "报警设置成功!";
// MainWindow.mainViewModel.AlarmSetMessageForeground = new SolidColorBrush(Colors.Green);
// });
// }
//}
}
else if (byteList[2] == 0x01 && byteList[3] == 0x2E) //继电器报警阀值 2
{
}
else if (byteList[2] == 0x01 && byteList[3] == 0x2F) //继电器报警功能
{
//if (Tools.CompareByte(byteList.ToArray(), MainWindow.mainViewModel.AlarmFuncRecv))
//{
// MainWindow.mainViewModel.AlarmSetBtnIsReceived2 = true;
//}
}
else if (byteList[2] == 0x01 && byteList[3] == 0x30) //继电器输出
{
//if (Tools.CompareByte(byteList.ToArray(), MainWindow.mainViewModel.RelayActivationRecv))
//{
// App.Current.Dispatcher.Invoke(() =>
// {
// // 更新UI的代码
// MainWindow.mainViewModel.RelayActivationBtnIsReceived = true;
// MainWindow.mainViewModel.RelayActivationBtnIsEnabled = true;
// MainWindow.mainViewModel.timerRelayActivation.Stop();
// MainWindow.mainViewModel.RelayControlMsg = "继电开启成功!";
// MainWindow.mainViewModel.RelayControlMsgForeground = new SolidColorBrush(Colors.Green);
// });
//}
//if (Tools.CompareByte(byteList.ToArray(), MainWindow.mainViewModel.RelayShutdownRecv))
//{
// App.Current.Dispatcher.Invoke(() =>
// {
// // 更新UI的代码
// MainWindow.mainViewModel.RelayShutdownBtnIsReceived = true;
// MainWindow.mainViewModel.RelayShutdownBtnIsEnabled = true;
// MainWindow.mainViewModel.timerRelayShutdown.Stop();
// MainWindow.mainViewModel.RelayControlMsg = "继电关闭成功!";
// MainWindow.mainViewModel.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.CRCCalcRev(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 (Tools.CompareByte(byteList.ToArray(), MainWindow.mainViewModel.AlarmThresholdRecv))
//{
// MainWindow.mainViewModel.AlarmSetBtnIsReceived3 = true;
// //若均为true则说明报警设置成功
// if (MainWindow.mainViewModel.AlarmSetBtnIsReceived1 && MainWindow.mainViewModel.AlarmSetBtnIsReceived2 && MainWindow.mainViewModel.AlarmSetBtnIsReceived3)
// {
// App.Current.Dispatcher.Invoke(() =>
// {
// // 更新UI的代码
// MainWindow.mainViewModel.AlarmSetButtonIsEnabled = true;
// MainWindow.mainViewModel.timerAlarmSet.Stop();
// MainWindow.mainViewModel.AlarmSetMessage = "报警设置成功!";
// MainWindow.mainViewModel.AlarmSetMessageForeground = new SolidColorBrush(Colors.Green);
// });
// }
//}
}
}
}
}
}