Example #1
0
        private bool readProjEntry(string fileName, ref string msg)
        {
            // read Project header data
            FileStream   fs = new FileStream(fileName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);

            m_BlockOverview = new Dictionary <int, BlockInfo>(m_ProjectInfo.ObjectCount);

            br.BaseStream.Seek(m_ProjectInfo.OffsetBlockData, SeekOrigin.Begin);
            long p = br.BaseStream.Position;

            for (int i = 0; i < m_ProjectInfo.ObjectCount; i++)
            {
                BlockInfo bi = new BlockInfo();
                bi.BlockNumber    = br.ReadByte();
                bi.BlockType      = (eBlockType)br.ReadByte();
                bi.isActive       = br.ReadBoolean();
                bi.Size           = (uint)br.ReadInt32();
                bi.InstrCount     = (uint)br.ReadInt32();
                bi.UpdateNumber   = (uint)br.ReadInt32();
                bi.DateCreation   = new DateTime(br.ReadUInt16(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
                bi.DateLastModify = new DateTime(br.ReadUInt16(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
                bi.FileVersion    = new Version(br.ReadByte(), br.ReadByte());

                List <char> stringList = new List <char>();
                while (br.PeekChar() != 0)
                {
                    stringList.Add((char)br.ReadByte());
                }
                br.ReadChar();
                bi.Name = new String(stringList.ToArray());

                stringList = new List <char>();
                while (br.PeekChar() != 0)
                {
                    stringList.Add((char)br.ReadByte());
                }
                br.ReadChar();
                bi.NameCompany = new String(stringList.ToArray());

                stringList = new List <char>();
                while (br.PeekChar() != 0)
                {
                    stringList.Add((char)br.ReadByte());
                }
                br.ReadChar();
                bi.NameAuthor = new String(stringList.ToArray());

                //check error case file is to early at the end
                int c = br.PeekChar();
                if (c == -1 && i != m_ProjectInfo.ObjectCount - 1)
                {
                    msg = "Unexpected file end\n-> " + fileName;
                    return(true);
                }

                m_BlockOverview.Add(i, bi);
            }

            br.Close();
            fs.Close();

            return(false);
        }
Example #2
0
        private bool readBlocks(int i, string path, string fileName, ref string msg, ref List <byte> instrList)
        {
            FileInfo fi = new FileInfo(Path.Combine(path, fileName));

            if (!fi.Exists || fi.Length < 26)    // file size min 26 byte (file header + 2 instructions)
            {
                msg = "File not found or to small:\n-> " + fi.FullName;
                return(true);
            }

            FileStream   fs = new FileStream(fi.FullName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);

            COBFileHeader header = new COBFileHeader(
                br.ReadByte(), br.ReadByte(),
                br.ReadByte(), br.ReadBoolean(),
                br.ReadUInt32(), br.ReadUInt32(),
                br.ReadByte(),
                br.ReadChar(), br.ReadChar());

            br.ReadByte();  // read reserved byte

            if (header.FileTag[0] != 'O' || header.FileTag[1] != 'B')
            {
                br.Close();
                fs.Close();
                msg = "File is no CKT-PLC Object file or is corupted!\n-> " + fi.Name;
                return(true);
            }
            else if (header.CompilerVersion > PLCCPU.CPU_VERSION)
            {
                br.Close();
                fs.Close();
                msg = "The Compiler version is not supported from this CPU!\nFILE: " + header.CompilerVersion.ToString() + " | CPU: " + PLCCPU.CPU_VERSION;
                return(true);
            }
            else if (header.BlockType != eBlockType.PB)  //TODO: add the other Blocktypes when ready
            {
                br.Close();
                fs.Close();
                msg = "File has a not supported Block type!\n-> " + fi.Name;
                return(true);
            }
            else if (header.InstrCount < 2)
            {
                br.Close();
                fs.Close();
                msg = "File has not enough instructions!\nCOUNT = " + header.InstrCount + "\n-> " + fi.Name;
                return(true);
            }

            BlockInfo tmp = m_BlockOverview[i];

            tmp.CompilerVersion = header.CompilerVersion;
            m_BlockOverview.Remove(i);
            m_BlockOverview.Add(i, tmp);

            if (m_BlockOverview[i].isActive && header.isActive)
            {
                msg += "\nNAME: " + fi.Name.ToUpper();
                msg += "\nSIZE: " + fi.Length;
                msg += " | INSTR: " + header.InstrCount;
                msg += "\nDATE: " + fi.LastWriteTime;

                List <byte> BlockInstrList = new List <byte>((int)(header.InstrCount * INSTR_SIZE));
                int         j = 0;
                while (br.PeekChar() != -1)
                {
                    BlockInstrList.Add(br.ReadByte());
                    if (BlockInstrList[j] == 0)
                    {
                        msg = "No CPU command found, File corupted!\n-> " + fi.Name;
                        return(true);
                    }

                    BlockInstrList.Add(br.ReadByte());
                    BlockInstrList.Add(br.ReadByte());
                    BlockInstrList.Add(br.ReadByte());
                    BlockInstrList.Add(br.ReadByte());

                    j += INSTR_SIZE;
                }

                br.Close();
                fs.Close();

                if (BlockInstrList.Count != header.InstrCount * INSTR_SIZE)
                {
                    msg = "Instruction count from file not correct!\nFILE SHOULD: " + header.InstrCount +
                          "\nFILE HAS:    " + (BlockInstrList.Count / INSTR_SIZE) +
                          "\n-> " + fi.Name;
                    return(true);
                }

                foreach (byte b in BlockInstrList)
                {
                    instrList.Add(b);
                }
            }
            else
            {
                msg += "\nFile is not active!";
                msg += "\nNAME: " + fi.Name.ToUpper();
                msg += "\nSIZE: " + fi.Length;
                msg += " | INSTR: " + header.InstrCount;
                msg += "\nDATE: " + fi.LastWriteTime;
            }

            Console.Clear();
            Console.WriteLine("Object file loaded:");
            Console.WriteLine(msg);

            Console.ReadKey();

            return(false);
        }
Example #3
0
        private bool readProjEntry(string fileName, ref string msg)
        {
            // read Project header data
            FileStream fs = new FileStream(fileName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);

            m_BlockOverview = new Dictionary<int, BlockInfo>(m_ProjectInfo.ObjectCount);

            br.BaseStream.Seek(m_ProjectInfo.OffsetBlockData, SeekOrigin.Begin);
            long p = br.BaseStream.Position;
            for (int i = 0; i < m_ProjectInfo.ObjectCount; i++)
            {
                BlockInfo bi = new BlockInfo();
                bi.BlockNumber = br.ReadByte();
                bi.BlockType = (eBlockType)br.ReadByte();
                bi.isActive = br.ReadBoolean();
                bi.Size = (uint)br.ReadInt32();
                bi.InstrCount = (uint)br.ReadInt32();
                bi.UpdateNumber = (uint)br.ReadInt32();
                bi.DateCreation = new DateTime(br.ReadUInt16(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
                bi.DateLastModify = new DateTime(br.ReadUInt16(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
                bi.FileVersion = new Version(br.ReadByte(), br.ReadByte());

                List<char> stringList = new List<char>();
                while (br.PeekChar() != 0)
                {
                    stringList.Add((char)br.ReadByte());
                }
                br.ReadChar();
                bi.Name = new String(stringList.ToArray());

                stringList = new List<char>();
                while (br.PeekChar() != 0)
                {
                    stringList.Add((char)br.ReadByte());
                }
                br.ReadChar();
                bi.NameCompany = new String(stringList.ToArray());

                stringList = new List<char>();
                while (br.PeekChar() != 0)
                {
                    stringList.Add((char)br.ReadByte());
                }
                br.ReadChar();
                bi.NameAuthor = new String(stringList.ToArray());

                //check error case file is to early at the end
                int c = br.PeekChar();
                if (c == -1 && i != m_ProjectInfo.ObjectCount - 1)
                {
                    msg = "Unexpected file end\n-> " + fileName;
                    return true;
                }

                m_BlockOverview.Add(i, bi);
            }

            br.Close();
            fs.Close();

            return false;
        }