用户登陆 用户注册
您的位置:首页> 技术文章>内容正文
在西门子PLC中使用SCL语言编程的技巧
[正文]:前言:两年半前我就在工控网上发表了有关scl编程的知识<<在s7300400型plc中使用高级语言编程>>,但发表完后,即使我自己都从没有把任何使用scl编写的程序用到实际控制中,当时的感觉是使用scl编程到处受限,没有stl语言灵活和强大。
直到最近使用施耐德的unity软件编程,并使用这种已经国际标准化的文本语言(等同于西门子的scl语言),才体会到它的优点:<1>、程序容易阅读,便于诊断和维护;<2>、程序容易在不同厂家之间的plc之间转换。
西门子的stl语言是强大,但难于阅读,编写程序也需要异常小心,其最强大的可能是它的寄存器寻址(类似于一些计算机高级语言中的地址指针),scl没有这个功能,那就多费一些程序代码来实现同样的功能,程序是否优秀更应该看重程序的架构和提高程序生产效率的标准化,好的plc程序不应该只有自己明白,而是让更多的人明白。
在西门子plc中使用scl语言的场合一般是编写标准功能块fb,其编程方式和西门子的其他编程语言,如梯形图lad、语句表stl是完全不同的,同时为了实现程序的国际标准化,即为了方便的将程序代码移植到不同厂家的plc系统上,尽量不要在scl中使用西门子独有的功能块。
1、 在fb块中使用结构 编写fb块的准则,就是其使用的内部变量尽量与外部隔离,除了像plc的新启动/重启动标志,以及一些方波/脉冲波等全局变量可以在fb块中使用外,其他的任何全局变量都不应该在fb内部使用,即使是自定义结构也应该在fb中单独定义,在fb块中使用结构应该在静态类型变量中定义,如下: var // static variables im:struct //data structure of internal flags h1_afcountimp:bool:=false; // aux flag counter impulse h1_countimp:bool:=false; // counter impulse h1_elcountmv:bool:=false; // endless counter maximum value end_struct; //other data structure … end_var 在使用这些结构时,可以按照如下方式: im. h1_countimp:=imp; 2、 在scl中替代set/reset指令的方法 scl中不存在set/reset指令,或者说也没有必要使用。
在scl中,不使用排他条件else的条件语句就是一个set/reset指令。
如下编程: if then variable name:=1; end_if; 其等同于: (s)
点击查看大图片
若加上else条件,如下: if then variable name:=1; else variable name:=0; end_if; 则等同于: ( )
点击查看大图片
一条完整的包含置位和复位的语句可以使用如下方式编程: if then variable name:=1; end_if; if then variable name:=0; end_if; 其等效于sr指令,若将上面的两个条件语句的先后次序颠倒一下,则等效于rs指令。
3、 简化程序指令 <1>、尽量使用赋值语句替代那些不用于sr/rs指令的bool型赋值条件语句,如下: if fnadd &(button=false) then pus1:=true; else pus1:=false; end_if; 其等效于pus1:= fnadd &(not button),这样使程序看起来更加简洁和容易阅读。
<2>、对于非bool型赋值语句则不能这如上简化,而是可以通过sel函数实现: if fnadd &(button=false) then pus1:=value1; else pus1:= value2; end_if; 其等效于pus1:= sel (g:= fnadd &(button=false), in0:= value2,in1:= value1); 使用该函数时注意两点:<1>、参数名不能省略;<2>、当选择条件g为true时,选择后一个参数值in1,为false时,选择前一个参数值in0,这点与计算机c语言等正好相反。
<3>、xor指令有着比and 和or更为复杂的表达,能使用xor的地方应该尽量使用 if (condition1 and (not condition2)) or (condition2 and ( not condition1)) then result:=true; else result:=false; end_if; 其等效于result:= condition1 xor condition2; xor功能就是两条件不同输出true,相同输出false 4、 脉冲沿检测功能使用以下两条语句替代脉冲上升沿检测函数,譬如检测button_input上升沿的代码如下: puls:=button_input & (not button_last); button_last:= button_input; 同样的下降沿脉冲检测如下: puls:= ( not button_input) & button_last; button_last:= button_input; 5、 编写脉冲发生器 波峰持续时间仅为一个plc扫描周期的波形称为脉冲波,而波峰持续时间大于或等于两个plc扫描周期的波形称为方波,脉冲波可用于计数、定时,方波可用于控制信号灯的闪烁输出,可以在西门子plc的硬件配置中配置一个字节的各种时间的方波(波峰时间和波谷时间为1:1),假设"fp_1sec" 为这个字节中1秒的方波,则: <1>、间隔1秒的脉冲波“impls_1sec” 如下编程: “impls_1sec” := "fp_1sec" and (not “impls_1sec_aux”); “impls_1sec_aux”:= "fp_1sec" ; <2>、间隔10秒的脉冲波“impls_10sec” 如下编程: if (“impls_10sec” ) then count_ actual:=0; “impls_10sec”:=0; else if (“impls_1sec” ) then count_ actual:= count _ actual +1; end_if; “impls_10sec”:= count_ actual>=10; end_if; count_ actual的初始值为0,同时当系统新启动时,也需将其设为零。
间隔更长时间的脉冲波编程都可以按照上面的方式编程。
6、尽量使用编程计数功能来替代定时器功能,这样使程序更可靠和易于阅读假设input_condition为输入,output_delay为通过定时处理后的输出,timer_setpoint为时间设定点,timer_actual为当前时间计数的实际值,“impls_1sec” 为系统编程产生的1秒脉冲。
<1>、在输入条件满足的情况下,延时输出的定时器: if (not input_condition) then timer_actual:= 0; output_delay:= 0; else if (“impls_1sec” and not output_delay) then timer_ actual:= timer_ actual +1; end_if; output_delay:= timer_actual >= timer_setpoint; end_if;
点击查看大图片
<2>、有记忆的延时输出定时器,即在延时过程中,若输入条件终止,不影响延时,这种 定时器必须使用其它的信号复位。
if input_condition then output_aux:=1; end_if; if (not output_aux) then timer_actual:= 0; output_delay:=0; else if (“impls_1sec” and not output_delay) then timer_ actual:= timer_ actual +1; end_if; output_delay:= timer_actual >= timer_setpoint; end_if; 若想终止output_delay的输出,必须在后面追加一条条件语句,用于复位output_aux
点击查看大图片
<3>、立即输出,延时断开的定时器 if input_condition then timer_actual:= 0; output_aux:= 0; output_delay:=1; //立即输出 else if (“impls_1sec” and not output_aux) then timer_ actual:= timer_ actual +1; end_if; output_aux:= timer_actual >= timer_setpoint; end_if; if output_aux then output_delay:=0; //延时断开 end_if;
点击查看大图片
<4>、在检测到一个上升沿脉冲后,立即输出,并开始计时,在 时间到达后断开。
if input_condition then output_aux:=1; end_if; if (not output_aux) then timer_actual:= 0; timer_arrived := 0; else if (not timer_ arrived and “impls_1sec” ) then timer_actual:= timer_actual +1; end_if; timer_ arrived := timer_actual >= timer_setpoint; end_if; if timer_ arrived then output_aux:=0; end_if; output_delay:= output_aux;
点击查看大图片
通过以上的编程方式可以实现任何定时器功能,而代码却可以为不同的plc系统所使用。
7、使用编程计数功能来替代计数器在scl语言中使用计数功能是最为简单的,其关键是必须首先对输入进行脉冲检测假设input_imp为输入脉冲,countimp为输入脉冲检测,counter为计数值,factor为计数因子(更详细点就是每来一次脉冲,计数值增加多少)。
(*----- create impulse (impulse evaluation) -----------------------------------------------------*) countimp:= input_imp and (not countimp_old); countimp_old:= input_imp; (*----- counter ---------------------------------------------------------------------------------*) if countimp then counter:= counter+factor; end_if; 一个完整的计数程序应该还有计数器复位功能以及计数值上限检测条件(以防止计数值溢出)。
8、 新故障/新警告的检测 一个完整的fb块应该能够检测故障/警告,以及新故障/新警告,假设input1, input2… inputn对应故障的输入(有信号表示ok),fault1, fault2… faultn对应故障位,nfault1, nfault2…nfaultn对应新故障位,flt和nflt分别对应综合的故障和新故障,ackn对应故障应答输入,为常开点,mute对应新故障消除输入(或者称为蜂鸣器沉寂),为常开点: fault1:= not input1 or (fault1 and not ackn); nfault1:= fault1 and (mute or nfault1); fault2:= not input2 or (fault2 and not ackn); nfault2:= fault2 and (mute or nfault2); … flt := fault1 or fault2 or faultn nflt :=(fault1 and not nfault1) or (fault2 and not nfault2) or (faultn and not nfaultn) nflt就是最终的新故障输出指示,新警告的检测与之类似。
9、字中取位 字中取位有两种方式,一种是通过西门子所特有的字取位方式实现,一种是通过计算机编程的标准方式实现,假设input_word为输入参数,word类型,w0,w1,…w15为位变量。
<1>、通过西门子的m变量实现: temp_aux:=mw[10]; mw[10]:=input_word; w0:=m[11,0]; w1:=m[11,1]; w2:=m[11,2]; w3:=m[11,3]; w4:=m[11,4]; w5:=m[11,5]; w6:=m[11,6]; w7:=m[11,7]; w8:=m[10,0]; w9:=m[10,1]; w10:=m[10,2]; w11:=m[10,3]; w12:=m[10,4]; w13:=m[10,5]; w14:=m[10,6]; w15:=m[10,7]; mw[10]:=temp_aux; <2>、通过标准编程实现 w0:=(input_word & 16#1)=16#1; w1:=(input_word & 16#2)=16#2; w2:=(input_word & 16#4)=16#4; w3:=(input_word & 16#8)=16#8; w4:=(input_word & 16#10)=16#10; w5:=(input_word & 16#20)=16#20; w6:=(input_word & 16#40)=16#40; w7:=(input_word & 16#80)=16#80; w8:=(input_word & 16#100)=16#100; w9:=(input_word & 16#200)=16#200; w10:=(input_word & 16#400)=16#400; w11:=(input_word & 16#800)=16#800; w12:=(input_word & 16#1000)=16#1000; w13:=(input_word & 16#2000)=16#2000; w14:=(input_word & 16#4000)=16#4000; w15:=(input_word & 16#8000)=16#8000; 使用方式1会更加简单和容易理解一些,但方式2具有更加宽广的应用场合,更加标准化,即使是当今的计算机编程在取位操作时也类似于上面的编程。
字取位的场合,一般用于总线数据(譬如变频器的状态数据),则可能是字/整数,此时就需要用到上面的编程。
10、将位组合成字 相当于“字中取位”的反向操作,这也有两种方法,一种方法是使用m变量,类似于“字中取位”的方式<1>,另一种也是标准编程,假设output_word为输出参数,word类型,w0,w1,…w15为位变量。
<1>、通过西门子的m变量实现: temp_aux:=mw[10]; m[11,0] := w0; m[11,1] := w1; m[11,2] := w2; m[11,3] := w3; m[11,4] := w4; m[11,5] := w5; m[11,6] := w6; m[11,7] := w7; m[10,0] := w8; m[10,1] := w9; m[10,2] := w10; m[10,3] := w11; m[10,4] := w12; m[10,5] := w13; m[10,6] := w14; m[10,7] := w15; output_word:=mw[10]; mw[10]:=temp_aux; <2>、通过标准编程实现 if w0 then output_word:=output_word or 16#1; else output_word:=output_word and (not 16#1); end_if; if w1 then output_word:=output_word or 16#2; else output_word:=output_word and (not 16#2); end_if; if w2 then output_word:=output_word or 16#4; else output_word:=output_word and (not 16#4); end_if; if w3 then output_word:=output_word or 16#8; else output_word:=output_word and (not 16#8); end_if; if w4 then output_word:=output_word or 16#10; else output_word:=output_word and (not 16#10); end_if; if w5 then output_word:=output_word or 16#20; else output_word:=output_word and (not 16#20); end_if; if w6 then output_word:=output_word or 16#40; else output_word:=output_word and (not 16#40); end_if; if w7 then output_word:=output_word or 16#80; else output_word:=output_word and (not 16#80); end_if; if w8 then output_word:=output_word or 16#100; else output_word:=output_word and (not 16#100); end_if; if w9 then output_word:=output_word or 16#200; else output_word:=output_word and (not 16#200); end_if; if w10 then output_word:=output_word or 16#400; else output_word:=output_word and (not 16#400); end_if; if w11 then output_word:=output_word or 16#800; else output_word:=output_word and (not 16#800); end_if; if w12 then output_word:=output_word or 16#1000; else output_word:=output_word and (not 16#1000); end_if; if w13 then output_word:=output_word or 16#2000; else output_word:=output_word and (not 16#2000); end_if; if w14 then output_word:=output_word or 16#4000; else output_word:=output_word and (not 16#4000); end_if; if w15 then output_word:=output_word or 16#8000; else output_word:=output_word and (not 16#8000); end_if; 同样的,使用标准化编程会繁琐一些,但有着很强的通用性,在总线通讯控制中,很多控制字(如变频器)都是以字的形式传递,所以需要把一些bool数据合并到一个字中,可以采用上面的对字中的位进行置位/复位操作的方式,但事实上使用时,控制命令可能只有启动/停止和方向控制等,所以这是可以直接对输出赋值,譬如当我们知道16#0f对应启动命令和正传时,可以直接使用如下赋值语句即可控制变频器正向运转:output_word:= 16#0f,如需反向运转,则再赋另一个值即可,而不需要像上面那样对字的每一位操作。
以上是我总结的一些使用技巧,其编程可能有更好的实现方式,欢迎来信探讨。



网站首页 培训课程 维修指南
技术文章 家电专栏 供应信息
求购信息 培训资讯 展会信息
电脑专栏 教程下载 资料下载
常用软件 PLC教程 PLC资料
变频伺服 低压电器 维修资料
人机界面 自控仪表 工控机类
文章标题: 搜文章
中国工控资源网手机版 2012
电话:010-67577139 13811659603
培训咨询QQ:657167934 471895637 销售咨询QQ:623769457
联系邮箱:zggkzyw@163.com
 京ICP备11002135号
报时(2026-04-06 03:39:28) 流量统计