public static bool TryParse(BinaryReader reader, out BinFileInfomation info) { info = new BinFileInfomation(); try { var head = reader.ReadByte(); info.TargetObject = reader.ReadByte(); info._binFileSize = reader.ReadBytes(4); info._updateMode = reader.ReadBytes(2); info._firmwareReleaseDate = reader.ReadBytes(6); info._firmwareVersion = reader.ReadBytes(4); info._description = reader.ReadBytes(233); info._binFileCheckSum = reader.ReadBytes(2); info._describeCheckSum = reader.ReadBytes(2); reader.ReadByte(); var binfileBytes = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); var binCheck = BitConverter.GetBytes(CrcCheckSum.GetUsmbcrc16(binfileBytes, binfileBytes.Length)); Array.Reverse(binCheck); if (!info._binFileCheckSum.ToArray().SequenceEqual(binCheck)) { info = null; return(false); } var descCheck = new List <byte> { head, info.TargetObject }; var binSize = new byte[4]; info._binFileSize.CopyTo(binSize, 0); descCheck.AddRange(binSize); descCheck.AddRange(info._updateMode); descCheck.AddRange(info._firmwareReleaseDate); descCheck.AddRange(info._firmwareVersion); descCheck.AddRange(info._description); descCheck.AddRange(binCheck); var descCheckSum = BitConverter.GetBytes(CrcCheckSum.GetUsmbcrc16(descCheck.ToArray(), descCheck.Count)); Array.Reverse(descCheckSum); if (!info._describeCheckSum.ToArray().SequenceEqual(descCheckSum)) { info = null; return(false); } } catch (Exception) { info = null; return(false); } return(true); }
public override void DecodeFrame(byte[] buffer) { base.DecodeFrame(buffer); if (buffer.Length < BasicLength) { PackageStatus = PackageStatus.BufferHaveNoEnoughLength; return; } _decodeIndex = 0; if (buffer[_decodeIndex] != ProtocolHead) { PackageStatus = PackageStatus.InvalidHead; return; } _decodeIndex++; if (buffer[_decodeIndex] != CommandType) { PackageStatus = PackageStatus.ComponentError; return; } _decodeIndex++; if (buffer[_decodeIndex] != CommandByte) { PackageStatus = PackageStatus.ComponentError; return; } _decodeIndex++; OperateCode = buffer[_decodeIndex]; _decodeIndex++; if (buffer[_decodeIndex] != RequestCode[0] || buffer[_decodeIndex + 1] != RequestCode[1]) { PackageStatus = PackageStatus.ComponentError; return; } _decodeIndex += 2; var payloadDataLenth = buffer[_decodeIndex] << 8 | buffer[_decodeIndex + 1]; _decodeIndex += 2; if (_decodeIndex + payloadDataLenth + 3 > buffer.Length) { PackageStatus = PackageStatus.BufferHaveNoEnoughLength; return; } PayloadData = buffer.SubArray(_decodeIndex, payloadDataLenth); _decodeIndex += payloadDataLenth; CrcCheck = buffer.SubArray(_decodeIndex, 2); if (CrcCheckSum.GetUsmbcrc16(buffer, _decodeIndex) != (CrcCheck[0] << 8 | CrcCheck[1])) { PackageStatus = PackageStatus.CrcCheckFaild; return; } _decodeIndex += 2; if (buffer[_decodeIndex] != ProtocolTail) { PackageStatus = PackageStatus.InvalidTail; return; } _statusCode = PayloadData[14]; if (PayloadData.Length > 15) { Description = Encoding.GetEncoding("GBK").GetString(PayloadData, 15, PayloadData.Length - 15); } PackageStatus = PackageStatus.DecodeCompleted; }