Prechádzať zdrojové kódy

增加与设备的时钟同步功能

djs 4 mesiacov pred
rodič
commit
5745ca71d8

+ 5 - 0
MV485/Dlg/waitUpgradeWindow.xaml.cs

@@ -43,6 +43,11 @@ namespace MV485.Dlg
             }
         }
 
+        public void WaitCheck()
+        {
+            this.btnStop.Visibility = Visibility.Collapsed;
+        }
+
         public WaitUpgradeWindow(string titleInfo)
         {
             InitializeComponent();

+ 3 - 3
MV485/MV485.csproj

@@ -139,8 +139,8 @@
     <Compile Include="Dlg\RegisterDlg.xaml.cs">
       <DependentUpon>RegisterDlg.xaml</DependentUpon>
     </Compile>
-    <Compile Include="Dlg\waitUpgradeWindow.xaml.cs">
-      <DependentUpon>waitUpgradeWindow.xaml</DependentUpon>
+    <Compile Include="Dlg\WaitUpgradeWindow.xaml.cs">
+      <DependentUpon>WaitUpgradeWindow.xaml</DependentUpon>
     </Compile>
     <Compile Include="Dlg\WaitWindow.xaml.cs">
       <DependentUpon>WaitWindow.xaml</DependentUpon>
@@ -257,7 +257,7 @@
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
-    <Page Include="Dlg\waitUpgradeWindow.xaml">
+    <Page Include="Dlg\WaitUpgradeWindow.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>

+ 11 - 5
MV485/helper/DeviceUpgrade.cs

@@ -26,7 +26,8 @@ namespace MV485.helper
         private readonly int _ackTimeout = 500; //2000;
 
         private IProgress<string> _progress;
-        private CancellationToken _cancellationToken;
+        //private CancellationToken _cancellationToken;
+        private CancellationTokenSource _cts;
 
         public bool IsOpen => _serialPort?.IsOpen ?? false;
 
@@ -88,9 +89,10 @@ namespace MV485.helper
             }
         }
 
-        public async Task StopSendFileAsync()
+        public void StopSendFile()
         {
-            _cancellationToken?.c
+            _cts?.Cancel();
+            _progress?.Report("用户取消了升级任务");
         }
 
         //开始异步发送升级包文件
@@ -102,12 +104,13 @@ namespace MV485.helper
             try
             {
                 _progress = progress;
-                _cancellationToken = new CancellationToken();
+                //_cancellationToken = new CancellationToken();
+                _cts = new CancellationTokenSource();
 
                 bool blSend = false;
                 if (OpenPort(portName, baudRate))
                 {
-                    blSend = await SendFileAsync(fileData, progress, _cancellationToken);                    
+                    blSend = await SendFileAsync(fileData, progress, _cts.Token);                    
                 }
                 return blSend;
             }
@@ -126,6 +129,7 @@ namespace MV485.helper
 
         private async Task<bool> SendFileAsync(byte[] fileData, IProgress<string> progress, CancellationToken cancellationToken)
         {
+            cancellationToken.ThrowIfCancellationRequested();
             //byte[] fileData = File.ReadAllBytes(filePath);
             //常用的 trick,用于模拟 向上取整除法
             int totalPackets = (fileData.Length + _packetSize - 1) / _packetSize;
@@ -144,6 +148,8 @@ namespace MV485.helper
 
             for (int packetNum = 1; packetNum <= totalPackets; packetNum++)
             {
+                cancellationToken.ThrowIfCancellationRequested();
+
                 int offset = (packetNum - 1) * _packetSize;
                 int length = Math.Min(_packetSize, fileData.Length - offset);
                 byte[] dataBlock = new byte[_packetSize];

+ 2 - 1
MV485/helper/RWRunConfig.cs

@@ -720,7 +720,8 @@ namespace MV485.helper
                 infos[2] = (ushort)(fileCrc32 >> 16);
                 infos[3] = (ushort)(fileCrc32 & 0xFFFF);
                 _modbusMaster.WriteMultipleRegisters(devId, Constant.MB_REGISTER_ADD_UPGRADE_DATA, infos);
-                GenerateValueLog(writeName, infos);
+                string upgradeInfo = $"文件长度: {fileSize},CRC32: {fileCrc32}";
+                GenerateValueLog(writeName, upgradeInfo);
                 GenerateSendAndRecvHexLog(false, writeName);
 
                 blWrite = true;

+ 19 - 0
MV485/model/TUpgradeHis.cs

@@ -64,6 +64,7 @@ namespace MV485.model
                     _upgradeResult = value;
                     OnPropertyChanged(nameof(UpgradeResult));
                     OnPropertyChanged(nameof(UpgradeResultName));
+                    OnPropertyChanged(nameof(UpgradeResultColor));
                 }
             }
         }
@@ -99,6 +100,24 @@ namespace MV485.model
             }            
         }
 
+        public string UpgradeResultColor
+        {
+            get
+            {
+                if(UpgradeResult > 0)
+                {
+                    return "#28a745";
+                }
+                else if(UpgradeResult < 0)
+                {
+                    return "#dc3545";
+                }
+                else{
+                    return "#000000";
+                }
+            }
+        }
+
         private int _index;
         public int Index
         {

+ 2 - 1
MV485/uc/UCDeviceUpgrade.xaml

@@ -274,7 +274,8 @@
                             <DataGridTemplateColumn Header="升级结果"  Width="80">
                                 <DataGridTemplateColumn.CellTemplate>
                                     <DataTemplate>
-                                        <TextBlock Text="{Binding UpgradeResultName}" FontSize="14px" Foreground="Black"  
+                                        <TextBlock Text="{Binding UpgradeResultName}" FontSize="14px" 
+                                                   Foreground="{Binding UpgradeResultColor}"  
                                                        HorizontalAlignment="Left" 
                                                        VerticalAlignment="Center"
                                                        Padding="5 0 0 0"/>

+ 12 - 10
MV485/uc/UCDeviceUpgrade.xaml.cs

@@ -390,7 +390,7 @@ namespace MV485.uc
             {
                 Owner = Application.Current.MainWindow,
                 WindowStartupLocation = WindowStartupLocation.CenterOwner
-            };            
+            };
             waitWindow.Show();
             Application.Current.MainWindow.IsEnabled = false;
             try
@@ -445,9 +445,12 @@ namespace MV485.uc
                     //});
                 };
 
-                waitWindow.StopUpgrade += () =>
+                waitWindow.StopUpgrade += async () =>
                 {
-
+                    deviceUpgrade.StopSendFile();
+                    upgradeHis.UpgradeResult = -1;  //升级失败
+                    await UpdateUpgradeResult(upgradeHis);
+                    return;
                 };
 
                 bool blSend = await deviceUpgrade.StartSendFileAsync(progress, portName, baudrate, fileData);
@@ -459,13 +462,13 @@ namespace MV485.uc
                     //提示继续等待设备重启,并验证升级是否成功
                     //MessageBox.Show(Application.Current.MainWindow,"向设备写入新固件成功,")
 
-                    MessageBoxResult result = MessageBox.Show("向设备写入新固件成功,设备即将重启。\n是否等待自动验证重启后的版本是否正确", 
-                        "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
+                    //MessageBoxResult result = MessageBox.Show("向设备写入新固件成功,设备即将重启。\n是否等待自动验证重启后的版本是否正确", 
+                    //    "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
 
-                    if (result == MessageBoxResult.No) return;
+                    //if (result == MessageBoxResult.No) return;
 
                     //继续等待失败是否成功
-                    titleInfo = $"正在等待验证重启后版本是否正确(1~2分钟)";
+                    titleInfo = $"向设备写入新固件成功,设备即将重启。\n正在等待验证重启后版本是否正确(1~2分钟)";
                     waitWindow.TitleInfo = titleInfo;
 
                     //进入等待线程
@@ -485,7 +488,7 @@ namespace MV485.uc
                             //    return blReadFireware;
                             //}
                             if (blReadFireware) return true;
-                            Task.Delay(1000);
+                            Task.Delay(3000);
                             timeout -= 1000;
                         }                        
                         return false;
@@ -499,7 +502,7 @@ namespace MV485.uc
                     }
                     else
                     {
-                        MessageBox.Show(Application.Current.MainWindow, "等待验证超时", "警告",
+                        MessageBox.Show(Application.Current.MainWindow, "等待验证超时,请手动检查升级后的版本是否正确。", "警告",
                             MessageBoxButton.OK, MessageBoxImage.Warning);
                     }
                 }
@@ -517,7 +520,6 @@ namespace MV485.uc
                 waitWindow.Close();
                 Application.Current.MainWindow.IsEnabled = true;
             }
-
         }
 
         private async void BtnClearUpgradeHis_Click(object sender, RoutedEventArgs e)

+ 26 - 2
MV485/uc/UCRunConfig.xaml

@@ -10,7 +10,7 @@
              Padding="5,2,5,5"
              Background="WhiteSmoke" 
              SizeChanged="UserControl_SizeChanged"
-             d:DesignHeight="800" d:DesignWidth="1366">
+             d:DesignHeight="900" d:DesignWidth="1366">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto" />
@@ -179,7 +179,7 @@
                         <StackPanel Orientation="Vertical" Margin="0 0 0 0">
                             <Grid Height="35" Margin="10 0 10 0">
                                 <StackPanel Orientation="Horizontal">
-                                    <TextBlock Text="抄表器配置项: " FontSize="14" FontWeight="Bold"
+                                    <TextBlock Text="抄表器运行参数: " FontSize="14" FontWeight="Bold"
                                                VerticalAlignment="Center" Foreground="DarkBlue" />
                                     <!--<TextBlock Text="设置参数后,抄表器需要重启,请等待1-2分钟" VerticalAlignment="Center" Foreground="Red" />-->
                                 </StackPanel>
@@ -417,6 +417,30 @@
                                                           Height="28" FontSize="14" Margin="10 0 10 0" />
                             </Grid>
 
+                            <Grid Height="50" Margin="10 0 10 0">
+                                <Grid.ColumnDefinitions>
+                                    <ColumnDefinition Width="90" />
+                                    <ColumnDefinition Width="*" />
+                                    <!--<ColumnDefinition Width="80" />-->
+                                </Grid.ColumnDefinitions>
+                                <TextBlock x:Name="txtFeadtureRegionTitle" Grid.Column="0" Text="抄表器时钟" VerticalAlignment="Center" FontSize="14" TextWrapping="Wrap" />
+                                <TextBox Grid.Column="1" x:Name="txtDeviceTime" Height="26" Padding="2" FontSize="14px" IsReadOnly="True" />
+                            </Grid>
+
+                            <Grid Height="40">
+                                <Grid.ColumnDefinitions>
+                                    <ColumnDefinition Width="*" />
+                                    <ColumnDefinition Width="*" />
+                                </Grid.ColumnDefinitions>
+                                <zdfflatui:FlatButton Grid.Column="0" x:Name="btnReadDeviceTime" Background="#4CAF50" Foreground="White" 
+                                                          Content="读取抄表器当前时间"  Click="BtnReadDeviceTime_Click" 
+                                                          Height="28" FontSize="14" Margin="10 0 10 0" />
+
+                                <zdfflatui:FlatButton Grid.Column="1" x:Name="btnWriteDeviceTime" Background="#2196F3" Foreground="White" 
+                                                          Content="用电脑时钟校准设备"  Click="BtnWriteDeviceTime_Click" 
+                                                          Height="28" FontSize="14" Margin="10 0 10 0" />
+                            </Grid>
+
                         </StackPanel>
                     </Border>
                 </ScrollViewer>