2

TwinCAT and (I think so) every other IEC61131-3 language supports both conversion types <type>_TO_<type> and TO_<type>. I am wondering, is there any cons to using the latter, so TO_<type>? For example, I have a calculation and we all know, that typing the extra 5 letters makes or breaks your day ;)

nResult : INT;
fDivisor : REAL;

// Does this perform any slower, or are there any cons to using it like this?
nResult := TO_INT(32767.0/fDivisor);
nResult := REAL_TO_INT(32767.0/fDivisor);
ziga
  • 159
  • 1
  • 6

1 Answers1

0

I have used only the shorthand functions TO_REAL() etc. for last 1-2 years without any issues. They simplify the code alot and you don't need to write so much code.

I'm 99% sure that there are no cons / performance issues for the PLC runtime, as the compiler most probably just automatically checks the data type during compilation.

I haven't measured the performance, but the only downside I can think of is that the compiler needs to check some data types during compilation. However, when usin X_TO_Y() conversion, the compiler still checks the data type and gives a warning/error if needed.

Quirzo
  • 1,183
  • 8
  • 10
  • Honestly, I didn't even know TO_ even existed (worked with Beckhoff PLCs for the past 3+ years haha) until not long ago when I was doing C# and switched to PLC the next day and did it automatically. Good to know, thanks. – ziga Sep 02 '23 at 08:50
  • Hopefully someone has better information for accepted answer :) But I certainly would use these instead of the "old ones". And the syntax is also more similar to C# Convert.ToInt32() etc. – Quirzo Sep 02 '23 at 13:52