public void Dispose() { parent = null; }
static private void Read(ProjectModel pmodel, IList <byte> _odata, IList <byte> _edata) { odata = _odata; oid = 0; edata = _edata; eid = 0; regs.Clear(); try { option = edata[eid++]; while (eid < edata.Count) { int head = edata[eid++]; switch (head) { case 0xff: LadderDiagramViewModel ldvmodel = new LadderDiagramViewModel(String.Empty, pmodel); Read(ldvmodel); if (ldvmodel.ProgramName.Equals("main")) { pmodel.MainRoutine = ldvmodel; } else { pmodel.Add(ldvmodel); } break; case 0xfd: FuncBlockViewModel fbvmodel = new FuncBlockViewModel(String.Empty, pmodel); Read(fbvmodel); pmodel.Add(fbvmodel); break; case 0xfc: ReadRegisters(); break; case 0xfb: ModbusTableViewModel mtvmodel = new ModbusTableViewModel(pmodel); Read(mtvmodel); pmodel.MTVModel = mtvmodel; break; case 0xf9: MainMonitor mmoni = new MainMonitor(pmodel); Read(mmoni); pmodel.MMonitorManager.MMWindow = mmoni; break; default: throw new FormatException( String.Format("非法头标志符0x{0x2X}", head)); } } pmodel.MainRoutine = new LadderDiagramViewModel("main", pmodel); } catch (OutOfMemoryException) { } catch (FormatException) { } }
/// <summary> /// 将工程文件写入二进制数据,保存在根目录下的down.bin中 /// </summary> /// <param name="pmodel">工程类</param> /// <param name="option">选项</param> static public void Write(ProjectModel pmodel, int option) { // 初始化,清空所有记录 odata.Clear(); edata.Clear(); regs.Clear(); regids.Clear(); // 保存选项 edata.Add(Int32_Low(option)); // 写入设置参数 WriteParameters(pmodel); // 获取所有已经使用的软元件 GetRegisters(pmodel.MainRoutine); foreach (LadderDiagramViewModel ldvmodel in pmodel.SubRoutines) { GetRegisters(ldvmodel); } if ((option & OPTION_MONITOR) != 0) { foreach (MonitorVariableTable mvtable in pmodel.MMonitorManager.MMWindow.tables) { GetRegisters(mvtable); } } // 写入所有软元件 WriteRegisters(option); // 写入所有梯形图 Write(pmodel.MainRoutine, option); foreach (LadderDiagramViewModel ldvmodel in pmodel.SubRoutines) { Write(ldvmodel, option); } // 写入所有函数功能块 foreach (FuncBlockViewModel fbvmdoel in pmodel.FuncBlocks) { Write(fbvmdoel, option); } Write(pmodel.MTVModel, option); // 写入监视表 Write(pmodel.MMonitorManager.MMWindow, option); // 写入工程树 ProjectTreeView ptview = pmodel.IFacade.PTView; Write(ptview, option); // 设置路径 string currentpath = FileHelper.AppRootPath; string execfile = String.Format(@"{0:s}\downc.bin", currentpath); string datafile = String.Format(@"{0:s}\downe.bin", currentpath); string packfile = String.Format(@"{0:s}\downe.rar", currentpath); // 写入待压缩的压缩数据 BinaryWriter bw = new BinaryWriter( new FileStream(datafile, FileMode.Create)); bw.Write(edata.ToArray()); bw.Close(); // 后台调用Rar进行压缩 Process cmd = null; cmd = new Process(); cmd.StartInfo.FileName = String.Format(@"{0:s}\rar\Rar", currentpath); cmd.StartInfo.Arguments = String.Format("a -m5 -ep \"{0:s}\" \"{1:s}\"", packfile, datafile); cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.RedirectStandardError = true; cmd.Start(); cmd.WaitForExit(); BinaryReader br = null; // 获得执行程序 br = new BinaryReader( new FileStream(execfile, FileMode.Open)); odata.Clear(); while (br.BaseStream.CanRead) { try { odata.Add(br.ReadByte()); } catch (EndOfStreamException) { break; } } br.Close(); // 获得压缩后的数据 br = new BinaryReader( new FileStream(packfile, FileMode.Open)); edata.Clear(); /* * while (br.BaseStream.CanRead) * { * try * { * edata.Add(br.ReadByte()); * } * catch (EndOfStreamException) * { * break; * } * } * br.Close(); */ }
/// <summary> /// 写入所有参数设置 /// </summary> /// <param name="pmodel">工程类</param> static private void WriteParameters(ProjectModel pmodel) { int szid = odata.Count(); odata.Add(0x00); odata.Add(0x00); CommunicationInterfaceParams com232params = (CommunicationInterfaceParams)(ProjectPropertyManager.ProjectPropertyDic["CommunParams232"]); CommunicationInterfaceParams com485params = (CommunicationInterfaceParams)(ProjectPropertyManager.ProjectPropertyDic["CommunParams485"]); PasswordParams pwparams = (PasswordParams)(ProjectPropertyManager.ProjectPropertyDic["PasswordParams"]); FilterParams ftparams = (FilterParams)(ProjectPropertyManager.ProjectPropertyDic["FilterParams"]); HoldingSectionParams hsparams = (HoldingSectionParams)(ProjectPropertyManager.ProjectPropertyDic["HoldingSectParams"]); odata.Add(Int32_Low(com232params.BaudRateIndex)); odata.Add(Int32_Low(com232params.DataBitIndex)); odata.Add(Int32_Low(com232params.StopBitIndex)); odata.Add(Int32_Low(com232params.CheckCodeIndex)); odata.Add(Int32_Low(com485params.BaudRateIndex)); odata.Add(Int32_Low(com485params.DataBitIndex)); odata.Add(Int32_Low(com485params.StopBitIndex)); odata.Add(Int32_Low(com485params.CheckCodeIndex)); odata.Add(Int32_Low(com232params.StationNum)); odata.Add(Int32_Low(Int32_Low(com232params.Timeout))); odata.Add(Int32_Low(Int32_High(com232params.Timeout))); odata.Add((byte)(pwparams.UPIsChecked ? 1 : 0)); WriteO(pwparams.UPassword); odata.Add((byte)(com232params.CommuType)); odata.Add((byte)(com485params.CommuType)); odata.Add((byte)(pwparams.DPIsChecked ? 1 : 0)); WriteO(pwparams.DPassword); odata.Add((byte)(pwparams.MPIsChecked ? 1 : 0)); WriteO(pwparams.MPassword); odata.Add((byte)(ftparams.IsChecked ? 1 : 0)); int fttime = 1 << (ftparams.FilterTimeIndex + 1); odata.Add(Int32_Low(fttime)); odata.Add(Int32_High(fttime)); odata.Add(Int32_Low(hsparams.MStartAddr)); odata.Add(Int32_High(hsparams.MStartAddr)); odata.Add(Int32_Low(hsparams.MLength)); odata.Add(Int32_High(hsparams.MLength)); odata.Add(Int32_Low(hsparams.SStartAddr)); odata.Add(Int32_High(hsparams.SStartAddr)); odata.Add(Int32_Low(hsparams.SLength)); odata.Add(Int32_High(hsparams.SLength)); odata.Add(Int32_Low(hsparams.DStartAddr)); odata.Add(Int32_High(hsparams.DStartAddr)); odata.Add(Int32_Low(hsparams.DLength)); odata.Add(Int32_High(hsparams.DLength)); odata.Add(Int32_Low(hsparams.CVStartAddr)); odata.Add(Int32_High(hsparams.CVStartAddr)); odata.Add(Int32_Low(hsparams.CVLength)); odata.Add(Int32_High(hsparams.CVLength)); int sz = odata.Count() - szid - 2; odata[szid] = Int32_Low(sz); odata[szid + 1] = Int32_High(sz); }
static public int Simulate(ProjectModel _pmodel) { pmodel = _pmodel; smodel = new SimulateModel(); smmanager = new SimulateMonitorManager(_pmodel.MMonitorManager.MMWindow, smodel); Setup(pmodel.MainRoutine); foreach (LadderDiagramViewModel lvdmodel in pmodel.SubRoutines) { Setup(lvdmodel); } int ret = GenerateHelper.GenerateSimu(pmodel); switch (ret) { case SimulateDllModel.LOADDLL_OK: smodel.ShowWindow(); pmodel.LadderMode = LadderMode.Simulate; smmanager.Initialize(); SimuBrpoWindow bpwindow = pmodel.IFacade.BPWindow; bpwindow.Route(pmodel); bpwindow.SModel = smodel; bpwindow.SManager = smodel.SManager; smodel.Initialize(); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_DLLFILE: LocalizedMessageBox.Show("Error : 找不到生成的dll文件\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_GETBIT: LocalizedMessageBox.Show("Error : 找不到入口GetBit\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_GETWORD: LocalizedMessageBox.Show("Error : 找不到入口GetWord\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_GETDWORD: LocalizedMessageBox.Show("Error : 找不到入口GetDWord\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_GETFLOAT: LocalizedMessageBox.Show("Error : 找不到入口GetFloat\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_GETFEQ: LocalizedMessageBox.Show("Error : 找不到入口GetFeq\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_SETBIT: LocalizedMessageBox.Show("Error : 找不到入口SetBit\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_SETWORD: LocalizedMessageBox.Show("Error : 找不到入口SetWord\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_SETDWORD: LocalizedMessageBox.Show("Error : 找不到入口SetDWord\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_SETFLOAT: LocalizedMessageBox.Show("Error : 找不到入口SetFloat\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_SETFEQ: LocalizedMessageBox.Show("Error : 找不到入口SetFeq\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_BEFORERUNLADDER: LocalizedMessageBox.Show("Error : 找不到入口BeforeRunLadder\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_AFTERRUNLADDER: LocalizedMessageBox.Show("Error : 找不到入口AfterRunLadder\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_INITRUNLADDER: LocalizedMessageBox.Show("Error : 找不到入口InitRunLadder\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNNT_FOUND_GETCLOCK: LocalizedMessageBox.Show("Error : 找不到入口GetClock\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_INITCLOCK: LocalizedMessageBox.Show("Error : 找不到入口InitClock\r\n", LocalizedMessageIcon.Error); break; case SimulateDllModel.LOADDLL_CANNOT_FOUND_SETCLOCKRATE: LocalizedMessageBox.Show("Error : 找不到入口SetClockRate\r\n", LocalizedMessageIcon.Error); break; default: LocalizedMessageBox.Show("Error : 发生未知错误\r\n", LocalizedMessageIcon.Error); break; } return(ret); }
public static LadderDiagramViewModel CreateLadderDiagramByXElement(XElement xEle, ProjectModel _projectmodel) { LadderDiagramViewModel result = new LadderDiagramViewModel(xEle.Attribute("Name").Value, _projectmodel); if (xEle.Attribute("IsMain") != null && xEle.Attribute("IsMain").Value == "True") { result.IsMainLadder = true; } else { result.IsMainLadder = false; } result.IsExpand = bool.Parse(xEle.Attribute("IsExpand").Value); result.InitNetworks(); foreach (XElement netNode in xEle.Elements("Network")) { var net = CreateLadderNetworkByXElement(netNode, result); result.AppendNetwork(net); net.IsExpand = bool.Parse(netNode.Attribute("IsExpand").Value); } return(result); }