恭喜,你发布的帖子
发布于 2020-11-27 13:16:13
1楼
//=============================================================================
// Siemens AG
// (c)Copyright 2017
//-----------------------------------------------------------------------------
// Library: LGF (Library General Functions)
// Tested with: CPU1212C DC/DC/DC FW:V4.2
// Engineering: TIA Portal V13 SP1 Upd 4
// Restrictions: -
// Requirements: PLC (S7-1200 / S7-1500)
// Functionality: This function generates random numbers in defined limits
// (Datatype Int)
//-----------------------------------------------------------------------------
// Change log table:
// Version Date In charge / Changes applied
// 01.00.00 19.08.2015 Siemens Industry Online Support
// First released version
// 01.00.01 02.01.2017 Siemens Industry Online Support
// Upgrade: TIA Portal V14 Update 1
// 01.00.02 17.08.2018 Siemens Industry Online Support
// Upgrade: TIA V15 Update 2
//-----------------------------------------------------------------------------
//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;
#LGF_RandomInt := 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);
#LGF_RandomInt := 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 := #MIN_INT, VALUE := #tempRandomValue, MAX := #MAX_INT);
#LGF_RandomInt := SCALE_X(MIN := #minValue, VALUE := #tempNormReal, MAX := #maxValue);
#status := #NO_ERROR;
这是官方的库文件里面随机数生成的办法,通过时间然后做标定得到的
请填写推广理由:
分享
只看
楼主