/// <summary>
        /// Reads an Block from the Project and returns the block data that is stored in the S7 Project
        /// </summary>
        /// <param name="blkInfo">The Block info object that identifies the block to read from Disk</param>
        /// <param name="myConvOpt">Defines options that determine how the Block will be converted</param>
        /// <returns></returns>
        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 this is an interface DB, then rather take the Interface declaration from the instance FB,
                    //instead of the data sotred in the project.
                    //The reason is that if you change the comment in an FB, the DB data is not actualized and my contain outdated
                    //Declarations. When you change the interface, Step7 tells you to "regenerate" the instance DB which only then would
                    //Actualize the comments. Simple Commentary changes do not change the Datablocks row.
                    if (retVal.IsInstanceDB && myConvOpt.UseFBDeclarationForInstanceDB)
                    {
                        //load the FB data from the Project
                        tmpBlock InstFB = GetBlockBytes((myTmpBlk.IsSFB ? "SFB" : "FB") + myTmpBlk.FBNumber);

                        //Resolve both interfaces
                        List <string> tmpPar = new List <string>();
                        if (InstFB != null)
                        {
                            S7DataRow InterfaceFB =
                                Parameter.GetInterfaceOrDBFromStep7ProjectString(InstFB.blkinterface, ref tmpPar,
                                                                                 PLCBlockType.FB, false, this, null);
                            S7DataRow InterfaceDB =
                                Parameter.GetInterfaceOrDBFromStep7ProjectString(myTmpBlk.blkinterface, ref tmpPar,
                                                                                 PLCBlockType.DB, false, this, null);

                            //Only use the FB interface Declaration if they are compatible
                            if (Parameter.IsInterfaceCompatible(InterfaceFB, InterfaceDB))
                            {
                                myTmpBlk.blkinterface = InstFB.blkinterface;
                            }
                        }
                    }

                    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;
                    retVal.CheckSum = myTmpBlk.CheckSum;
                    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.Parameter);

                        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;
                    retVal.CheckSum = myTmpBlk.CheckSum;
                    blkInfo._Block  = retVal;

                    return(retVal);
                }
            }
            return(null);
        }
예제 #2
0
        public Block GetBlock(ProjectBlockInfo blkInfo)
        {
            if (blkInfo._Block != null)
            {
                return(blkInfo._Block);
            }

            ProjectPlcBlockInfo plcblkInfo = (ProjectPlcBlockInfo)blkInfo;
            //string aa = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(((Step5ProjectBlockInfo)blkInfo)._blkByte);
            //string bb = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(((Step5ProjectBlockInfo)blkInfo)._blkHeaderByte);
            Block retVal = null;

            if (blkInfo.BlockType == PLCBlockType.S5_DV)
            {
                retVal = MC5toDB.GetDB((ProjectPlcBlockInfo)plcblkInfo, null, ((S5ProjectBlockInfo)blkInfo)._blkByte, null);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_DVX)
            {
                retVal = MC5toDB.GetDB((ProjectPlcBlockInfo)plcblkInfo, null, ((S5ProjectBlockInfo)blkInfo)._blkByte, null);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_DB)
            {
                retVal = MC5toDB.GetDB((ProjectPlcBlockInfo)plcblkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, GetBlockInByte("S5_DV" + plcblkInfo.BlockNumber.ToString()), GetBlockInByte("S5_DK" + plcblkInfo.BlockNumber.ToString()));
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_DX)
            {
                retVal = MC5toDB.GetDB((ProjectPlcBlockInfo)plcblkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, GetBlockInByte("S5_DVX" + plcblkInfo.BlockNumber.ToString()), GetBlockInByte("S5_DKX" + plcblkInfo.BlockNumber.ToString()));
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_DK)
            {
                retVal = MC5toComment.GetCommentBlock(blkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_DKX)
            {
                retVal = MC5toComment.GetCommentBlock(blkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_PB)
            {
                retVal = MC5toAWL.GetFunctionBlock((ProjectPlcBlockInfo)blkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, null, GetBlockInByte("S5_PK" + plcblkInfo.BlockNumber.ToString()), (Step5ProgrammFolder)blkInfo.ParentFolder.Parent);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_FB)
            {
                retVal = MC5toAWL.GetFunctionBlock(plcblkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, GetBlockInByte("S5_FV" + plcblkInfo.BlockNumber.ToString()), GetBlockInByte("S5_FK" + plcblkInfo.BlockNumber.ToString()), (Step5ProgrammFolder)blkInfo.ParentFolder.Parent);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_SB)
            {
                retVal = MC5toAWL.GetFunctionBlock(plcblkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, null, GetBlockInByte("S5_SK" + plcblkInfo.BlockNumber.ToString()), (Step5ProgrammFolder)blkInfo.ParentFolder.Parent);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_FX)
            {
                retVal = MC5toAWL.GetFunctionBlock(plcblkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, GetBlockInByte("S5_FVX" + plcblkInfo.BlockNumber.ToString()), GetBlockInByte("S5_FKX" + plcblkInfo.BlockNumber.ToString()), (Step5ProgrammFolder)blkInfo.ParentFolder.Parent);
            }
            else if (blkInfo.BlockType == PLCBlockType.S5_OB)
            {
                retVal = MC5toAWL.GetFunctionBlock(plcblkInfo, ((S5ProjectBlockInfo)blkInfo)._blkByte, null, GetBlockInByte("S5_OK" + plcblkInfo.BlockNumber.ToString()), (Step5ProgrammFolder)blkInfo.ParentFolder.Parent);
            }

            if (retVal != null)
            {
                retVal.ParentFolder = this;
            }

            blkInfo._Block = retVal;

            return(retVal);
        }
예제 #3
0
        public static S5FunctionBlock GetFunctionBlock(ProjectPlcBlockInfo blkInfo, byte[] block, byte[] preHeader, byte[] commentBlock, Step5ProgrammFolder prjBlkFld)
        {
            S5FunctionBlock retVal = new S5FunctionBlock();

            retVal.BlockType   = blkInfo.BlockType;
            retVal.BlockNumber = blkInfo.BlockNumber;

            if (blkInfo.BlockType == PLCBlockType.S5_PB || blkInfo.BlockType == PLCBlockType.S5_SB || blkInfo.BlockType == PLCBlockType.S5_OB)
            {
                retVal.AWLCode = GetMC5Rows(block, 10, null, (Step5BlocksFolder)blkInfo.ParentFolder, retVal);
            }
            else
            {
                string fbNM = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(block, 12, 8);

                retVal.Name = fbNM;

                List <S5Parameter> pars = GetParameters(block);

                retVal.Parameter = pars;

                retVal.AWLCode = GetMC5Rows(block, /*10 + spa*2*/ 10 + ((pars.Count * 3) + 5) * 2, pars, (Step5BlocksFolder)blkInfo.ParentFolder, retVal);
            }
            //Go throug retval and add Symbols...
            if (prjBlkFld != null)
            {
                if (prjBlkFld.SymbolTable != null)
                {
                    foreach (S5FunctionBlockRow awlRow in retVal.AWLCode)
                    {
                        string para = awlRow.Parameter.Replace(" ", "");

                        awlRow.SymbolTableEntry = prjBlkFld.SymbolTable.GetEntryFromOperand(para);
                    }
                }
            }

            retVal.Networks = new List <Network>();

            S5FunctionBlockNetwork nw = null;

            if (retVal.AWLCode != null)
            {
                foreach (S5FunctionBlockRow s5FunctionBlockRow in retVal.AWLCode)
                {
                    if (s5FunctionBlockRow.Command == "BLD" && s5FunctionBlockRow.Parameter == "255")
                    {
                        if (nw != null)
                        {
                            nw.AWLCode.Add(s5FunctionBlockRow);
                        }
                        nw         = new S5FunctionBlockNetwork();
                        nw.Parent  = retVal;
                        nw.AWLCode = new List <FunctionBlockRow>();
                        ((S5FunctionBlockNetwork)nw).NetworkFunctionBlockRow = s5FunctionBlockRow;
                        retVal.Networks.Add(nw);
                    }
                    else
                    {
                        nw.AWLCode.Add(s5FunctionBlockRow);
                    }
                }
            }

            if (commentBlock != null)
            {
                int nr      = 26;
                int netzwnr = 0;
                Dictionary <int, object> commandLineNumerList = new Dictionary <int, object>();
                while (nr + 3 < commentBlock.Length)
                {
                    int zeile = commentBlock[nr];

                    int    len = 0x7f & commentBlock[nr + 2];
                    string cmt = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(commentBlock, nr + 3, len);

                    if (commentBlock[nr + 1] == 0x00)
                    {
                        netzwnr = zeile;
                        if (retVal.Networks.Count > netzwnr - 1 && netzwnr > 0)
                        {
                            ((S5FunctionBlockNetwork)retVal.Networks[netzwnr - 1]).Name = cmt;
                            commandLineNumerList.Clear();
                            int lineNr = 0;
                            foreach (S5FunctionBlockRow akRow in retVal.Networks[netzwnr - 1].AWLCode)
                            {
                                commandLineNumerList.Add(lineNr, akRow);
                                lineNr++;

                                if (IsCall(akRow))
                                {
                                    foreach (S5Parameter extPar in akRow.ExtParameter)
                                    {
                                        commandLineNumerList.Add(lineNr, extPar);
                                        lineNr++;
                                    }
                                }
                            }
                        }
                        else
                        {
                            //Todo: Throw an error when this happens???
                            //throw new Exception("Error in Block: " + retVal.BlockName);
                        }
                    }
                    else
                    {
                        if (commandLineNumerList.ContainsKey(zeile - 1))
                        {
                            object obj = commandLineNumerList[zeile - 1];
                            if (obj is S5Parameter)
                            {
                                ((S5Parameter)obj).Comment = cmt;
                            }
                            else
                            {
                                ((S5FunctionBlockRow)obj).Comment = cmt;
                            }
                        }
                    }
                    nr += len + 3;
                }
            }

            return(retVal);
        }
예제 #4
0
        public List <ProjectBlockInfo> readPlcBlocksList()
        {
            SymbolTable symtab = null;

            if (((S7ProgrammFolder)Parent) != null)
            {
                symtab = (SymbolTable)((S7ProgrammFolder)Parent).SymbolTable;
            }

            List <ProjectBlockInfo> retVal = new List <ProjectBlockInfo>();

            Connection.Connect();
            List <string> blks = Connection.PLCListBlocks(PLCBlockType.AllBlocks);

            foreach (var blk in blks)
            {
                ProjectPlcBlockInfo tmp = new ProjectPlcBlockInfo();
                tmp.ParentFolder = this;
                if (blk.Substring(0, 2) == "DB")
                {
                    tmp.BlockType   = PLCBlockType.DB;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(2));
                }
                else if (blk.Substring(0, 2) == "OB")
                {
                    tmp.BlockType   = PLCBlockType.OB;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(2));
                }
                else if (blk.Substring(0, 2) == "FC")
                {
                    tmp.BlockType   = PLCBlockType.FC;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(2));
                }
                else if (blk.Substring(0, 2) == "FB")
                {
                    tmp.BlockType   = PLCBlockType.FB;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(2));
                }
                else if (blk.Substring(0, 3) == "SFC")
                {
                    tmp.BlockType   = PLCBlockType.SFC;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(3));
                }
                else if (blk.Substring(0, 3) == "SFB")
                {
                    tmp.BlockType   = PLCBlockType.SFB;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(3));
                }
                else if (blk.Substring(0, 3) == "SDB")
                {
                    tmp.BlockType   = PLCBlockType.SDB;
                    tmp.BlockNumber = Convert.ToInt32(blk.Substring(3));
                }

                /*
                 * if (symtab != null)
                 * {
                 *  SymbolTableEntry sym = symtab.GetEntryFromOperand(tmp.ToString());
                 *  if (sym != null)
                 *      tmp.Symbol = sym.Symbol;
                 * }
                 */
                retVal.Add(tmp);
            }


            return(retVal);
        }
예제 #5
0
        public static S5DataBlock GetDB(ProjectPlcBlockInfo blkInfo, byte[] block, byte[] preHeader, byte[] commentBlock)
        {
            S5DataBlock retVal = new S5DataBlock();

            retVal.BlockType   = blkInfo.BlockType;
            retVal.BlockNumber = blkInfo.BlockNumber;

            S7DataRow main = new S7DataRow("STATIC", S7DataRowType.STRUCT, retVal);

            retVal.Structure = main;

            var zeilenListe = new List <S7DataRow>();

            if (preHeader != null)
            {
                int akcnt = 0;
                //S7DataRowType akRwTp = (S7DataRowType) (preHeader[9] | 0xf00);
                int anzTypes = (((preHeader[6] * 256 + preHeader[7]) - 2) / 2); //How many different Types are in the Header
                for (int n = 1; n <= anzTypes; n++)
                {
                    int rowStart = preHeader[(n - 1) * 4 + 10] * 256 + preHeader[(n - 1) * 4 + 11];
                    int rowStop  = preHeader[n * 4 + 10] * 256 + preHeader[n * 4 + 11];
                    var akRwTp   = (S7DataRowType)(preHeader[9 + (n - 1) * 4] | 0xf00);


                    if (akRwTp == S7DataRowType.S5_C || akRwTp == S7DataRowType.S5_KC)
                    {
                        for (int j = rowStart; j < rowStop; j += 24)
                        {
                            var row = new S7DataRow(string.Empty, akRwTp, retVal);
                            row.StringSize = rowStop - j > 12 ? 24 : (rowStop - j) * 2;
                            main.Add(row);
                            for (int q = 0; q < (row.StringSize / 2); q++)
                            {
                                zeilenListe.Add(row);
                            }
                        }
                    }
                    else if (akRwTp == S7DataRowType.S5_KG)
                    {
                        for (int j = rowStart; j < rowStop; j += 2)
                        {
                            var row = new S7DataRow(string.Empty, akRwTp, retVal);
                            main.Add(row);
                            zeilenListe.Add(row);
                            zeilenListe.Add(row);
                        }
                    }
                    else
                    {
                        for (int j = rowStart; j < rowStop; j += 1)
                        {
                            var row = new S7DataRow(string.Empty, akRwTp, retVal);
                            main.Add(row);
                            zeilenListe.Add(row);
                        }
                    }
                }
            }


            try
            {
                int st = 10;

                int maxZ = (block[8] * 256 + block[9]);
                int n    = 0;

                while (n < maxZ - 5)
                //foreach (var s7DataRow in main.Children)
                {
                    S7DataRow s7DataRow;
                    if (zeilenListe.Count > n)
                    {
                        s7DataRow = zeilenListe[n];
                    }
                    else
                    {
                        s7DataRow = new S7DataRow(string.Empty, S7DataRowType.S5_KH, retVal);
                        main.Add(s7DataRow);
                    }

                    switch (s7DataRow.DataType)
                    {
                    case S7DataRowType.S5_KF:
                        s7DataRow.Value = libnodave.getS16from(block, st);
                        break;

                    case S7DataRowType.S5_KZ:
                        s7DataRow.Value = libnodave.getBCD16from(block, st);
                        break;

                    case S7DataRowType.S5_KM:
                        s7DataRow.Value = libnodave.getU16from(block, st);
                        break;

                    case S7DataRowType.S5_KH:
                        s7DataRow.Value = libnodave.getU16from(block, st);
                        break;

                    case S7DataRowType.S5_KY:
                        s7DataRow.Value = libnodave.getU16from(block, st);
                        break;

                    case S7DataRowType.S5_KG:
                        s7DataRow.Value = libnodave.getS5Floatfrom(block, st);
                        break;

                    case S7DataRowType.S5_KC:
                    case S7DataRowType.S5_C:
                        s7DataRow.Value = System.Text.ASCIIEncoding.ASCII.GetString(block, st, s7DataRow.StringSize);
                        break;

                    case S7DataRowType.S5_KT:
                        s7DataRow.Value = libnodave.getS5Timefrom(block, st);
                        break;

                    default:
                        s7DataRow.Value = libnodave.getU16from(block, st);
                        break;
                    }

                    st += s7DataRow.ByteLength;
                    n  += s7DataRow.ByteLength / 2;
                }
            }
            catch (Exception ex)
            { }


            try
            {
                if (commentBlock != null && main._children != null && main._children.Count > 0)
                {
                    int nr    = 28;
                    int hdlen = 0x7f & commentBlock[nr];

                    retVal.Name = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(commentBlock, nr + 1, hdlen);

                    nr += hdlen + 1;
                    while (nr + 3 < commentBlock.Length)
                    {
                        int    zeile = ((commentBlock[nr + 1] - 128) * 256) + commentBlock[nr];
                        int    len   = 0x7f & commentBlock[nr + 2];
                        string cmt   = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(commentBlock, nr + 3, len);
                        //main._children[zeile].Comment = cmt;
                        zeilenListe[zeile].Comment = cmt;

                        nr += len + 3;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error parsing the Block Comments! Maybe the Step5 project is broken? \n" + ex.Message);
            }

            return(retVal);
        }
예제 #6
0
        public Block GetBlock(ProjectBlockInfo blkInfo, S7ConvertingOptions myConvOpt)
        {
            //Todo: Enable this, but then myConvOpt is only used the first time!
            //if (blkInfo._Block != null)
            //    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.BlockLanguage = (PLCLanguage)myTmpBlk.Language;

                    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;
                    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;
                    if (blkInfo.BlockType != PLCBlockType.UDT)
                    {
                        retVal.CodeSize = myTmpBlk.mc7code.Length + 36;
                    }

                    retVal.BlockLanguage = (PLCLanguage)myTmpBlk.Language;

                    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();
                    blkInfo._Block = retVal;

                    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.CodeSize          = myTmpBlk.mc7code.Length + 36;

                    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);
                    }

                    retVal.BlockLanguage = (PLCLanguage)myTmpBlk.Language;

                    return(retVal);

                    if (blkInfo.BlockType == PLCBlockType.FC || blkInfo.BlockType == PLCBlockType.FB || blkInfo.BlockType == PLCBlockType.OB)
                    {
                        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.AWLCode = JumpMarks.AddJumpmarks(retVal.AWLCode, myTmpBlk.jumpmarks, myTmpBlk.nwinfo);

                        LocalDataConverter.ConvertLocaldataToSymbols(retVal, myConvOpt);

                        FBStaticAccessConverter.ReplaceStaticAccess(retVal, prgFld, myConvOpt);

                        CallConverter.ConvertUCToCall(retVal, prgFld, this, myConvOpt, null);

                        #region UseComments from Block
                        if (myConvOpt.UseComments)
                        {
                            List <FunctionBlockRow> newAwlCode = new List <FunctionBlockRow>();

                            int n      = 0;
                            int j      = 0;
                            int subCnt = 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 > j)
                                        {
                                            while (retVal.AWLCode.Count - 1 > j && retVal.AWLCode[j].Command != "NETWORK")
                                            {
                                                newAwlCode.Add(retVal.AWLCode[j]);
                                                j++;
                                            }
                                            ((S7FunctionBlockRow)retVal.AWLCode[j]).NetworkName = tx1;
                                            ((S7FunctionBlockRow)retVal.AWLCode[j]).Comment     = tx2;
                                            newAwlCode.Add(retVal.AWLCode[j]);
                                        }
                                        j++;

                                        subCnt = 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 > j)
                                            {
                                                S7FunctionBlockRow akRw = (S7FunctionBlockRow)retVal.AWLCode[j];

                                                if (cmt[n + 4] == 0xc0 && q == anzUebsprungZeilen - 1)
                                                {
                                                    akRw.CombineDBAccess = false;
                                                }

                                                //Db Zugriff zusammenfügen...
                                                if (akRw.CombineDBAccess)
                                                {
                                                    S7FunctionBlockRow nRw = (S7FunctionBlockRow)retVal.AWLCode[j + 1];
                                                    nRw.Parameter = akRw.Parameter + "." + nRw.Parameter;
                                                    nRw.MC7       = Helper.CombineByteArray(akRw.MC7, nRw.MC7);

                                                    akRw = nRw;
                                                    retVal.AWLCode.RemoveAt(j + 1);
                                                }


                                                if (akRw.GetNumberOfLines() == 1)
                                                {
                                                    subCnt = 0;

                                                    lastRow = akRw;

                                                    newAwlCode.Add(akRw);
                                                    j++;
                                                }
                                                else
                                                {
                                                    lastRow = akRw;
                                                    if (subCnt == 0)
                                                    {
                                                        newAwlCode.Add(akRw);
                                                    }

                                                    if (akRw.GetNumberOfLines() - 1 == subCnt)
                                                    {
                                                        j++;
                                                        //subCnt = 0;
                                                        subCnt++;
                                                    }
                                                    else
                                                    {
                                                        subCnt++;
                                                    }
                                                }
                                            }
                                        }


                                        //if (lastRow == null || cmt[n + 4] != 0x80)
                                        if (lastRow == null || (cmt[n + 4] != 0x80 && cmt[n + 4] != 0xc0))
                                        {
                                            lastRow = new S7FunctionBlockRow();
                                            newAwlCode.Add(lastRow);
                                            subCnt = 0;
                                        }

                                        string tx1 = Project.ProjectEncoding.GetString(cmt, n + 6, kommLen);
                                        if (subCnt == 0)
                                        {
                                            lastRow.Comment = tx1;
                                        }
                                        else
                                        if (lastRow.Command == "CALL")
                                        {
                                            if (subCnt == 1)
                                            {
                                                lastRow.Comment = tx1;
                                            }
                                            else
                                            {
                                                lastRow.CallParameter[subCnt - 2].Comment = tx1;
                                            }
                                        }
                                        n += kommLen + 6;

                                        //subCnt = 0;
                                    }
                                }
                            }
                            while (j < retVal.AWLCode.Count)
                            {
                                newAwlCode.Add(retVal.AWLCode[j]);
                                j++;
                            }
                            retVal.AWLCode = newAwlCode;
                        }
                        #endregion
                    }

                    retVal.Networks = NetWork.GetNetworksList(retVal);

                    retVal.ParentFolder = this;

                    return(retVal);
                }
            }
            return(null);
        }