using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CanTool { public class DiagItem { public byte Sid; public UInt16 Did; public byte[] ParamsArray; public DiagItem() { this.ParamsArray = new byte[0]; } public DiagItem(DiagItem diagItem) { Sid = diagItem.Sid; Did = diagItem.Did; ParamsArray = new byte[diagItem.ParamsArray.Length]; diagItem.ParamsArray.CopyTo(ParamsArray, 0); } public DiagItem(byte sid, UInt16 did, byte[] paramsArray) { Sid = sid; Did = did; ParamsArray = new byte[paramsArray.Length]; paramsArray.CopyTo(ParamsArray, 0); } public UInt64 GetSidAndDidAndParams() { UInt64 SidAndDidAndParams = (UInt64)((UInt64)this.Sid << 56 | (UInt64)this.Did << 40); for(int i = 0;i < this.ParamsArray.Length;i++) { SidAndDidAndParams |= (UInt64)((UInt64)this.ParamsArray[i] << ((4 - i) * 8)); } return SidAndDidAndParams; } } public delegate string AnalysisVersionDelegate(byte[] data, UInt32 len); public class VersionParams { public string VersionComment; //版本号说明 public DiagItem DiagItem; //版本号对应的诊断 public AnalysisVersionDelegate AnalysisVersionFunc; public VersionParams() { VersionComment = ""; DiagItem = new DiagItem(); } public VersionParams(string versionComment, DiagItem diagItem, AnalysisVersionDelegate analysisVersionFunc) { VersionComment = versionComment; DiagItem = diagItem; AnalysisVersionFunc = analysisVersionFunc; } public VersionParams(string versionComment, byte sid, UInt16 did, byte[] paramsArray, AnalysisVersionDelegate analysisVersionFunc) { VersionComment = versionComment; DiagItem = new DiagItem(sid, did, paramsArray); AnalysisVersionFunc = analysisVersionFunc; } } public class ReadVersionConfig { public VersionParams[] VersionParamsTable; public ReadVersionConfig() { VersionParamsTable = new VersionParams[0]; } public ReadVersionConfig(VersionParams[] versionParamsTable) { VersionParamsTable = versionParamsTable; } public DiagItem GetDiagItem(int index) { DiagItem ret; if(index < VersionParamsTable.Length) { ret = new DiagItem(VersionParamsTable[index].DiagItem); } else { ret = new DiagItem(0xff, 0xff, new byte[0]); } return ret; } public string GetVersionComment(int index) { string ret; if ((index >= 0) && (index < VersionParamsTable.Length) ) { ret = VersionParamsTable[index].VersionComment; } else { ret = ""; } return ret; } //通过SID DID 参数来寻找具体的项 public int IndexOf(byte sid, UInt16 did, byte[] paramsArray) { int index = -1; int paramsLenMin; for (int i = 0; i < VersionParamsTable.Length; i++) { //对比SID和DID if (((VersionParamsTable[i].DiagItem.Sid + 0x40) != (sid)) || (VersionParamsTable[i].DiagItem.Did != did) ) { continue; } //获取参数长度最小的 paramsLenMin = Math.Min(VersionParamsTable[i].DiagItem.ParamsArray.Length, paramsArray.Length); //对比参数 int j; for (j = 0; j < paramsLenMin; j++) { if (VersionParamsTable[i].DiagItem.ParamsArray[j] != paramsArray[j]) { break; } } //参数也全部相等 则找到对应的诊断项 if (j == paramsLenMin) { index = i; break; } } return index; } //通过数据解析版本号 public string AnalysisVersion(int index, byte[] data, UInt32 len) { string str = ""; if ((index >= 0) && (index < VersionParamsTable.Length) ) { if (VersionParamsTable[index].AnalysisVersionFunc != null) { str = VersionParamsTable[index].AnalysisVersionFunc(data, len); } } return str; } //默认解析22读取的信息 static public string DefaultAnalysisVersion0x22(byte[] data, UInt32 len) { return Encoding.ASCII.GetString(data, 3, (int)(len - 3)); } //默认解析内控版本号信息 static public string DefaultAnalysisVersion0x2f(byte[] data, UInt32 len) { return Encoding.ASCII.GetString(data, 5, (int)(len - 5)); } } }