How to Import SCL into TIA Portal
TIA Portal can turn a text file with SCL source code into real program blocks. This is how you bring in code from SCLcode, from a colleague, from version control or from an older project. The feature is called external source files.
Import an .scl file step by step
- Open your project and expand the CPU in the Project tree (for example
PLC_1 [CPU 1516-3 PN/DP]). - Expand External source files and double-click Add new external file.
- Select your
.sclfile and click Open. The file now appears under External source files. - Right-click the file and choose Generate blocks from source.
- Confirm the dialog. TIA Portal compiles the source and creates the FBs, FCs, UDTs or DBs it contains under Program blocks.
- Call the new FB from an OB or another block, which creates its instance DB, then compile the software and download as usual.
The reverse also works: right-click blocks under Program blocks and choose Generate source from blocks to export them as SCL text.
What an importable SCL file looks like
The file contains complete block definitions, not just statements. One file may hold several blocks; a block must come after the blocks it uses. Here a line FB uses FB_DelayedStart from the SCL code examples as a multi-instance, so FB_DelayedStart must be earlier in the same file or already exist in the project:
FUNCTION_BLOCK "FB_Line"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
StartLine : Bool;
StopLine : Bool;
END_VAR
VAR_OUTPUT
ConveyorRunning : Bool;
END_VAR
VAR
Conveyor : "FB_DelayedStart"; // Multi-instance: data lives in this FB's DB
END_VAR
BEGIN
#Conveyor(Start := #StartLine AND NOT #StopLine,
Delay := T#3S);
#ConveyorRunning := #Conveyor.Running;
END_FUNCTION_BLOCK
- Block names are in double quotes:
FUNCTION_BLOCK "FB_Line". { S7_Optimized_Access := 'TRUE' }creates an optimized block, the default on S7-1200 and S7-1500.- The statement part starts with
BEGINand the block ends withEND_FUNCTION_BLOCKorEND_FUNCTION.
Can I paste SCL straight into a block?
Only the statement part. The SCL editor of an existing block holds the code after BEGIN; the interface (VAR_INPUT, VAR_OUTPUT, …) lives in the interface table above it. Pasting a whole source file into the editor gives syntax errors. For complete blocks, use an external source file as above.
Common errors when generating blocks from source
| Error or symptom | Usual cause and fix |
|---|---|
| Block or data type not found | A called FB or UDT is not in the project yet. Put it earlier in the file or import it first. |
| Unknown function such as TIME or NOW | Not SCL. Use a TON_TIME timer for durations and RD_SYS_T for the clock. |
| Data type not supported | LREAL, SINT or UDINT on an S7-300/400 target. Use REAL, INT or DINT. |
| Missing semicolon or END_IF | Every statement ends with ; and every IF, CASE and FOR has its END_ with a semicolon. |
| Block exists already | TIA Portal asks before overwriting. Rename the block in the file or confirm the overwrite. |
Paste the source into the free SCL validator before importing to catch most of these offline. More debugging tips: debugging and testing SCL in TIA Portal.
Generate your own SCL block
Describe the logic in plain language and get a Siemens SCL function block you can import into TIA Portal. Free account, no credit card.