20230201_145_upperpc/InSituLaboratory/Common/DataParsing.cs

199 lines
6.5 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 InSituLaboratory.Entities;
using InSituLaboratory.Service;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InSituLaboratory.Common
{
/// <summary>
/// 数据解析
/// </summary>
public class DataParsing
{
//public DataParsing() { }
public DataParsingModel dataParsingModel = new DataParsingModel();
//获取当前程序运行路径
private string Save_Path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"数据记录\";
//记录第一包数据的创建时间
public static DateTime Record_firstCode = DateTime.Now;
//标志位头
string flag = "7f";
//应答流水号
string ResponseNum = "";
//应答ID
string ResponID = "";
//通用应答消息ID
string MessageID = "1000";
//crc
static byte nr_crc = 0;
/// <summary>
/// 数据解析
/// </summary>
/// <param name="byteList"></param>
public void ParsingData(List<byte> byteList)
{
//如果数组长度为0 舍弃
if (byteList.Count() == 0)
return;
//将报文中的内容截取出来 并保存至本地TXT文件内
string NR_TXT = "";
for (int i = 0; i < byteList.Count; i++)
{
NR_TXT += byteList[i].ToString("X2") + " ";
}
tools.AddLgoToTXT("原始数据报文.txt", Save_Path + System.DateTime.Now.ToString("yyyy_MM_dd") + @"\", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ---- " + NR_TXT + "\r\n");
//转义
int j = 0;
for (int i = 0; i < byteList.Count; i++)
{
if (byteList[i] == 0x7e && byteList[i + 1] == 0x01)
{
byteList[j] = 0x7e;
i++;
}
else if (byteList[i] == 0x7e && byteList[i + 1] == 0x02)
{
byteList[j] = 0x7f;
i++;
}
else
{
byteList[j] = byteList[i];
}
j++;
}
//定义转义后的byte数组
byte[] dataTemp = new byte[j];
for (int i = 0; i < j; i++)
{
dataTemp[i] = byteList[i];
}
//定义转义后的list数组
List<byte> dataNew = new List<byte>();
string Latter_TXT = "";
for (int i = 0; i < dataTemp.Length; i++)
{
dataNew.Add(dataTemp[i]);
Latter_TXT += dataTemp[i].ToString("X2") + " ";
}
tools.AddLgoToTXT("转义后数据报文.txt", Save_Path + System.DateTime.Now.ToString("yyyy_MM_dd") + @"\", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ---- " + Latter_TXT + "\r\n");
nr_crc = tools.CRC(dataNew.ToArray(), 0, dataNew.Count - 1);
///包头包尾 crc校验
if (((dataNew[0].ToString("X2") != flag) || (dataNew[byteList.Count - 1].ToString("X2")) != flag) || dataNew[14] != nr_crc)
return;
//通用应答 消息ID0x1000
if (MessageID == dataNew[1].ToString("X2") + dataNew[2].ToString("X2"))
{
dataParsingModel.CreateTime = System.DateTime.Now;
//应答流水号
dataParsingModel.ResponseNum = dataNew[9].ToString("X2") + dataNew[10].ToString("X2");
//应答ID
dataParsingModel.ResponseID = dataNew[11].ToString("X2") + dataNew[12].ToString("X2");
//结果
switch (dataNew[13].ToString("X2"))
{
case "0":
dataParsingModel.ResponseResult = "成功/确认";
break;
case "1":
dataParsingModel.ResponseResult = "失败/不存在";
break;
case "2":
dataParsingModel.ResponseResult = "消息错误";
break;
case "3":
dataParsingModel.ResponseResult = "不支持";
break;
default:
break;
}
//this.Insert<DataParsingModel>(dataParsingModel);
}
}
#region
/// <summary>
/// 转义
/// </summary>
/// <param name="tBuffer">没有头尾和校验的byte数组</param>
/// <param name="code">校验码</param>
/// <param name="flag">标志位</param>
/// <returns></returns>
public static List<byte> Transform(List<byte> tBuffer, byte code, byte flag)
{
int j = 0;
tBuffer.Add(code);
int length = tBuffer.ToArray().Length;
byte[] newAnswer = tBuffer.ToArray();
for (int i = 0; i < length; i++)
{
if (newAnswer[i] == 0x7e || newAnswer[i] == 0x7f)
{
j++;
}
}
byte[] newSendBuffer = new byte[length + j];
for (int i = 0; i < length; i++)
{
newSendBuffer[i] = newAnswer[i];
}
for (int i = 0; i < length + j; i++)
{
if (newSendBuffer[i] == 0x7e)
{
for (int k = length + j - 1; k > i + 1; k--)
{
newSendBuffer[k] = newSendBuffer[k - 1];
}
newSendBuffer[i + 1] = 0x01;
}
if (newSendBuffer[i] == 0x7f)
{
newSendBuffer[i] = 0x7e;
for (int k = length + j - 1; k > i + 1; k--)
{
newSendBuffer[k] = newSendBuffer[k - 1];
}
newSendBuffer[i + 1] = 0x02;
}
}
List<byte> buffer = new List<byte>();
buffer.Add(flag);
for (int i = 0; i < newSendBuffer.Length; i++)
{
buffer.Add(newSendBuffer[i]);
}
buffer.Add(flag);
return buffer;
}
#endregion
}
}