School Pets Food Code & PLC Professionals 💡 Suggest
EN

Code & PLC Syntax Converter

Pick a source and a target language, then a topic: you get the same practical example written in both, with a Copy button and the differences that really cause bugs (:= vs =, inclusive FOR bounds, scan cycle vs blocking sleep). Built for people who switch between PLCs and software every day.

IF / ELSIF / ELSE Siemens SCL → Python

The logic is the same but the spelling changes: PLCs compare with =, not-equal is <>, they use AND/OR/NOT and close the block with END_IF. Software compares with == (=== in JavaScript), not-equal is != and the block lives in braces or indentation (Python, with elif).

Siemens SCLTIA Portal · S7-1200/1500
IF #Temp > 80.0 THEN
    #Alarm := TRUE;
    #Fan := TRUE;
ELSIF #Temp > 60.0 AND NOT #Manual THEN
    #Alarm := FALSE;
    #Fan := TRUE;
ELSE
    #Alarm := FALSE;
    #Fan := FALSE;
END_IF;

// compare with = (single), "not equal" is <>
IF #Mode <> 2 OR #Level = 0 THEN
    #Ready := FALSE;
END_IF;
Python 3
if temp > 80.0:
    alarm = True
    fan = True
elif temp > 60.0 and not manual:
    alarm = False
    fan = True
else:
    alarm = False
    fan = False

# compare with ==, "not equal" is !=
if mode != 2 or level == 0:
    ready = False

⚠️ Key differences & gotchas to avoid

RuleSiemens SCLPython
Equality test===
Not equal<>!=
Logical operatorsAND OR NOT XORand or not
Code blocksIF … THEN … END_IF;indentation + “:”
Boolean valuesTRUE / FALSETrue / False

💡 Practical tips

  • Gotcha #1: on a PLC “=” compares, in C, C++, C#, Java and JavaScript “=” assigns. if (mode = 2) in C compiles and changes the value of mode.
Compare all 8 languages — IF / ELSIF / ELSE
Siemens SCLTIA Portal · S7-1200/1500
IF #Temp > 80.0 THEN
    #Alarm := TRUE;
    #Fan := TRUE;
ELSIF #Temp > 60.0 AND NOT #Manual THEN
    #Alarm := FALSE;
    #Fan := TRUE;
ELSE
    #Alarm := FALSE;
    #Fan := FALSE;
END_IF;

// compare with = (single), "not equal" is <>
IF #Mode <> 2 OR #Level = 0 THEN
    #Ready := FALSE;
END_IF;
Rockwell STStudio 5000 · Logix
IF Temp > 80.0 THEN
    Alarm := 1;
    Fan := 1;
ELSIF Temp > 60.0 AND NOT Manual THEN
    Alarm := 0;
    Fan := 1;
ELSE
    Alarm := 0;
    Fan := 0;
END_IF;

// compare with = (single), "not equal" is <>
IF Mode <> 2 OR Level = 0 THEN
    Ready := 0;
END_IF;
IEC 61131-3 STCODESYS
IF Temp > 80.0 THEN
    Alarm := TRUE;
    Fan := TRUE;
ELSIF Temp > 60.0 AND NOT Manual THEN
    Alarm := FALSE;
    Fan := TRUE;
ELSE
    Alarm := FALSE;
    Fan := FALSE;
END_IF;

(* compare with = (single), "not equal" is <> *)
IF Mode <> 2 OR Level = 0 THEN
    Ready := FALSE;
END_IF;
Python 3
if temp > 80.0:
    alarm = True
    fan = True
elif temp > 60.0 and not manual:
    alarm = False
    fan = True
else:
    alarm = False
    fan = False

# compare with ==, "not equal" is !=
if mode != 2 or level == 0:
    ready = False
JavaScriptES6+
if (temp > 80.0) {
  alarm = true;
  fan = true;
} else if (temp > 60.0 && !manual) {
  alarm = false;
  fan = true;
} else {
  alarm = false;
  fan = false;
}

// use === and !== (strict: no type conversion)
if (mode !== 2 || level === 0) {
  ready = false;
}
C / C++
if (temp > 80.0) {
    alarm = true;
    fan = true;
} else if (temp > 60.0 && !manual) {
    alarm = false;
    fan = true;
} else {
    alarm = false;
    fan = false;
}

// compare with ==  (if (mode = 2) would ASSIGN 2!)
if (mode != 2 || level == 0) {
    ready = false;
}
C#.NET
if (temp > 80.0)
{
    alarm = true;
    fan = true;
}
else if (temp > 60.0 && !manual)
{
    alarm = false;
    fan = true;
}
else
{
    alarm = false;
    fan = false;
}

if (mode != 2 || level == 0)
{
    ready = false;
}
Java17+
if (temp > 80.0) {
    alarm = true;
    fan = true;
} else if (temp > 60.0 && !manual) {
    alarm = false;
    fan = true;
} else {
    alarm = false;
    fan = false;
}

// for objects (String) use .equals(), not ==
if (mode != 2 || level == 0) {
    ready = false;
}

Examples are ready-to-paste fragments: in C/C++, C# and Java put them inside a function or method; on PLCs inside the block body, with the variables declared in the interface (or as tags). Always test them on your own PLC or compiler before using them on a machine.

Syntax comparison between 3 PLC Structured Text dialects (IEC 61131-3) and 5 software languages (Python, JavaScript, C/C++, C#, Java) across 14 constructs: variables, IF, CASE, FOR, WHILE, functions, arrays, strings, on-delay timers, rising edge, analog scaling, bitwise operations, structures and error handling.

Quick syntax cheat sheet (8 languages)

RuleSiemens SCLRockwell STIEC 61131-3 STPythonJavaScriptC / C++C#Java
Assignment:=:=:======
Equality test==============
Not equal<><><>!=!==!=!=!=
Logical operatorsAND OR NOT XORAND OR NOT XORAND OR NOT XORand or not&& || !&& || !&& || !&& || !
Boolean valuesTRUE / FALSE1 / 0TRUE / FALSETrue / Falsetrue / falsetrue / falsetrue / falsetrue / false
Code blocksIF … THEN … END_IF;IF … THEN … END_IF;IF … THEN … END_IF;indentation + “:”{ … }{ … }{ … }{ … }
Comments// … (* … *)// … (* … *) /* … */// … (* … *)# … """docstring"""// … /* … */// … /* … */// … /* … */ /// doc// … /* … */ /** doc */
FOR end bound0 TO 9 = 10 passes, end INCLUDED0 TO 9 = 10 passes, end INCLUDED0 TO 9 = 10 passes, end INCLUDEDrange(10) = 0…9, end EXCLUDEDi < 10: condition checked every passi < 10: condition checked every passi < 10: condition checked every passi < 10: condition checked every pass
Array indexesArray[0..4] (you choose the start)REAL[5] (from 0)ARRAY[1..5] (you choose the start)list[0], list[-1] (from 0)arr[0], arr.length (from 0)v[0], v.size() (from 0)a[0], a.Length, a[^1] (from 0)a[0], a.length (from 0)
Positions in a stringfrom 1 (0 = not found)from 1 (0 = not found)from 1 (0 = not found)from 0 (-1 = not found)from 0 (-1 = not found)from 0 (npos = not found)from 0 (-1 = not found)from 0 (-1 = not found)
LengthLEN(s)s.LENLEN(s)len(s)s.lengths.size()s.Lengths.length()
Timers & delaysTON_TIME (T#5S)TONR + FBD_TIMER (ms)TON (T#5S)time.monotonic()performance.now() setTimeout()std::chrono::steady_clockStopwatch Task.Delay()System.nanoTime()
Rising edgeR_TRIGOSRI + FBD_ONESHOTR_TRIGby hand: compare with the previous stateby hand: compare with the previous stateby hand: compare with the previous stateby hand: compare with the previous stateby hand: compare with the previous state
Defining a functionFUNCTION … END_FUNCTIONAdd-On Instruction (AOI), no FUNCTION in STFUNCTION … END_FUNCTIONdef f(x): return …function f(x) { return … }double f(double x) { return …; }static double F(double x) => …;static double f(double x) { return …; }
Error handlingno exceptions: IF guards (+ GET_ERR_ID)no exceptions: IF guards + fault routineIF guards (__TRY on some runtimes)try / excepttry / catchtry / catchtry / catchtry / catch

All topics, explained

Variables & types

On PLCs every variable is declared first with a fixed type (INT, DINT, REAL, BOOL, STRING) and assigned with :=. Python and JavaScript infer the type and assign with =; C/C++, C# and Java are typed like a PLC but with other names (INT = 16-bit short, DINT = 32-bit int).

IF / ELSIF / ELSE

The logic is the same but the spelling changes: PLCs compare with =, not-equal is <>, they use AND/OR/NOT and close the block with END_IF. Software compares with == (=== in JavaScript), not-equal is != and the block lives in braces or indentation (Python, with elif).

CASE / SWITCH

It is the heart of state machines (steps 0, 10, 20…). The PLC CASE runs one branch only and accepts lists (20, 21) and ranges (90..99); in C, C++ and JavaScript a switch without break falls through, C# forces break, Java has the arrow form and Python uses match since 3.10.

FOR loop

The PLC FOR includes the end bound: FOR i := 0 TO 9 runs 10 times. In Python range(10) stops at 9 (10 is excluded), while C, C#, Java and JavaScript repeat while i < 10 is true: translating “0 TO 10” into “i < 10” silently drops the last element.

WHILE loop

WHILE repeats while the condition is true, REPEAT…UNTIL (do…while in software) at least once. On a PLC the whole loop runs inside one scan: if you wait for an input inside a WHILE the program hangs and the cycle-time watchdog stops the CPU. Always add a safety limit.

Functions & parameters

A function takes parameters and returns a value. In SCL and IEC the return value is assigned to the function name and parameters are passed by name (Price := 50.0); Logix uses an Add-On Instruction; software uses return, with default values in Python, JavaScript, C++ and C# (Java uses overloads).

Arrays & lists

PLC arrays have a fixed size and declared bounds (Array[0..4] or [1..5]): an out-of-range index can stop the CPU. In software indexes always start at 0, the last one is length - 1 and lists such as list, vector, List or ArrayList can grow at runtime.

Strings

PLC strings have a maximum length (STRING[40], 82 characters in Logix), are joined with CONCAT and positions start at 1 (FIND returns 0 when not found). Software uses + or interpolated strings, positions start at 0 and “not found” is -1.

TON timer & delays

A TON (on-delay timer) never blocks the PLC: you call it every scan and its output Q turns true once the input has stayed on for the preset time PT. The classic software mistake is sleep: time.sleep or Thread.sleep freeze the whole program; the correct equivalent compares the elapsed time on every pass (or uses setTimeout/async).

Rising edge (R_TRIG)

A button held down stays true for hundreds of scans: to count a single press you need the rising edge (R_TRIG in SCL and IEC, OSRI in Logix), true for one scan only. In software you do it by hand: remember the previous state and check “true now and false before”.

Analog scaling

An analog input arrives as a raw number (0…27648 on the most common 4–20 mA or 0–10 V cards) and must be converted to engineering units: y = (x − in_min) × (out_max − out_min) ÷ (in_max − in_min) + out_min, then clamped. Beware of integer division, which truncates the decimals.

Bitwise & masks

Reading or writing a single bit of a status WORD is daily work in automation: PLCs have direct bit access (Status.%X3 in SCL, Status.3 in Logix and IEC). Software uses masks and shifts: (status >> 3) & 1 to read, |= to set, &= ~ to clear, always with parentheses.

Structures, UDTs & objects

A structure groups the data of a real object (name, speed and state of a motor). On PLCs it is a user data type (UDT) and assigning it copies the whole content; in Python, JavaScript, C# and Java classes are references: assigning does not copy, both variables point to the same object. C/C++ and C# structs, instead, are copied like on a PLC.

Comments & error handling

PLCs have no exceptions: you prevent the error with guards (divisor not zero, index within bounds) and report it with an alarm bit. Software uses try/catch (try/except in Python), but beware: in JavaScript and with floating-point numbers dividing by zero gives Infinity with no exception, and in C/C++ integer division by zero is undefined behaviour.

The 8 supported languages

PLC · Structured Text

  • Siemens SCL (TIA Portal · S7-1200/1500)
  • Rockwell ST (Studio 5000 · Logix)
  • IEC 61131-3 ST (CODESYS)

Software

  • Python 3
  • JavaScript (ES6+)
  • C / C++
  • C# (.NET)
  • Java (17+)

Frequently asked questions

Is there an automatic converter from PLC Structured Text to Python or C?

A line-by-line translation only partly works, because a PLC runs all its code every scan while software runs one line after another. That is why you find here, for every construct, the idiomatic hand-written equivalent and the differences to respect (non-blocking timers, edges, inclusive FOR bounds).

What is the difference between := and = ?

In Structured Text := assigns a value and = compares two values. In Python, JavaScript, C, C# and Java = assigns and the comparison is == (=== in JavaScript). Mixing them up is the most common mistake when switching worlds.

How do you translate a TON timer into Python or JavaScript?

Not with sleep, which blocks the program. You store the moment the input turns true and on every pass compare the elapsed time with the preset (time.monotonic() in Python, performance.now() in JavaScript): the output turns true after the delay without stopping anything else.

Why does FOR i := 0 TO 9 run 10 times while range(10) stops at 9?

In the PLC FOR the end value is included (0, 1, … 9 = 10 values). In Python range(10) says how many numbers to generate starting from 0, so the last is 9. In C, C#, Java and JavaScript the loop runs while i < 10 is true, with the same result.

Which PLC dialects are included?

Three Structured Text variants: Siemens TIA Portal SCL (S7-1200/1500, with # local variables), Rockwell Studio 5000 / Logix Structured Text (tags, AOI, TONR, OSRI) and the IEC 61131-3 standard used by CODESYS and many other environments.

Can I use these examples directly on a machine?

They are teaching examples, correct but simplified: variable names, types and firmware versions change from project to project. Test them in simulation or on the bench first and always follow the machine's safety rules: safety functions are not programmed with these examples.

Siemens, SIMATIC, TIA Portal, Rockwell Automation, Allen-Bradley, Studio 5000, Logix, CODESYS, Python, JavaScript, Java, C# and the other product and language names mentioned are trademarks of their respective owners, used here purely for technical and educational reference. FunConvert is not affiliated with, endorsed or sponsored by any of them.

What do you think?

Your rating