/// <summary> /// 检查bytes是否匹配dataFieldCollection /// </summary> /// <param name="bytes"></param> /// <returns></returns> public ParseResult CheckBytes(string receivePartName, byte[] bytes) { if (bytes == null) { return(new LengthErrorResult(receivePartName, this.DataFields.BytesLength, 0)); } // 1. check length // // TODO: 2010-06-08 return CheckLength max length while fail // //if (!this.DataFields.CheckLength(bytes)) // return new LengthErrorResult(receivePartName, this.DataFields.BytesLength, bytes.Length); int dfsBytesLength = this.DataFields.Length; if (bytes.Length < dfsBytesLength) { return(new LengthErrorResult(receivePartName, dfsBytesLength, bytes.Length)); } // 1.1 check match check bytes // foreach (DataField df in this.DataFields) { if (df.IsMatchCheck) { log.Debug("MatchCheck datafield: {0}", df.Name); byte[] bs = df.GetMatch(bytes); log.Debug("Expected: {0}, Actual: {1}", HexStringConverter.Default.ConvertToObject(df.Bytes), HexStringConverter.Default.ConvertToObject(bs)); bool eq = BytesValueComparer.AreEqual(df.Bytes, bs); if (!eq) { return(new DataErrorResult(receivePartName, df.Bytes, bs)); } } } // 2. check crc // get crcfg // DataField crcdf = this.DataFields.CRCDataField; if (crcdf != null) { byte[] crcbs = this.CRCer.Calc(bytes, 0, this.DataFields.Length - crcdf.DataLength); crcdf.Bytes = crcbs; if (!crcdf.Match(bytes, 0)) { return(new CRCErrorResult(receivePartName, crcbs, crcdf.GetMatch(bytes, 0))); } } // 3. check need match df // return(new SuccessResult(receivePartName)); }
/// <summary> /// 检查bytes是否匹配dataFieldCollection /// </summary> /// <param name="bytes"></param> /// <returns></returns> public IParseResult CheckBytes(string receivePartName, byte[] bytes) { if (bytes == null) { IParseResult pr = new LengthErrorResult(receivePartName, this.DataFields.BytesLength, 0); pr.Name = receivePartName; return(pr); } // 1. check length // // 2010-06-08 return CheckLength max length while fail // //if (!this.DataFields.CheckLength(bytes)) // return new LengthErrorResult(receivePartName, this.DataFields.BytesLength, bytes.Length); int dfsBytesLength = this.DataFields.Length; if (bytes.Length < dfsBytesLength) { IParseResult pr = new LengthErrorResult(receivePartName, dfsBytesLength, bytes.Length); pr.Name = receivePartName; return(pr); } // 1.1 check match check bytes // foreach (DataField df in this.DataFields) { if (df.IsMatchCheck) { log.Debug("MatchCheck datafield: {0}", df.Name); byte[] bs = df.GetMatch(bytes); log.Debug("Expected: {0}, Actual: {1}", HexStringConverter.Default.ConvertToObject(df.Bytes), HexStringConverter.Default.ConvertToObject(bs)); bool eq = BytesValueComparer.AreEqual(df.Bytes, bs); if (!eq) { return(new DataErrorResult(receivePartName, df.Bytes, bs)); } } } // 2. check crc // get crcfg // DataField crcdf = this.DataFields.CRCDataField; if (crcdf != null) { // 2011-01-05 crc calc error // // // byte[] crcbs = this.CRCer.Calc(bytes, 0, this.DataFields.Length - crcdf.DataLength); int bsLengthForCRC = crcdf.BeginPosition; // 2011-12-30 crc calc begin position // //byte[] crcbs = this.CRCer.Calc(bytes, 0, bsLengthForCRC); int calcLen = crcdf.BeginPosition - this.CRCBegin; CheckCRCLength(calcLen); byte[] crcbs = this.CRCer.Calc(bytes, this.CRCBegin, calcLen); crcdf.Bytes = crcbs; if (!crcdf.Match(bytes, 0)) { return(new CRCErrorResult(receivePartName, crcbs, crcdf.GetMatch(bytes, 0))); } } // 3. check need match df // return(new SuccessResult(receivePartName)); }