协议解析
This commit is contained in:
parent
274b1a7a83
commit
f98212a550
@ -6,10 +6,12 @@ namespace JiShe.CollectBus.Common.Enums
|
|||||||
{
|
{
|
||||||
public enum CommandChunkEnum
|
public enum CommandChunkEnum
|
||||||
{
|
{
|
||||||
|
Len = 1,
|
||||||
C = 6,
|
C = 6,
|
||||||
A = 7,
|
A = 7,
|
||||||
AFN = 12,
|
AFN = 12,
|
||||||
SEQ = 13,
|
SEQ = 13,
|
||||||
FN = 17,
|
FN = 17,
|
||||||
|
Data = 18,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,6 +68,12 @@ namespace JiShe.CollectBus.Common.Extensions
|
|||||||
PRSEQ = prseqBin.BinToDec(),
|
PRSEQ = prseqBin.BinToDec(),
|
||||||
TpV = tpV
|
TpV = tpV
|
||||||
};
|
};
|
||||||
|
case CommandChunkEnum.Data:
|
||||||
|
var lenIndex = (int)CommandChunkEnum.Len;
|
||||||
|
var lenBin = (hexStringList[lenIndex + 1]+hexStringList[lenIndex]).HexToBin();
|
||||||
|
var len = lenBin.Remove(lenBin.Length - 2).BinToDec();
|
||||||
|
var dataHexList = hexStringList.Skip(FixedLength).Take(len + hearderLen - FixedLength).ToList();
|
||||||
|
return dataHexList;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException(nameof(chunk), chunk, null);
|
throw new ArgumentOutOfRangeException(nameof(chunk), chunk, null);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ using JiShe.CollectBus.Common.Models;
|
|||||||
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
||||||
using JiShe.CollectBus.Protocol.Contracts.Models;
|
using JiShe.CollectBus.Protocol.Contracts.Models;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
|
namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
|
||||||
{
|
{
|
||||||
@ -103,7 +104,151 @@ namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 确认或否认
|
||||||
|
/// </summary>
|
||||||
|
/// <param name=""></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task AnswerAsync(MessageReceivedHeartbeatEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
|
||||||
|
{
|
||||||
|
var hexStringList = messageReceivedEvent.MessageHexString.StringToPairs();
|
||||||
|
var fn = hexStringList.GetAnalyzeValue(CommandChunkEnum.FN);
|
||||||
|
//1:全部确认
|
||||||
|
//2:全部否认
|
||||||
|
//3:按数据单元表示确认和否认
|
||||||
|
//4 硬件安全认证错误应答
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 电表档案解析
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="messageReceivedEvent"></param>
|
||||||
|
/// <param name="sendAction"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task AmmeterParameterAsync(MessageReceivedHeartbeatEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
|
||||||
|
{
|
||||||
|
var hexStringList = messageReceivedEvent.MessageHexString.StringToPairs();
|
||||||
|
var hexDatas = (List<string>)hexStringList.GetAnalyzeValue(CommandChunkEnum.Data);
|
||||||
|
|
||||||
|
var meterList = new List<AmmeterParameter>();
|
||||||
|
var count = (hexDatas[1] + hexDatas[0]).HexToDec();
|
||||||
|
//if (2 + count * 27 != hexDatas.Count - pWLen - tPLen - 2)
|
||||||
|
// return;
|
||||||
|
var index = 2;//数量
|
||||||
|
for (int i = 1; i <= count; i++)
|
||||||
|
{
|
||||||
|
var meterNumber = $"{hexDatas[index + 1]}{hexDatas[index]}".HexToDec();
|
||||||
|
index += 2;
|
||||||
|
|
||||||
|
var pn = $"{hexDatas[index + 1]}{hexDatas[index]}".HexToDec();
|
||||||
|
index += 2;
|
||||||
|
|
||||||
|
var baudRateAndPortBin = hexDatas[index].HexToBin().PadLeft(8, '0');
|
||||||
|
var baudRate = baudRateAndPortBin.Substring(0, 3).BinToDec();
|
||||||
|
var port = baudRateAndPortBin.Substring(3, 5).BinToDec();
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
var protocolType = (CommunicationProtocolType)hexDatas[index].HexToDec();
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
var addressHexList = hexDatas.Skip(index).Take(6).ToList();
|
||||||
|
addressHexList.Reverse();
|
||||||
|
var address = string.Join("", addressHexList);
|
||||||
|
index += 6;
|
||||||
|
|
||||||
|
var pwdHexList = hexDatas.Skip(index).Take(6).ToList();
|
||||||
|
pwdHexList.Reverse();
|
||||||
|
var password = string.Join("", pwdHexList.Take(3).ToList());
|
||||||
|
index += 6;
|
||||||
|
|
||||||
|
var rateNumberBin = hexDatas[index].HexToBin().PadLeft(8, '0');
|
||||||
|
var rateNumber = rateNumberBin.Substring(4).BinToDec();
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
var intBitAndDecBitNumberBin = hexDatas[index].HexToBin().PadLeft(8, '0');
|
||||||
|
var intBitNumber = intBitAndDecBitNumberBin.Substring(4, 2).BinToDec() + 4;
|
||||||
|
var decBitNumber = intBitAndDecBitNumberBin.Substring(6, 2).BinToDec() + 1;
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
// hexDatas.GetRange()
|
||||||
|
var collectorAddressHexList = hexDatas.Skip(index).Take(6).ToList();
|
||||||
|
collectorAddressHexList.Reverse();
|
||||||
|
var collectorAddress = string.Join("", collectorAddressHexList);
|
||||||
|
index += 6;
|
||||||
|
|
||||||
|
var userClassNumberBin = hexDatas[index].HexToBin().PadLeft(8, '0');
|
||||||
|
var userClass = userClassNumberBin.Substring(0, 4).BinToDec();
|
||||||
|
var userSubClass = userClassNumberBin.Substring(4, 4).BinToDec();
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
meterList.Add(new AmmeterParameter()
|
||||||
|
{
|
||||||
|
Pn = pn,
|
||||||
|
BaudRate = baudRate,
|
||||||
|
Port = port,
|
||||||
|
ProtocolType = protocolType,
|
||||||
|
Address = address,
|
||||||
|
Password = password,
|
||||||
|
RateNumber = rateNumber,
|
||||||
|
IntegerBitNumber = intBitNumber,
|
||||||
|
DecimalBitNumber = decBitNumber,
|
||||||
|
CollectorAddress = collectorAddress,
|
||||||
|
UserCategoryNumber = userClass,
|
||||||
|
UserSubclassNumber = userSubClass,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前正向有功电能示值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="messageReceivedEvent">报文</param>
|
||||||
|
/// <param name="sendAction">发送委托</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task ActivePowerIndicationAsync(MessageReceivedHeartbeatEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
|
||||||
|
{
|
||||||
|
var hexStringList = messageReceivedEvent.MessageHexString.StringToPairs();
|
||||||
|
var hexDatas = (List<string>)hexStringList.GetAnalyzeValue(CommandChunkEnum.Data);
|
||||||
|
|
||||||
|
var minutes = Convert.ToInt32(hexDatas[0]); // 获取当前分钟数
|
||||||
|
var hours = Convert.ToInt32(hexDatas[1]); // 获取当前小时数
|
||||||
|
var day = Convert.ToInt32(hexDatas[2]); // 获取当前日期的日数
|
||||||
|
var month = Convert.ToInt32(hexDatas[3]); // 获取当前月份
|
||||||
|
var year = Convert.ToInt32(hexDatas[4]); // 获取当前日期的年份
|
||||||
|
|
||||||
|
var rateNumber = Convert.ToInt32(hexDatas[5]);
|
||||||
|
var kwhTotal = hexDatas.Skip(5).Take(5).ToList();
|
||||||
|
var kwhList = new List<decimal>();
|
||||||
|
var index = 11;
|
||||||
|
for (int i = 0; i < rateNumber; i++)
|
||||||
|
{
|
||||||
|
var kwhHexList = hexDatas.Skip(11).Take(5).ToList();
|
||||||
|
kwhHexList.Reverse();
|
||||||
|
var integerStr = $"{kwhHexList.Take(0)}{kwhHexList.Take(1)}{kwhHexList.Take(2)}";
|
||||||
|
var decimalValStr = $"{kwhHexList[3]}{kwhHexList[4]}";
|
||||||
|
var val = decimal.Parse($"{integerStr}{decimalValStr}");
|
||||||
|
kwhList.Add(val);
|
||||||
|
index += 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日冻结正向有功电能示值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="messageReceivedEvent"></param>
|
||||||
|
/// <param name="sendAction"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task DailyFrozenAsync(MessageReceivedHeartbeatEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
|
||||||
|
{
|
||||||
|
var hexStringList = messageReceivedEvent.MessageHexString.StringToPairs();
|
||||||
|
var hexDatas = (List<string>)hexStringList.GetAnalyzeValue(CommandChunkEnum.Data);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
//TODO:F17 当前总加有功功率 附录A.2 kw 2字节
|
||||||
|
//F18 当前总加无功功率 附录A.2 kvar 2字节
|
||||||
|
//F25 161
|
||||||
#region 通用解析
|
#region 通用解析
|
||||||
|
|
||||||
public byte[] GetCommandBytes(ReqParameter reqParameter, List<string>? dataUnit = null)
|
public byte[] GetCommandBytes(ReqParameter reqParameter, List<string>? dataUnit = null)
|
||||||
|
|||||||
@ -32,7 +32,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Models
|
|||||||
public int Fn { get; set; }
|
public int Fn { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MeterParameter
|
public class AmmeterParameter
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 测量点号 0~2040 为0被删除
|
/// 测量点号 0~2040 为0被删除
|
||||||
|
|||||||
@ -48,6 +48,8 @@ namespace JiShe.CollectBus.Protocol
|
|||||||
await base.HeartbeatAsync(messageReceivedEvent, SendAction);
|
await base.HeartbeatAsync(messageReceivedEvent, SendAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region 上行命令
|
#region 上行命令
|
||||||
|
|
||||||
//68
|
//68
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user