Por que a Automação é Inevitável
A engenharia manual no TIA Portal é lenta e propensa a erros. Cliques com o botão direito, criação de blocos, copiar e colar código... Essas tarefas repetitivas devem desaparecer. Graças à Openness API (e seu wrapper REST T-IA Connect), você pode controlar o TIA Portal como qualquer software moderno.
Pré-requisitos
- TIA Portal V16, V17, V18, V19 ou V21 instalado
- Uma licença T-IA Connect (ou plano Freemium)
- PowerShell ou Python instalado na sua máquina
Passo 1: Iniciar a API REST
Em vez de iniciar o TIA Portal manualmente, iniciaremos o servidor T-IA Connect que atuará como gateway. Abra seu terminal e execute:
./TiaPortalApi.App.exe --headless
Passo 2: Health Check
Antes de tudo, vamos verificar se a API está funcionando e quais versões do TIA Portal estão disponíveis.
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"]
}Passo 3: Criar um Projeto
Chega de menus 'Arquivo > Novo'. Vamos enviar uma requisição POST para instanciar um projeto em branco.
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
}Passo 4: Adicionar um PLC e Criar um FB
Pesquise uma CPU no catálogo de hardware, adicione-a ao projeto e crie um Function Block com código SCL — tudo em poucas chamadas 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
}Passo 5: Criar Tags
Importe tags PLC (entradas, saídas, memória) em massa com uma única chamada 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" }
]
}Passo 6: Compilar o Projeto
Inicie uma compilação e acompanhe o progresso pela API de Jobs. A compilação é executada de forma assíncrona.
# 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
}Passo 7: Exportar para XML
Exporte seus blocos no formato SimaticML (XML) para controle de versão com 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"]
}Passo 8: Salvar e Fechar
Salve o projeto e feche-o corretamente. A instância do TIA Portal fica livre para a próxima execução.
curl -X POST http://localhost:9000/api/projects/actions/save curl -X POST http://localhost:9000/api/projects/actions/close
{ "saved": true }
{ "closed": true }