|
|
@@ -51,6 +51,10 @@ namespace CanOpenSlaveTest
|
|
|
private Label lblPeriodTime;
|
|
|
private TextBox txtPeriodTime;
|
|
|
private Label lblPeriodUnit;
|
|
|
+ // 连续发送运行时长相关控件
|
|
|
+ private Label lblContinuousDuration;
|
|
|
+ private TextBox txtContinuousDuration;
|
|
|
+ private Label lblContinuousDurationUnit;
|
|
|
// 规则发送运行时长相关控件
|
|
|
private Label lblRuleDuration;
|
|
|
private TextBox txtRuleDuration;
|
|
|
@@ -64,6 +68,9 @@ namespace CanOpenSlaveTest
|
|
|
private bool m_isContinuousSending = false;
|
|
|
private CancellationTokenSource m_continuousSendCts;
|
|
|
private long m_continuousSendCounter = 0;
|
|
|
+ // 连续发送运行时长相关
|
|
|
+ private DateTime m_continuousSendStartTime;
|
|
|
+ private int m_continuousSendDurationMs = 0;
|
|
|
|
|
|
// 规则发送状态
|
|
|
private bool m_isRuleSending = false;
|
|
|
@@ -129,6 +136,10 @@ namespace CanOpenSlaveTest
|
|
|
this.lblPeriodTime = new System.Windows.Forms.Label();
|
|
|
this.txtPeriodTime = new System.Windows.Forms.TextBox();
|
|
|
this.lblPeriodUnit = new System.Windows.Forms.Label();
|
|
|
+ // 添加连续发送时长相关控件
|
|
|
+ this.lblContinuousDuration = new System.Windows.Forms.Label();
|
|
|
+ this.txtContinuousDuration = new System.Windows.Forms.TextBox();
|
|
|
+ this.lblContinuousDurationUnit = new System.Windows.Forms.Label();
|
|
|
// 添加规则发送时长相关控件
|
|
|
this.lblRuleDuration = new System.Windows.Forms.Label();
|
|
|
this.txtRuleDuration = new System.Windows.Forms.TextBox();
|
|
|
@@ -233,13 +244,17 @@ namespace CanOpenSlaveTest
|
|
|
this.grpPdo1Send.Controls.Add(this.lblPeriodTime);
|
|
|
this.grpPdo1Send.Controls.Add(this.txtPeriodTime);
|
|
|
this.grpPdo1Send.Controls.Add(this.lblPeriodUnit);
|
|
|
+ // 添加连续发送时长相关控件
|
|
|
+ this.grpPdo1Send.Controls.Add(this.lblContinuousDuration);
|
|
|
+ this.grpPdo1Send.Controls.Add(this.txtContinuousDuration);
|
|
|
+ this.grpPdo1Send.Controls.Add(this.lblContinuousDurationUnit);
|
|
|
// 添加规则发送时长相关控件到界面
|
|
|
this.grpPdo1Send.Controls.Add(this.lblRuleDuration);
|
|
|
this.grpPdo1Send.Controls.Add(this.txtRuleDuration);
|
|
|
this.grpPdo1Send.Controls.Add(this.lblRuleDurationUnit);
|
|
|
this.grpPdo1Send.Location = new System.Drawing.Point(10, 120);
|
|
|
this.grpPdo1Send.Name = "grpPdo1Send";
|
|
|
- this.grpPdo1Send.Size = new System.Drawing.Size(770, 160); // 增加高度以容纳新控件
|
|
|
+ this.grpPdo1Send.Size = new System.Drawing.Size(770, 190); // 增加高度以容纳新控件
|
|
|
this.grpPdo1Send.TabIndex = 2;
|
|
|
this.grpPdo1Send.TabStop = false;
|
|
|
this.grpPdo1Send.Text = "TPDO1发送配置";
|
|
|
@@ -453,22 +468,49 @@ namespace CanOpenSlaveTest
|
|
|
this.lblPeriodUnit.TabIndex = 21;
|
|
|
this.lblPeriodUnit.Text = "us";
|
|
|
//
|
|
|
+ // lblContinuousDuration
|
|
|
+ //
|
|
|
+ this.lblContinuousDuration.AutoSize = true;
|
|
|
+ this.lblContinuousDuration.Location = new System.Drawing.Point(10, 128);
|
|
|
+ this.lblContinuousDuration.Name = "lblContinuousDuration";
|
|
|
+ this.lblContinuousDuration.Size = new System.Drawing.Size(77, 15);
|
|
|
+ this.lblContinuousDuration.TabIndex = 22;
|
|
|
+ this.lblContinuousDuration.Text = "持续时间:";
|
|
|
+ //
|
|
|
+ // txtContinuousDuration
|
|
|
+ //
|
|
|
+ this.txtContinuousDuration.Location = new System.Drawing.Point(90, 125);
|
|
|
+ this.txtContinuousDuration.Name = "txtContinuousDuration";
|
|
|
+ this.txtContinuousDuration.Size = new System.Drawing.Size(80, 25);
|
|
|
+ this.txtContinuousDuration.TabIndex = 23;
|
|
|
+ this.txtContinuousDuration.Text = "0";
|
|
|
+ this.txtContinuousDuration.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
|
|
|
+ //
|
|
|
+ // lblContinuousDurationUnit
|
|
|
+ //
|
|
|
+ this.lblContinuousDurationUnit.AutoSize = true;
|
|
|
+ this.lblContinuousDurationUnit.Location = new System.Drawing.Point(180, 128);
|
|
|
+ this.lblContinuousDurationUnit.Name = "lblContinuousDurationUnit";
|
|
|
+ this.lblContinuousDurationUnit.Size = new System.Drawing.Size(62, 15);
|
|
|
+ this.lblContinuousDurationUnit.TabIndex = 24;
|
|
|
+ this.lblContinuousDurationUnit.Text = "秒(0=无限制)";
|
|
|
+ //
|
|
|
// lblRuleDuration
|
|
|
//
|
|
|
this.lblRuleDuration.AutoSize = true;
|
|
|
this.lblRuleDuration.Location = new System.Drawing.Point(350, 128);
|
|
|
this.lblRuleDuration.Name = "lblRuleDuration";
|
|
|
this.lblRuleDuration.Size = new System.Drawing.Size(77, 15);
|
|
|
- this.lblRuleDuration.TabIndex = 22;
|
|
|
- this.lblRuleDuration.Text = "持续时间:";
|
|
|
+ this.lblRuleDuration.TabIndex = 25;
|
|
|
+ this.lblRuleDuration.Text = "规则时长:";
|
|
|
//
|
|
|
// txtRuleDuration
|
|
|
//
|
|
|
this.txtRuleDuration.Location = new System.Drawing.Point(430, 125);
|
|
|
this.txtRuleDuration.Name = "txtRuleDuration";
|
|
|
this.txtRuleDuration.Size = new System.Drawing.Size(80, 25);
|
|
|
- this.txtRuleDuration.TabIndex = 23;
|
|
|
- this.txtRuleDuration.Text = "10";
|
|
|
+ this.txtRuleDuration.TabIndex = 26;
|
|
|
+ this.txtRuleDuration.Text = "0";
|
|
|
this.txtRuleDuration.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
|
|
|
//
|
|
|
// lblRuleDurationUnit
|
|
|
@@ -476,21 +518,21 @@ namespace CanOpenSlaveTest
|
|
|
this.lblRuleDurationUnit.AutoSize = true;
|
|
|
this.lblRuleDurationUnit.Location = new System.Drawing.Point(520, 128);
|
|
|
this.lblRuleDurationUnit.Name = "lblRuleDurationUnit";
|
|
|
- this.lblRuleDurationUnit.Size = new System.Drawing.Size(23, 15);
|
|
|
- this.lblRuleDurationUnit.TabIndex = 24;
|
|
|
- this.lblRuleDurationUnit.Text = "秒";
|
|
|
+ this.lblRuleDurationUnit.Size = new System.Drawing.Size(62, 15);
|
|
|
+ this.lblRuleDurationUnit.TabIndex = 27;
|
|
|
+ this.lblRuleDurationUnit.Text = "秒(0=无限制)";
|
|
|
//
|
|
|
// txtLog
|
|
|
//
|
|
|
this.txtLog.BackColor = System.Drawing.Color.White;
|
|
|
this.txtLog.Font = new System.Drawing.Font("微软雅黑", 9F);
|
|
|
this.txtLog.ForeColor = System.Drawing.Color.Black;
|
|
|
- this.txtLog.Location = new System.Drawing.Point(10, 260);
|
|
|
+ this.txtLog.Location = new System.Drawing.Point(10, 320); // 调整位置
|
|
|
this.txtLog.Multiline = true;
|
|
|
this.txtLog.Name = "txtLog";
|
|
|
this.txtLog.ReadOnly = true;
|
|
|
this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
|
|
- this.txtLog.Size = new System.Drawing.Size(770, 270);
|
|
|
+ this.txtLog.Size = new System.Drawing.Size(770, 210); // 调整尺寸
|
|
|
this.txtLog.TabIndex = 3;
|
|
|
//
|
|
|
// m_updateTimer
|
|
|
@@ -661,8 +703,18 @@ namespace CanOpenSlaveTest
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 解析运行时长(秒转毫秒),如果输入为空或无效,默认为0(无限制)
|
|
|
+ int durationSeconds = 0;
|
|
|
+ if (int.TryParse(txtContinuousDuration.Text.Trim(), out int parsedSeconds))
|
|
|
+ {
|
|
|
+ durationSeconds = Math.Max(0, parsedSeconds); // 确保非负数
|
|
|
+ }
|
|
|
+
|
|
|
+ m_continuousSendDurationMs = durationSeconds * 1000; // 转换为毫秒
|
|
|
+
|
|
|
// 重置计数器
|
|
|
m_continuousSendCounter = 0;
|
|
|
+ m_continuousSendStartTime = DateTime.Now;
|
|
|
|
|
|
// 创建取消令牌
|
|
|
m_continuousSendCts = new CancellationTokenSource();
|
|
|
@@ -671,10 +723,32 @@ namespace CanOpenSlaveTest
|
|
|
// 更新按钮文本
|
|
|
btnContinuousSend.Text = "停止发送";
|
|
|
|
|
|
- AppendLog("▶ 开始连续发送 - 周期: " + periodUs.ToString("F0") + "us");
|
|
|
+ string durationText = durationSeconds > 0 ? durationSeconds.ToString() + "秒" : "无限制";
|
|
|
+ AppendLog($"▶ 开始连续发送 - 周期: {periodUs:F0}us, 时长: {durationText}");
|
|
|
|
|
|
// 启动异步发送任务
|
|
|
Task.Run(() => ContinuousSendLoop(periodUs, m_continuousSendCts.Token));
|
|
|
+
|
|
|
+ // 如果设置了运行时长,则启动定时器来检查时间
|
|
|
+ if (m_continuousSendDurationMs > 0)
|
|
|
+ {
|
|
|
+ Task.Delay(m_continuousSendDurationMs, m_continuousSendCts.Token)
|
|
|
+ .ContinueWith(t =>
|
|
|
+ {
|
|
|
+ if (!t.IsCanceled)
|
|
|
+ {
|
|
|
+ // 时间到了,自动停止连续发送
|
|
|
+ if (this.InvokeRequired)
|
|
|
+ {
|
|
|
+ this.Invoke(new Action(StopContinuousSend));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ StopContinuousSend();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -721,6 +795,17 @@ namespace CanOpenSlaveTest
|
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
{
|
|
|
+ // 检查是否达到运行时长限制
|
|
|
+ if (m_continuousSendDurationMs > 0)
|
|
|
+ {
|
|
|
+ double elapsedMs = (DateTime.Now - m_continuousSendStartTime).TotalMilliseconds;
|
|
|
+ if (elapsedMs >= m_continuousSendDurationMs)
|
|
|
+ {
|
|
|
+ StopContinuousSend();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
// 记录开始时间
|