From b8249a2788abfc0332ee614b4bb84cf3bf4de756 Mon Sep 17 00:00:00 2001
From: maqiao <625215135@qq.com>
Date: Tue, 13 Aug 2024 17:05:27 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B8=A9=E6=B9=BF=E5=BA=A6=E6=B7=BB=E5=8A=A0tr?=
=?UTF-8?q?y=20catch?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
DM_Weight/App.config | 4 +-
DM_Weight/Port/PortUtil.cs | 43 ++++++++++++---------
DM_Weight/ViewModels/HomeWindowViewModel.cs | 17 +++++---
DM_Weight/Views/MachineRecordWindow.xaml | 8 ++--
4 files changed, 42 insertions(+), 30 deletions(-)
diff --git a/DM_Weight/App.config b/DM_Weight/App.config
index d7768c3..2d4cf09 100644
--- a/DM_Weight/App.config
+++ b/DM_Weight/App.config
@@ -54,7 +54,7 @@
-
+
@@ -121,7 +121,7 @@
-
+
diff --git a/DM_Weight/Port/PortUtil.cs b/DM_Weight/Port/PortUtil.cs
index a7a55c8..9a8974a 100644
--- a/DM_Weight/Port/PortUtil.cs
+++ b/DM_Weight/Port/PortUtil.cs
@@ -1880,27 +1880,34 @@ namespace DM_Weight.Port
public TemperatureHumidityInfo GetWSD()
{
TemperatureHumidityInfo temperatureHumidityInfo = new TemperatureHumidityInfo();
- var master = ModbusSerialMaster.CreateRtu(wsdSerial);
- int no = 1;
- var r = master.ReadHoldingRegisters((byte)no, 00, 2);
-
- if (r != null && r.Length > 0)
+ try
{
- var result = string.Join("", r);
- var result1 = result.Substring(0, 3);
- var result2 = result.Substring(3, 3);
- float temp = Int32.Parse(result2) / 10.0F;
- float humi = Int32.Parse(result1) / 10.0F;
- temperatureHumidityInfo.GroupNo = "DM5";
- temperatureHumidityInfo.Temp = temp.ToString();
- temperatureHumidityInfo.Humi = humi.ToString();
+ var master = ModbusSerialMaster.CreateRtu(wsdSerial);
+ int no = 1;
+ var r = master.ReadHoldingRegisters((byte)no, 00, 2);
+ if (r != null && r.Length > 0)
+ {
+ var result = string.Join("", r);
+ var result1 = result.Substring(0, 3);
+ var result2 = result.Substring(3, 3);
+ float temp = Int32.Parse(result2) / 10.0F;
+ float humi = Int32.Parse(result1) / 10.0F;
+ temperatureHumidityInfo.GroupNo = "DM5";
+ temperatureHumidityInfo.Temp = temp.ToString();
+ temperatureHumidityInfo.Humi = humi.ToString();
+
+ }
+ //if (wsdSerial.IsOpen)
+ //{
+ // wsdSerial.Close();
+ //}
+ master.Dispose();
+ }
+ catch (Exception ex)
+ {
+ logger.Info($"获取温湿度异常{ex.Message}");
}
- //if (wsdSerial.IsOpen)
- //{
- // wsdSerial.Close();
- //}
- master.Dispose();
return temperatureHumidityInfo;
}
#endregion
diff --git a/DM_Weight/ViewModels/HomeWindowViewModel.cs b/DM_Weight/ViewModels/HomeWindowViewModel.cs
index 292d83d..5acdb39 100644
--- a/DM_Weight/ViewModels/HomeWindowViewModel.cs
+++ b/DM_Weight/ViewModels/HomeWindowViewModel.cs
@@ -132,14 +132,14 @@ namespace DM_Weight.ViewModels
}
else if (value.PremissionPath.Equals("SettingMainWindow") || value.PremissionPath.Equals("SettingWindow"))
{
- if (Convert.ToInt32(ConfigurationManager.AppSettings["hasFridge"])>0)
+ if (Convert.ToInt32(ConfigurationManager.AppSettings["hasFridge"]) > 0)
{
_regionManager.RequestNavigate("ContentRegion", "SettingMainWindow");
}
else
{
_regionManager.RequestNavigate("ContentRegion", "SettingWindow");
- }
+ }
}
else
{
@@ -311,7 +311,7 @@ namespace DM_Weight.ViewModels
{
string retStr = string.Empty;
byte[] data = null;
- float retT = await _portUtil.GetFridgeTemperature(1);
+ float retT = await _portUtil.GetFridgeTemperature(1);
Thread.Sleep(80);
WD = $"恒温冷藏抽屉当前温度:{Math.Round((retT - 1.5), 2)}℃;";
if (Convert.ToInt32(ConfigurationManager.AppSettings["hasFridge"]) > 1)
@@ -323,10 +323,10 @@ namespace DM_Weight.ViewModels
}
}
//保存温湿度信息
- private async void GetWSD(object sender,ElapsedEventArgs e)
+ private async void GetWSD(object sender, ElapsedEventArgs e)
{
TemperatureHumidityInfo temp = _portUtil.GetWSD();
- if(temp != null)
+ if (temp != null)
{
SqlSugarHelper.Db.Insertable(new TemperatureHumidityInfo()
{
@@ -335,8 +335,13 @@ namespace DM_Weight.ViewModels
Humi = temp.Humi,
AddTime = temp.AddTime
}).ExecuteCommand();
+ logger.Info($"保存温湿度信息:{temp.Temp},{temp.Humi}");
+ }
+ else
+ {
+
+ logger.Info("温湿度信息返回空");
}
-
}
diff --git a/DM_Weight/Views/MachineRecordWindow.xaml b/DM_Weight/Views/MachineRecordWindow.xaml
index f0d7fc7..c005b65 100644
--- a/DM_Weight/Views/MachineRecordWindow.xaml
+++ b/DM_Weight/Views/MachineRecordWindow.xaml
@@ -78,16 +78,16 @@
-
-
-
-