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