You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求包含外部文本语言例程的IEC 61131-3 PLC源程序示例

Hey there! I’ve put together a complete IEC 61131-3 program example that checks all your boxes—it includes embedded C code (an "external" language per IEC 61131-3:2013 §4.3) for both a function and a function block, with all other code strictly following the standard’s Structured Text (ST) rules from Chapters 6, 7, and Appendix A.

Here’s the full continuous program, starting with PROGRAM and ending with END_PROGRAM:

PROGRAM MainProgram
VAR
    InputData: ARRAY[0..15] OF BYTE; // IEC-compliant ST variable declaration
    CRCResult: DWORD;
    TempSensorInput: REAL;
    FilteredTemp: REAL;
    TempFilter: TemperatureFilter; // Instance of our external-language FB
END_VAR

// ------------------------------
// External-language (C) Function definition
// Implements a CRC32 calculation, full source embedded
FUNCTION CalculateCRC: DWORD
VAR_INPUT
    Data: POINTER TO BYTE;
    Length: UINT;
END_VAR
{C
#include <stdint.h>

uint32_t CalculateCRC(uint8_t* Data, uint16_t Length) {
    uint32_t crc = 0xFFFFFFFF;
    for (uint16_t i = 0; i < Length; i++) {
        crc ^= Data[i];
        for (uint8_t j = 0; j < 8; j++) {
            crc = (crc >> 1) ^ (0xEDB88320 * (crc & 1));
        }
    }
    return ~crc;
}
}
END_FUNCTION

// ------------------------------
// External-language (C) Function Block Type definition
// Implements a simple exponential moving average filter, full source embedded
FUNCTION_BLOCK TemperatureFilter
VAR_INPUT
    RawTemp: REAL;
    Alpha: REAL := 0.2; // Smoothing factor (0-1)
END_VAR
VAR_OUTPUT
    FilteredTemp: REAL;
END_VAR
VAR
    PreviousValue: REAL := 0.0; // Internal state variable
END_VAR
{C
#include <math.h>

void TemperatureFilter_Run(TemperatureFilter* instance) {
    // Apply exponential moving average formula
    instance->FilteredTemp = (instance->Alpha * instance->RawTemp) + 
                             ((1 - instance->Alpha) * instance->PreviousValue);
    // Update state for next cycle
    instance->PreviousValue = instance->FilteredTemp;
}
}
// IEC-compliant ST execution logic for the FB
TemperatureFilter_Run(THIS);
END_FUNCTION_BLOCK

// ------------------------------
// Main program logic (pure ST, IEC-compliant)
InputData := [16#01, 16#02, 16#03, 16#04, 16#05, 16#06, 16#07, 16#08,
              16#09, 16#0A, 16#0B, 16#0C, 16#0D, 16#0E, 16#0F, 16#10];
CRCResult := CalculateCRC(ADR(InputData), SIZEOF(InputData));

TempSensorInput := 25.3; // Simulated sensor input
TempFilter.RawTemp := TempSensorInput;
TempFilter(); // Execute the function block
FilteredTemp := TempFilter.FilteredTemp;
END_PROGRAM

Key Details:

  • The outer program structure, variable declarations, and main execution logic use Structured Text (ST), fully compliant with IEC 61131-3:2013 Chapters 6, 7, and Appendix A (no deprecated Instruction List).
  • The CalculateCRC function and TemperatureFilter function block are implemented in C (an external language not defined by IEC 61131-3), with their full source code embedded directly in the program (no external libraries).
  • The {C} / closing delimiter pattern is a common vendor-supported way to mark external language code, aligning with the standard’s allowance for non-IEC languages.
  • The FB’s execution logic uses ST syntax to invoke the C implementation, maintaining compliance for the non-external code portions.

内容的提问来源于stack exchange,提问作者Kirk Hansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 09:12:39