private void _SimulateThread() { PLCDevice.Device device = PLCDeviceManager.GetPLCDeviceManager().SelectDevice; SetBaseBit(device.BitNumber); SetBPEnable(1); SetClockRate(1); // 初始化 try { InitRunLadder(); } // 触发异常时暂停并发送事件等待处理 catch (Exception exce) { Pause(); SimulateException(exce, new RoutedEventArgs()); //SimulateAbort(this, new RoutedEventArgs()); } // 存活状态下运行循环 while (simulateActive) { // 暂停状态下运行这个循环 while (!simulateRunning) { // 未存活时跳出这个循环 if (!simulateActive) { break; } // 等待 Thread.Sleep(10); } // 未存活时跳出这个循环 if (!simulateActive) { break; } // 运行一次仿真PLC BeforeRunLadder(); try { RunLadder(); } // 触发异常时暂停并发送事件等待处理 catch (Exception exce) { Pause(); SimulateException(exce, new RoutedEventArgs()); //SimulateAbort(this, new RoutedEventArgs()); } AfterRunLadder(); } // 发送线程终止的事件 SimulateAbort(this, new RoutedEventArgs()); }
static public ArgumentValue Create(string argname, string argtype, string name, PLCDevice.Device device) { IValueModel argument = null; switch (argtype) { case "BIT*": if (!ValueParser.VerifyBitRegex3.Match(name).Success) { throw new ValueParseException(String.Format("{0:s}的寄存器名称错误!位寄存器只允许Y/M/S", argname)); } try { argument = ValueParser.ParseBitValue(name, device); } catch (ValueParseException) { throw new ValueParseException(String.Format("{0:s}的寄存器超出允许的范围!", argname)); } break; case "WORD*": if (!ValueParser.VerifyWordRegex2.Match(name).Success) { throw new ValueParseException(String.Format("{0:s}的寄存器名称错误!单字寄存器只允许D/TV/CV/AO", argname)); } try { argument = ValueParser.ParseWordValue(name, device); } catch (ValueParseException) { throw new ValueParseException(String.Format("{0:s}的寄存器超出允许的范围!", argname)); } break; case "DWORD*": if (!ValueParser.VerifyDoubleWordRegex1.Match(name).Success) { throw new ValueParseException(String.Format("{0:s}的寄存器名称错误!双字寄存器只允许D/CV", argname)); } try { argument = ValueParser.ParseDoubleWordValue(name, device); } catch (ValueParseException) { throw new ValueParseException(String.Format("{0:s}的寄存器超出允许的范围!", argname)); } break; case "FLOAT*": if (!ValueParser.VerifyFloatRegex.Match(name).Success) { throw new ValueParseException(String.Format("{0:s}的寄存器名称错误!浮点寄存器只允许D", argname)); } try { argument = ValueParser.ParseFloatValue(name, device); } catch (ValueParseException) { throw new ValueParseException(String.Format("{0:s}的寄存器超出允许的范围!", argname)); } break; } return(new ArgumentValue(argname, argtype, argument)); }
/// <summary> /// 仿真线程要运行的函数(图表模式) /// </summary> public void _SimulateThread_Chart() { PLCDevice.Device device = PLCDeviceManager.GetPLCDeviceManager().SelectDevice; SetBaseBit(device.BitNumber); SetClockRate(1); InitRunLadder(); InitClock(TimeStart); // 当前时间 int time = GetClock(); int count = 0; double progress = 0.0; while (simulateActive) { count++; // 设置锁定的视点 foreach (SimulateDataModel sdmodel in locksdmodels) { sdmodel.Set(this, time); } while (!simulateRunning) { if (!simulateActive) { break; } Thread.Sleep(10); } if (!simulateActive) { break; } BeforeRunLadder(); try { RunLadder(); } // 触发异常时发送事件,并暂停运行等待处理 catch (Exception exce) { SimulateException(exce, new RoutedEventArgs()); simulateRunning = false; } AfterRunLadder(); // 检查是否超过终止时间 time = GetClock(); if (time >= TimeEnd) { simulateActive = false; } if (!simulateActive) { break; } double _progress = (double)(time - TimeStart) / (TimeEnd - TimeStart) * 100; if (_progress - progress > 1.0) { progress = _progress; SimulateProgress(progress, new RoutedEventArgs()); } // 读取监视的视点 foreach (SimulateDataModel sdmodel in viewsdmodels) { sdmodel.Update(this, time); } } // 发送线程终止的事件 SimulateAbort(this, new RoutedEventArgs()); }