소스 검색

优化一些下拉框的选择方式

djs 3 달 전
부모
커밋
057c0f2184
8개의 변경된 파일537개의 추가작업 그리고 54개의 파일을 삭제
  1. 34 0
      MV485/db/DBUpgradeHis.cs
  2. 195 5
      MV485/helper/RWRunConfig.cs
  3. 25 3
      MV485/helper/Tools.cs
  4. 7 0
      MV485/model/Constant.cs
  5. 12 5
      MV485/uc/UCDeviceUpgrade.xaml
  6. 112 5
      MV485/uc/UCDeviceUpgrade.xaml.cs
  7. 12 7
      MV485/uc/UCRunConfig.xaml
  8. 140 29
      MV485/uc/UCRunConfig.xaml.cs

+ 34 - 0
MV485/db/DBUpgradeHis.cs

@@ -252,6 +252,40 @@ namespace MV485.db
                 return false;
             }
         }
+
+        public static bool UpdateUpgradeResultAndNewFrieware(TUpgradeHis his)
+        {
+            // 定义更新的 SQL 语句
+            string sql = @"
+                UPDATE t_upgrade_his 
+                SET 
+                    new_fireware = @NewFireware,
+                    upgrade_result = @UpgradeResult
+                WHERE his_id = @HisId";
+
+            // 创建 SQL 参数
+            SQLiteParameter[] parameters = new SQLiteParameter[]
+            {
+                new SQLiteParameter("@HisId", his.HisId),
+                new SQLiteParameter("@NewFireware", his.NewFireware),
+                new SQLiteParameter("@UpgradeResult", his.UpgradeResult)
+            };
+
+            try
+            {
+                // 调用 SQLiteHelper 执行更新操作
+                int rowsAffected = SQLiteHelper.ExecuteSql(sql, parameters);
+
+                // 如果更新成功,返回 true,否则返回 false
+                return rowsAffected > 0;
+            }
+            catch (Exception ex)
+            {
+                // 处理异常(如果有的话)
+                Console.WriteLine("Error updating data: " + ex.Message);
+                return false;
+            }
+        }
         //----------------------------------------------------------------------------------
     }
 

+ 195 - 5
MV485/helper/RWRunConfig.cs

@@ -114,14 +114,15 @@ namespace MV485.helper
                     GenerateValueLog(readName, indCount);
                 }
 
-                readName = "照片亮度放大倍率";
+                //此项已经不需要了
+                /*readName = "照片亮度放大倍率";
                 readRegisters = _modbusMaster.ReadHoldingRegisters(devId,
                     Constant.MB_REGISTER_ADD_BRIGHT_VALUE, Constant.MB_REGISTER_NUM_BRIGHT_VALUE);
                 byte birghtValue = (byte)(readRegisters[0] & 0xFF);  // 10f;
                 runConfig.BrightValueWRFlag = true;
                 runConfig.BrightValue = birghtValue / 10f;
                 GenerateSendAndRecvHexLog(true, readName);
-                GenerateValueLog(readName, birghtValue);
+                GenerateValueLog(readName, birghtValue);*/
 
                 readName = "尾数单位等级";
                 readRegisters = _modbusMaster.ReadHoldingRegisters(devId,
@@ -374,6 +375,73 @@ namespace MV485.helper
             }
         }
 
+
+        //读取抄表器的当前时钟
+        public string ReadDeviceTime(string portName,int baudrate,byte devId)
+        {
+            string deviceTime = null;
+            _portName = portName;
+            _baudrate = baudrate;
+            _address = devId;
+
+            if (OpenSerial(portName, baudrate))
+            {
+                deviceTime = GetDeviceTime(devId);
+            }
+            CloseSerial();
+
+            return deviceTime;
+        }
+
+        private string GetDeviceTime(byte devId)
+        {
+            string readName = "";
+            try
+            {
+                readName = "读抄表器时钟";
+                ushort[] readRegisters;
+                readRegisters = _modbusMaster.ReadHoldingRegisters(devId,
+                    Constant.MB_REGISTER_ADD_DEVICE_TIME, Constant.MB_REGISTER_NUM_DEVICE_TIME);
+                int year = (byte)(readRegisters[0] & 0xFF);
+                int month = (byte)(readRegisters[1] & 0xFF);
+                int day = (byte)(readRegisters[2] & 0xFF);
+                int hour = (byte)(readRegisters[3] & 0xFF);
+                int minute = (byte)(readRegisters[4] & 0xFF);
+                int second = (byte)(readRegisters[5] & 0xFF);
+
+                string deviceTime = $"{2000 + year}-{month.ToString("D2")}-{day.ToString("D2")} " +
+                    $"{hour.ToString("D2")}:{minute.ToString("D2")}:{second.ToString("D2")}";
+
+                GenerateSendAndRecvHexLog(true, readName);
+                GenerateValueLog(readName, deviceTime);
+
+                return deviceTime;
+            }
+            catch (TimeoutException ex)
+            {
+                _message = $"读取{readName}超时,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+                return null;
+            }
+            catch (SlaveException ex)
+            {
+                GenerateSendHexLog(readName);
+                _message = $"读取{readName}Modbus错误,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+                return null;
+            }
+            catch (Exception ex)
+            {
+                GenerateSendHexLog(readName);
+                _message = $"读取{readName}错误,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+                return null;
+            }
+        }
+
         public bool ReadFireware(string portName,int baudrate,byte devId,out string deviceSn,out string fireware)
         {
             bool blRead = false;
@@ -589,14 +657,15 @@ namespace MV485.helper
                     GenerateSendAndRecvHexLog(false, writeName);
                 }
 
-                if (runConfig.BrightValueWRFlag)
+                //此项已经不需要了
+                /*if (runConfig.BrightValueWRFlag)
                 {
                     writeName = "照片亮度系数";
                     ushort brightValue = (ushort)(runConfig.BrightValue * 10);
                     _modbusMaster.WriteSingleRegister(devId, Constant.MB_REGISTER_ADD_BRIGHT_VALUE, brightValue);
                     GenerateValueLog(writeName, runConfig.BrightValue);
                     GenerateSendAndRecvHexLog(false, writeName);
-                }
+                }*/
 
                 if (runConfig.LastUnitLevelWRFlag)
                 {
@@ -674,25 +743,146 @@ namespace MV485.helper
             {
                 GenerateSendHexLog(writeName);
                 _message = $"写入{writeName}超时,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+            }
+            catch (SlaveException ex)
+            {
+                GenerateSendHexLog(writeName);
+                _message = $"写入{writeName}Modbus错误,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+            }
+            catch (Exception ex)
+            {
+                GenerateSendHexLog(writeName);
+                _message = $"写入{writeName}错误,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+            }
+
+            return blWrite;
+        }
+
+
+        public bool WriteDeviceTime(string portName,int baudrate,byte devId,DateTime dateTime)
+        {
+            bool blWrite = false;
+            _portName = portName;
+            _baudrate = baudrate;
+            _address = devId;
+            if (OpenSerial(portName, baudrate))
+            {
+                blWrite = SetDeviceTime(devId, dateTime);
+            }
+            CloseSerial();
+            return blWrite;
+        }
+
+        private bool SetDeviceTime(byte devId,DateTime dateTime)
+        {
+            bool blWrite = false;
+            string writeName = "";
+
+            try
+            {
+                writeName = "抄表器时钟";
+
+                ushort[] result = new ushort[6];
+                result[0] = (ushort)(dateTime.Year - 2000);
+                result[1] = (ushort)dateTime.Month;
+                result[2] = (ushort)dateTime.Day;
+                result[3] = (ushort)dateTime.Hour;
+                result[4] = (ushort)dateTime.Minute;
+                result[5] = (ushort)dateTime.Second;
+
+                _modbusMaster.WriteMultipleRegisters(devId, Constant.MB_REGISTER_ADD_DEVICE_TIME , result);
+                GenerateValueLog(writeName, dateTime.ToString("yyyy-MM-dd HH:mm:ss"));
+                GenerateSendAndRecvHexLog(false, writeName);
+
+                blWrite = true;
+            }
+            catch (TimeoutException ex)
+            {
+                GenerateSendHexLog(writeName);
+                _message = $"写入{writeName}超时,{ex.Message}";
+                _lastErrorMessage = _message;
                 RWLog?.Invoke(_message);
             }
             catch (SlaveException ex)
             {
                 GenerateSendHexLog(writeName);
                 _message = $"写入{writeName}Modbus错误,{ex.Message}";
+                _lastErrorMessage = _message;
                 RWLog?.Invoke(_message);
             }
             catch (Exception ex)
             {
                 GenerateSendHexLog(writeName);
                 _message = $"写入{writeName}错误,{ex.Message}";
+                _lastErrorMessage = _message;
                 RWLog?.Invoke(_message);
             }
+            return blWrite;
+        }
+
+
+        //设置抄表器进行固件版本的回滚
+        public bool WriteDeviceRollback(string portName,int baudrate,byte devId)
+        {
+            bool blWrite = false;
+            _portName = portName;
+            _baudrate = baudrate;
+            _address = devId;
+            if (OpenSerial(portName, baudrate))
+            {
+                blWrite = SetDeviceRollback(devId);
+            }
+            CloseSerial();
+            return blWrite;
+        }
+
+        private bool SetDeviceRollback(byte devId)
+        {
+            bool blWrite = false;
+            string writeName = "";
 
+            try
+            {
+                writeName = "固件版本回滚";
+
+                _modbusMaster.WriteSingleRegister(devId, Constant.MB_REGISTER_ADD_FIREWARE_ROLLBACK, 0x0001);
+                GenerateValueLog(writeName, "=1");
+                GenerateSendAndRecvHexLog(false, writeName);
+
+                blWrite = true;
+            }
+            catch (TimeoutException ex)
+            {
+                GenerateSendHexLog(writeName);
+                _message = $"写入{writeName}超时,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+            }
+            catch (SlaveException ex)
+            {
+                GenerateSendHexLog(writeName);
+                _message = $"写入{writeName}Modbus错误,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+            }
+            catch (Exception ex)
+            {
+                GenerateSendHexLog(writeName);
+                _message = $"写入{writeName}错误,{ex.Message}";
+                _lastErrorMessage = _message;
+                RWLog?.Invoke(_message);
+            }
             return blWrite;
         }
 
-        //写升级包的信息
+
+                //写升级包的信息
         public bool WrtieUpgradeInfo(string portName,int baudrate,byte devId,uint fileSize,uint fileCrc32)
         {
             bool blWrite = false;

+ 25 - 3
MV485/helper/Tools.cs

@@ -64,13 +64,35 @@ namespace MV485.helper
                         new KeyValuePair<ushort, string>(5000, "DN600 - 5000"),
                         new KeyValuePair<ushort, string>(7875, "DN800 - 7875"),
             };
+        public static List<KeyValuePair<ushort, string>> DnValueList =
+            new List<KeyValuePair<ushort, string>>()
+            {
+                        new KeyValuePair<ushort, string>(3, "DN15"),
+                        new KeyValuePair<ushort, string>(5, "DN20"),
+                        new KeyValuePair<ushort, string>(8, "DN25"),
+                        new KeyValuePair<ushort, string>(12, "DN32"),
+                        new KeyValuePair<ushort, string>(20, "DN40"),
+                        new KeyValuePair<ushort, string>(32, "DN50"),
+                        new KeyValuePair<ushort, string>(60, "DN65"),
+                        new KeyValuePair<ushort, string>(79, "DN80"),
+                        new KeyValuePair<ushort, string>(125, "DN100"),
+                        new KeyValuePair<ushort, string>(200, "DN125"),
+                        new KeyValuePair<ushort, string>(312, "DN150"),
+                        new KeyValuePair<ushort, string>(500, "DN200"),
+                        new KeyValuePair<ushort, string>(787, "DN250"),
+                        new KeyValuePair<ushort, string>(1250, "DN300"),
+                        new KeyValuePair<ushort, string>(2000, "DN400"),
+                        new KeyValuePair<ushort, string>(3125, "DN500"),
+                        new KeyValuePair<ushort, string>(5000, "DN600"),
+                        new KeyValuePair<ushort, string>(7875, "DN800"),
+            };
 
         public static List<KeyValuePair<ushort, string>> SampleIntervalList =
             new List<KeyValuePair<ushort, string>>()
     {
-                        new KeyValuePair<ushort, string>(1, "1分钟"),
+                        //new KeyValuePair<ushort, string>(1, "1分钟"),
                         new KeyValuePair<ushort, string>(2, "2分钟"),
-                        new KeyValuePair<ushort, string>(3, "3分钟"),
+                        //new KeyValuePair<ushort, string>(3, "3分钟"),
                         new KeyValuePair<ushort, string>(5, "5分钟"),
                         new KeyValuePair<ushort, string>(10, "10分钟"),
                         new KeyValuePair<ushort, string>(30, "30分钟"),
@@ -82,7 +104,7 @@ namespace MV485.helper
                         new KeyValuePair<ushort, string>(480, "8小时"),
                         new KeyValuePair<ushort, string>(720, "12小时"),
                         new KeyValuePair<ushort, string>(1440, "24小时"),
-                        new KeyValuePair<ushort, string>(9898, "一周"),
+                        new KeyValuePair<ushort, string>(9898, "一周(每周一)"),
     };
 
         public static List<double> UnitList = new List<double>

+ 7 - 0
MV485/model/Constant.cs

@@ -29,6 +29,13 @@ namespace MV485.model
         public const ushort MB_REGISTER_ADD_SAMPLE_FIRST_HOUR = 0x000F;     //首次采样时间
         public const ushort MB_REGISTER_NUM_SAMPLE_FIRST_HOUR = 1;
 
+
+        public const ushort MB_REGISTER_ADD_DEVICE_TIME = 0x0053;		//设备时钟
+        public const ushort MB_REGISTER_NUM_DEVICE_TIME = 6;			//6个寄存器
+
+        public const ushort MB_REGISTER_ADD_FIREWARE_ROLLBACK = 0x005A;		//固件回滚指令
+        public const ushort MB_REGISTER_NUM_FIREWARE_ROLLBACK = 1;			//1个寄存器
+
         //写升级包信息
         public const ushort MB_REGISTER_ADD_UPGRADE_DATA = 0x005C;      //升级包的基本信息
         //前2个地址为升级包的大小,后2个字节为升级文件的CRC32

+ 12 - 5
MV485/uc/UCDeviceUpgrade.xaml

@@ -10,7 +10,7 @@
              Background="WhiteSmoke"             
              Unloaded="UserControl_Unloaded" 
              SizeChanged="UserControl_SizeChanged"
-             d:DesignHeight="600" d:DesignWidth="1366">
+             d:DesignHeight="800" d:DesignWidth="1366">
     <Grid Margin="10">
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="300" />
@@ -114,12 +114,19 @@
                 </Grid>
 
                 <Grid Height="40">
-                    <zdfflatui:FlatButton Grid.Column="1" x:Name="btnDeviceUpgrade" Background="#2196F3" Foreground="White" 
-                                                          Content="开始升级固件"  Click="BtnDeviceUpgrade_Click" 
+                    <Grid.ColumnDefinitions>
+                        <ColumnDefinition Width="*" />
+                        <ColumnDefinition Width="*" />
+                    </Grid.ColumnDefinitions>
+                    <zdfflatui:FlatButton Grid.Column="0" x:Name="btnDeviceUpgrade" Background="#2196F3" Foreground="White" 
+                                                          Content="升级设备固件"  Click="BtnDeviceUpgrade_Click" 
+                                                          Height="28" FontSize="14" Margin="10 0 10 0" />
+                    <zdfflatui:FlatButton Grid.Column="1" x:Name="btnDeviceRollback" Background="#FF4C4C" Foreground="White" 
+                                                          Content="回滚设备固件"  Click="BtnDeviceRollback_Click" 
                                                           Height="28" FontSize="14" Margin="10 0 10 0" />
                 </Grid>
-                <Grid Height="50" >
-                    <TextBlock Text="注意:要先读取到当前设备的版本信息后,再进行固件升级。" 
+                <Grid Height="60" >
+                    <TextBlock Text="注意:要先读取到当前设备的版本信息后,再进行固件升级或回滚操作。" 
                                TextWrapping="Wrap" Foreground="Red" Margin="5 0 5 0"
                                FontSize="14" VerticalAlignment="Center"/>
                 </Grid>

+ 112 - 5
MV485/uc/UCDeviceUpgrade.xaml.cs

@@ -482,11 +482,6 @@ namespace MV485.uc
                         {
                             //读取版本信息
                             bool blReadFireware = _rwRunConfig.ReadFireware(portName, baudrate, devId, out chDeviceSn, out chFireware);
-                            //if (blReadFireware)
-                            //{
-                            //return fireware.Equals(txtNewFireware.Text);
-                            //    return blReadFireware;
-                            //}
                             if (blReadFireware) return true;
                             Task.Delay(3000);
                             timeout -= 1000;
@@ -745,6 +740,21 @@ namespace MV485.uc
             catch { }
         }
 
+        private async Task UpdateUpgradeResultAndNewFireware(TUpgradeHis his)
+        {
+            if (his == null) return;
+            try
+            {
+                bool blUpdate = await Task.Run(() =>
+                {
+                    return DBUpgradeHis.UpdateUpgradeResultAndNewFrieware(his);
+                });
+                AppendLog("更新条目的升级结果");
+            }
+            catch { }
+        }
+
+
         private async void BtnClearDataA_Click(object sender, RoutedEventArgs e)
         {
             TUpgradeHis his = new TUpgradeHis()
@@ -911,6 +921,103 @@ namespace MV485.uc
                 //txtNewFireware.Tag = version;       //存储版本
             }
         }
+
+        private async void BtnDeviceRollback_Click(object sender, RoutedEventArgs e)
+        {
+            if (!GetSerialParameter(out string portName, out byte devId, out int baudrate))
+            {
+                return;
+            }
+
+            //判断是否读取了deviceSn,设备的当前版本
+            if (string.IsNullOrEmpty(txtDeviceSn.Text) || string.IsNullOrEmpty(txtOldFireware.Text))
+            {
+                MessageBox.Show(Application.Current.MainWindow,
+                    $"请先读取设备的当前版本", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                return;
+            }
+
+            //开始通过xModem协议读取照片
+            string titleInfo = "正在通知设备回滚固件版本";
+            WaitWindow waitWindow = new WaitWindow(titleInfo)
+            {
+                Owner = Application.Current.MainWindow,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner
+            };
+            waitWindow.Show();
+            Application.Current.MainWindow.IsEnabled = false;
+            try
+            {
+                bool blWrite = await Task.Run(() =>
+                {
+                    return _rwRunConfig.WriteDeviceRollback(portName, baudrate, devId);
+                });
+                string msgInfo = $"通知回滚固件版本{(blWrite ? "成功" : "失败")}";
+                AppendLog(msgInfo);
+                if (!blWrite)
+                {
+                    //msgInfo += "\n抄表器会重启,请等待1-2分钟,再尝试读取设备的版本信息。";
+                    MessageBox.Show(Application.Current.MainWindow, "通知回滚固件版本失败", "错误",
+                        MessageBoxButton.OK, MessageBoxImage.Error);
+                    return;
+                }
+
+                TUpgradeHis upgradeHis = new TUpgradeHis()
+                {
+                    HisId = Guid.NewGuid().ToString().Replace("-", ""),
+                    DeviceSn = txtDeviceSn.Text,
+                    PortName = portName,
+                    BaudRate = baudrate,
+                    Address = devId,
+                    //OldFireware = txtOldFireware.Text,
+                    UpgradeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
+                    NewFireware = txtNewFireware.Text,
+                    UpgradeResult = 0
+                };
+                await AddUpgradeItem(upgradeHis);
+
+                titleInfo = $"通知回滚固件版本成功,设备即将重启。\n正在等待验证重启后的版本(1~2分钟)。";
+                waitWindow.TitleInfo = titleInfo;
+
+
+                //进入等待线程
+                //开始及时
+                string chDeviceSn = "";
+                string chFireware = "";
+                bool blWait = await Task.Run(() =>
+                {
+                    int timeout = 2 * 60 * 1000;    //等待2分钟
+                    while (timeout > 0)
+                    {
+                        //读取版本信息
+                        bool blReadFireware = _rwRunConfig.ReadFireware(portName, baudrate, devId, out chDeviceSn, out chFireware);
+                        if (blReadFireware) return true;
+                        Task.Delay(3000);
+                        timeout -= 1000;
+                    }
+                    return false;
+                });
+
+                if (blWait)
+                {
+                    upgradeHis.UpgradeResult = 2;   //回滚成功标志
+                    upgradeHis.NewFireware = chFireware;
+                    await UpdateUpgradeResultAndNewFireware(upgradeHis);
+                    MessageBox.Show(Application.Current.MainWindow, $"回滚后的版本为{chFireware}", "提示",
+                        MessageBoxButton.OK, MessageBoxImage.Information);
+                }
+                else
+                {
+                    MessageBox.Show(Application.Current.MainWindow, "等待验证超时,请手动检查回滚后的版本是否正确。", "警告",
+                        MessageBoxButton.OK, MessageBoxImage.Warning);
+                }
+            }
+            catch { }
+            finally
+            {
+                waitWindow.Close();
+            }
+        }
         //---------------------------------------------------------
     }
 }

+ 12 - 7
MV485/uc/UCRunConfig.xaml

@@ -210,12 +210,15 @@
                                     <ColumnDefinition Width="80" />
                                 </Grid.ColumnDefinitions>
                                 <TextBlock Grid.Column="0" Text="水表口径" VerticalAlignment="Center" FontSize="14" />
-                                <ComboBox Grid.Column="1" x:Name="cmbFlowRate"  Height="26" FontSize="14px"
+                                <!--<ComboBox Grid.Column="1" x:Name="cmbFlowRate"  Height="26" FontSize="14px"
                                           IsEditable="True" PreviewTextInput="TextBox_PreviewTextInput"                              
                                           IsTextSearchCaseSensitive="False"
                                           Loaded="CmbFlowRate_Loaded"
                                           DropDownClosed="CmbFlowRate_DropDownClosed"
+                                          SelectedValuePath="Key" DisplayMemberPath="Value" />-->
+                                <ComboBox x:Name="cmbDnValue"  Height="26" FontSize="14px" Grid.Column="1"                                          
                                           SelectedValuePath="Key" DisplayMemberPath="Value" />
+
                                 <Button x:Name="btnFlowRate" Click="BtnFlowRate_Click" 
                                         Grid.Column="2" Content="说明示例" BorderThickness="0 0 0 1" BorderBrush="Blue"
                                             Height="20" Width="60" Background="Transparent" Foreground="Blue" FontSize="14" />
@@ -248,7 +251,7 @@
                                         Grid.Column="2" Content="说明示例" BorderThickness="0 0 0 1" BorderBrush="Blue"
                                             Height="20" Width="60" Background="Transparent" Foreground="Blue" FontSize="14" />
                             </Grid>
-                            <Grid Height="50" Margin="10 0 10 0">
+                            <Grid Height="50" Margin="10 0 10 0" Visibility="Collapsed">
                                 <Grid.ColumnDefinitions>
                                     <ColumnDefinition Width="90" />
                                     <ColumnDefinition Width="*" />
@@ -357,7 +360,7 @@
                                 <Grid.ColumnDefinitions>
                                     <ColumnDefinition Width="90" />
                                     <ColumnDefinition Width="*" />
-                                    <ColumnDefinition Width="40" />
+                                    <!--<ColumnDefinition Width="40" />-->
                                     <ColumnDefinition Width="80" />
                                 </Grid.ColumnDefinitions>
                                 <!--<TextBlock Grid.Column="0" Text="采样周期" VerticalAlignment="Center" FontSize="14" TextWrapping="Wrap" />-->
@@ -367,17 +370,19 @@
                                           IsEditable="True" PreviewTextInput="TextBox_PreviewTextInput"                              
                                           SelectionChanged="CmbFlowRate_SelectionChanged"
                                           SelectedValuePath="Key" DisplayMemberPath="Value" />-->
-                                <ComboBox Grid.Column="1" x:Name="cmbSampleInterval"  Height="26" FontSize="14px"
+                                <!--<ComboBox Grid.Column="1" x:Name="cmbSampleInterval"  Height="26" FontSize="14px"
                                            IsTextSearchEnabled="False"
                                           IsEditable="True" PreviewTextInput="TextBox_PreviewTextInput"    
                                           DropDownClosed="CmbSampleInterval_DropDownClosed"
                                           Loaded="CmbSampleInterval_Loaded"
+                                          SelectedValuePath="Key" DisplayMemberPath="Value" />-->
+                                <ComboBox x:Name="cmbSampleInterval"  Height="26" FontSize="14px" Grid.Column="1"                                          
                                           SelectedValuePath="Key" DisplayMemberPath="Value" />
 
-                                <TextBlock Grid.Column="2" Text="分钟" FontSize="14" Foreground="Black" Margin="5 0 0 0" HorizontalAlignment="Right"
-                                               VerticalAlignment="Center" PreviewTextInput="TextBox_PreviewTextInput" />
+                                <!--<TextBlock Grid.Column="2" Text="分钟" FontSize="14" Foreground="Black" Margin="5 0 0 0" HorizontalAlignment="Right"
+                                               VerticalAlignment="Center" PreviewTextInput="TextBox_PreviewTextInput" />-->
                                 <Button x:Name="btnSampleInterval" Click="BtnSampleInterval_Click"
-                                        Grid.Column="3" Content="说明示例" BorderThickness="0 0 0 1" BorderBrush="Blue"
+                                        Grid.Column="2" Content="说明示例" BorderThickness="0 0 0 1" BorderBrush="Blue"
                                             Height="20" Width="60" Background="Transparent" Foreground="Blue" FontSize="14" />
                             </Grid>
 

+ 140 - 29
MV485/uc/UCRunConfig.xaml.cs

@@ -138,17 +138,19 @@ namespace MV485.uc
         private void InitConfigView()
         {
             cmbMeterType.SelectedItem = null;
-            cmbFlowRate.Text = "";
+            //cmbFlowRate.Text = "";
+            cmbDnValue.SelectedItem = null;
             cmbNumCount.Text = "";
             cmbIndCount.Text = "";
-            cmbBrightVal.Text = "";
+            //cmbBrightVal.Text = "";
             cmbLastUnit.SelectedItem = null;
             txtLastValue.Text = "";
             dtpLastValueTime.Text = "";
             txtMeterRegion.Text = "";
             txtFeatureRegion.Text = "";
-            //txtSampleInterval.Text = "";
-            cmbSampleInterval.Text = "";
+
+            //cmbSampleInterval.Text = "";
+            cmbSampleInterval.SelectedItem = null;
             //txtFirstHour.Text = "";
             cmbFirstHour.SelectedItem = null;
         }
@@ -156,11 +158,13 @@ namespace MV485.uc
         private void LoadConfigItems()
         {
             cmbMeterType.ItemsSource = Tools.MeterTypeList;
-            cmbFlowRate.ItemsSource = Tools.FlowRateList;
-            cmbLastUnit.ItemsSource = Tools.LastUnitMap; //.UnitList;
+            //cmbFlowRate.ItemsSource = Tools.FlowRateList;
+            cmbDnValue.ItemsSource = Tools.DnValueList;
+
+            cmbLastUnit.ItemsSource = Tools.LastUnitMap;    //.UnitList;
             cmbNumCount.ItemsSource = Tools.NumList;
             cmbIndCount.ItemsSource = Tools.NumList;
-            cmbBrightVal.ItemsSource = Tools.BrightList;
+            //cmbBrightVal.ItemsSource = Tools.BrightList;
             cmbSampleInterval.ItemsSource = Tools.SampleIntervalList;
             cmbFirstHour.ItemsSource = Enumerable.Range(0, 24).ToList();
         }
@@ -181,6 +185,7 @@ namespace MV485.uc
             //初始化读数栏视图
             InitWMDataView();
             InitConfigView();
+            txtDeviceTime.Text = "";
         }
 
 
@@ -710,7 +715,8 @@ namespace MV485.uc
             }
             if (runConfig.DnValueWRFlag)
             {
-                cmbFlowRate.Text = runConfig.DnValue.ToString();
+                //cmbFlowRate.Text = runConfig.DnValue.ToString();
+                cmbDnValue.SelectedValue = runConfig.DnValue;
             }
 
             if (runConfig.DigitCountWRFlag && 
@@ -727,10 +733,12 @@ namespace MV485.uc
                 grdIndCount.Visibility = Visibility.Visible;
             }
 
-            if (runConfig.BrightValueWRFlag)
+            //此项已经不需要了
+            /*if (runConfig.BrightValueWRFlag)
             {
                 cmbBrightVal.Text = runConfig.BrightValue.ToString();
-            }
+            }*/
+
             if (runConfig.LastUnitLevelWRFlag)
             {
                 cmbLastUnit.SelectedValue = runConfig.LastUnitLevel;
@@ -745,8 +753,8 @@ namespace MV485.uc
 
             if (runConfig.SampleIntervalFlag)
             {
-                //txtSampleInterval.Text = runConfig.SampleInterval.ToString();
-                cmbSampleInterval.Text = runConfig.SampleInterval.ToString();
+                //cmbSampleInterval.Text = runConfig.SampleInterval.ToString();
+                cmbSampleInterval.SelectedValue = runConfig.SampleInterval;
             }
 
             if (runConfig.FirstHourFlag)
@@ -825,7 +833,8 @@ namespace MV485.uc
             runConfig.MeterTypeWRFlag = true;
             runConfig.MeterType = (byte)cmbMeterType.SelectedValue;
             
-            if(cmbFlowRate.SelectedItem != null)
+            //改变形式
+            /*if(cmbFlowRate.SelectedItem != null)
             {
                 runConfig.DnValueWRFlag = true;
                 runConfig.DnValue = (ushort)cmbFlowRate.SelectedValue;
@@ -840,7 +849,15 @@ namespace MV485.uc
                 MessageBox.Show(Application.Current.MainWindow, "请输入水表口径对应的每小时最大水流", "提示",
                     MessageBoxButton.OK, MessageBoxImage.Warning);
                 return;
+            }*/
+            if(cmbDnValue.SelectedItem == null)
+            {
+                MessageBox.Show(Application.Current.MainWindow, "请选择水表口径", "提示",
+                    MessageBoxButton.OK, MessageBoxImage.Warning);
+                return;
             }
+            runConfig.DnValueWRFlag = true;
+            runConfig.DnValue = (ushort)cmbDnValue.SelectedValue;
 
             if (runConfig.MeterType == Constant.METER_TYPE_NUM_IND || runConfig.MeterType == Constant.METER_TYPE_NUM)
             {
@@ -870,7 +887,8 @@ namespace MV485.uc
                 runConfig.IndCount = indCount;
             }
 
-            if (string.IsNullOrEmpty(cmbBrightVal.Text.Trim()) || 
+            //此项已经不需要了
+            /*if (string.IsNullOrEmpty(cmbBrightVal.Text.Trim()) || 
                 !float.TryParse(cmbBrightVal.Text.Trim(),out float brightValue) ||
                 !(brightValue >= 0.8 && brightValue <= 2.0))
             {
@@ -879,7 +897,8 @@ namespace MV485.uc
                 return;
             }
             runConfig.BrightValueWRFlag = true;
-            runConfig.BrightValue = brightValue;
+            runConfig.BrightValue = brightValue;*/
+
 
             if(cmbLastUnit.SelectedItem == null)
             {
@@ -899,16 +918,17 @@ namespace MV485.uc
             }
             runConfig.LatestValueWRFlag = true;
             runConfig.LatestValue = latestValue * Constant.CUBE_VALUE;
-            
-            if(string.IsNullOrWhiteSpace(dtpLastValueTime.Text) ||
-                !DateTime.TryParse(dtpLastValueTime.Text,out DateTime dtLatestTime))
+
+            //if(string.IsNullOrWhiteSpace(dtpLastValueTime.Text) ||
+            //    !DateTime.TryParse(dtpLastValueTime.Value,out DateTime dtLatestTime))
+            if (dtpLastValueTime.Value == null)
             {
                 MessageBox.Show(Application.Current.MainWindow, "请输入表底读数对应的时间", "提示",
                     MessageBoxButton.OK, MessageBoxImage.Warning);
                 return;
             }
             runConfig.LatestTimeWRFlag = true;
-            runConfig.LatestTime = dtLatestTime;
+            runConfig.LatestTime = dtpLastValueTime.Value.GetValueOrDefault();
 
             if (string.IsNullOrWhiteSpace(txtMeterRegion.Text))
             {
@@ -940,15 +960,21 @@ namespace MV485.uc
 
             if (chkSampleInterval.IsChecked == true)
             {
-                if(string.IsNullOrWhiteSpace(cmbSampleInterval.Text) ||
-                !ushort.TryParse(cmbSampleInterval.Text, out ushort sampleInterval))
+                //if(string.IsNullOrWhiteSpace(cmbSampleInterval.Text) ||
+                //!ushort.TryParse(cmbSampleInterval.Text, out ushort sampleInterval))
+                //{
+                //    MessageBox.Show(Application.Current.MainWindow, "请输入正确的采样时间间隔(1~9898)", "提示",
+                //        MessageBoxButton.OK, MessageBoxImage.Warning);
+                //    return;
+                //}
+                if(cmbSampleInterval.SelectedItem == null)
                 {
-                    MessageBox.Show(Application.Current.MainWindow, "请输入正确的采样时间间隔(1~9898)", "提示",
+                    MessageBox.Show(Application.Current.MainWindow, "请选择采样间隔", "提示",
                         MessageBoxButton.OK, MessageBoxImage.Warning);
                     return;
                 }
                 runConfig.SampleIntervalFlag = true;
-                runConfig.SampleInterval = sampleInterval;
+                runConfig.SampleInterval = (ushort)cmbSampleInterval.SelectedValue;    //sampleInterval;
             }
 
             if (chkFirstHour.IsChecked == true)
@@ -1152,16 +1178,18 @@ namespace MV485.uc
             }
         }
 
+        //暂时不需要这种形式
 
-        private void CmbSampleInterval_DropDownClosed(object sender, EventArgs e)
+       /* private void CmbSampleInterval_DropDownClosed(object sender, EventArgs e)
         {
             if (cmbSampleInterval.SelectedItem is KeyValuePair<ushort, string> kv)
             {
                 cmbSampleInterval.Text = kv.Key.ToString();
             }
-        }
+        }*/
 
-        private void CmbFlowRate_DropDownClosed(object sender, EventArgs e)
+        //暂时不需要这种形式
+        /*private void CmbFlowRate_DropDownClosed(object sender, EventArgs e)
         {
             if (cmbFlowRate.SelectedItem is KeyValuePair<ushort, string> kv)
             {
@@ -1175,15 +1203,16 @@ namespace MV485.uc
             {
                 textBox.MaxLength = 4; // 限制最多输入 4 个字符
             }
-        }
+        }*/
 
-        private void CmbSampleInterval_Loaded(object sender, RoutedEventArgs e)
+        //暂时不需要这种形式
+        /*private void CmbSampleInterval_Loaded(object sender, RoutedEventArgs e)
         {
             if (cmbSampleInterval.Template.FindName("PART_EditableTextBox", cmbSampleInterval) is TextBox textBox)
             {
                 textBox.MaxLength = 4; // 限制最多输入 4 个字符
             }
-        }
+        }*/
 
         private void ImgMeter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
@@ -1198,6 +1227,88 @@ namespace MV485.uc
             }
         }
 
+        private async void BtnReadDeviceTime_Click(object sender, RoutedEventArgs e)
+        {
+            if (!GetSerialParameter(out string portName, out byte devId, out int baudrate))
+            {
+                return;
+            }
+            txtDeviceTime.Text = "";
+
+            //开始通过xModem协议读取照片
+            string titleInfo = "正在读取抄表器的当前时钟";
+            WaitWindow waitWindow = new WaitWindow(titleInfo)
+            {
+                Owner = Application.Current.MainWindow,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner
+            };
+            waitWindow.Show();
+
+            try
+            {
+                string deviceTime = await Task.Run(() =>
+                {
+                    return _rwRunConfig.ReadDeviceTime(portName, baudrate, devId);
+                });
+                AppendLog($"读取抄表器当前时钟成功{(deviceTime != null ? "成功" : "失败")}");
+                if (deviceTime != null)
+                {
+                    //LoadWMDataView(data);
+                    txtDeviceTime.Text = deviceTime;
+                }
+                else
+                {
+                    MessageBox.Show(Application.Current.MainWindow, _rwRunConfig.GetLastError(), "错误",
+                        MessageBoxButton.OK, MessageBoxImage.Error);
+                }
+            }
+            catch { }
+            finally
+            {
+                waitWindow.Close();
+            }
+        }
+
+        private async void BtnWriteDeviceTime_Click(object sender, RoutedEventArgs e)
+        {
+            if (!GetSerialParameter(out string portName, out byte devId, out int baudrate))
+            {
+                return;
+            }
+            txtDeviceTime.Text = "";
+
+            //开始通过xModem协议读取照片
+            string titleInfo = "正在同步抄表器的时钟";
+            WaitWindow waitWindow = new WaitWindow(titleInfo)
+            {
+                Owner = Application.Current.MainWindow,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner
+            };
+            waitWindow.Show();
+
+            try
+            {
+                bool blWrite = await Task.Run(() =>
+                {
+                    return _rwRunConfig.WriteDeviceTime(portName, baudrate, devId,DateTime.Now);
+                });
+                string msgInfo = $"同步抄表器的时钟{(blWrite ? "成功" : "失败")}";
+                AppendLog(msgInfo);
+                if (blWrite)
+                {
+                    msgInfo += "\n抄表器会重启,请等待1-2分钟,再尝试读取参数进行核对。";
+                }
+
+                MessageBox.Show(Application.Current.MainWindow, msgInfo, "提示",
+                    MessageBoxButton.OK, blWrite ? MessageBoxImage.Information : MessageBoxImage.Warning);
+            }
+            catch { }
+            finally
+            {
+                waitWindow.Close();
+            }
+        }
+
         //------------------------------------------------------------------
     }
 }