啊鑫
2025-07-26 da107f3b89271191052d9ece45e0010bb2d7d795
config/ModbusDataParser.cs
@@ -18,9 +18,22 @@
        /// <returns>解析后的数据对象</returns>
        public static object? ParseFieldData(int[] registers, DataField fieldConfig)
        {
            if (fieldConfig == null)
            {
                Console.WriteLine("[PARSER-ERROR] 字段配置为null");
                return null;
            }
            if (registers == null || registers.Length == 0)
            {
                Console.WriteLine($"[PARSER-ERROR] 字段 '{fieldConfig.DisplayName}' 的寄存器数据为空");
                return GetDefaultValue(fieldConfig.DataType);
            }
            if (string.IsNullOrEmpty(fieldConfig.DataType))
            {
                Console.WriteLine($"[PARSER-ERROR] 字段 '{fieldConfig.DisplayName}' 的数据类型未定义");
                return null;
            }
            try
@@ -39,6 +52,7 @@
            catch (Exception ex)
            {
                Console.WriteLine($"[PARSER-ERROR] 解析字段 '{fieldConfig.DisplayName}' 时发生错误: {ex.Message}");
                Console.WriteLine($"[PARSER-ERROR] 堆栈跟踪: {ex.StackTrace}");
                return GetDefaultValue(fieldConfig.DataType);
            }
        }
@@ -272,8 +286,11 @@
        /// </summary>
        /// <param name="dataType">数据类型字符串</param>
        /// <returns>默认值对象</returns>
        private static object? GetDefaultValue(string dataType)
        private static object? GetDefaultValue(string? dataType)
        {
            if (string.IsNullOrEmpty(dataType))
                return null;
            return dataType.ToLower() switch
            {
                "byte" => (byte)0,