台达PLC设定暂停
无需特殊的指令,只是需要使用积算型的时间定时器,设置一个暂停标志位或暂停按钮,就可以实现这个功能。
特点
台达PLC以高速、稳健、高可靠度而著称,广泛应用于各种工业自动化机械;
台达PLC除了具有快速执行程序运算、丰富指令集、多元扩展功能卡及高性价比等特色外,并且支持多种通讯协议,使工业自动控制系统联成一个整体。
为适应工业环境使用,与一般控制装置相比较,
PLC有以下特点:
1、可靠性高,抗干扰能力强
2、硬件和软件两大措施保证控制设备的可靠性:
有一种简单的方法是在你想停住的地方加一句getchar(),这样运行到这里的时候你在键盘上按任意键才会继续运行后面的代码。不过这种方法来暂停就必须要按键继续,如果你是想暂停一定的时间,就要用ls说的sleep的方法。
在线程中,如果要暂停一定的时间,可以使用Thread的Sleep方法,让线程暂停。在微软新的win8API中,已经用Task来实现原来用线程实现的许多功能,同样,Task也有Delay方法,让程序暂停一定的时间。
以下程序利用SystemThreadingTimer让程序每隔间隔的时间执行回调方法:
using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemThreading;
namespace TimerApp{
class Program{
static void Main(string[] args){
ConsoleWriteLine(" Working with Timer type /n");
// Create the delegate for the Timer type
TimerCallback timeCB = new TimerCallback(PrintTime);
// Establish timer settings
Timer t = new Timer(
timeCB, // The TimerCallback delegate type
"Hello From Main", // Any info to pass into the called method (null for no info)
5000, // Amount of time to wait before starting
TimeoutInfinite); //一秒钟调用一次 Interval of time between calls (in milliseconds)
ConsoleWriteLine("Hit key to terminate");
ConsoleReadLine();}
static void PrintTime(object state){
ConsoleWriteLine("Time is: {0}, Param is: {1}",
DateTimeNowToLongTimeString(), stateToString());}
如果把时间间隔1000改成 TimeoutInfinite,这样回调方法PrintTime只会在1秒之后调用一次。
除了Threading下有个Timer类之外,net中还有另一个Timer,就是SystemTimer名称空间下的Timer。此Timer的用法和Threading下的Timer不太相同。
SystemTimersTimer t2 = new SystemTimersTimer(100);
t2Elapsed += t2_Elapsed;
//t1AutoReset = false;
t2Enabled = true;}}
void t2_Elapsed(object sender, SystemTimersElapsedEventArgs e){}
其中AutoReset属性的含义是是否在每次时间间隔到了都触发Elapsed事件还是只触发一次(t1AutoReset = false;)。
解决此类问题的一个小例子(实现在窗体上相隔指定时间显示字幕)
//创建StopNtime类
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemThreading;
namespace 程序运行暂停器{
class StopNtime{
public StopNtime(){
SystemWindowsFormsControlCheckForIllegalCrossThreadCalls = false;//,跨线程调用控件必加上去 }
private int stopTime = 0;//暂停的时间
ThreadStart myStart;
Thread TheStop;
public readonly object MyLockWord = new object();
public void stopWay(int stopTime){
thisstopTime = stopTime;
myStart = new ThreadStart(this ToStop );
TheStop = new Thread(myStart );
TheStopStart();
private void ToStop(){
lock (MyLockWord){
ThreadSleep(this stopTime );
ThreadCurrentThreadAbort();
//主窗体代码
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemLinq;
using SystemText;
using SystemWindowsForms;
using SystemThreading ;
using SystemCollections;
namespace 程序运行暂停器{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();}
private void Form1_Load(object sender, EventArgs e){
label1Text = "";
Thread f = new Thread(f1 );
fStart();}
string MyWord = "问题:1+1= \n答案是:2 ";
private void f1(){
StopNtime mmm = new StopNtime();
foreach (char m in MyWord){
lock (mmmMyLockWord ){
label1Text += mToString();
mmmstopWay(300);
//运行结果
补充: 看到其他答案说到修改EXE、设断点、特权指令等,其实没那么复杂,现在的系统是按线程调度的,线程的状态是有系统决定的,要让进程暂停,只需让系统把进程的所有线程挂起,不分配CPU,进程当然就没法运行啦,而系统就提供了这样的接口啊,用就可以了。 对于修改EXE,可以在你要暂停的地方插入JMP指令,跳到你的暂停代码里,暂停代码用Sleep或WaitforSingleObject等待都可以,不过想在原代码插入JMP指令很难,修改了原指令就要恢复的,这可不简单啊。 对于设断点,这个可以,VB也可以实现,用DEBUG API就可以了,不过这是暂停的程序就处于DEBUG状态,而不是普通状态了,这是有差别的。 至于用特权指令暂停CPU,这好像不行吧,CPU停了,我们的程序也暂停了。这是内核级调试程序用的。 追问: 能给出代码吗,看是看懂了,但不知道这些函数怎么声明和使用。 回答: 不好意思,我用手机上的网,没电脑,没法给你声明,也没法给你代码,不过你可以查查MSDN的C原型,在转为VB声明,VB有个可以插入API声明的工具,可以用该工具对已有的声明进行修改。
使用由ntdll导出的NtSuspendProcess函数,可以将任意程序暂停下来,NtResumeProcess函数能让这个程序继续运行。这两函数在VB自带API浏览器中找不到,需要手动声明。请勿使用这些函数暂停自己,否则用户无法点击按钮
以上就是关于台达PLC如何暂停程序全部的内容,包括:台达PLC如何暂停程序、C语言中如何控制程序暂停、如何让C#程序在执行中暂停一段时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)