jy-plc/Services/HJ212_2017.cs

145 lines
5.0 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LangGuan.Services
{
public class HJ212_2017
{
/// <summary>
/// 数据帧头
/// </summary>
public string header { get; set; }
/// <summary>
/// 数据长度
/// </summary>
public string data_length { get; set; }
/// <summary>
/// 数据头
/// </summary>
public Dictionary<string, string> DATA_HEAD { get; set; }
/// <summary>
///
/// </summary>
public Dictionary<string, string> CP { get; set; }
/// <summary>
/// CRC校验
/// </summary>
public string CRC { get; set; }
/// <summary>
///
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public bool DecodeData(string Text)
{
try
{
if (Text.Length < 12 || !Text.StartsWith("##"))
{
Console.WriteLine("不是HJ212协议的报文!");
return false;
}
Console.WriteLine($"开始解码数据");
Text = Text.ToUpper().Replace(";CP=", "").Replace(" ", "");//有些厂家协议不很标准,有的大小写不一致,此处强制转大写字母
header = Text.Substring(0, 2);
data_length = Text.Substring(2, 4);
string[] data_0 = Text.Substring(6, Text.Length - 6).Split(new char[] { '&', '&' }, StringSplitOptions.RemoveEmptyEntries);
string[] data_1 = data_0[0].Split(new char[] { ';' });
DATA_HEAD = new Dictionary<string, string>();
for (int h = 0; h < data_1.Length; h++)
{
string[] d_1 = data_1[h].Split(new char[] { '=' });
DATA_HEAD.Add(d_1[0], d_1[1]);
}
string[] data_2 = data_0[1].Split(new char[] { ';' });
CP = new Dictionary<string, string>();
for (int i = 0; i < data_2.Length; i++)
{
string[] d_6 = data_2[i].Split(new char[] { ',' });
for (int j = 0; j < d_6.Length; j++)
{
string[] d_7 = d_6[j].Split(new char[] { '=' });
CP.Add(d_7[0].Replace("-RTD", ""), d_7[1]);
}
}
CRC = data_0[2];
return true;
}
catch (Exception ex)
{
//数据接收不完整
Console.WriteLine($" 解码失败err{ex},数据有问题," + Text);
//throw;
return false;
}
}
/// <summary>
/// 判断数据是否通过校验
/// </summary>
/// <param name="Text">原始数据</param>
/// <returns>是否通过</returns>
public bool Crc16(string Text)
{
try
{
string CRC = Text.Substring(Text.Length - 4, 4);
Text = Text.Substring(Text.IndexOf("QN"));
Text = Text.Substring(0, Text.Length - 4);
byte[] bytes = Encoding.ASCII.GetBytes(Text);
int crcRegister = 0xFFFF;
for (int i = 0; i < bytes.Length; i++)
{
crcRegister = (crcRegister >> 8) ^ bytes[i];
for (int j = 0; j < 8; j++)
{
int check = crcRegister & 0x0001;
crcRegister >>= 1;
if (check == 0x0001)
{
crcRegister ^= 0xA001;
}
}
}
string result = string.Format("{0:X}", crcRegister);//转十六进制
for (int i = result.Length; i < 4; i++)//补足 4 位
{
result = "0" + result;
}
//LogApi.WriteLog("计算校验码:" + result);
if (result == CRC)
{
return true;
}
else
{
Console.WriteLine("校验码有误," + CRC);
Console.WriteLine("待校验数据:" + Text);
Console.WriteLine("计算校验码:" + result);
//LogApi.WriteLog("校验码有误:" + CRC);
//LogApi.WriteLog("待校验数据:" + Text);
//LogApi.WriteLog("计算校验码:" + result);
return false;
}
}
catch (Exception)
{
Console.WriteLine("数据校验:数据有问题:" + Text);
//数据接收不完整
return false;
//throw;
}
}
}
}