SCL语言入门SCL是一种类似PASCAL语言的S7高级编成语言,不包含在基本的STEP7定货号中,需另外购买,安装完SCL语言后.建立一个标准的STEP项目,并进入Sources目录右键点击右部空白处插入SCLSource文件然后双击SCL文件就进入了基本的SCL编程环境,接下来我们就以一个典型的工程为例子来说明SCL语言的功能,普通的STEP工程包括OB块,FB块,FC块,DB块以及互相之间的调用关系DB块的标准程序DATA_BLOCKglobParamDBTITLE='Globalparameters'VERSION:1.1AUTHOR:CDNAME:GlobParaFAMILY:SystemSTRUCTB_OFF:BOOL:=FALSE;//Const:FALSEflagB_ON:BOOL:=TRUE;//Const:TRUEflagB_TRASH:BOOL:=FALSE;//Trash:multipurposeBOOLvariableN_BYTE_TRASH:BYTE:=B#16#0;//Trash:multipurposeBYTEvariableN_WORD_TRASH:WORD:=W#16#0;//Trash:multipurposeWORDvariableglobParamDB为DB块的名字,必须在符号表中声明,例如:globParamDBDB10DB10DB块的一些描述DB块中的内容N_DWORD_TRASH:DWORD:=DW#16#0;//Trash:multipurposeDWORDvariableN_INT_TRASH:INT:=0;//Trash:multipurposeINTvariableN_DINT_TRASH:DINT:=L#0;//Trash:multipurposeDINTvariableF_TRASH:REAL:=0.0;//Trash:multipurposeREALvariablebFirstCycleOn:BOOL;//BitthatistrueonlyduringthefirstPLCcycleB_REPLACE:BOOL;//BittoreplacewhenmakinganapplicationN_BYTE_REPLACE:BYTE;//BytevariabletoreplacewhenmakinganapplicationN_WORD_REPLACE:WORD;//WordvariabletoreplacewhenmakinganapplicationN_DWORD_REPLACE:DWORD;//DoublewordvariabletoreplacewhenmakinganapplicationN_INT_REPLACE:INT;//IntegervariabletoreplacewhenmakinganapplicationN_DINT_REPLACE:DINT;//DoubleintegervariabletoreplacewhenmakinganapplicationF_REPLACE:REAL;//RealvariabletoreplacewhenmakinganapplicationEND_STRUCTBEGINEND_DATA_BLOCK定义完成后可以点击进行编译以生成DB块.(以后其他块类似)FC块的标准程序FUNCTIONTEST_FC:VOIDTITLE='Analoginputmeasurement'VERSION:'1.0'AUTHOR:JVaNAME:AIFAMILY:StandardVAR_INPUTnPIW:WORD;//PIWaddresswheretheAIisreadfMinValue:REAL;//Scalingminimumvaluecorresponding0VfMaxValue:REAL;//ScalingmaximumvaluecorrespondingnominalvoltagebMode:Bool;//Locking,i.e.enablesimulationbModeEND_VARVar_IN_OUTfMeas:REAL;//ScaledmeasurementvalueEND_VARBEGIN////TheAI-channelisnotreadifsimulationlockisON//IFNOTbModeTHEN////Analog(=Integer)value27648correspondsthenominalvalueofvoltagemeasurementfMeas:=(fMaxValue-fMinValue)*WORD_TO_INT(nPIW)/27648.0+fMinValue;END_IF;END_FUNCTIONFB块的标准程序TEST_FC为FC块的名字,须在符号表中声明,VOID表示FC的返回值为空FC的输入形参FC的输入/输出形参具体的FC逻辑FUNCTION_BLOCKTest_FBTITLE='CP/CPKCalculation'VERSION:'1.0'//KNOW_HOW_PROTECT//ToprotectyourblockremovethatlinefromcommentAUTHOR:LBA//8charactersmaxNAME:CP_CPK//8charactersmaxFAMILY:STATISTI//8charactersmax//**********************************************************//*STARTOFVARIABLEDECLARATION*//**********************************************************VAR_INPUT//InputparametersbEnable:BOOL;//EnablecalculationsignalfStDeviationMe:REAL;//StandartdeviationvalueresultEND_VARVAR_OUTPUT//OutputparametersfMemoCpkMe:REAL;//MemoCpkvalueaftercopyfMemoCpkYou:REAL;//MemoCpkvalueaftercopyEND_VAR//VAR_IN_OUT//In/Outparameters//END_VARVAR_TEMP//TemporaryvariablesfCmk2:REAL;//Capability2END_VARVAR//StaticvariablesbMemoReset:BOOL;//MemoResetpulsesignalEND_VAR//CONST//Constants//END_CONST//*********************************************************//*STARTOFFUNCTIONDEFINITION*//*********************************************************BEGIN//Codesection//EdgesDetection//CpcalculationIFbEnable=TRUEANDfStDeviationMe0THENfMemoCpkMe:=fStDeviationMe+fCmk2;END_IF;TEST_FC(nPIW:=PIW256//IN:WORDTest_FB为FB块的名字,须在符号表中声明FB的输入形参FB的输出形参FB的临时变量FB的静态变量FB中的逻辑语法FB中调用FC,其中PIW256为实际的I/O地址,其余参数也需赋值,fMinValue:=1000.0//IN:REAL,fMaxValue:=0.0//IN:REAL,bMode:=TRUE//IN:BOOL,fMeas:=OUT_M//INOUT:REAL);//VOIDglobParamDB.bFirstCycleOn:=FALSE;END_FUNCTION_BLOCK//*********************************************************//*INSTANCEDBDEFINITION*//*********************************************************DATA_BLOCKTEST_DBTest_FBBEGINEND_DATA_BLOCKOB块的标准程序ORGANIZATION_BLOCKCYCL_EXCVAR_TEMPOB1_EV_CLASS:BYTE;//Bits0-3=1(Comingevent),Bits4-7=1(Eventclass1)OB1_SCAN_1:BYTE;//1(Coldrestartscan1ofOB1),3(Scan2-nofOB1)OB1_PRIORITY:BYTE;//1(Priorityof1islowest)OB1_OB_NUMBR:BYTE;//1(Organizationblock1,OB1)OB1_RESERVED_1:BYTE;//ReservedforsystemOB1_RESERVED_2:BYTE;//ReservedforsystemOB1_PREV_CYCLE:INT;//CycletimeofpreviousOB1scan(milliseconds)OB1_MIN_CYCLE:INT;//MinimumcycletimeofOB1(milliseconds)OB1_MAX_CYCLE:INT;//MaximumcycletimeofOB1(milliseconds)OB1_DATE_TIME:DATE_AND_TIME;//DateandtimeOB1startedEND_VARBEGINTest_FB.TEST_DB(bEnable:=TRUE//IN:BOOL,fStDeviationMe:=0.9//IN:REAL);TEST_M:=TEST_DB.fMemoCpkMe;//OUT:REALTEST_M:=TEST_DB.fMemoCpkYou;//OUT:REALEND_ORGANIZATION_BLOCK当所以块完成后,如果数量较多可以通过建立SCLCOMPILECONTROLFILE来一次性一起编译.SCLCOMPILECONTROLFILE的标准程序DB_EXAMPLEFC_EXAMPLEFB_EXAMPLEOB_EXAMPLE最后只要编译此文件就可以生成所有的块FB中读写DB块中的内容(FC类似)定义关联此FB块的DB块,DB块需先在符号表中做如下定义(定义关联)TEST_DBDB20FB10CYCL_EXC为OB块的名字,须在符号表中声明OB的临时变量OB中调用FB块,TEST_DB为FB块的背景数据块,分别输入FB参数管脚的实际地址,其中TEST_M的实际地址为MD100(同样需在符号表中声明)SCL文件的名字,需注意应把底层的块放在前部,以便优先编译当块编译完成后,进行调试时需注意,SCL的调试除了可以通过功能进行变量监视外,还可以通过设置断点的方式进行单步调试.在激活断点功能前,首先需在OPTION菜单栏中选中Createdebuginfo,然后移动光标到想设置断点的地方并点击设置断点,重新编译并下载程序连接上PLC后通过选择Debug-Operation-TestOpeartion然后点击激活断点,此时CPU会进入HOLD状态(STOP灯黄闪)这时旁边的调试按钮(如单步执行)就可以选择,根据需要进行调试,右部则显示执行结果.调试工作完成后,删除断点,并在Debug菜单中把操作模式选择为Processoperatioin,CPU就会转入运行模式(RUN灯亮起)