Waarom Automatisering Onvermijdelijk is
Handmatige engineering in TIA Portal is traag en foutgevoelig. Rechtsklikken, blokken maken, code kopiëren en plakken... Deze repetitieve taken moeten verdwijnen. Dankzij de Openness API (en de T-IA Connect REST wrapper), kunt u TIA Portal besturen zoals elke moderne software.
Vereisten
- TIA Portal V16, V17, V18, V19 of V21 geïnstalleerd
- Een T-IA Connect licentie (of Freemium-plan)
- PowerShell of Python geïnstalleerd op uw machine
Stap 1: Start de REST API
In plaats van TIA Portal handmatig te starten, starten we de T-IA Connect server die als gateway zal fungeren. Open uw terminal en voer uit:
./TiaPortalApi.App.exe --headless
Stap 2: Health Check
Laten we eerst controleren of de API draait en welke TIA Portal versies beschikbaar zijn.
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"]
}Stap 3: Een Project Aanmaken
Geen 'Bestand > Nieuw' menu's meer. Laten we een POST-verzoek sturen om een leeg project aan te maken.
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
}Stap 4: PLC Toevoegen & FB Aanmaken
Zoek een CPU in de hardwarecatalogus, voeg deze toe aan het project en maak een Functieblok met SCL-code — alles in een paar API-aanroepen.
# 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
}Stap 5: Tags Aanmaken
Importeer PLC-tags (ingangen, uitgangen, geheugen) in bulk via een enkele API-aanroep.
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" }
]
}Stap 6: Project Compileren
Start een compilatie en volg de voortgang via de Jobs API. De compilatie wordt asynchroon uitgevoerd.
# 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
}Stap 7: Exporteren naar XML
Exporteer uw blokken naar SimaticML (XML) formaat voor versiebeheer met Git.
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"]
}Stap 8: Opslaan en Sluiten
Sla het project op en sluit het netjes af. De TIA Portal-instantie is vrij voor de volgende geautomatiseerde run.
curl -X POST http://localhost:9000/api/projects/actions/save curl -X POST http://localhost:9000/api/projects/actions/close
{ "saved": true }
{ "closed": true }