Почему автоматизация неизбежна
Ручная разработка в TIA Portal медленная и подвержена ошибкам. Правые клики, создание блоков, копирование кода... Эти повторяющиеся задачи должны исчезнуть. Благодаря Openness API (и оболочке REST T-IA Connect), вы можете управлять TIA Portal как любым современным ПО.
Предварительные требования
- Установлен TIA Portal V16, V17, V18, V19 или V21
- Лицензия T-IA Connect (или план Freemium)
- PowerShell или Python установлен на вашем компьютере
Шаг 1: Запуск REST API
Вместо ручного запуска TIA Portal мы запустим сервер T-IA Connect, который будет действовать как шлюз. Откройте терминал и выполните:
./TiaPortalApi.App.exe --headless
Шаг 2: Проверка работоспособности
Прежде всего проверим, что API работает и какие версии TIA Portal доступны.
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"]
}Шаг 3: Создание проекта
Больше никаких меню 'Файл > Создать'. Отправим POST-запрос для создания пустого проекта.
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
}Шаг 4: Добавление PLC и создание FB
Найдите CPU в каталоге оборудования, добавьте его в проект и создайте Function Block с кодом SCL — всё за несколько API-вызовов.
# 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
}Шаг 5: Создание тегов
Импортируйте теги PLC (входы, выходы, память) массово одним вызовом API.
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" }
]
}Шаг 6: Компиляция проекта
Запустите компиляцию и отслеживайте прогресс через API Jobs. Компиляция выполняется асинхронно.
# 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
}Шаг 7: Экспорт в XML
Экспортируйте блоки в формат SimaticML (XML) для контроля версий с 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"]
}Шаг 8: Сохранение и закрытие
Сохраните проект и корректно закройте его. Экземпляр TIA Portal освобождён для следующего запуска.
curl -X POST http://localhost:9000/api/projects/actions/save curl -X POST http://localhost:9000/api/projects/actions/close
{ "saved": true }
{ "closed": true }