WPF上位機 - 使用轉換器實現TIA Wincc中的文字列表功能

4LPH4_αX發表於2024-05-03

TIA wincc中可以根據變數的值,顯示出定義的文字。

在WPF中可以透過轉換器實現。

使用雜湊表儲存變數和文字,根據變數值返回對應的文字顯示在View 中


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace SiemensAxisControl.Model.Converter
{
    internal class StringToStatusConverter : IValueConverter
    {
        private static readonly Dictionary<string, string> StatusDic = new Dictionary<string, string>()
        {
            {"7000", "STATUS_NO_CALL" },
            {"7001", "STATUS_FB_FIRST_CALL" },
            {"7002", "STATUS_SUBSEQUENT_CALL" },
            {"8001", "ERR_INVALID_BASIC_MOTION_CMD" },
            {"8002", "ERR_INVALID_EXTENDED_CMD" },
            {"8200", "ERR_INVALID_CAM" },
            {"8201", "ERR_INVALID_JOG_MODE" },
            {"8202", "ERR_INVALID_HOMING_EXTENDED_MODE" },
            {"8203", "ERR_TORQUE_LIMITING_NOT_ALLOWED" },
            {"8204", "ERR_INVALID_JOG_DIR" },
            {"8205", "ERR_INVALID_POS_RELATIVE_DIR" },
            {"8206", "ERR_INVALID_POS_SUPERIMPOSED_DIR" },
            {"8207", "ERR_HOMING_MODE_NOT_ALLOWED" },
            {"8208", "ERR_MODULO_NOT_ALLOWED" },
            {"8209", "ERR_INVALID_TORQUE_LIMIT" },
            {"8210", "ERR_INVALID_VELOCITY" },
            {"8600", "ERR_MC_POWER" },
            {"8601", "ERR_MC_RESET" },
            {"8602", "ERR_MC_HOME" },
            {"8603", "ERR_MC_TORQUELIMITING" },
            {"8604", "ERR_MC_HALT" },
            {"8605", "ERR_MC_MOVEJOG" },
            {"8606", "ERR_MC_MOVEVELOCITY" },
            {"8607", "ERR_MC_MOVERELATIVE" },
            {"8608", "ERR_MC_MOVEABSOLUTE" },
            {"8609", "ERR_MC_MOVESUPERIMPOSED" },
            {"860A", "ERR_MC_GEARIN" },
            {"860B", "ERR_MC_PHASINGABSOLUTE"},
            {"860C", "ERR_MC_PHASINGRELATIVE" },
            {"860D", "ERR_MC_CAMIN" },
            {"860E", "ERR_MC_GEARINPOS" },
            {"860F", "ERR_MC_SYNCHRONIZEDMOTIONSIMULATION" },
            {"8700", "ERR_UNDEFINED_FB_STATE" },
            {"8701", "ERR_UNDEFINED_RESET_STATE" },
            {"8702", "ERR_UNDEFINED_BASIC_MOTION_STATE" },
            {"8703", "ERR_UNDEFINED_TORQUE_LIMITING_STATE" },
            {"8704", "ERR_UNDEFINED_INCREMENTAL_JOG_STATE" },
            {"8705", "ERR_UNDEFINED_INCREMENTAL_JOG_SUBSTATE" },
            {"8706", "ERR_UNDEFINED_EXTENDED_HOMING_STATE" },
            {"8707", "ERR_UNDEFINED_EXTENDED_HOMING_SUBSTATE" },
            {"8708", "ERR_UNDEFINED_SUPERIMPOSED_MOTION_STATE" },
            {"8709", "ERR_UNDEFINED_MOTION_SIMULATION_STATE" },
            {"8710", "ERR_UNDEFINED_MAIN_SELECTION_STATE" }
        };
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string Status = "Last Command Ok";
            // 檢查傳入的值是否在指定的範圍內
            if (value is string key)
            {

                if (StatusDic.ContainsKey(key))
                {
                    Status = StatusDic[key];
                }
            }

            return Status;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

相關文章