软件版本:TIA15.1
功能:用户设定随机数范围后,可输出随机数。
此函数参考西门子官方库修改得来。简化成最简单的了。
封装:
代码如下:
//Status Codes:
// 16#7000: No current jobs
// 16#0000: Job finished; Note: There is no "Busy"-Status because the block
// is finished within a single cycle
// 16#8200: maxValue is samller then minValue
//=============================================================================
//Set "No current job" status
#error := false;
#statusID := #ERROR_IN_THIS_BLOCK;
#status := #NO_CURRENT_JOBS;
//Check if the maximal Value is less than the minimal value
IF (#minValue > #maxValue) THEN
#error := true;
#statusID := #ERROR_IN_THIS_BLOCK;
#status := #MAX_LESS_MIN;
#生成随机数 := 0;
RETURN;
END_IF;
//Read system time
#tempTimeStatus := RD_SYS_T(#tempTime);
IF (#tempTimeStatus <> 0) THEN
#error := true;
#statusID := #ERROR_RD_SYS_T;
#status := INT_TO_WORD(#tempTimeStatus);
#生成随机数 := 0;
RETURN;
END_IF;
//Callculate a random-start-value depending on the time
#tempRandomValue.%B1 := #tempTime.NANOSECOND.%B0;
#tempRandomValue.%B0 := #tempTime.NANOSECOND.%B1;
//adapt the calculated random number to the given number span
#tempNormReal := NORM_X(MIN := 1, VALUE := #tempRandomValue.%B1, MAX := 255);
#生成随机数 := SCALE_X(MIN := #minValue, VALUE := #tempNormReal, MAX := #maxValue);
#status := #NO_ERROR;
附件为随机函数的全局库文件
随机函数全局库.rar
编写不易 点个赞呗