|
@@ -51,6 +51,10 @@ namespace CanOpenSlaveTest
|
|
|
private Label lblPeriodTime;
|
|
private Label lblPeriodTime;
|
|
|
private TextBox txtPeriodTime;
|
|
private TextBox txtPeriodTime;
|
|
|
private Label lblPeriodUnit;
|
|
private Label lblPeriodUnit;
|
|
|
|
|
+ // 规则发送运行时长相关控件
|
|
|
|
|
+ private Label lblRuleDuration;
|
|
|
|
|
+ private TextBox txtRuleDuration;
|
|
|
|
|
+ private Label lblRuleDurationUnit;
|
|
|
|
|
|
|
|
private TextBox txtLog;
|
|
private TextBox txtLog;
|
|
|
private System.ComponentModel.IContainer components;
|
|
private System.ComponentModel.IContainer components;
|
|
@@ -64,6 +68,10 @@ namespace CanOpenSlaveTest
|
|
|
// 规则发送状态
|
|
// 规则发送状态
|
|
|
private bool m_isRuleSending = false;
|
|
private bool m_isRuleSending = false;
|
|
|
private long m_ruleSendCounter = 0;
|
|
private long m_ruleSendCounter = 0;
|
|
|
|
|
+ // 规则发送运行时长相关
|
|
|
|
|
+ private DateTime m_ruleSendStartTime;
|
|
|
|
|
+ private int m_ruleSendDurationMs = 0;
|
|
|
|
|
+ private CancellationTokenSource m_ruleSendCts;
|
|
|
|
|
|
|
|
public SlaveTestForm()
|
|
public SlaveTestForm()
|
|
|
{
|
|
{
|
|
@@ -121,6 +129,10 @@ namespace CanOpenSlaveTest
|
|
|
this.lblPeriodTime = new System.Windows.Forms.Label();
|
|
this.lblPeriodTime = new System.Windows.Forms.Label();
|
|
|
this.txtPeriodTime = new System.Windows.Forms.TextBox();
|
|
this.txtPeriodTime = new System.Windows.Forms.TextBox();
|
|
|
this.lblPeriodUnit = new System.Windows.Forms.Label();
|
|
this.lblPeriodUnit = new System.Windows.Forms.Label();
|
|
|
|
|
+ // 添加规则发送时长相关控件
|
|
|
|
|
+ this.lblRuleDuration = new System.Windows.Forms.Label();
|
|
|
|
|
+ this.txtRuleDuration = new System.Windows.Forms.TextBox();
|
|
|
|
|
+ this.lblRuleDurationUnit = new System.Windows.Forms.Label();
|
|
|
this.txtLog = new System.Windows.Forms.TextBox();
|
|
this.txtLog = new System.Windows.Forms.TextBox();
|
|
|
this.m_updateTimer = new System.Windows.Forms.Timer(this.components);
|
|
this.m_updateTimer = new System.Windows.Forms.Timer(this.components);
|
|
|
this.grpDeviceInfo.SuspendLayout();
|
|
this.grpDeviceInfo.SuspendLayout();
|
|
@@ -221,9 +233,13 @@ namespace CanOpenSlaveTest
|
|
|
this.grpPdo1Send.Controls.Add(this.lblPeriodTime);
|
|
this.grpPdo1Send.Controls.Add(this.lblPeriodTime);
|
|
|
this.grpPdo1Send.Controls.Add(this.txtPeriodTime);
|
|
this.grpPdo1Send.Controls.Add(this.txtPeriodTime);
|
|
|
this.grpPdo1Send.Controls.Add(this.lblPeriodUnit);
|
|
this.grpPdo1Send.Controls.Add(this.lblPeriodUnit);
|
|
|
|
|
+ // 添加规则发送时长相关控件到界面
|
|
|
|
|
+ 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.Location = new System.Drawing.Point(10, 120);
|
|
|
this.grpPdo1Send.Name = "grpPdo1Send";
|
|
this.grpPdo1Send.Name = "grpPdo1Send";
|
|
|
- this.grpPdo1Send.Size = new System.Drawing.Size(770, 130);
|
|
|
|
|
|
|
+ this.grpPdo1Send.Size = new System.Drawing.Size(770, 160); // 增加高度以容纳新控件
|
|
|
this.grpPdo1Send.TabIndex = 2;
|
|
this.grpPdo1Send.TabIndex = 2;
|
|
|
this.grpPdo1Send.TabStop = false;
|
|
this.grpPdo1Send.TabStop = false;
|
|
|
this.grpPdo1Send.Text = "TPDO1发送配置";
|
|
this.grpPdo1Send.Text = "TPDO1发送配置";
|
|
@@ -437,6 +453,33 @@ namespace CanOpenSlaveTest
|
|
|
this.lblPeriodUnit.TabIndex = 21;
|
|
this.lblPeriodUnit.TabIndex = 21;
|
|
|
this.lblPeriodUnit.Text = "us";
|
|
this.lblPeriodUnit.Text = "us";
|
|
|
//
|
|
//
|
|
|
|
|
+ // 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 = "持续时间:";
|
|
|
|
|
+ //
|
|
|
|
|
+ // 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.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
|
|
|
|
|
+ //
|
|
|
|
|
+ // lblRuleDurationUnit
|
|
|
|
|
+ //
|
|
|
|
|
+ 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 = "秒";
|
|
|
|
|
+ //
|
|
|
// txtLog
|
|
// txtLog
|
|
|
//
|
|
//
|
|
|
this.txtLog.BackColor = System.Drawing.Color.White;
|
|
this.txtLog.BackColor = System.Drawing.Color.White;
|
|
@@ -758,6 +801,17 @@ namespace CanOpenSlaveTest
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
private void SendRuleBasedTpdo1()
|
|
private void SendRuleBasedTpdo1()
|
|
|
{
|
|
{
|
|
|
|
|
+ // 检查是否达到运行时长限制
|
|
|
|
|
+ if (m_ruleSendDurationMs > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ double elapsedMs = (DateTime.Now - m_ruleSendStartTime).TotalMilliseconds;
|
|
|
|
|
+ if (elapsedMs >= m_ruleSendDurationMs)
|
|
|
|
|
+ {
|
|
|
|
|
+ StopRuleSend();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
// 构建PDO数据 - 第一个字节自增
|
|
// 构建PDO数据 - 第一个字节自增
|
|
@@ -912,15 +966,47 @@ namespace CanOpenSlaveTest
|
|
|
{
|
|
{
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
|
|
+ // 解析运行时长(秒转毫秒),如果输入为空或无效,默认为0(无限制)
|
|
|
|
|
+ int durationSeconds = 0;
|
|
|
|
|
+ if (int.TryParse(txtRuleDuration.Text.Trim(), out int parsedSeconds))
|
|
|
|
|
+ {
|
|
|
|
|
+ durationSeconds = Math.Max(0, parsedSeconds); // 确保非负数
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ m_ruleSendDurationMs = durationSeconds * 1000; // 转换为毫秒
|
|
|
|
|
+
|
|
|
// 重置计数器
|
|
// 重置计数器
|
|
|
m_ruleSendCounter = 0;
|
|
m_ruleSendCounter = 0;
|
|
|
|
|
+ m_ruleSendStartTime = DateTime.Now;
|
|
|
m_isRuleSending = true;
|
|
m_isRuleSending = true;
|
|
|
-
|
|
|
|
|
|
|
+ m_ruleSendCts = new CancellationTokenSource();
|
|
|
|
|
+
|
|
|
// 更新按钮文本
|
|
// 更新按钮文本
|
|
|
- btnRuleSend.Text = "停止规则";
|
|
|
|
|
-
|
|
|
|
|
- AppendLog("▶ 规则发送已启动 - 将在每次收到RPDO时发送TPDO1");
|
|
|
|
|
- AppendLog(" 第一字节将自动递增,其他字节使用配置值");
|
|
|
|
|
|
|
+ btnRuleSend.Text = "停止规则发送";
|
|
|
|
|
+
|
|
|
|
|
+ string durationText = durationSeconds > 0 ? durationSeconds.ToString() + "秒" : "无限制";
|
|
|
|
|
+ AppendLog("▶ 开始规则发送 - 时长: " + durationText);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果设置了运行时长,则启动定时器来检查时间
|
|
|
|
|
+ if (m_ruleSendDurationMs > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ Task.Delay(m_ruleSendDurationMs, m_ruleSendCts.Token)
|
|
|
|
|
+ .ContinueWith(t =>
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!t.IsCanceled)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 时间到了,自动停止规则发送
|
|
|
|
|
+ if (this.InvokeRequired)
|
|
|
|
|
+ {
|
|
|
|
|
+ this.Invoke(new Action(StopRuleSend));
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ StopRuleSend();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
@@ -938,7 +1024,8 @@ namespace CanOpenSlaveTest
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
m_isRuleSending = false;
|
|
m_isRuleSending = false;
|
|
|
-
|
|
|
|
|
|
|
+ m_ruleSendCts?.Cancel();
|
|
|
|
|
+
|
|
|
btnRuleSend.Text = "规则发送";
|
|
btnRuleSend.Text = "规则发送";
|
|
|
AppendLog("⏹ 规则发送已停止 - 共发送 " + m_ruleSendCounter.ToString() + " 次");
|
|
AppendLog("⏹ 规则发送已停止 - 共发送 " + m_ruleSendCounter.ToString() + " 次");
|
|
|
}
|
|
}
|