脚本执行异常处理

This commit is contained in:
tfarcraw 2026-07-26 17:20:04 +08:00
parent 7f8cc4ecd2
commit 1d2df8e3cf
9 changed files with 171 additions and 142 deletions

View File

@ -72,8 +72,8 @@ VVMs/ # 业务模块(View-ViewModel 对,按功能分包
SDK/ # 第三方 SDK 包装
Camera/MvCameraControl.Net # 海康相机 SDK
MotionControl/EMC # 固高科技 EMC 运动控制板卡
MotionControl/SMC # 固高科技 SMC 运动控制板卡
MotionControl/EMC # 雷赛 EMC 运动控制板卡
MotionControl/SMC # 雷赛 SMC 运动控制板卡
```
### 关键设计模式

View File

@ -1,4 +1,4 @@
using PLCSharp.VVMs.ModeState;
using PLCSharp.VVMs.ModeState;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
@ -34,7 +34,12 @@ namespace PLCSharp.Models
if (ModeState.Sc.Reset)
{
TitleState.Background = Brushes.Gray;
foreach (var item in WorkflowsModel.Workflows)
{
item.Exception = false;
}
}
ModeState.Run();

View File

@ -64,7 +64,7 @@
<DataGrid.Columns>
<DataGridTextColumn MinWidth="60" Binding="{Binding Name, UpdateSourceTrigger=LostFocus}" Header="连接名" />
<DataGridTextColumn MinWidth="100" Binding="{Binding Type, UpdateSourceTrigger=LostFocus}" Header="协议类型" IsReadOnly="True" />
<DataGridTextColumn MinWidth="100" Binding="{Binding IP_SerialPort, UpdateSourceTrigger=LostFocus}" Header=" IP &#10;SerialPort" />
<DataGridTextColumn MinWidth="100" Binding="{Binding IP_SerialPort, UpdateSourceTrigger=LostFocus}" Header="IP&amp;SerialPort" />
<DataGridTextColumn MinWidth="40" Binding="{Binding Port, UpdateSourceTrigger=LostFocus}" Header="端口" />
<DataGridTextColumn MinWidth="100" Binding="{Binding Comment, UpdateSourceTrigger=LostFocus}" Header="备注" />
<DataGridTemplateColumn Header="在线">

View File

@ -1,4 +1,4 @@
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm.GalleryClassifier"
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm.GalleryClassifier"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@ -9,7 +9,7 @@
d:DesignHeight="500" d:DesignWidth="300">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<!-- ========== ROI 区域(选区用于分类,不影响原图) ========== -->
<!-- ========== ROI 区域(选区用于分类,不影响原图) ========== -->
<GroupBox Header="ROI 区域(选区用于分类,不影响原图)">
<StackPanel>
<Button Height="25" Margin="5" Command="{Binding DrawGalleryRoiCommand}" Content="画选区" />
@ -30,13 +30,16 @@
<TextBox Grid.Column="3" Text="{Binding SelectVisionFlow.DoubleParams[GalleryRoiTop], Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
<TextBlock Grid.Row="1" Margin="5,0" Text="宽度" VerticalAlignment="Center" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectVisionFlow.DoubleParams[GalleryRoiWidth], Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
<TextBlock Grid.Row="1" Grid.Column="2" Margin="5,0" Text="高度" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Grid.Column="2"
Margin="5,0"
Text="高度"
VerticalAlignment="Center" />
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding SelectVisionFlow.DoubleParams[GalleryRoiHeight], Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />
</Grid>
</StackPanel>
</GroupBox>
<!-- ========== 训练操作 ========== -->
<!-- ========== 训练操作 ========== -->
<GroupBox Header="训练">
<StackPanel>
<Grid Margin="5">
@ -48,23 +51,31 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="训练为 OK"
<Button Grid.Row="0" Grid.Column="0"
Height="32" Margin="0,0,3,3"
Content="训练为 OK"
Command="{Binding TrainGalleryOkCommand}"
Background="LimeGreen" Foreground="White" Height="32" Margin="0,0,3,3"/>
<Button Grid.Row="0" Grid.Column="1" Content="训练为 NG"
Background="LimeGreen" Foreground="White" />
<Button Grid.Row="0" Grid.Column="1"
Height="32" Margin="3,0,0,3"
Content="训练为 NG"
Command="{Binding TrainGalleryNgCommand}"
Background="Red" Foreground="White" Height="32" Margin="3,0,0,3"/>
<Button Grid.Row="1" Grid.Column="0" Content="纠正为 OK"
Command="{Binding CorrectGalleryToOkCommand}"
Height="28" Margin="0,0,3,3" FontSize="11"/>
<Button Grid.Row="1" Grid.Column="1" Content="纠正为 NG"
Command="{Binding CorrectGalleryToNgCommand}"
Height="28" Margin="3,0,0,3" FontSize="11"/>
Background="Red" Foreground="White" />
<Button Grid.Row="1" Grid.Column="0"
Height="28" Margin="0,0,3,3"
FontSize="11"
Content="纠正为 OK"
Command="{Binding CorrectGalleryToOkCommand}" />
<Button Grid.Row="1" Grid.Column="1"
Height="28" Margin="3,0,0,3"
FontSize="11"
Content="纠正为 NG"
Command="{Binding CorrectGalleryToNgCommand}" />
</Grid>
</StackPanel>
</GroupBox>
<!-- ========== 画廊状态 ========== -->
<!-- ========== 画廊状态 ========== -->
<GroupBox Header="画廊状态">
<StackPanel>
<Grid Margin="5">
@ -78,19 +89,20 @@
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="OK 库" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Text="{Binding GalleryOkCount, Mode=OneWay}"
IsReadOnly="True" Background="#FFF0F0F0" />
<TextBox Grid.Column="1" Text="{Binding GalleryOkCount, Mode=OneWay}" IsReadOnly="True" Background="#FFF0F0F0" />
<TextBlock Grid.Row="1" Text="NG 库" VerticalAlignment="Center" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding GalleryNgCount, Mode=OneWay}"
<TextBox Grid.Row="1" Grid.Column="1"
Text="{Binding GalleryNgCount, Mode=OneWay}"
IsReadOnly="True" Background="#FFF0F0F0" />
<TextBlock Grid.Row="2" Text="状态" VerticalAlignment="Center" />
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding GalleryStatus, Mode=OneWay}"
<TextBox Grid.Row="2" Grid.Column="1"
Text="{Binding GalleryStatus, Mode=OneWay}"
IsReadOnly="True" Background="#FFF0F0F0" />
</Grid>
</StackPanel>
</GroupBox>
<!-- ========== 结果 ========== -->
<!-- ========== 结果 ========== -->
<GroupBox Header="分类结果">
<Grid Margin="5">
<Grid.RowDefinitions>
@ -124,7 +136,7 @@
</Grid>
</GroupBox>
<!-- ========== 文件 ========== -->
<!-- ========== 文件 ========== -->
<GroupBox Header="文件">
<Grid Margin="5">
<Grid.RowDefinitions>
@ -135,10 +147,16 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="导出" Command="{Binding ExportGalleryCommand}" Height="28" Margin="0,0,3,3"/>
<Button Grid.Column="1" Content="导入" Command="{Binding ImportGalleryCommand}" Height="28" Margin="3,0,0,3"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding GalleryFileStatus, Mode=OneWay}"
Margin="0,3,0,0" FontSize="12" />
<Button Height="28" Margin="0,0,3,3" Content="导出" Command="{Binding ExportGalleryCommand}" />
<Button Grid.Column="1"
Height="28" Margin="3,0,0,3"
Content="导入"
Command="{Binding ImportGalleryCommand}" />
<TextBlock Grid.Row="1"
Grid.ColumnSpan="2"
Margin="0,3,0,0"
Text="{Binding GalleryFileStatus, Mode=OneWay}"
FontSize="12" />
</Grid>
</GroupBox>
</StackPanel>

View File

@ -45,7 +45,7 @@ namespace PLCSharp.VVMs.Vision
else
{
Cameras.Add(item);
System.Windows.MessageBox.Show($"未发现相机{item.Name},请检查连接");
SendErrDialog($"未发现相机{item.Name},请检查连接");
}
break;
@ -94,8 +94,7 @@ namespace PLCSharp.VVMs.Vision
itemcopy.ImageDatas = ImageDatas;
itemcopy.GlobalModel = GlobalModel;
var localImageDatas = _DatasContext.LocalImageDatas.Where(c => c.FuncID == item.ID);
foreach (var l in localImageDatas)
{
itemcopy.LocalImageDatas.Add(l);
@ -104,8 +103,7 @@ namespace PLCSharp.VVMs.Vision
}
}
/// <summary>
/// SearchCameras
/// </summary>
@ -119,7 +117,6 @@ namespace PLCSharp.VVMs.Vision
}
}
private ObservableCollection<VisionFunction> _VisionFunctions = [];
/// <summary>
/// VisionFunctions

View File

@ -120,6 +120,12 @@ namespace PLCSharp.VVMs.Workflows
get { return _AutomaticExecution; }
set { SetProperty(ref _AutomaticExecution, value); }
}
/// <summary>
/// 编译时间
/// </summary>
[NotMapped]
public DateTime CompiledTime { get; set; } = DateTime.Now;
private bool _IsCompiled;
/// <summary>
@ -236,7 +242,8 @@ namespace PLCSharp.VVMs.Workflows
IsRuning = false;
return;
}
//刚编译完成的脚本,延迟3秒运行,避免编译后立即运行导致的异常
if (CompiledTime.AddSeconds(3) > DateTime.Now) return;
try
{
IsRuning = true;
@ -244,10 +251,11 @@ namespace PLCSharp.VVMs.Workflows
}
catch (Exception ex)
{
Exception = true;
IsRuning = false;
DebugLog += $"运行时异常: {ex}\n";
globalModel.SendErr($"运行时异常: {ex}\n");
Exception = true;
}
}
@ -293,7 +301,7 @@ namespace PLCSharp.VVMs.Workflows
Thread.Sleep((int)(CycleDelayTime * 1000));
if (AutomaticExecution)
{
Run(GlobalModel);
}
else
@ -305,11 +313,6 @@ namespace PLCSharp.VVMs.Workflows
if (Exception)
{
GlobalModel.ModeState.Sc.Stop = true;
if (GlobalModel.ModeState.State.Stopped)
{
Exception = false;
}
}
}

View File

@ -124,6 +124,11 @@
Background="{Binding SelectedFlowTask.Exception, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Red, FallbackValue=Gray}" />
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" Content="程序自动运行" IsChecked="{Binding SelectedFlowTask.AutomaticExecution}" />
<Button Width="50" Margin="10,5" Command="{Binding Save}" Content="保存" />
<TextBlock Margin="5" Padding="10"
Text="{Binding SelectedFlowTask.Flow.Step,StringFormat=当前步: {0}}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</UserControl>

View File

@ -50,7 +50,8 @@ namespace PLCSharp.VVMs.Workflows
private async Task ExecuteCompileAsync()
{
SelectedFlowTask. CompiledTime = DateTime.Now;
SelectedFlowTask.Flow.Step = 0;
await CustomTextEditor.FormatCodeAsync();
GlobalModel.WorkflowsModel.Manage.Execute("Save");
SelectedFlowTask.Compile.Execute();
@ -124,7 +125,7 @@ namespace PLCSharp.VVMs.Workflows
}
else
{
System.Windows.MessageBox.Show("自动运行中,不能手动运行!"); ;
GlobalModel.SendErrDialog("自动运行中,不能手动运行!"); ;
}
}
readonly GlobalModel GlobalModel;

View File

@ -1,97 +1,97 @@
<UserControl x:Class="PLCSharp.VVMs.Workflows.Workflows"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PLCSharp.VVMs.Workflows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:WorkflowsViewModel}"
d:Height="550" d:Width="1000">
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="SizeToContent" Value="Manual" />
</Style>
</prism:Dialog.WindowStyle>
<Grid>
<DataGrid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}"
HorizontalAlignment="Center" VerticalAlignment="Center"
AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" CanUserSortColumns="False"
ItemsSource="{Binding WorkflowsModel.Workflows}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectedItem="{Binding WorkflowsModel.SelectedTask}"
Unloaded="DataGrid_Unloaded">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Prompt}" Value="已修改,请保存">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Prompt}" Value="">
<Setter Property="Foreground" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.ContextMenu>
<ContextMenu IsEnabled="True" StaysOpen="False">
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="New" Header="新增" />
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="Remove" Header="删除" />
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="Save" Header="保存" />
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="Config" Header="配置" />
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn MinWidth="60" Binding="{Binding Name, UpdateSourceTrigger=LostFocus}" Header="名称" />
<DataGridTextColumn MinWidth="200" Binding="{Binding Comment, UpdateSourceTrigger=LostFocus}" Header="备注" />
<DataGridTextColumn MinWidth="60" Binding="{Binding CycleDelayTime, UpdateSourceTrigger=LostFocus}" Header="循环结束延迟" />
<DataGridTextColumn MinWidth="60" Binding="{Binding CycleDelayTime, UpdateSourceTrigger=LostFocus}" Header="实际周期" IsReadOnly="True" />
<DataGridTextColumn MinWidth="60" Binding="{Binding Flow.Step, UpdateSourceTrigger=LostFocus}" Header="当前步" />
<DataGridTemplateColumn Header="已编译">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding IsCompiled, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Lime, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="执行异常">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding Exception, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Red, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="程序自动运行">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding AutomaticExecution, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Lime, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="运行中">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding IsRuning, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Lime, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn MinWidth="100" Binding="{Binding Prompt}" Header="提示" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
<UserControl x:Class="PLCSharp.VVMs.Workflows.Workflows"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PLCSharp.VVMs.Workflows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:WorkflowsViewModel}"
d:Height="550" d:Width="1000">
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="SizeToContent" Value="Manual" />
</Style>
</prism:Dialog.WindowStyle>
<Grid>
<DataGrid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}"
HorizontalAlignment="Center" VerticalAlignment="Center"
AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" CanUserSortColumns="False"
ItemsSource="{Binding WorkflowsModel.Workflows}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectedItem="{Binding WorkflowsModel.SelectedTask}"
Unloaded="DataGrid_Unloaded">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Prompt}" Value="已修改,请保存">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Prompt}" Value="">
<Setter Property="Foreground" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.ContextMenu>
<ContextMenu IsEnabled="True" StaysOpen="False">
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="New" Header="新增" />
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="Remove" Header="删除" />
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="Save" Header="保存" />
<MenuItem Command="{Binding WorkflowsModel.Manage}" CommandParameter="Config" Header="配置" />
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn MinWidth="60" Binding="{Binding Name, UpdateSourceTrigger=LostFocus}" Header="名称" />
<DataGridTextColumn MinWidth="60" Binding="{Binding CycleDelayTime, UpdateSourceTrigger=LostFocus}" Header="循环结束延迟" />
<DataGridTextColumn MinWidth="60" Binding="{Binding CycleDelayTime, UpdateSourceTrigger=LostFocus}" Header="实际周期" IsReadOnly="True" />
<DataGridTextColumn MinWidth="60" Binding="{Binding Flow.Step, UpdateSourceTrigger=LostFocus}" Header="当前步" />
<DataGridTemplateColumn Header="已编译">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding IsCompiled, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Lime, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="执行异常">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding Exception, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Red, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="程序自动运行">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding AutomaticExecution, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Lime, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="运行中">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox>
<Ellipse Width="10" Height="10" Fill="{Binding IsRuning, Converter={StaticResource BoolToValueConverter}, Mode=TwoWay, ConverterParameter=Lime, FallbackValue=Gray}" />
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn MinWidth="200" Binding="{Binding Comment, UpdateSourceTrigger=LostFocus}" Header="备注" />
<DataGridTextColumn MinWidth="100" Binding="{Binding Prompt}" Header="提示" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>