From bc55470b84ecaa1e2f6397a3ec33f0c7192224c5 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期五, 01 八月 2025 15:52:25 +0800
Subject: [PATCH] 修复整数数据解析逻辑:实现按字节读取高低字节交换组合
---
ConfigDemo.cs | 89 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 89 insertions(+), 0 deletions(-)
diff --git a/ConfigDemo.cs b/ConfigDemo.cs
index 33e3e3c..4665ac4 100644
--- a/ConfigDemo.cs
+++ b/ConfigDemo.cs
@@ -226,5 +226,94 @@
Console.WriteLine($"\n鏁版嵁璇诲彇鏃堕棿: {data.ReadTime:yyyy-MM-dd HH:mm:ss}");
}
+
+ /// <summary>
+ /// 娴嬭瘯鏁版嵁瑙f瀽閫昏緫 - 浣跨敤鍘熷鍊�.txt涓殑鏍锋湰鏁版嵁
+ /// </summary>
+ public static void TestDataParsing()
+ {
+ Console.WriteLine("\n=== 鏁版嵁瑙f瀽娴嬭瘯锛堝熀浜庡師濮嬪��.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($"瑙f瀽缁撴灉: {result}");
+ Console.WriteLine($"鏄剧ず鍊�: {displayValue}");
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"瑙f瀽澶辫触: {ex.Message}");
+ }
+ }
}
}
\ No newline at end of file
--
Gitblit v1.9.3