Generate a random number in PLCnext Engineer
Hello.
My question is very simple, is it possible to generate a random number in PLCnext Engineer?.
Thank you for your support
Hello.
My question is very simple, is it possible to generate a random number in PLCnext Engineer?.
Thank you for your support
Comments
Hello,
you can properbly generate a Random Numbe taking the CPU Temperature or RAM as a Seed.
There is no premade function for this thou. (as far as I know)
I will ask around and see If somebody already has prepared a lib for this.
kind regards,
Olver
Hi,
I would be interested by this library too. Does it already exists or do you have any advise on how to do it properly ?
Best regards,
Louis.
Hi,
I have used the OSCAT library (http://www.oscat.de/). It includes a FB for random numbers. At the moment there is only a version for PCWorx but the source code is open and can easily be converted to PLCnext Engineer.
@Stefan Brinkmann
Before I download PCWorx, do I understand your comment correct that if I open the OSCAT library in PCWorx, it is possible to convert it into a PLCNext Engineer library or code
- or-
did you mean that the library has to be manually converted by using the source code and replacing functions which are not current available in PLCN (such as the TIME() function)?
The OSCAT library is only available in PC Worx format. But the code is written in ST and thus can be easily used in PLCnext Engineer. It is also possible to export the library from PC Worx and import it in PLCnext Engineer. After that, you might need to do some manual work since some standard IEC61131-3 FBs have changed.
Hello!
If you need random numbers inside your plc, please use PLCNextBase library https://www.plcnextstore.com/world/app/2390
It has PBCL_SysRandom_1 function block, and many other interesting inside)
Best regards.
Yeah, most of it can be used - but there are some function blocks which require functions not available in PLCNext Engineer. One of those is a normally system-wide function called TIME() which is a simple microsecond counter from the PLC which restarts at each power-on.
I have re-written some OSCAT code which uses this function to calculate the program interval time, and rather used the PBCL_SysGetMicrotick_1-function which spits out the program interval time directly.
But my point being that not all is directly convertible due to some missing standard functions in PLCNExt Engineer.
Since FW2023.6 there is GET_MICROSECONDS fc avaliable (hope your plc type have this fw release...)
I know, but it is not available yet for AXC3152. I made a workaround like this instead, using plcnextbase library:
(*Store the initial value from SysGetMicroTick as it starts before the program starts*)
IF NOT FirstRun THEN
MicroTick_HeadStart := PBCL_SysGetMicrotick_2(TRUE);
FirstRun := TRUE;
END_IF
(*Subtract the "head start" from SysGetMicroTick to get the program's actual elapsed time*)
Elapsed_us := PBCL_SysGetMicrotick_2(TRUE) - MicroTick_HeadStart;