Warum Automatisierung unvermeidlich ist
Manuelles Engineering im TIA Portal ist langsam und fehleranfällig. Rechtsklick, Blöcke erstellen, Code kopieren und einfügen... Diese repetitiven Aufgaben müssen verschwinden. Dank der Openness-API (und ihrem T-IA Connect REST-Wrapper) können Sie das TIA Portal wie jede moderne Software steuern.
Voraussetzungen
- TIA Portal V16, V17, V18, V19 oder V21 installiert
- Eine T-IA Connect-Lizenz (oder Freemium-Version)
- PowerShell oder Python auf Ihrem Rechner installiert
Schritt 1: Starten der REST-API
Anstatt das TIA Portal manuell zu starten, starten wir den T-IA Connect-Server, der als Gateway fungiert. Öffnen Sie Ihr Terminal und führen Sie aus:
./TiaPortalApi.App.exe --headless
Schritt 2: Health Check
Bevor wir beginnen, überprüfen wir, ob die API läuft und welche TIA Portal-Versionen auf dem Rechner verfügbar sind.
curl http://localhost:9000/api/health curl http://localhost:9000/api/health/versions
{
"status": "healthy",
"tiaPortalConnected": true,
"uptime": "00:01:23"
}
{
"installedVersions": ["V17", "V18", "V19"]
}Schritt 3: Projekt erstellen
Keine 'Datei > Neu'-Menüs mehr. Senden wir eine POST-Anfrage, um ein leeres Projekt zu instanziieren.
curl -X POST http://localhost:9000/api/projects/actions/create \
-H "Content-Type: application/json" \
-d '{
"name": "MyAutomatedProject",
"path": "C:\\TIA_Projects",
"version": "V19"
}'{
"name": "MyAutomatedProject",
"path": "C:\\TIA_Projects\\MyAutomatedProject",
"version": "V19",
"created": true
}Schritt 4: PLC hinzufügen & FB erstellen
Durchsuchen Sie den Hardwarekatalog nach einer CPU, fügen Sie sie dem Projekt hinzu und erstellen Sie dann einen Funktionsbaustein mit SCL-Code — alles in wenigen API-Aufrufen.
# Search the hardware catalog
curl -X POST http://localhost:9000/api/catalog/actions/search \
-H "Content-Type: application/json" \
-d '{ "searchPattern": "CPU 1511" }'
# Add the device to the project
curl -X POST http://localhost:9000/api/projects/devices/actions/add \
-H "Content-Type: application/json" \
-d '{
"name": "PLC_1",
"typeId": "<typeId from search>",
"deviceName": "CPU 1511C-1 PN"
}'
# Create a Function Block
curl -X POST http://localhost:9000/api/devices/PLC_1/blocks \
-H "Content-Type: application/json" \
-d '{
"name": "FB_MotorControl",
"type": "FB",
"programmingLanguage": "SCL"
}'
# Add SCL code to the block
curl -X POST http://localhost:9000/api/devices/PLC_1/blocks/FB_MotorControl/networks \
-H "Content-Type: application/json" \
-d '{
"title": "Motor control logic",
"code": "#Running := #Start AND NOT #Stop;\nIF #Running THEN\n #Speed := #SpeedSetpoint;\nEND_IF;"
}'// Block creation response
{
"name": "FB_MotorControl",
"type": "FB",
"programmingLanguage": "SCL",
"number": 1
}
// Network added
{
"networkId": 1,
"title": "Motor control logic",
"created": true
}Schritt 5: Tags erstellen
Importieren Sie PLC-Tags (Eingänge, Ausgänge, Speicher) per Massenimport über einen einzigen API-Aufruf. Kein einzelnes Durchklicken der Tag-Tabellen mehr.
curl -X POST http://localhost:9000/api/devices/PLC_1/tags/actions/import \
-H "Content-Type: application/json" \
-d '{
"tagTable": "Motors",
"tags": [
{ "name": "Motor1_Start", "dataType": "Bool", "address": "%I0.0" },
{ "name": "Motor1_Stop", "dataType": "Bool", "address": "%I0.1" },
{ "name": "Motor1_Running", "dataType": "Bool", "address": "%Q0.0" },
{ "name": "Motor1_Speed", "dataType": "Real", "address": "%QD4" },
{ "name": "Motor1_Fault", "dataType": "Bool", "address": "%Q0.1" }
]
}'{
"tagTable": "Motors",
"importedCount": 5,
"tags": [
{ "name": "Motor1_Start", "dataType": "Bool", "address": "%I0.0" },
{ "name": "Motor1_Stop", "dataType": "Bool", "address": "%I0.1" },
{ "name": "Motor1_Running", "dataType": "Bool", "address": "%Q0.0" },
{ "name": "Motor1_Speed", "dataType": "Real", "address": "%QD4" },
{ "name": "Motor1_Fault", "dataType": "Bool", "address": "%Q0.1" }
]
}Schritt 6: Projekt kompilieren
Lösen Sie eine Kompilierung aus und verfolgen Sie den Fortschritt über die Jobs-API. Die Kompilierung läuft asynchron — fragen Sie den Job-Status ab, bis er abgeschlossen ist.
# Start compilation curl -X POST http://localhost:9000/api/devices/PLC_1/actions/compile # Poll the job status (replace <jobId>) curl http://localhost:9000/api/jobs/<jobId>
// Compilation started
{ "jobId": "c7f3a1b2-...", "status": "running" }
// Job completed
{
"jobId": "c7f3a1b2-...",
"status": "completed",
"errors": [],
"warnings": 2
}Schritt 7: Nach XML exportieren
Exportieren Sie Ihre Bausteine im SimaticML-Format (XML) zur Versionskontrolle mit Git. Dies ist der Schlüssel, um SPS-Code wie jeden anderen Quellcode zu behandeln.
curl -X POST http://localhost:9000/api/devices/PLC_1/blocks/actions/export \
-H "Content-Type: application/json" \
-d '{
"blocks": ["FB_MotorControl"],
"exportPath": "C:\\TIA_Projects\\Export"
}'{
"exportedCount": 1,
"exportPath": "C:\\TIA_Projects\\Export",
"files": ["FB_MotorControl.xml"]
}Schritt 8: Speichern und Schließen
Speichern Sie das Projekt und schließen Sie es ordnungsgemäß. Die TIA Portal-Instanz wird freigegeben und ist bereit für den nächsten automatisierten Lauf.
curl -X POST http://localhost:9000/api/projects/actions/save curl -X POST http://localhost:9000/api/projects/actions/close
{ "saved": true }
{ "closed": true }Was kommt als Nächstes?
Sie haben gerade 90 % der manuellen Projekterstellungsaufgaben automatisiert. Sie können dieses Skript nun in Ihre CI/CD-Pipeline (Jenkins, GitLab CI) integrieren, um Ihren Code bei jedem Commit zu validieren.
Laden Sie die vollständigen Skripte für dieses Tutorial herunter und probieren Sie sie mit unserer kostenlosen Freemium-Version aus.