为什么自动化是不可避免的
TIA Portal 中的手动工程既慢又容易出错。右键单击、创建块、复制粘贴代码……这些重复性任务必须消失。多亏了 Openness API(及其 T-IA Connect REST 包装器),您可以像控制任何现代软件一样控制 TIA Portal。
先决条件
- 安装了 TIA Portal V16、V17、V18、V19 或 V21
- T-IA Connect 许可证(或 Freemium 版本)
- PowerShell或Python已安装在您的计算机上
第 1 步:启动 REST API
我们将启动充当网关的 T-IA Connect 服务器,而不是手动启动 TIA Portal。打开终端并运行:
PowerShell
./TiaPortalApi.App.exe --headless
步骤 2:健康检查
首先,让我们验证API是否正在运行,以及机器上有哪些TIA Portal版本可用。
curl
curl http://localhost:9000/api/health curl http://localhost:9000/api/health/versions
Response
{
"status": "healthy",
"tiaPortalConnected": true,
"uptime": "00:01:23"
}
{
"installedVersions": ["V17", "V18", "V19"]
}步骤 3:创建项目
不再需要“文件 > 新建”菜单。让我们发送一个POST请求来创建一个空项目。
curl — POST /api/projects/actions/create
curl -X POST http://localhost:9000/api/projects/actions/create \
-H "Content-Type: application/json" \
-d '{
"name": "MyAutomatedProject",
"path": "C:\\TIA_Projects",
"version": "V19"
}'Response
{
"name": "MyAutomatedProject",
"path": "C:\\TIA_Projects\\MyAutomatedProject",
"version": "V19",
"created": true
}步骤 4:添加PLC并创建FB
在硬件目录中搜索CPU,将其添加到项目中,然后创建带有SCL代码的功能块——只需几个API调用。
curl — Search catalog + Add device
# 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;"
}'Response
// Block creation response
{
"name": "FB_MotorControl",
"type": "FB",
"programmingLanguage": "SCL",
"number": 1
}
// Network added
{
"networkId": 1,
"title": "Motor control logic",
"created": true
}步骤 5:创建标签
通过单个API调用批量导入PLC标签(输入、输出、存储器)。
curl — POST /api/devices/PLC_1/tags/actions/import
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" }
]
}'Response
{
"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:编译项目
触发编译并通过Jobs API跟踪进度。编译异步运行——轮询作业状态直到完成。
curl — Compile + Poll job
# 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>
Response
// Compilation started
{ "jobId": "c7f3a1b2-...", "status": "running" }
// Job completed
{
"jobId": "c7f3a1b2-...",
"status": "completed",
"errors": [],
"warnings": 2
}步骤 7:导出为XML
将您的块导出为SimaticML(XML)格式,用于Git版本控制。
curl — POST /api/devices/PLC_1/blocks/actions/export
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"
}'Response
{
"exportedCount": 1,
"exportPath": "C:\\TIA_Projects\\Export",
"files": ["FB_MotorControl.xml"]
}步骤 8:保存并关闭
保存项目并正常关闭。TIA Portal实例已释放,准备进行下一次自动化运行。
curl — Save & Close
curl -X POST http://localhost:9000/api/projects/actions/save curl -X POST http://localhost:9000/api/projects/actions/close
Response
{ "saved": true }
{ "closed": true }