| | |
| | | /// <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 |
| | |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"[PARSER-ERROR] 解析字段 '{fieldConfig.DisplayName}' 时发生错误: {ex.Message}"); |
| | | Console.WriteLine($"[PARSER-ERROR] 堆栈跟踪: {ex.StackTrace}"); |
| | | return GetDefaultValue(fieldConfig.DataType); |
| | | } |
| | | } |
| | |
| | | /// </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, |