Declare a function in STL

mspath

Member
Join Date
Mar 2004
Posts
7
Hello Folks:
I'm trying to create a function that takes an DINT to dynamically set the range to a random number generator.
Below is the code, I commented out the section I thought would create it as a function.
Thanks for any help!

(*
FUNCTION RandNum
VAR_IN
iMax : DINT;
END_VAR
*)
GSV(Program, MainProgram, LASTSCANTIME, iLastScanTime);
GSV(WallClockTime, , LocalDateTime, arMyTime[0]);
myTime.Microsecond := arMyTime[6];
dRes := SIN(iLastScanTime * myTime.Microsecond);
dRandom := ABS(dRes * 7);

//END_FUNCTION
 
Random is hard to do by program.
I once made some similar to this but then I only needed a random number every 1 or 10 sec. I cant quite remember, so this is reconstructed out of my memory and not tested.
Make sure it is not called from a periodic Task.

Random Will change every scan but there will be a pattern especially if you need a random number every scan.
Not before 4 scan it will work.

Not perfect but it did the trick for me

1. Read system time in microSec
2. BSL 8 Random
3. Masked move to the last 8 bit of randem


Random.PNG
 
I think you are trying to do something that is not supported by AB structured text. You can't declare functions like you can in the CoDeSys stuff. The closest thing in AB is to create an Add-On Instruction (AOI) in structured text and call that from another routine.

Keith
 
mspath needs to come back and tell us for sure but I don't think he is asking how to code a random number generator. I think he is asking how to declare a function in an AB Logix processor.

Keith
 

Similar Topics

I'm new in PLC programming. I have a small project which have 12 temperature sensor. How I should declare the variable for this 12 sensor. I...
Replies
4
Views
2,442
I'm new in PLC programming. I have a small project which have 15 temperature sensor. How I should declare the variable for this 15 sensor. I...
Replies
3
Views
1,915
In my WinCC Runtime program, I have to declare these variables: Dim AllProperties As HMIProperties Dim objProperty As HMIProperty Dim...
Replies
0
Views
2,258
Hi all, Anyone knows how to declare a one second pulse in the Rslogix 5000? Is there any command for this function? It does not same like GE or...
Replies
34
Views
86,212
How to declare PIW PQW in WinCC (Analog Values)with simatic manager.... Any Examples Appreciated ..........
Replies
14
Views
7,769
Back
Top Bottom