public IProtocolPackage EncodeCommand(IProtocolCommand command, Dictionary <string, byte[]> paramBytes = null) { var package = new BytesProtocolPackage(command) { [StructureNames.CmdType] = { ComponentContent = command.CommandTypeCode }, [StructureNames.CmdByte] = { ComponentContent = command.CommandCode } }; foreach (var definition in command.CommandDefinitions) { package[definition.StructureName].ComponentContent = definition.ContentBytes; } if (paramBytes != null) { foreach (var paramByte in paramBytes) { package[paramByte.Key].ComponentContent = paramByte.Value; } } var crcValue = Globals.GetUsmbcrc16(package.GetBytes(), (ushort)(package.PackageLenth - 3)); package[StructureNames.CRCValue].ComponentContent = Globals.Uint16ToBytes(crcValue, false); package.Finalization(); return(package); }
public IProtocolPackage DecodeProtocol(byte[] bufferBytes, Protocol matchedProtocol) { var package = new BytesProtocolPackage() { Protocol = matchedProtocol, ReceiveDateTime = DateTime.Now }; var structures = matchedProtocol.ProtocolStructures.ToList(); var currentIndex = 0; for (var i = 0; i < structures.Count; i++) { var structure = structures.First(obj => obj.StructureIndex == i); //协议中,数据段如果是自由组织的形式,那么数据库中设置数据段长度为零。解码时,按照协议中的DataLength段的值解码数据段。 var componentDataLength = structure.StructureName == StructureNames.Data && structure.StructureDataLength == 0 ? Globals.BytesToInt16(package["DataLength"].ComponentContent, 0, true) : structure.StructureDataLength; if (currentIndex + componentDataLength > bufferBytes.Length) { package.Status = PackageStatus.NoEnoughBuffer; return(package); } if (structure.StructureName == StructureNames.Data) { DetectCommand(package, matchedProtocol); componentDataLength = package.Command.ReceiveBytesLength == 0 ? componentDataLength : package.Command.ReceiveBytesLength; } var component = new PackageComponent <byte[]> { ComponentName = structure.StructureName, DataType = structure.DataType, ComponentIndex = structure.StructureIndex, ComponentContent = bufferBytes.SubBytes(currentIndex, currentIndex + componentDataLength) }; currentIndex += componentDataLength; package[structure.StructureName] = component; } DecodeCommand(package); return(package); }