public Block GetBlock(ProjectBlockInfo blkInfo, S7ConvertingOptions myConvOpt) { if (blkInfo._Block != null && ((blkInfo._Block) as S7Block).usedS7ConvertingOptions.Equals(myConvOpt)) return blkInfo._Block; ProjectPlcBlockInfo plcblkifo = (ProjectPlcBlockInfo)blkInfo; tmpBlock myTmpBlk = GetBlockBytes(blkInfo); List<Step7Attribute> step7Attributes = null; if (myTmpBlk != null) { if (myTmpBlk.uda != null) { int uPos = 2; if (myTmpBlk.uda != null && myTmpBlk.uda.Length > 0 && myTmpBlk.uda[0] > 0) { step7Attributes = new List<Step7Attribute>(); for (int j = 0; j < myTmpBlk.uda[0]; j++) { string t1 = Project.ProjectEncoding.GetString(myTmpBlk.uda, uPos + 1, myTmpBlk.uda[uPos]); uPos += myTmpBlk.uda[uPos] + 1; string t2 = Project.ProjectEncoding.GetString(myTmpBlk.uda, uPos + 1, myTmpBlk.uda[uPos]); uPos += myTmpBlk.uda[uPos] + 1; step7Attributes.Add(new Step7Attribute(t1, t2)); } } } //Begin with the Block Reading... if (blkInfo.BlockType == PLCBlockType.VAT) { S7VATBlock retValBlock = new S7VATBlock(myTmpBlk.mc7code, myTmpBlk.comments, plcblkifo.BlockNumber, Project.ProjectEncoding); retValBlock.Attributes = step7Attributes; retValBlock.LastCodeChange = myTmpBlk.LastCodeChange; retValBlock.LastInterfaceChange = myTmpBlk.LastInterfaceChange; retValBlock.ParentFolder = this; retValBlock.usedS7ConvertingOptions = myConvOpt; blkInfo._Block = retValBlock; return retValBlock; } else if (blkInfo.BlockType == PLCBlockType.DB || blkInfo.BlockType == PLCBlockType.UDT) { List<string> tmpList = new List<string>(); S7DataBlock retVal = new S7DataBlock(); retVal.IsInstanceDB = myTmpBlk.IsInstanceDB; retVal.FBNumber = myTmpBlk.FBNumber; if (myTmpBlk.mc7code != null) retVal.CodeSize = myTmpBlk.mc7code.Length; retVal.StructureFromString = Parameter.GetInterfaceOrDBFromStep7ProjectString(myTmpBlk.blkinterface, ref tmpList, blkInfo.BlockType, false, this, retVal, myTmpBlk.mc7code); if (myTmpBlk.blkinterfaceInMC5 != null) { //List<string> tmp = new List<string>(); //retVal.StructureFromMC7 = Parameter.GetInterface(myTmpBlk.blkinterfaceInMC5, myTmpBlk.mc7code, ref tmp, blkInfo.BlockType, myTmpBlk.IsInstanceDB, retVal); } retVal.BlockNumber = plcblkifo.BlockNumber; retVal.BlockType = blkInfo.BlockType; retVal.Attributes = step7Attributes; retVal.LastCodeChange = myTmpBlk.LastCodeChange; retVal.LastInterfaceChange = myTmpBlk.LastInterfaceChange; retVal.ParentFolder = this; retVal.usedS7ConvertingOptions = myConvOpt; blkInfo._Block = retVal; return retVal; } else if (blkInfo.BlockType == PLCBlockType.FC || blkInfo.BlockType == PLCBlockType.FB || blkInfo.BlockType == PLCBlockType.OB || blkInfo.BlockType == PLCBlockType.SFB || blkInfo.BlockType == PLCBlockType.SFC) { List<string> ParaList = new List<string>(); S7FunctionBlock retVal = new S7FunctionBlock(); retVal.LastCodeChange = myTmpBlk.LastCodeChange; retVal.LastInterfaceChange = myTmpBlk.LastInterfaceChange; retVal.BlockNumber = plcblkifo.BlockNumber; retVal.BlockType = blkInfo.BlockType; retVal.Attributes = step7Attributes; retVal.KnowHowProtection = myTmpBlk.knowHowProtection; retVal.MnemonicLanguage = Project.ProjectLanguage; retVal.Author = myTmpBlk.username; retVal.Version = myTmpBlk.version; retVal.Parameter = Parameter.GetInterfaceOrDBFromStep7ProjectString(myTmpBlk.blkinterface, ref ParaList, blkInfo.BlockType, false, this, retVal); if (myTmpBlk.blockdescription != null) { retVal.Title = Project.ProjectEncoding.GetString(myTmpBlk.blockdescription, 3, myTmpBlk.blockdescription[1] - 4); retVal.Description = Project.ProjectEncoding.GetString(myTmpBlk.blockdescription, myTmpBlk.blockdescription[1], myTmpBlk.blockdescription.Length - myTmpBlk.blockdescription[1] - 1).Replace("\n", Environment.NewLine); } if (blkInfo.BlockType == PLCBlockType.FC || blkInfo.BlockType == PLCBlockType.FB || blkInfo.BlockType == PLCBlockType.OB) { retVal.CodeSize = myTmpBlk.mc7code.Length; int[] Networks; Networks = NetWork.GetNetworks(0, myTmpBlk.nwinfo); S7ProgrammFolder prgFld = null; if (this.Parent is S7ProgrammFolder) prgFld = (S7ProgrammFolder)this.Parent; retVal.AWLCode = MC7toAWL.GetAWL(0, myTmpBlk.mc7code.Length - 2, (int)myConvOpt.Mnemonic, myTmpBlk.mc7code, Networks, ParaList, prgFld, retVal); retVal.AWLCode = JumpMarks.AddJumpmarks(retVal.AWLCode, myTmpBlk.jumpmarks, myTmpBlk.nwinfo, myConvOpt); LocalDataConverter.ConvertLocaldataToSymbols(retVal, myConvOpt); CallConverter.ConvertUCToCall(retVal, prgFld, this, myConvOpt, null); FBStaticAccessConverter.ReplaceStaticAccess(retVal, prgFld, myConvOpt); #region UseComments from Block if (myConvOpt.UseComments) { List<FunctionBlockRow> newAwlCode = new List<FunctionBlockRow>(); int n = 0; int akRowInAwlCode = 0; int lineNumberInCall = 0; //Counter wich line in Command (for Calls and UCs) if (myTmpBlk.comments != null) { byte[] cmt = myTmpBlk.comments; //string aa = System.Text.Encoding.GetEncoding("Windows-1251").GetString(cmt); //string testaa = ""; while (n < myTmpBlk.comments.Length) { int kommLen = cmt[n + 0]; int startNWKomm = cmt[n + 1]; int anzUebsprungZeilen = cmt[n + 2] + cmt[n + 3] * 0x100; int lenNWKommZeile = cmt[n + 3] + cmt[n + 4] * 0x100; //Console.WriteLine(cmt[n + 5].ToString("X")); if (cmt[n + 5] == 0x06) { //NWKomentar: string tx1 = Project.ProjectEncoding.GetString(cmt, n + 6, startNWKomm - 7); string tx2 = Project.ProjectEncoding.GetString(cmt, n + startNWKomm, lenNWKommZeile - startNWKomm - 1).Replace("\n", Environment.NewLine); n += lenNWKommZeile; if (retVal.AWLCode.Count > akRowInAwlCode) { while (retVal.AWLCode.Count - 1 > akRowInAwlCode && retVal.AWLCode[akRowInAwlCode].Command != "NETWORK") { if (!newAwlCode.Contains(retVal.AWLCode[akRowInAwlCode])) { //newAwlCode.Add(retVal.AWLCode[akRowInAwlCode]); S7FunctionBlockRow akRw = (S7FunctionBlockRow)retVal.AWLCode[akRowInAwlCode]; if (akRw.CombineDBAccess) { S7FunctionBlockRow nRw = (S7FunctionBlockRow)retVal.AWLCode[akRowInAwlCode + 1]; if (!nRw.Parameter.Contains("[")) { nRw.Parameter = akRw.Parameter + "." + nRw.Parameter; nRw.MC7 = Helper.CombineByteArray(akRw.MC7, nRw.MC7); nRw.Label = akRw.Label ?? nRw.Label; akRw = nRw; retVal.AWLCode.RemoveAt(akRowInAwlCode + 1); } } if (!newAwlCode.Contains(akRw)) newAwlCode.Add(akRw); } akRowInAwlCode++; } ((S7FunctionBlockRow)retVal.AWLCode[akRowInAwlCode]).NetworkName = tx1; ((S7FunctionBlockRow)retVal.AWLCode[akRowInAwlCode]).Comment = tx2; newAwlCode.Add(retVal.AWLCode[akRowInAwlCode]); } akRowInAwlCode++; lineNumberInCall = 0; } else { S7FunctionBlockRow lastRow = null; //Anzahl der Anweisungen vor diesem Kommentar (inklusive aktueller Zeile!) for (int q = 0; q < (anzUebsprungZeilen); q++) { if (retVal.AWLCode.Count > akRowInAwlCode) { S7FunctionBlockRow akRw = (S7FunctionBlockRow) retVal.AWLCode[akRowInAwlCode]; if (cmt[n + 4] == 0xc0 && q == anzUebsprungZeilen-1) akRw.CombineDBAccess = false; //Db Zugriff zusammenfügen... if (akRw.CombineDBAccess) { S7FunctionBlockRow nRw = (S7FunctionBlockRow) retVal.AWLCode[akRowInAwlCode + 1]; nRw.Parameter = akRw.Parameter + "." + nRw.Parameter; nRw.MC7 = Helper.CombineByteArray(akRw.MC7, nRw.MC7); nRw.Label = akRw.Label ?? nRw.Label; akRw = nRw; retVal.AWLCode.RemoveAt(akRowInAwlCode + 1); } if (!newAwlCode.Contains(akRw)) newAwlCode.Add(akRw); if (akRw.GetNumberOfLines() == 1) { lineNumberInCall = 0; lastRow = akRw; //if (!newAwlCode.Contains(akRw)) // newAwlCode.Add(akRw); akRowInAwlCode++; } else { lastRow = akRw; if (lineNumberInCall == 0 && !(cmt[n + 4] != 0x80 && cmt[n + 4] != 0xc0)) { //if (!newAwlCode.Contains(akRw)) // newAwlCode.Add(akRw); } if (akRw.GetNumberOfLines() - 1 == lineNumberInCall) { akRowInAwlCode++; lineNumberInCall = 0; //subCnt++; //The set to zero was wrong here, but maybe now comments on calls do not work, need to check! } else { lineNumberInCall++; } } } } //if (lastRow == null || cmt[n + 4] != 0x80) if (lastRow == null || (cmt[n + 4] != 0x80 && cmt[n + 4] != 0xc0)) { lastRow = new S7FunctionBlockRow(){ Parent = retVal }; newAwlCode.Add(lastRow); lineNumberInCall = 0; } string tx1 = Project.ProjectEncoding.GetString(cmt, n + 6, kommLen); if (lineNumberInCall == 0) lastRow.Comment = tx1; else if (lastRow.Command == "CALL") if (lineNumberInCall == 1) lastRow.Comment = tx1; else { if (lastRow.CallParameter.Count >= lineNumberInCall - 2) { lastRow.CallParameter[lineNumberInCall - 2].Comment = tx1; } } n += kommLen + 6; //subCnt = 0; } } } while (akRowInAwlCode < retVal.AWLCode.Count) { newAwlCode.Add(retVal.AWLCode[akRowInAwlCode]); akRowInAwlCode++; } retVal.AWLCode = newAwlCode; } #endregion } retVal.Networks = NetWork.GetNetworksList(retVal); retVal.ParentFolder = this; retVal.usedS7ConvertingOptions = myConvOpt; blkInfo._Block = retVal; return retVal; } } return null; }
private static S7Block CreateBlock(string blockType, string blockNumber, string type) { S7Block akBlock = null; switch (blockType.ToUpper()) { case ("TYPE"): { akBlock = new S7DataBlock() { BlockType = DataTypes.PLCBlockType.UDT, BlockLanguage = PLCLanguage.DB }; } break; case ("DATA_BLOCK"): { akBlock = new S7DataBlock() { BlockType = DataTypes.PLCBlockType.DB, BlockLanguage = PLCLanguage.DB }; } break; case ("FUNCTION"): { akBlock = new S7FunctionBlock() { BlockType = DataTypes.PLCBlockType.FC, BlockLanguage = PLCLanguage.AWL }; S7DataRow parameterRoot = new S7DataRow("ROOTNODE", S7DataRowType.STRUCT, akBlock); S7DataRow parameterIN = new S7DataRow("IN", S7DataRowType.STRUCT, akBlock); S7DataRow parameterOUT = new S7DataRow("OUT", S7DataRowType.STRUCT, akBlock); S7DataRow parameterINOUT = new S7DataRow("IN_OUT", S7DataRowType.STRUCT, akBlock); S7DataRow parameterSTAT = new S7DataRow("STATIC", S7DataRowType.STRUCT, akBlock); S7DataRow parameterTEMP = new S7DataRow("TEMP", S7DataRowType.STRUCT, akBlock); parameterOUT.Add(new S7DataRow("RET_VAL", (S7DataRowType)Enum.Parse(typeof(S7DataRowType), type), akBlock)); parameterRoot.Children.Add(parameterIN); parameterRoot.Children.Add(parameterOUT); parameterRoot.Children.Add(parameterINOUT); parameterRoot.Children.Add(parameterSTAT); parameterRoot.Children.Add(parameterTEMP); } break; case ("FUNCTION_BLOCK"): { akBlock = new S7FunctionBlock() { BlockType = DataTypes.PLCBlockType.FB, BlockLanguage = PLCLanguage.AWL }; S7DataRow parameterRoot = new S7DataRow("ROOTNODE", S7DataRowType.STRUCT, akBlock); S7DataRow parameterIN = new S7DataRow("IN", S7DataRowType.STRUCT, akBlock); S7DataRow parameterOUT = new S7DataRow("OUT", S7DataRowType.STRUCT, akBlock); S7DataRow parameterINOUT = new S7DataRow("IN_OUT", S7DataRowType.STRUCT, akBlock); S7DataRow parameterTEMP = new S7DataRow("TEMP", S7DataRowType.STRUCT, akBlock); parameterRoot.Children.Add(parameterIN); parameterRoot.Children.Add(parameterOUT); parameterRoot.Children.Add(parameterINOUT); parameterRoot.Children.Add(parameterTEMP); } break; } return akBlock; }