20240301_JSEQ_upperpc/JiangsuEarthquake/JiangsuEarthquake/Models/VolCurCollectorModel.cs
春风过客 0b9514a317 新增功能:
1 完成升压站单路直流电压电流组合采集器报警设置功能指令、通信编写,并进行串口通信测试;

新增bug:
1 串口通信内部存在bug,导致两个串口内部有关联,从而导致两个串口无法同时通信;
2024-04-18 19:04:33 +08:00

148 lines
6.3 KiB
C#
Raw 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 Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JiangsuEarthquake.Models
{
public class VolCurCollectorModel
{
private byte fromDeviceAddress { get; set; } //从设备地址
private byte functionCode { get; set; } //功能码
private byte[] startingRegisterAddress { get; set; } //起始寄存器地址
private byte[] registerNumber { get; set; } //寄存器个数
private byte[] checkCodeCRC { get; set; } //校验码
private List<byte> checkByte { get; set; } //校验码Byte
private byte dataAreaBytes { get; set; } //数据区字节数
private List<byte> dataArea { get; set; } //数据区
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)
{
if (byteList.Count == 0)
return;
//目前为读取寄存器 0000H-000FH即所有数据
//原始数据存储
string sql = $"insert into boosterstationoriginaldata(StationID,RecordTime,OriginalData) values('1','{DateTime.Now}','{BitConverter.ToString(byteList.ToArray())}');";
DBHelper.ExecuteNonQuery(sql, 1);
if (byteList[1] == 0x03) //查询从设备寄存器内容
{
if (byteList.Count < 37)
return;
//长度校验
dataAreaBytes = byteList[2];
if (dataAreaBytes + 5 != byteList.Count)
return;
//CRC校验
for (int i = 0; i < byteList.Count - 2; i++)
checkByte.Add(byteList[i]);
checkCodeCRC = Tools.CRCCalc(checkByte.ToArray());
//CRC校验失败
if (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('1','{DateTime.Now}','{voltage}','{current}','{power}','{posEleDegree}'," +
$"'{revEleDegree}','{uVoltage}','{uCurrent}','{relayStatus}','{highResVoltage}'," +
$"'{highResCurrent}','{forAmpereHour}','{revAmpereHour}');";
DBHelper.ExecuteNonQuery(sql, 1);
}
else if(byteList[1] == 0x06) //对从设备单个寄存器置数
{
//长度校验
if (byteList.Count != 8)
return;
//CRC校验
for (int i = 0; i < byteList.Count - 2; i++)
checkByte.Add(byteList[i]);
checkCodeCRC = Tools.CRCCalc(checkByte.ToArray());
//CRC校验失败
if (checkCodeCRC != new byte[2] { byteList[byteList.Count - 2], byteList[byteList.Count - 1] })
return;
//需要判断发送内容和接收内容是否相同,相同则设置成功
//根据寄存器地址判断接收的到信息是对什么进行设置的
if (byteList[2] ==0x01 && byteList[3]==0x2C) //继电器报警参数
{
}
else if(byteList[2] == 0x01 && byteList[3] == 0x2D) //继电器报警阀值 1
{
}
else if(byteList[2] == 0x01 && byteList[3] == 0x2E) //继电器报警阀值 2
{
}
else if(byteList[2] == 0x01 && byteList[3] == 0x2F) //继电器报警功能
{
}
else if (byteList[2] == 0x01 && byteList[3] == 0x30) //继电器输出
{
}
}
}
}
}