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

解决CODESYS可视化中XY Chart连接最新值与原点的异常问题

CODESYS XY Chart绘图异常问题及解决

问题现象

在CODESYS可视化中使用XY Chart绘制每秒更新的电压值数组时,出现异常:XY Chart会将最新采集的电压值与(0,0)连接,形成一条多余的线条,导致绘图显示错误。尝试将数组初始化为5后,异常线条变为连接到(0,5)而非(0,0)。

相关代码

PROGRAM PLC_PRG
    VAR
        // *** Constants and Parameters ***
        Max_Samples   : INT := 20;                 // Maximum number of samples to be collected

        // *** ADC Input Reading ***
        ADC_1         : INT;                       // Raw ADC value from the input

        // *** Data Storage Arrays ***
        arr_Voltages  : ARRAY[0..20] OF REAL;      // Array initialized with NaN (Not-a-Number) values
        arr_Time      : ARRAY[0..20] OF INT;       // Array to store time-related sample indices

        // *** Timer and Counters ***
        Sample_Timer  : TON;                       // Timer instance for sampling intervals (TON type)
        Sample_Time   : TIME := T#1s;              // Sampling interval duration (1 second)
        sample_index  : INT := 0;                  // Current index for writing data into arrays
        start_Timing  : BOOL;                      // Control signal to start or stop the timer
        i             : INT;                       // General counter variable

        // *** Web Visualization Variables ***
        start_Measurement : BOOL := FALSE;         // Control flag for starting the measurement process
        reset_Visu        : BOOL := FALSE;         // Control flag for resetting the visualization
        status_LED        : BOOL := FALSE;         // Status indicator (e.g., for LED on the Web Visu)
    END_VAR

    // Timer configuration: The timer is controlled by 'start_Timing' and runs for 'Sample_Time'.
    Sample_Timer(IN := start_Timing, PT := Sample_Time); // Timer runs with a duration of 1 second.

    IF start_Measurement THEN   // Check if measurement should start.
        start_Timing := TRUE;   // Activate the timer to begin the sampling process.
        status_LED := TRUE;
    END_IF
  
    IF Sample_Timer.Q THEN  // Check if the timer has completed (Q = TRUE indicates the timer is done).
        start_Timing := FALSE;          // Stop the timer to reset its state.
        Sample_Timer(IN := FALSE);      // Explicitly reset the timer to prepare for the next interval.

        IF sample_index <= Max_Samples THEN  // Ensure the index is within the allowed range.
            arr_Time[sample_index] := sample_index;       // Store the current index as the timestamp.
            arr_Voltages[sample_index] := get_Voltage(ADC_1); // Record the measured voltage from the ADC.
            sample_index := sample_index + 1;            // Increment the sample index for the next measurement.
        ELSE
            sample_index := 0;            // Reset the sample index to the beginning.
            start_Measurement := FALSE;  // Stop the measurement process as the maximum samples are reached.
            status_LED := FALSE;
        END_IF
    END_IF

    IF reset_Visu THEN
        // Reset all elements of arr_Time to 0
        FOR i := 0 TO Max_Samples DO
            arr_Time[i] := 0;
            arr_Voltages[i] := 0.0; // Use 0.0 for REAL array elements
        END_FOR

        sample_index := 0;          // Reset the sample index
    END_IF

异常绘图

XY Chart异常绘图

解决方案

新增变量plot_Index,并将其设置为XY Chart的Array Indexes Maximum,代码如下:

plot_Index := sample_index-1;

减1调整是必要的:数组首个索引为0,而每次采集完成后sample_index会自增1,此时sample_index指向的是下一个待填充的位置(未赋值,默认值为0或初始化值)。若不调整,XY Chart会读取到该未赋值元素,从而出现连接到原点/初始值的异常线条。

疑问

CODESYS中仅声明但未显式初始化的数组,是否会自动初始化为0?目前观察到仅声明数组时,所有元素默认值似乎都是0。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 02:57:12