Skip to content

Commit

Permalink
修复延时误差问题,新增误差补偿选项
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperStudio committed Sep 24, 2023
1 parent 5407c6f commit 7f47785
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 20 deletions.
32 changes: 32 additions & 0 deletions SuperCom/Core/Config/AdvancedSendSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ public class AdvancedSendSettings : AbstractConfig
{
public const double DEFAULT_WINDOW_OPACITY = 0.5;
public const double DEFAULT_LOG_OPACITY = 0.8;

/// <summary>
/// 发送延时的误差补偿
/// </summary>
public const long DEFAULT_SEND_COMPENSATION = 30;
private AdvancedSendSettings() : base(ConfigManager.SQLITE_DATA_PATH, $"WindowConfig.AdvancedSendSettings")
{
Width = SystemParameters.WorkArea.Width * 0.7;
Height = SystemParameters.WorkArea.Height * 0.7;
FirstRun = true;
LogOpacity = DEFAULT_LOG_OPACITY;
WindowOpacity = DEFAULT_WINDOW_OPACITY;

EnableSendCompensation = false;
SendCompensation = DEFAULT_SEND_COMPENSATION;
}

private static AdvancedSendSettings _instance = null;
Expand All @@ -40,6 +48,30 @@ public static AdvancedSendSettings CreateInstance()
public bool LogAutoWrap { get; set; }
public double LogOpacity { get; set; }

private bool _EnableSendCompensation;

/// <summary>
/// 是否开启误差补偿
/// </summary>
public bool EnableSendCompensation {
get { return _EnableSendCompensation; }
set {
_EnableSendCompensation = value;
RaisePropertyChanged();
}
}

private long _SendCompensation;

/// <summary>
/// 误差补偿大小
/// </summary>
public long SendCompensation {
get { return _SendCompensation; }
set {
_SendCompensation = value;
RaisePropertyChanged();
}
}
}
}
47 changes: 32 additions & 15 deletions SuperCom/Core/Entity/AdvancedSend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static void InitSqlite()
private async Task<bool> AsyncSendCommand(int idx, PortTabItem portTabItem, SendCommand command, AdvancedSend advancedSend)
{
bool success = false;
await App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate {
await App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate {
string value = command.Command;
success = portTabItem.SendCommand(value, true);
if (!success) {
Expand All @@ -184,12 +184,39 @@ await App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)
return success;
}

private async Task<bool> CmdDelay(int delay)
{
//if (command.Delay > 0) {
// int delay = 10;
// for (int i = 1; i <= command.Delay; i += delay) {
// if (!portTabItem.RunningCommands)
// break;
// await Task.Delay(delay);
// advancedSend.CommandList[idx].StatusText = $"{command.Delay - i} ms";
// }
// advancedSend.CommandList[idx].StatusText = "0 ms";
//}

int time = delay;
if (ConfigManager.AdvancedSendSettings.EnableSendCompensation) {
time -= (int)ConfigManager.AdvancedSendSettings.SendCompensation;
}

if (time < 0)
return true;

// Task.Delay 在 window 系统上会有 15 ms 的误差
// 参考:https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.delay?view=netframework-4.7.2&redirectedfrom=MSDN#System_Threading_Tasks_Task_Delay_System_Int32
await Task.Delay(time);
return true;
}

/// <summary>
/// 发送命令的任务
/// </summary>
/// <param name="advancedSend"></param>
/// <param name="portName"></param>
/// <param name="button"></param>
/// <param name="advancedSend">需要发送的命令集合</param>
/// <param name="portTabItem">指定发送串口</param>
/// <param name="onSetRunningStatus">发送状态回调函数</param>
public void BeginSendCommands(AdvancedSend advancedSend, PortTabItem portTabItem, Action<bool> onSetRunningStatus)
{
if (advancedSend == null || string.IsNullOrEmpty(advancedSend.Commands) || portTabItem == null)
Expand All @@ -205,7 +232,6 @@ public void BeginSendCommands(AdvancedSend advancedSend, PortTabItem portTabItem
return;
}
portTabItem.RunningCommands = true;

onSetRunningStatus?.Invoke(true);
Task.Run(async () => {
int idx = 0;
Expand All @@ -218,16 +244,7 @@ public void BeginSendCommands(AdvancedSend advancedSend, PortTabItem portTabItem
if (!success)
break;
advancedSend.CommandList[idx].Status = RunningStatus.WaitingDelay;
if (command.Delay > 0) {
int delay = 10;
for (int i = 1; i <= command.Delay; i += delay) {
if (!portTabItem.RunningCommands)
break;
await Task.Delay(delay);
advancedSend.CommandList[idx].StatusText = $"{command.Delay - i} ms";
}
advancedSend.CommandList[idx].StatusText = "0 ms";
}
await CmdDelay(command.Delay);
advancedSend.CommandList[idx].Status = RunningStatus.WaitingToRun;
idx++;
if (idx >= advancedSend.CommandList.Count) {
Expand Down
13 changes: 9 additions & 4 deletions SuperCom/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,9 +1846,14 @@ private void StartSendCommands(object sender, RoutedEventArgs e)
PortTabItem portTabItem = vieModel.PortTabItems.FirstOrDefault(arg => arg.Name.Equals(portName));
if (advancedSend != null) {
Logger.Info($"start run command: {advancedSend.ProjectName}");
advancedSend.BeginSendCommands(advancedSend, portTabItem, (status) => {
SetRunningStatus(button, status);
});
try {
advancedSend.BeginSendCommands(advancedSend, portTabItem, (status) => {
SetRunningStatus(button, status);
});
} catch (Exception ex) {
Logger.Error(ex);
MessageCard.Error(ex.Message);
}
}
}
}
Expand Down Expand Up @@ -2223,7 +2228,7 @@ private void EditCommandCancel(object sender, RoutedEventArgs e)




private void OpenShortCut(object sender, RoutedEventArgs e)
{
if (window_ShortCut != null)
Expand Down
34 changes: 33 additions & 1 deletion SuperCom/Windows/Window_AdvancedSend.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
xmlns:config="clr-namespace:SuperCom.Config"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:super="https://github.com/SuperStudio/SuperControls"
Expand Down Expand Up @@ -394,6 +395,28 @@
Value="{Binding WindowOpacity, Mode=OneWay}" />

</StackPanel>

<TextBlock Style="{StaticResource BaseTextBlock}" Text="补偿延时误差" />
<super:HoverPath
Width="23"
Height="23"
Style="{StaticResource HelpHoverPath}"
ToolTip="系统定时器误差在 15-50ms" />
<super:Switch
Height="25"
Background="{DynamicResource Window.Title.Background}"
IsChecked="{Binding EnableSendCompensation, Source={x:Static config:ConfigManager.AdvancedSendSettings}}" />
<super:SearchBox
Width="80"
Height="25"
ShowClearButton="False"
Text="{Binding SendCompensation, Source={x:Static config:ConfigManager.AdvancedSendSettings}, Mode=TwoWay}"
TextAlignment="Center"
Visibility="{Binding EnableSendCompensation, Source={x:Static config:ConfigManager.AdvancedSendSettings}, Converter={StaticResource BoolToVisibilityConverter}}" />
<TextBlock
Style="{StaticResource BaseTextBlock}"
Text="ms"
Visibility="{Binding EnableSendCompensation, Source={x:Static config:ConfigManager.AdvancedSendSettings}, Converter={StaticResource BoolToVisibilityConverter}}" />
</StackPanel>


Expand Down Expand Up @@ -534,8 +557,17 @@
<DataGridTemplateColumn
Width="auto"
MinWidth="60"
Header="延时(ms)"
SortMemberPath="Delay">
<DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="延时(ms)" />
<super:HoverPath
Width="23"
Height="23"
Style="{StaticResource HelpHoverPath}"
ToolTip="系统定时器误差在 15-50ms" />
</StackPanel>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<super:SearchBox
Expand Down

0 comments on commit 7f47785

Please sign in to comment.