0

I want to multiply a TIME variable by an integer. Documentation says that you can use * with TIME variables but when I try:

CYCLE_TIME          : TIME := T#5MS;
CYCLE_TIME_OFFSET   : TIME := CYCLE_TIME * 2;

Or

CYCLE_TIME          : TIME := T#5MS;
CYCLE_TIME_OFFSET   : TIME := CYCLE_TIME * TO_TIME(2);

Or

CYCLE_TIME          : TIME := T#5MS;
CYCLE_TIME_OFFSET   : TIME := (CYCLE_TIME * T#2MS);

I get an error I don't understand:

enter image description here

Can I multiply time by an integer, if so, how?

Malinko
  • 124
  • 11

1 Answers1

1

While codesys does support TIME multiplication, strangely it seems to not support it at compile time (in the definition). For example:

t1: TIME := T#5MS;
multiplier: UDINT := 2;
t2: TIME := t1 * multiplier;

Gives C0073: Cannot multiply multiple operands of type 'TIME', while

t1: TIME := T#5MS;
multiplier: UDINT := 2;
t3: TIME := MUL(t1, multiplier);

Gives C0007: Expression expected instead of 'MUL'.

Using any of the two above at runtime works as expected:

Ladder Logic Program Example

Result of the Above Ladder Logic Program Example

If you absolutely must multiply TIME in the definition phase, then you can always cast TIME to UDINT/DWORD (TIME <=> UDINT/DWORD, where 1 equals to 1 millisecond, so 12345 is 12 seconds and 345 milliseconds), do whatever operations you want, and then cast it back to TIME:

t6: TIME := UDINT_TO_TIME(TIME_TO_UDINT(t1) * multiplier);

Result of t6

Guiorgy
  • 1,405
  • 9
  • 26