Twincat FOR LOOPS

Join Date
Feb 2009
Location
Ant
Posts
54
Hi guys,

Im writing some program for a CX. Is there a way to get early out of a FOR loop when a certain conditions gets TRUE?


Like the 'return' operator in C.

kind regards,
Jo
 
I don't have TwinCAT available to me right now but I believe there is a 'break' operator that will get you out of any iterative loop (for-next, do-while, etc).

Keith
 
A soft PLC running on a Windows CE doesnt like WHILE loops.

Cause it need to run IO an Visualization tasks at certain times.
Twincat is not a pre-emptive OS. So eventually the system will crash.

regards,
Jo
 
o_O

Code:
FOR i:=1 TO 6 DO
    IF rM[i]=0.0 THEN EXIT; END_IF;
    rc[i]:= rM[i];
END_FOR;

i:=1;
WHILE i<=6 AND rM[i]<>0.0 DO
    rc[i]:=rM[i]; 
    i:=i+1;
END_WHILE;
 
For curiosity, why in earth would it handle for loops better than while?

For me it looks like:

while statement
do stuff

do viz & others
go back to while start and check condition

For for same. So is it just ****ty implementation on twincat, or something else?
 
Thats a nice situation LD. Straightforward code that will never crash.

But im waiting on communication to come in from a serial device.
So when it get stuck in that WHILE loop the system will crash.

Like for example I have to wait 50ms on an answer from the serial device.
 
Its a RS232 communication. I need to wait on an answer from the device before I can send the next package.

Thats damn hard code when while loops get me stuck.
 
Im on loss, how can while get you stuck, but for not? For can be infinite loop also.. what is the real difference?

PS. have you tought of doing it someway else than waiting in loooop? seems like flawed way to do it anyway.
 
Last edited:
Thats true. I think I have an other problem.

I need a timeout of maximum 300ms. But this would kill my I/O and Visu tasks.

How can I handle this?
 
Loop

Divide the loop. Don't use For Next or Do While if it can get stuck. In PLC's you need to take care with such instructions.

You can increment (add +1) every every cycle for example. Instead of a For Next loop from 1 to 6, it will now be devided in 6 cycles. You can devide it in every way you like with some simple math.

PLC is not C !!

You need to think in a way that the CPU remains in good cycle times. Loops can exceed the cycle times when not well written, I call this bugs, because your code should prevent to exceed the cycle time.

BTW: Turpo Urpo is right, For Next, Do While, I also don't see problems with using these instructions, when you use them well.

Thats true. I think I have an other problem.

I need a timeout of maximum 300ms. But this would kill my I/O and Visu tasks.

How can I handle this?
 
Last edited:
Depending on what you need to do, but comms I would do:

Send message, set message pending bit, start timeout timer.
try to receive, if received, clear message pending bit, if timeout, then handle properly.

When need the data message delivers, first check if data is current (message pendign := false), if current, handle data, else skip.

But, I dont know your exact need.
 

Similar Topics

Hi guys, I am using TwinCAT 3 and I have a laser sensor that sends me data in Array. At the beginning of the measurement I run reference scan...
Replies
6
Views
2,585
I am using twincat 3 to send some strings over TCP/IP. Where the server is a sensor and my PLC is the client. I noticed that the sensor didnt...
Replies
2
Views
74
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
222
Hi! I am trying to run the 'SimpleSample' (https://infosys.beckhoff.com/content/1033/TF5100_TC3_NC_I/Resources/3438746891/.zip ) to understand the...
Replies
2
Views
106
I am developing a library in twincat and one of the function uses IID_ITcVnAccess_TcVnPoint2_DINT and the definition of this type is defined in...
Replies
0
Views
72
Back
Top Bottom