diff --git a/JiShe.CollectBus.Common/DataConvert.cs b/JiShe.CollectBus.Common/DataConvert.cs new file mode 100644 index 0000000..71d2fcf --- /dev/null +++ b/JiShe.CollectBus.Common/DataConvert.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace JiShe.CollectBus.Common +{ + public class DataConvert + { + /// + /// 十进制转二进制 + /// + /// 十进制数 + /// + public static string DecToBin(int decimalNumber) + { + var binaryString = Convert.ToString(decimalNumber, 2); + return binaryString; + } + + /// + /// 二进制转十六进制 + /// + /// + /// + public static string BinToHex(string binaryString) + { + var decimalNumber = Convert.ToInt32(binaryString, 2);// 将二进制字符串转换为整数 + var hexString = Convert.ToString(decimalNumber, 16); //decimalNumber.ToString("X"); // 将整数转换为十六进制字符串 + return hexString; + } + + /// + /// 十进制转十六进制 + /// + /// + /// + public static string DecToHex(int decimalNumber) + { + var hexString = decimalNumber.ToString("X"); + return hexString; + } + + /// + /// 二进制转十进制 + /// + /// + /// + public static int BinToDec(string binaryString) + { + var decimalNumber = Convert.ToInt32(binaryString, 2); + return decimalNumber; + } + + /// + /// 十六进制转十进制 + /// + /// + /// + public static int HexToDec(string hexString) + { + var decimalNumber = Convert.ToInt32(hexString, 16); + return decimalNumber; + } + + /// + /// 十六进制转二进制 + /// + /// + /// + public static string HexToBin(string hexString) + { + var binaryValue = Convert.ToString(Convert.ToInt32(hexString, 16), 2); + return binaryValue; + } + + + /// + /// 字符串倒序 + /// + /// + /// + public static string StringReversed(string str) + { + var reversed = new string(str.Reverse().ToArray()); + return reversed; + } + + /// + /// 字符串分割成2个字符一组 + /// + /// + /// + public static List StringToPairs(string str) + { + var pairs = str.Select((ch, index) => new { ch, index }) + .GroupBy(x => x.index / 2) + .Select(g => string.Concat(g.Select(x => x.ch))) + .ToList(); + return pairs; + } + + /// + /// 格式化字符串 + /// + /// + /// + public static string StrAddSpan(string str) + { + if (str == "") + { + return ""; + } + return Regex.Replace(str.Replace(" ", ""), @"(?<=[0-9A-Za-z]{2})[0-9A-Za-z]{2}", " $0").Trim(); + } + + /// + /// 格式化字符串且反转 + /// + /// + /// + public static string StrReverseOrder(string ste) + { + if (ste == "") + { + return ""; + } + string[] strArr = ste.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries); + + return string.Join(" ", strArr.Reverse()); + } + + /// + /// 数据值加33 + /// + /// + /// + public static string StrAddHex33(string str) + { + if (str == "") + { + return ""; + } + string[] strArr = str.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < strArr.Length; i++) + { + strArr[i] = (Convert.ToInt32(strArr[i], 16) + Convert.ToInt32("33", 16)).ToString("X2"); + if (strArr[i].Length > 2) + { + strArr[i] = strArr[i].Substring(strArr[i].Length - 2); + } + } + return string.Join(" ", strArr); + } + + private static string AddHex33(string strGet) + { + string result; + if (string.IsNullOrEmpty(strGet)) + { + result = ""; + } + else + { + string[] source = StrAddSpan(strGet).Split(new char[] + { + ' ' + }, StringSplitOptions.RemoveEmptyEntries); + result = string.Join("", from s in source + select (Convert.ToInt32(s, 16) + Convert.ToInt32("33", 16)).ToString("X2")); + } + return result; + } + + } +} diff --git a/JiShe.CollectBus.Common/RequiredAttribute.cs b/JiShe.CollectBus.Common/RequiredAttribute.cs new file mode 100644 index 0000000..3c1950a --- /dev/null +++ b/JiShe.CollectBus.Common/RequiredAttribute.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JiShe.CollectBus.Common +{ + public class RequiredAttribute:Attribute + { + public bool IsRequired { get; private set; } + + public RequiredAttribute(bool isRequired) + { + IsRequired = isRequired; + } + + } +} diff --git a/JiShe.CollectBus.Protocol.Contracts/JiShe.CollectBus.Protocol.Contracts.csproj b/JiShe.CollectBus.Protocol.Contracts/JiShe.CollectBus.Protocol.Contracts.csproj index 04db03d..2434590 100644 --- a/JiShe.CollectBus.Protocol.Contracts/JiShe.CollectBus.Protocol.Contracts.csproj +++ b/JiShe.CollectBus.Protocol.Contracts/JiShe.CollectBus.Protocol.Contracts.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/JiShe.CollectBus.Protocol.Contracts/Models/CommandReuslt.cs b/JiShe.CollectBus.Protocol.Contracts/Models/CommandReuslt.cs new file mode 100644 index 0000000..89db4d4 --- /dev/null +++ b/JiShe.CollectBus.Protocol.Contracts/Models/CommandReuslt.cs @@ -0,0 +1,61 @@ +using JiShe.CollectBus.Common; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +namespace JiShe.CollectBus.Protocol.Contracts.Models +{ + //TODO + public class CommandReulstMsg + { + //Code + //Msg + //CommandReulst + } + + public class CommandReulst + { + + public int CmdLength { get; set; } + + [Required(true)] + public string A { get; set; } + + public int MSA { get; set; } + + public AFN AFN { get; set; } + + [Required(true)] + public Seq Seq { get; set; } + + public int Pn { get; set; } + + public int Fn { get; set; } + + /// + /// 数据报文 + /// + public List? HexDatas { get; set; } + + /// + /// 数据体 + /// + public string? JsonData { get; set; } + + /// + /// 响应下发的数据报文 + /// + public byte[]? ReplyBytes { get; set; } + } + + public class Seq + { + public TpV TpV { get; set; } + + public FIRFIN FIRFIN { get; set; } + + public CON CON { get; set; } + + public int PRSEQ { get; set; } + } +} diff --git a/JiShe.CollectBus.Protocol.Contracts/Models/Enums.cs b/JiShe.CollectBus.Protocol.Contracts/Models/Enums.cs new file mode 100644 index 0000000..0b4a5a6 --- /dev/null +++ b/JiShe.CollectBus.Protocol.Contracts/Models/Enums.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JiShe.CollectBus.Protocol.Contracts.Models +{ + /// + /// 规约标识 高位在前 D1+D0 + /// + public enum ProtocolIdentification + { + 禁用 = 00, + 电力负荷管理系统数据传输 = 01, + 本规约使用 = 10, + 保留 = 11 + } + + /// + /// 传输方向位 + /// + public enum DIR + { + 主站下行报文 = 0, + 终端上行报文 = 1 + } + + /// + /// 启动标识位 + /// + public enum PRM + { + 从动站报文 = 0, + 启动站报文 = 1, + } + + /// + /// 帧计数有效位 + /// + public enum FCV + { + FCB位无效 = 0, + FCB位有效 = 1 + } + + /// + /// 控制域(PRM=1)启动站功能码 + /// + public enum CMasterStationFunCode + { + 复位命令 = 1, + 用户数据 = 4, + 链路测试 = 9, + 请求1级数据 = 10, + 请求2级数据 = 11 + } + + /// + /// 控制域(PRM=0)从动站功能码 + /// + public enum CFromStationFunCode + { + 确认 = 0, + 用户数据 = 8, + 无所召唤的数据 = 9, + 链路数据 = 11 + } + + /// + /// 应用层功能码 + /// + public enum AFN + { + 确认或否认 = 00, + 复位 = 01, + 链路接口检测 = 02, + 中继站命令 = 03, + 设置参数 = 04, + 控制命令 = 05, + 身份认证及密钥协商 = 06, + 备用 = 07, + 请求被级联终端主动上报 = 08, + 请求终端配置 = 09, + 查询参数 = 10, + 请求任务数据 = 11, + 请求实时数据 = 12, + 请求历史数据 = 13, + 请求事件数据 = 14, + 文件传输 = 15, + 数据转发 = 16, + } + + /// + /// 帧时间标签有效位 + /// + public enum TpV + { + 附加信息域中无时间标签 = 0, + 附加信息域中带时间标签 = 1 + } + + /// + /// 首帧末帧标志 + /// + public enum FIRFIN + { + 中间帧 = 00, + 结束帧 = 01, + 第一帧 = 10, + 单帧 = 11 + } + + /// + /// 请求确认标志位 + /// + public enum CON + { + 不需要对该帧进行确认 = 0, + 需要对该帧进行确认 = 1 + } + + /// + /// 通信协议类型 数值0-255 + /// + public enum ProtocolType + { + DLT6451997 = 1, + 交流采样装置 = 2, + DLT6452007 = 30, + 串行接口连接窄带低压载波通讯 = 31 + } + + public enum BaudRate + { + Br300 = 0, + Br600 = 1, + Br1200 = 2, + Br2400 = 3, + Br4800 = 4, + Br7200 = 5, + Br9600 = 6, + Br19200 = 7 + } + public enum StopBit + { + Stop1 = 0, + Stop2 + } + public enum Parity + { + None = 0, + /// + /// 偶校验 + /// + Even, + /// + /// 奇校验 + /// + Odd + } + public enum DataBit + { + D5 = 0, + D6, + D7, + D8 + } +} diff --git a/JiShe.CollectBus.Protocol.Contracts/Models/ReqParameter.cs b/JiShe.CollectBus.Protocol.Contracts/Models/ReqParameter.cs new file mode 100644 index 0000000..1d6761d --- /dev/null +++ b/JiShe.CollectBus.Protocol.Contracts/Models/ReqParameter.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using JiShe.CollectBus.Common; + +namespace JiShe.CollectBus.Protocol.Contracts.Models +{ + public class ReqParameter + { + public AFN AFN { get; set; } + + public CMasterStationFunCode CMasterStationFunCode { get; set; } + + public PRM PRM { get; set; } = PRM.启动站报文; + + [Required(true)] + public string A { get; set; } + + [Required(true)] + public Seq Seq { get; set; } + + public int MSA { get; set; } = 0; + } + + public class ReqParameter2 : ReqParameter + { + public int Pn { get; set; } + public int Fn { get; set; } + } + + public class MeterParameter + { + /// + /// 测量点号 0~2040 为0被删除 + /// + public int Pn { get; set; } + + /// + /// 波特率600起 1~7 + /// + public int BaudRate { get; set; } + + /// + /// 端口号 1-31 + /// + public int Port { get; set; } + + public ProtocolType ProtocolType { get; set; } + /// + /// 通信地址 0~999999999999 + /// + public string Address { get; set; } + + /// + /// 通信密码 + /// + public string Password { get; set; } = "000000"; + + /// + /// 电能费率个数 1~12 + /// + public int RateNumber { get; set; } + + /// + /// 整数位个数 0~3 对应4~7位整数 + /// + public int IntegerBitNumber { get; set; } + + /// + /// 小数位个数0~3 对应1~4位小数 + /// + public int DecimalBitNumber { get; set; } + + /// + /// 所属采集器通信地址 + /// + public string CollectorAddress { get; set; } + + /// + /// 用户大类号 0~15 + /// + public int UserCategoryNumber { get; set; } + + /// + /// 用户小类号 0~15 + /// + public int UserSubclassNumber { get; set; } + } + + +} diff --git a/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs b/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs index f53f957..daa7e26 100644 --- a/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs +++ b/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs @@ -24,5 +24,78 @@ namespace JiShe.CollectBus.Protocol.Test { throw new NotImplementedException(); } + + //档案下发 + //var listMeter = new List() { new MeterParameter(){ + // Pn = 1, + // BaudRate = 3, + // Port = 2, + // ProtocolType = ProtocolType.DLT6452007, + // Address = "312408006642", + // Password = "000000", + // RateNumber = 4, + // IntegerBitNumber = 4, + // DecimalBitNumber = 4, + // CollectorAddress = "000000000000", + // UserCategoryNumber = 0, + // UserSubclassNumber = 0 + //} }; + //new BuildCommand().GetAFN04F10DataUnit(new ReqParameter2() { + // AFN = AFN.设置参数, + // CMasterStationFunCode = CMasterStationFunCode.请求1级数据, + // A= "322009872", + // Seq = new Seq() + // { + // TpV = TpV.附加信息域中无时间标签, + // FIRFIN = FIRFIN.单帧, + // CON = CON.需要对该帧进行确认, + // PRSEQ = 10, + // }, + // MSA = 13, + // Pn = 0, + // Fn = 10 + //},listMeter); + //档案读取 + //new BuildCommand().GetAFN10F10DataUnit(new ReqParameter2() + //{ + // AFN = AFN.查询参数, + // CMasterStationFunCode = CMasterStationFunCode.请求2级数据, + // A = "322009872", + // Seq = new Seq() + // { + // TpV = TpV.附加信息域中无时间标签, + // FIRFIN = FIRFIN.单帧, + // CON = CON.不需要对该帧进行确认, + // PRSEQ = 2, + // }, + // MSA = 13, + // Pn = 0, + // Fn = 10 + //},new List() {1,2 }); + + //var str = "68A600A6006888203290261A0A6200000201010001000100621E426622082431000000000000040300000000000000CA16"; + //var cmdResult = new BuildCommand().AnalysisCmd(str); + //if(cmdResult != null) + //{ + // var list = new BuildCommand().AnalysisAFN04F10DataUnit(cmdResult.HexDatas); + //} + //new BuildCommand().GetCommandBytes(new ReqParameter2() + //{ + // AFN = AFN.请求实时数据, + // CMasterStationFunCode = CMasterStationFunCode.请求2级数据, + // A = "322009872", + // Seq = new Seq() + // { + // TpV = TpV.附加信息域中无时间标签, + // FIRFIN = FIRFIN.单帧, + // CON = CON.不需要对该帧进行确认, + // PRSEQ = 2, + // }, + // MSA = 13, + // Pn = 1, + // Fn = 129 + // }); + + //new BuildCommand().AmmeterValveControl("312408006642", "", "000000", true); } } diff --git a/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj b/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj index e56c90b..f260732 100644 --- a/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj +++ b/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj @@ -12,6 +12,7 @@ + diff --git a/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs b/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs index 2424512..ccdafce 100644 --- a/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs +++ b/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs @@ -1,8 +1,10 @@ -using JiShe.CollectBus.Protocol.Contracts.Abstracts; +using JiShe.CollectBus.Common; +using JiShe.CollectBus.Protocol.Contracts.Abstracts; using JiShe.CollectBus.Protocol.Contracts.Attributes; using JiShe.CollectBus.Protocol.Contracts.DependencyInjection; using JiShe.CollectBus.Protocol.Contracts.Models; using Microsoft.Extensions.Caching.Distributed; +using System.Data; using TouchSocket.Sockets; namespace JiShe.CollectBus.Protocol @@ -10,6 +12,18 @@ namespace JiShe.CollectBus.Protocol [ProtocolName("StandardProtocol")] public class StandardProtocolPlugin(IDistributedCache cache) : BaseProtocolPlugin(cache), ISingletonDependency { + //起始字符 + private const string stx = "68"; + //结束字符 + private const string end = "16"; + //头部字节长度 + private const int hearderLen = 6; + //消息认证码字段长度 + private const int pWLen = 16; + + private const int tPLen = 6; + + public override ProtocolInfo Get() { return new ProtocolInfo("Standard", "376.1", "TCP","376.1协议","DTS1980"); @@ -22,7 +36,13 @@ namespace JiShe.CollectBus.Protocol public override void Received(ReceivedDataEventArgs e) { - throw new NotImplementedException(); + var messageHexString = Convert.ToHexString(e.ByteBlock.Span); + var cmdResult = AnalysisCmd(messageHexString); + if (cmdResult == null) + { + return; + } + AnalysisData(cmdResult); } public override void Send() @@ -30,6 +50,867 @@ namespace JiShe.CollectBus.Protocol throw new NotImplementedException(); } + #region 下行命令 + + /// + /// 设置电表档案 + /// + /// + /// + public void GetAFN04F10DataUnit(ReqParameter reqParameter, List meterParameters) + { + var dataUnit = GetAFN04F10DataUnit(meterParameters); + var bytes = GetCommandBytes(reqParameter, dataUnit); + } + + /// + /// 查询电表档案 + /// + /// + /// 对象序号 + public void GetAFN10F10DataUnit(ReqParameter reqParameter, List meterNumberList) + { + var dataUnit = new List(); + var countHex = DataConvert.DecToHex(meterNumberList.Count()).PadLeft(4, '0'); + var countHexPairs = DataConvert.StringToPairs(countHex); + countHexPairs.Reverse(); + dataUnit.AddRange(countHexPairs); + + foreach (var number in meterNumberList) + { + var numberHex = DataConvert.DecToHex(number).PadLeft(4, '0'); + var numberHexPairs = DataConvert.StringToPairs(numberHex); + numberHexPairs.Reverse(); + dataUnit.AddRange(numberHexPairs); + } + var bytes = GetCommandBytes(reqParameter, dataUnit); + } + + /// + /// 组装电表阀控 + /// + /// 电表地址 + /// 特殊控制码 + /// 密码 + /// 是否为开阀 + /// + public List AmmeterValveControl(string address, string specialnocode, string password, bool state, string modelCode = "") + { + address = address.Trim().TrimStart('0'); + if (address.Length < 12) address = address.PadLeft(12, '0'); + string Code = string.Empty; + + if (state) + { + if (string.IsNullOrEmpty(specialnocode)) + Code = "1B"; + else + Code = specialnocode == "1B" || specialnocode == "1C" ? specialnocode : "1C"; + } + else + Code = "1A";//跳闸 + + if (specialnocode == "1W") + { + if (state) + Code = "1A"; + else + Code = "1C"; + } + + var pwdLevel = "02"; + if (modelCode == "HL_DTSU2625" || modelCode == "DDZY9866") + pwdLevel = "04"; + else if (modelCode == "DDS2705") + pwdLevel = "03"; + + if (!string.IsNullOrWhiteSpace(password) && password.Contains("|")) + { + var sp = password.Split('|'); + pwdLevel = sp[1]; + password = sp[0]; + } + + string strDate = DataConvert.StrAddSpan(DateTime.Now.AddYears(3).ToString("000012ddMMyy")); + if (specialnocode == "1D" || modelCode == "SZBD_DDZY1225") + strDate = "FF FF FF FF FF FF"; + string strP = DataConvert.StrReverseOrder(DataConvert.StrAddSpan(password)); + string strSJY = " " + pwdLevel + " " + strP + " 01 00 00 00 " + Code + " 00 " + strDate; + string strLen = (strSJY.Replace(" ", "").Length / 2).ToString("X2"); + string strReturn = "68 " + DataConvert.StrReverseOrder(DataConvert.StrAddSpan(address)) + " 68 1C " + strLen + " " + DataConvert.StrAddHex33(strSJY) + " "; + string strSum = strReturn.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(i => Convert.ToInt32(i, 16)).Sum().ToString("X"); + strReturn += strSum.Substring(strSum.Length - 2) + " 16"; + + return strReturn.Split(' ').ToList(); + } + + /// + /// 帧命令组装 + /// + /// 请求参数 + /// 数据单元 + /// + public byte[] GetCommandBytes(ReqParameter reqParameter, List? dataUnit = null) + { + var cmdStrList = new List(); + var userDatas = GetUserData(reqParameter, dataUnit); + var hearders = GetHeaders(userDatas.Count); + var cs = GetCS(userDatas); + cmdStrList.AddRange(hearders); + cmdStrList.AddRange(userDatas); + cmdStrList.Add(cs); + cmdStrList.Add(end); + Console.WriteLine(string.Join(" ", cmdStrList)); + var bytes = cmdStrList.Select(x => Convert.ToByte(x, 16)).ToArray(); + return bytes; + } + + + /// + /// 固定长度的报文头 起始字符+长度+长度+起始字符 + /// + /// + /// + private List GetHeaders(int length) + { + var headers = new List(); + headers.Add(stx); + var l = GetLength(length); + headers.AddRange(l); + headers.AddRange(l); + headers.Add(stx); + return headers; + } + + /// + /// 用户数据区 + /// + /// + /// + public List GetUserData(ReqParameter reqParameter, List? dataUnit) + { + var c = GetC(reqParameter.CMasterStationFunCode, reqParameter.PRM); + var a = GetAList(reqParameter.A, reqParameter.MSA); + + var linkUserData = GetLinkUserData(reqParameter.AFN, reqParameter.Seq, + ((ReqParameter2)reqParameter).Pn, ((ReqParameter2)reqParameter).Fn, dataUnit); + + var list = new List() { c }; + list.AddRange(a); + list.AddRange(linkUserData); + return list; + } + + + /// + /// 长度 2字节 [用户数据区长度] + /// + /// + private List GetLength(int length1) + { + var binaryLen = DataConvert.DecToBin(length1); + var protocolIdentification = Enum.Format(typeof(ProtocolIdentification), + ProtocolIdentification.本规约使用, "d").PadLeft(2, '0'); + var lenStr = $"{binaryLen}{protocolIdentification}"; + var hexLen = DataConvert.BinToHex(lenStr); + hexLen = hexLen.PadLeft(4, '0'); + var list = DataConvert.StringToPairs(hexLen); + list.Reverse(); + return list; + } + + /// + /// 控制域 + /// + /// + /// + /// + /// + private string GetC(CMasterStationFunCode cMasterStationFunCode, PRM pRM, int fcb = 0, FCV fcv = FCV.FCB位无效) + { + var cMasterStationFunCodeHex = DataConvert.DecToBin((int)cMasterStationFunCode); + cMasterStationFunCodeHex = cMasterStationFunCodeHex.ToString().PadLeft(4, '0'); + var strC = $"{(int)DIR.主站下行报文}{(int)pRM}{fcb}{(int)fcv}{cMasterStationFunCodeHex}"; + var hexC = DataConvert.BinToHex(strC).PadLeft(2, '0'); + return hexC; + } + + /// + /// 地址域 3220 09872 + /// + /// 行政区划码 BCD码 3220=2032 + /// 逻辑地址 BIN 09872=2690=>9026 + /// 主站地址 BIN 0~127 + /// + private List GetAList(string a, int mSA) + { + var list = new List(); + + var a1 = a.Substring(0, 4); + var a1Pairs = DataConvert.StringToPairs(a1); + a1Pairs.Reverse(); + list.AddRange(a1Pairs); + + var a2 = Convert.ToInt32(a.Substring(4)); + var decA2 = DataConvert.DecToHex(a2); + var a2Pairs = DataConvert.StringToPairs(decA2.PadLeft(4, '0')); + a2Pairs.Reverse(); + list.AddRange(a2Pairs); + + //TODO:主站地址和组地址标志 + var a3Bin = $"{DataConvert.DecToBin(mSA).PadLeft(7, '0')}0"; + list.Add(DataConvert.BinToHex(a3Bin)); + + return list; + } + + #region 链路用户数据 + + private List GetLinkUserData(AFN aFN, Seq seq, int pn, int fn, List? dataUnit) + { + var aFNValue = DataConvert.DecToHex((int)aFN).PadLeft(2, '0'); + var sEQ = GetSEQ(seq.TpV, seq.FIRFIN, seq.CON, seq.PRSEQ); + var dA = GetDA(pn); + var dT = GetDT(fn); + var list = new List() { aFNValue, sEQ }; + list.AddRange(dA); + list.AddRange(dT); + + if (dataUnit != null) + { + list.AddRange(dataUnit); + } + //list.AddRange(GetDataUnit(aFN,seq)); + + if (seq.TpV == TpV.附加信息域中带时间标签) + list.AddRange(GetTp("00")); + + return list; + } + + + /// + /// 帧序列域 + /// + /// + /// + /// + /// + private string GetSEQ(TpV tpV, FIRFIN fIRFIN, CON cON, int pRSEQ) + { + var tpVValue = Enum.Format(typeof(TpV), + tpV, "d"); + var fIRFINValue = Enum.Format(typeof(FIRFIN), + fIRFIN, "d"); + var cONValue = (int)cON; + var sEQBin = $"{tpVValue}{fIRFINValue}{cONValue}{DataConvert.DecToBin(pRSEQ).PadLeft(4, '0')}"; + var hexSEQ = DataConvert.BinToHex(sEQBin).PadLeft(2, '0'); + return hexSEQ; + } + + /// + /// 信息点标识 + /// + /// 计量点 + /// + private List GetDA(int pn) + { + if (pn == 0) + return new List() { "00", "00" }; + var dA2 = (pn - 1) / 8 + 1;//信息点组从1开始 第几组 + var dA1 = pn - (dA2 - 1) * 8;//pn % 8 + var dA1Hex = DataConvert.BinToHex("1".PadRight(dA1, '0'));//对位信息 第几位 二进制有效位 + var dA2Hex = DataConvert.DecToHex(dA2); + return new List() { dA1Hex.PadLeft(2, '0'), dA2Hex.PadLeft(2, '0') }; + + } + + /// + /// 数据单元标识 + /// + /// + /// + private List GetDT(int fn) + { + var dT2 = (fn - 1) / 8;//从零开始 第几组 + var dT1 = fn - dT2 * 8; + var dT1Hex = DataConvert.BinToHex("1".PadRight(dT1, '0'));//对位信息 第几位 二进制有效位 + var dT2Hex = DataConvert.DecToHex(dT2); + return new List() { dT1Hex.PadLeft(2, '0'), dT2Hex.PadLeft(2, '0') }; + } + + private List GetDataUnit(AFN aFN, Seq seq) + { + var datas = new List(); + switch (aFN) + { + case AFN.确认或否认: + break; + case AFN.复位: + break; + case AFN.链路接口检测: + break; + case AFN.中继站命令: + break; + case AFN.设置参数: + break; + case AFN.控制命令: + break; + case AFN.身份认证及密钥协商: + break; + case AFN.备用: + break; + case AFN.请求被级联终端主动上报: + break; + case AFN.请求终端配置: + break; + case AFN.查询参数: + break; + case AFN.请求任务数据: + break; + case AFN.请求实时数据: + break; + case AFN.请求历史数据: + break; + case AFN.请求事件数据: + break; + case AFN.文件传输: + break; + case AFN.数据转发: + break; + default: + break; + } + if (seq.TpV == TpV.附加信息域中带时间标签) + datas.AddRange(GetTp("00")); + return datas; + } + + private void GetAFN00DataUnit(Seq seq) + { + //EC+Tp + + } + + /// + /// 终端电能表配置参数 + /// + /// + /// + public List GetAFN04F10DataUnit(List meterParameters) + { + var hexDatas = new List(); + + var countHex = DataConvert.DecToHex(meterParameters.Count()).PadLeft(4, '0'); + hexDatas.Add(countHex); + + //TODO 优化代码:目标数据入参,返回类型为出参 + for (int i = 0; i <= meterParameters.Count - 1; i++) + { + var meter = meterParameters[i]; + + var indexHex = DataConvert.DecToHex(i + 1).PadLeft(4, '0'); + hexDatas.Add(indexHex); + + var pnHex = DataConvert.DecToHex(meter.Pn).PadLeft(4, '0'); + hexDatas.Add(pnHex); + + var baudRateBin = DataConvert.DecToBin(meter.BaudRate).PadLeft(3, '0'); + var portBin = DataConvert.DecToBin(meter.Port).PadLeft(5, '0'); + var baudRateAndPortHex = DataConvert.BinToHex($"{baudRateBin}{portBin}").PadLeft(2, '0'); + hexDatas.Add(baudRateAndPortHex); + + var protocolTypeHex = DataConvert.DecToHex((int)meter.ProtocolType).PadLeft(2, '0'); + hexDatas.Add(protocolTypeHex); + + hexDatas.Add(meter.Address); + + hexDatas.Add(meter.Password.PadLeft(12, '0')); + + var rateNumberBin = $"0000{DataConvert.DecToBin(meter.RateNumber).PadLeft(4, '0')}"; + var rateNumberHex = DataConvert.BinToHex(rateNumberBin).PadLeft(2, '0'); + hexDatas.Add(rateNumberHex); + + var intBitNumberBin = DataConvert.DecToBin(meter.IntegerBitNumber - 4).PadLeft(2, '0'); + var decBitNumberBin = DataConvert.DecToBin(meter.DecimalBitNumber - 1).PadLeft(2, '0'); + var intAndDecBitNumberBin = $"0000{intBitNumberBin}{decBitNumberBin}"; + var intAndDecBitNumberHex = DataConvert.BinToHex(intAndDecBitNumberBin).PadLeft(2, '0'); + hexDatas.Add(intAndDecBitNumberHex); + + hexDatas.Add(meter.CollectorAddress.PadLeft(12, '0')); + + var userCategoryNumberBin = DataConvert.DecToBin(meter.UserCategoryNumber).PadLeft(4, '0'); + var userSubclassNumberBin = DataConvert.DecToBin(meter.UserSubclassNumber).PadLeft(4, '0'); + var userNumberHex = DataConvert.BinToHex($"{userCategoryNumberBin}{userSubclassNumberBin}").PadLeft(2, '0'); + hexDatas.Add(userNumberHex); + } + + //高位在前,低位在后 + var datas = new List(); + foreach (var hexData in hexDatas) + { + if (hexData.Length == 2) + datas.Add(hexData); + else + { + var lst = DataConvert.StringToPairs(hexData); + lst.Reverse(); + datas.AddRange(lst); + } + } + datas.AddRange(GetPW()); + return datas; + } + + /// + /// 透明转发 + /// + /// 终端通信端口 1~31 + /// 0~7 对应300,600,1200,2400,4800,7200,9600,19200 + /// + /// + /// + /// + public List GetAFN1001DataUnit(int port, BaudRate baudRate, StopBit stopBit, Parity parity, DataBit dataBit, + int waitContentTimeout, int waitByteTimeout, List datas) + { + var dataUnit = new List(); + + var portHex = DataConvert.DecToHex(port).PadLeft(2, '0'); + dataUnit.Add(portHex); + + var baudRateBin = DataConvert.DecToBin((int)baudRate).PadLeft(3, '0'); + var stopBitBin = DataConvert.DecToBin((int)stopBit); + var parityBin = parity != Parity.None ? $"1{DataConvert.DecToBin((int)parity)}" : $"0{DataConvert.DecToBin((int)parity)}"; + var dataBitBin = DataConvert.DecToBin((int)dataBit).PadLeft(2, '0'); + var controlHex = DataConvert.BinToHex($"{baudRateBin}{stopBitBin}{parityBin}{dataBitBin}").PadLeft(2, '0'); ; + dataUnit.Add(controlHex); + + var waitContentTimeoutBin = $"1{DataConvert.DecToBin(waitContentTimeout).PadLeft(7, '0')}"; + var waitContentTimeoutHex = DataConvert.BinToHex(waitContentTimeoutBin).PadLeft(2, '0'); + var waitByteTimeoutHex = DataConvert.DecToHex(waitByteTimeout).PadLeft(2, '0'); + + dataUnit.Add(waitContentTimeoutHex); + dataUnit.Add(waitByteTimeoutHex); + + var countHex = DataConvert.DecToHex(datas.Count).PadLeft(4, '0'); + var countHexPairs = DataConvert.StringToPairs(countHex); + countHexPairs.Reverse(); + dataUnit.AddRange(countHexPairs); + + dataUnit.AddRange(datas); + + return dataUnit; + } + + //TODO AUX=消息认证码字段(PW,16个字节)+时间标签 + private List GetPW() + { + var str = "00"; + var pWList = Enumerable.Repeat(str, pWLen).ToList(); + return pWList; + } + + /// + /// 时间标签 + /// + /// 启动帧帧序号计数器PFC 1字节 + /// 允许发送传输延时时间 min 1字节 + /// + private List GetTp(string pFC = "00", int delayTime = 0) + { + + var now = DateTime.Now; // 获取当前时间 + var seconds = now.Second.ToString().PadLeft(2, '0'); // 获取当前秒数 + var minutes = now.Minute.ToString().PadLeft(2, '0'); // 获取当前分钟数 + var hours = now.Hour.ToString().PadLeft(2, '0'); // 获取当前小时数 + var day = now.Day.ToString().PadLeft(2, '0'); // 获取当前日期的日数 + return new List() { pFC, seconds, minutes, hours, day, delayTime.ToString().PadLeft(2, '0') }; + } + + #endregion + + /// + /// 帧校验和 + /// + /// 用户数据区 + /// + private string GetCS(List userData) + { + byte sum = 0; + foreach (var d in userData) + { + var b = Convert.ToByte(d, 16); + sum += b; + } + return sum.ToString("X2"); + } + + #endregion + + #region 上行命令 + + //68 + //32 00 + //32 00 + //68 + //C9 1100'1001. 控制域C。 + // D7=1, (终端发送)上行方向。 + // D6=1, 此帧来自启动站。 + // D5=0, (上行方向)要求访问位。表示终端无事件数据等待访问。 + // D4=0, 保留 + // D3~D0=9, 功能码。链路测试 + + //20 32 行政区划码 + //90 26 终端地址 + //00 主站地址和组地址标志。终端为单地址。 //3220 09 87 2 + // 终端启动的发送帧的 MSA 应为 0, 其主站响应帧的 MSA 也应为 0. + //02 应用层功能码。AFN=2, 链路接口检测 + //70 0111'0000. 帧序列域。无时间标签、单帧、需要确认。 + //00 00 信息点。DA1和DA2全为“0”时,表示终端信息点。 + //01 00 信息类。F1, 登录。 + //44 帧尾,包含用户区数据校验和 + //16 帧结束标志 + + /// + /// 解析上行命令 + /// + /// + /// + public CommandReulst? AnalysisCmd(string cmd) + { + CommandReulst? commandReulst = null; + var hexStringList = DataConvert.StringToPairs(cmd); + if (hexStringList.Count < hearderLen) + { + return commandReulst; + } + if (hexStringList[0] != stx || hexStringList[5] != stx) + { + return commandReulst; + } + + var lenHexStr = $"{hexStringList[2]}{hexStringList[1]}"; + var lenBin = DataConvert.HexToBin(lenHexStr); + var len = DataConvert.BinToDec(lenBin.Remove(lenBin.Length - 2)); + if (hexStringList.Count - 2 != hearderLen + len) + return commandReulst; + + var userDataIndex = hearderLen; + var c = hexStringList[userDataIndex];//控制域 1字节 + userDataIndex += 1; + + var aHexList = hexStringList.Skip(userDataIndex).Take(5).ToList();//地址域 5字节 + var a = AnalysisA(aHexList); + var a3Bin = DataConvert.HexToBin(aHexList[4]).PadLeft(8, '0'); + var mSA = DataConvert.BinToDec(a3Bin.Substring(0, 7)); + userDataIndex += 5; + + var aFN = (AFN)DataConvert.HexToDec(hexStringList[userDataIndex]);//1字节 + userDataIndex += 1; + + var seq = DataConvert.HexToBin(hexStringList[userDataIndex]).PadLeft(8, '0'); + var tpV = (TpV)Convert.ToInt32(seq.Substring(0, 1)); + var fIRFIN = (FIRFIN)Convert.ToInt32(seq.Substring(1, 2)); + var cON = (CON)Convert.ToInt32(seq.Substring(3, 1)); + var prseqBin = seq.Substring(4, 4); + userDataIndex += 1; + + // (DA2 - 1) * 8 + DA1 = pn + var da1Bin = DataConvert.HexToBin(hexStringList[userDataIndex]); + var da1 = da1Bin == "0" ? 0 : da1Bin.Length; + userDataIndex += 1; + var da2 = DataConvert.HexToDec(hexStringList[userDataIndex]); + userDataIndex += 1; + var pn = da2 == 0 ? 0 : (da2 - 1) * 8 + da1; + + //(DT2*8)+DT1=fn + var dt1Bin = DataConvert.HexToBin(hexStringList[userDataIndex]); + var dt1 = dt1Bin != "0" ? dt1Bin.Length : 0; + userDataIndex += 1; + var dt2 = DataConvert.HexToDec(hexStringList[userDataIndex]); + userDataIndex += 1; + var fn = dt2 > 0 ? dt2 * 8 + dt1 : 0; + + //数据单元 + var datas = hexStringList.Skip(userDataIndex).Take(len + hearderLen - userDataIndex).ToList(); + + //EC + //Tp + commandReulst = new CommandReulst() + { + A = a, + MSA = mSA, + AFN = aFN, + Seq = new Seq() + { + TpV = tpV, + FIRFIN = fIRFIN, + CON = cON, + PRSEQ = DataConvert.BinToDec(prseqBin), + }, + CmdLength = len, + Pn = pn, + Fn = fn, + HexDatas = datas + }; + + return commandReulst; + } + + /// + /// 解析地址 + /// + /// + /// + private string AnalysisA(List aHexList) + { + var a1 = aHexList[1] + aHexList[0]; + var a2 = aHexList[3] + aHexList[2]; + var a2Dec = DataConvert.HexToDec(a2); + var a3 = aHexList[4]; + var a = $"{a1}{a2Dec.ToString().PadLeft(5, '0')}"; + return a; + } + + /// + /// 解析上行命令数据包 + /// + /// + public void AnalysisData(CommandReulst commandReulst) + { + switch (commandReulst.AFN) + { + case AFN.确认或否认: + //commandReulst.fn + //1:全部确认 + //2:全部否认 + //3:按数据单元表示确认和否认 + //4 硬件安全认证错误应答 + break; + case AFN.复位: + break; + case AFN.链路接口检测: + AnalysisAFN02(commandReulst); + break; + case AFN.中继站命令: + break; + case AFN.设置参数: + break; + case AFN.控制命令: + break; + case AFN.身份认证及密钥协商: + break; + case AFN.备用: + break; + case AFN.请求被级联终端主动上报: + break; + case AFN.请求终端配置: + break; + case AFN.查询参数: + break; + case AFN.请求任务数据: + break; + case AFN.请求实时数据: + break; + case AFN.请求历史数据: + break; + case AFN.请求事件数据: + break; + case AFN.文件传输: + break; + case AFN.数据转发: + break; + default: + break; + } + } + + public void AnalysisAFN02(CommandReulst commandReulst) + { + if (commandReulst.Fn == 1) //登录 + { + Console.WriteLine($"{commandReulst.A},登录:{DateTime.Now}"); + var reqParam = new ReqParameter2() + { + AFN = AFN.确认或否认, + CMasterStationFunCode = CMasterStationFunCode.链路测试, + PRM = PRM.从动站报文, + A = commandReulst.A, + Seq = new Seq() + { + TpV = TpV.附加信息域中无时间标签, + FIRFIN = FIRFIN.单帧, + CON = CON.不需要对该帧进行确认, + PRSEQ = commandReulst.Seq.PRSEQ, + }, + MSA = commandReulst.MSA, + Pn = 0, + Fn = 1 + }; + commandReulst.ReplyBytes = GetCommandBytes(reqParam); + } + else if (commandReulst.Fn == 2)//退出登录 + { + Console.WriteLine($"{commandReulst.A},退出登录:{DateTime.Now}"); + } + else if (commandReulst.Fn == 3)//心跳 + { + Console.WriteLine($"{commandReulst.A},心跳:{DateTime.Now}"); + AnalysisHeartbeat(commandReulst); + } + } + + public void AnalysisHeartbeat(CommandReulst commandReulst) + { + if (commandReulst.Seq.TpV == TpV.附加信息域中带时间标签) + { + //解析 + + } + if (commandReulst.Seq.CON == CON.需要对该帧进行确认) + { + var reqParam = new ReqParameter2() + { + AFN = AFN.确认或否认, + CMasterStationFunCode = CMasterStationFunCode.链路测试, + PRM = PRM.从动站报文, + A = commandReulst.A, + Seq = new Seq() + { + TpV = TpV.附加信息域中无时间标签, + FIRFIN = FIRFIN.单帧, + CON = CON.不需要对该帧进行确认, + PRSEQ = commandReulst.Seq.PRSEQ, + }, + MSA = commandReulst.MSA, + Pn = 0, + Fn = 1 + }; + //commandReulst.ReplyBytes = GetCommandBytes(reqParam); + } + } + + /// + /// 解析时间标签 + /// + /// + private void AnalysisTp(List hexDatas) + { + var pFC = DataConvert.HexToDec(hexDatas[0]);//启动帧帧序号计数器 + var seconds = Convert.ToInt32(hexDatas[1]); // 获取当前秒数 + var minutes = Convert.ToInt32(hexDatas[2]); // 获取当前分钟数 + var hours = Convert.ToInt32(hexDatas[3]); // 获取当前小时数 + var day = Convert.ToInt32(hexDatas[4]); // 获取当前日期的日数 + var delayTime = DataConvert.HexToDec(hexDatas[5]);//延迟时间 min + } + + /// + /// 解析电表档案 + /// + /// + /// + public List AnalysisAFN04F10DataUnit(List hexDatas) + { + var meterList = new List(); + var count = DataConvert.HexToDec($"{hexDatas[1]}{hexDatas[0]}"); + //if (2 + count * 27 != hexDatas.Count - pWLen - tPLen - 2) + // return; + var index = 2;//数量 + for (int i = 1; i <= count; i++) + { + var meterNumber = DataConvert.HexToDec($"{hexDatas[index + 1]}{hexDatas[index]}"); + index += 2; + + var pn = DataConvert.HexToDec($"{hexDatas[index + 1]}{hexDatas[index]}"); + index += 2; + + var baudRateAndPortBin = DataConvert.HexToBin(hexDatas[index]).PadLeft(8, '0'); + var baudRate = DataConvert.BinToDec(baudRateAndPortBin.Substring(0, 3)); + var port = DataConvert.BinToDec(baudRateAndPortBin.Substring(3, 5)); + index += 1; + + var protocolType = (ProtocolType)DataConvert.HexToDec(hexDatas[index]); + 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 = DataConvert.HexToBin(hexDatas[index]).PadLeft(8, '0'); + var rateNumber = DataConvert.BinToDec(rateNumberBin.Substring(4)); + index += 1; + + var intBitAndDecBitNumberBin = DataConvert.HexToBin(hexDatas[index]).PadLeft(8, '0'); + var intBitNumber = DataConvert.BinToDec(intBitAndDecBitNumberBin.Substring(4, 2)) + 4; + var decBitNumber = DataConvert.BinToDec(intBitAndDecBitNumberBin.Substring(6, 2)) + 1; + index += 1; + + // hexDatas.GetRange() + var collectorAddressHexList = hexDatas.Skip(index).Take(6).ToList(); + collectorAddressHexList.Reverse(); + var collectorAddress = string.Join("", collectorAddressHexList); + index += 6; + + var userClassNumberBin = DataConvert.HexToBin(hexDatas[index]).PadLeft(8, '0'); + var userClass = DataConvert.BinToDec(userClassNumberBin.Substring(0, 4)); + var userSubClass = DataConvert.BinToDec(userClassNumberBin.Substring(4, 4)); + index += 1; + + meterList.Add(new MeterParameter() + { + Pn = pn, + BaudRate = baudRate, + Port = port, + ProtocolType = protocolType, + Address = address, + Password = password, + RateNumber = rateNumber, + IntegerBitNumber = intBitNumber, + DecimalBitNumber = decBitNumber, + CollectorAddress = collectorAddress, + UserCategoryNumber = userClass, + UserSubclassNumber = userSubClass, + }); + } + return meterList; + } + + /// + /// 解析实时数据F129 + /// + /// + private void AnalysisAFN0CF129DataUnit(List hexDatas) + { + 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(); + 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; + } + } + + #endregion } }