啊鑫
2025-08-01 bc55470b84ecaa1e2f6397a3ec33f0c7192224c5
ConfigDemo.cs
@@ -226,5 +226,94 @@
            
            Console.WriteLine($"\n数据读取时间: {data.ReadTime:yyyy-MM-dd HH:mm:ss}");
        }
        /// <summary>
        /// 测试数据解析逻辑 - 使用原始值.txt中的样本数据
        /// </summary>
        public static void TestDataParsing()
        {
            Console.WriteLine("\n=== 数据解析测试(基于原始值.txt样本数据)===");
            // 创建测试用的字段配置
            var minInstallSizeConfig = new DataField
            {
                Address = 6033,
                DataType = "Integer",
                Length = 2,
                Scale = 0.01,
                Unit = "mm",
                DecimalPlaces = 2,
                DisplayName = "最小安装尺寸"
            };
            var maxInstallSizeConfig = new DataField
            {
                Address = 6035,
                DataType = "Integer",
                Length = 2,
                Scale = 0.01,
                Unit = "mm",
                DecimalPlaces = 2,
                DisplayName = "最大安装尺寸"
            };
            var workingVoltageConfig = new DataField
            {
                Address = 6041,
                DataType = "Integer",
                Length = 2,
                Scale = 0.01,
                Unit = "V",
                DecimalPlaces = 2,
                DisplayName = "工作电压"
            };
            var workingPressureConfig = new DataField
            {
                Address = 6045,
                DataType = "Integer",
                Length = 2,
                Scale = 0.01,
                Unit = "bar",
                DecimalPlaces = 2,
                DisplayName = "工作压力"
            };
            // 原始值.txt中的测试数据
            Console.WriteLine("\n测试样本数据:");
            TestSingleField("最小安装尺寸", new int[] { -31513, 0 }, minInstallSizeConfig);
            TestSingleField("最大安装尺寸", new int[] { -18440, 0 }, maxInstallSizeConfig);
            TestSingleField("工作电压", new int[] { 2891, 0 }, workingVoltageConfig);
            TestSingleField("工作压力", new int[] { -31039, 1 }, workingPressureConfig);
            // 额外的测试数据
            TestSingleField("速度", new int[] { 541, 0 }, new DataField
            {
                DataType = "Integer", Length = 2, Scale = 0.01, Unit = "mm/s", DecimalPlaces = 2, DisplayName = "速度"
            });
            TestSingleField("工作电流", new int[] { 125, 0 }, new DataField
            {
                DataType = "Integer", Length = 2, Scale = 0.01, Unit = "A", DecimalPlaces = 2, DisplayName = "工作电流"
            });
        }
        private static void TestSingleField(string testName, int[] registers, DataField config)
        {
            Console.WriteLine($"\n--- 测试 {testName} ---");
            Console.WriteLine($"原始寄存器: [{string.Join(", ", registers)}]");
            try
            {
                var result = ModbusDataParser.ParseFieldData(registers, config);
                var displayValue = ModbusDataParser.FormatDisplayValue(result, config);
                Console.WriteLine($"解析结果: {result}");
                Console.WriteLine($"显示值: {displayValue}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"解析失败: {ex.Message}");
            }
        }
    }
}