Math that's wrong without a fault
A shift total on the HMI reads -20,536 kg. The CPU is in Run, there's no fault, and nothing is flashing. The code multiplies a part count by a part weight, both stored as Int, and the answer was too big for an Int. In Siemens SCL, math like this can give a wrong answer with no fault and no warning, so you have to design the data types up front.
Three traps and how to avoid them
- The result type comes from the operands, not from where you store it. SCL picks the data type of an expression from its operands: two
Intvalues give anIntresult, and mixingIntandDIntgivesDInt.1 Storing the answer in aDIntdoesn't help if the multiply already overflowed. Convert an operand first (INT_TO_DINT, orCONVin LAD). - Typed constants set the math type.
INT#1is a constant with a fixed type; plain1is untyped. Mixing a typed constant with a big untyped one can force the whole expression into the smaller type, soINT#1 + 50000is calculated as anIntand comes out as -15,535. Siemens recommends not mixing typed and untyped constants in one expression.2 Type both constants, or neither. Realruns out of digits. A 32-bitRealholds about 7 significant digits. A flow total that keeps adding small amounts slows down and eventually stops changing. UseLRealon CPUs that support it, or keep totals as integers in a sensible unit and convert only for display. (Our run-hours tutorial shows the same problem on the Rockwell side.)
The part total, done right
One small example: a shift's total kilograms from a part count and a part weight, both Int because that's how the scale sends them. The fix is to widen both to DInt before the multiply.
Ladder
Three LAD networks: convert, convert, multiply, using the Siemens CONV and MUL boxes (parameters IN/OUT and IN1/IN2/OUT). This is a schematic drawing of the networks, not a TIA Portal screenshot; numbering starts at 0 here, while TIA calls them Network 1 to 3.
Schematic only, drawn in our usual ladder style with Siemens instruction names. In TIA Portal LAD each box also has EN/ENO pins and you pick the data type on the box. None of these are Rockwell instructions.
On a phone, swipe sideways to see the whole rung.
SCL
The same multiply in SCL, with the two other traps from this page shown as comments and code. Siemens SCL is its version of IEC 61131-3 structured text.
// Temp/static: #PartCount, #PartWeight_kg : Int
// #ShiftKg : DInt; #TotalLiters : LReal
// Trap 1: Int * Int is calculated as Int, then stored.
// 300 * 150 = 45000 does not fit in Int (max 32767).
#ShiftKg := #PartCount * #PartWeight_kg; // WRONG
// Fix: widen one operand first so the math runs in DInt.
#ShiftKg := INT_TO_DINT(#PartCount) * INT_TO_DINT(#PartWeight_kg); // OK
// Trap 2: a typed constant sets the data type of the math.
#ShiftKg := INT#1 + 50000; // WRONG: done in Int, gives -15535
// Don't mix typed and untyped constants. Type both, or neither:
#ShiftKg := INT#1 + DINT#50000; // OK: done in DInt
#ShiftKg := 1 + 50000; // OK: untyped, widest type on the CPU
// Trap 3: a Real total that stops adding.
// Real holds about 7 significant digits. Above 16,777,216
// adding 1.0 does nothing. Use LReal (S7-1500) for totals.
#TotalLiters := #TotalLiters + #FlowThisScan; // LReal
Overflow in SCL doesn't stop the CPU. Unless you turn on the block property "Set ENO automatically", SCL doesn't report it through ENO either; it just stores a wrong number.3
Educational example. This code has not been compiled, downloaded, or tested on a controller. It is not production-ready or drop-in code. Treat it as a starting point to review, adapt, and test on your own system. It is standard control logic, not safety logic.
Habits that prevent this
- Size tags for the worst case, not today's numbers. A count that's never over 300 today can be 3,000 after a line speed change.
- Turn on "Set ENO automatically" for blocks where a math error must be caught, and check ENO. It adds some run-time overhead, so use it where it matters.3
- Test the edges. In a simulator, force the inputs to their maximum values and see what the math does before it goes live.
- Check conversions. Converting a large
DInttoInt, or aRealto an integer, can fail or round. Look at the conversion rules for your CPU family.4
Sources
- Siemens, STEP 7 (TIA Portal) help, "Arithmetic expressions" (data type of the result): docs.tia.siemens.cloud.
- Siemens, STEP 7 help, "Calculating with constants in SCL" (typed and untyped constants): docs.tia.siemens.cloud.
- Siemens, STEP 7 help, "Enabling and disabling the property 'Set ENO automatically' in SCL": docs.tia.siemens.cloud.
- Siemens, STEP 7 help, "Overview of data type conversion (S7-1500, S7-1200 G2)": docs.tia.siemens.cloud.
Instruction details were checked against the vendor documentation linked above. Behavior can change between software and firmware versions, so check the help for the version you run.