Переглянути джерело

20260516001 CANOPEN从站规则发送测试完成,取消了界面数据增加效率

向羽 孟 1 місяць тому
батько
коміт
dbaea74323

+ 5 - 5
CanOpenSlaveTest/CanOpenSlaveDevice.cs

@@ -489,7 +489,7 @@ namespace CCDCount.DLL.CanBus
             {
                 try
                 {
-                    var frames = ReceiveCanFrames(50);
+                    var frames = ReceiveCanFrames(1);
                     
                     foreach (var frame in frames)
                     {
@@ -500,7 +500,7 @@ namespace CCDCount.DLL.CanBus
                 {
                     Console.WriteLine($"接收错误: {ex.Message}");
                 }
-                Thread.Sleep(1);  // 避免CPU占用过高
+                //Thread.Sleep(1);  // 避免CPU占用过高
             }
         }
         
@@ -914,11 +914,11 @@ namespace CCDCount.DLL.CanBus
             Console.WriteLine($"收到RPDO{pdoNumber}: 长度{length}字节");
             
             // 复制有效数据
-            byte[] validData = new byte[length];
-            Array.Copy(data, validData, length);
+            //byte[] validData = new byte[length];
+            //Array.Copy(data, validData, length);
             
             // 触发事件
-            OnRpdoReceived?.Invoke(m_nodeId, pdoNumber, validData);
+            OnRpdoReceived?.Invoke(m_nodeId, pdoNumber, data);
         }
 
         #endregion

+ 40 - 35
CanOpenSlaveTest/SlaveTestForm.cs

@@ -136,11 +136,9 @@ 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();
             this.lblRuleDurationUnit = new System.Windows.Forms.Label();
@@ -244,17 +242,15 @@ 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, 190); // 增加高度以容纳新控件
+            this.grpPdo1Send.Size = new System.Drawing.Size(770, 190);
             this.grpPdo1Send.TabIndex = 2;
             this.grpPdo1Send.TabStop = false;
             this.grpPdo1Send.Text = "TPDO1发送配置";
@@ -264,7 +260,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data0.AutoSize = true;
             this.lblPdo1Data0.Location = new System.Drawing.Point(10, 25);
             this.lblPdo1Data0.Name = "lblPdo1Data0";
-            this.lblPdo1Data0.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data0.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data0.TabIndex = 0;
             this.lblPdo1Data0.Text = "Byte0:";
             // 
@@ -273,7 +269,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data1.AutoSize = true;
             this.lblPdo1Data1.Location = new System.Drawing.Point(90, 25);
             this.lblPdo1Data1.Name = "lblPdo1Data1";
-            this.lblPdo1Data1.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data1.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data1.TabIndex = 1;
             this.lblPdo1Data1.Text = "Byte1:";
             // 
@@ -282,7 +278,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data2.AutoSize = true;
             this.lblPdo1Data2.Location = new System.Drawing.Point(170, 25);
             this.lblPdo1Data2.Name = "lblPdo1Data2";
-            this.lblPdo1Data2.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data2.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data2.TabIndex = 2;
             this.lblPdo1Data2.Text = "Byte2:";
             // 
@@ -291,7 +287,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data3.AutoSize = true;
             this.lblPdo1Data3.Location = new System.Drawing.Point(250, 25);
             this.lblPdo1Data3.Name = "lblPdo1Data3";
-            this.lblPdo1Data3.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data3.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data3.TabIndex = 3;
             this.lblPdo1Data3.Text = "Byte3:";
             // 
@@ -300,7 +296,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data4.AutoSize = true;
             this.lblPdo1Data4.Location = new System.Drawing.Point(330, 25);
             this.lblPdo1Data4.Name = "lblPdo1Data4";
-            this.lblPdo1Data4.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data4.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data4.TabIndex = 4;
             this.lblPdo1Data4.Text = "Byte4:";
             // 
@@ -309,7 +305,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data5.AutoSize = true;
             this.lblPdo1Data5.Location = new System.Drawing.Point(410, 25);
             this.lblPdo1Data5.Name = "lblPdo1Data5";
-            this.lblPdo1Data5.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data5.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data5.TabIndex = 5;
             this.lblPdo1Data5.Text = "Byte5:";
             // 
@@ -318,7 +314,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data6.AutoSize = true;
             this.lblPdo1Data6.Location = new System.Drawing.Point(490, 25);
             this.lblPdo1Data6.Name = "lblPdo1Data6";
-            this.lblPdo1Data6.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data6.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data6.TabIndex = 6;
             this.lblPdo1Data6.Text = "Byte6:";
             // 
@@ -327,7 +323,7 @@ namespace CanOpenSlaveTest
             this.lblPdo1Data7.AutoSize = true;
             this.lblPdo1Data7.Location = new System.Drawing.Point(570, 25);
             this.lblPdo1Data7.Name = "lblPdo1Data7";
-            this.lblPdo1Data7.Size = new System.Drawing.Size(47, 15);
+            this.lblPdo1Data7.Size = new System.Drawing.Size(55, 15);
             this.lblPdo1Data7.TabIndex = 7;
             this.lblPdo1Data7.Text = "Byte7:";
             // 
@@ -348,7 +344,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data1.Name = "txtPdo1Data1";
             this.txtPdo1Data1.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data1.TabIndex = 9;
-            this.txtPdo1Data1.Text = "255";
+            this.txtPdo1Data1.Text = "0";
             this.txtPdo1Data1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // txtPdo1Data2
@@ -358,7 +354,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data2.Name = "txtPdo1Data2";
             this.txtPdo1Data2.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data2.TabIndex = 10;
-            this.txtPdo1Data2.Text = "255";
+            this.txtPdo1Data2.Text = "0";
             this.txtPdo1Data2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // txtPdo1Data3
@@ -368,7 +364,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data3.Name = "txtPdo1Data3";
             this.txtPdo1Data3.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data3.TabIndex = 11;
-            this.txtPdo1Data3.Text = "255";
+            this.txtPdo1Data3.Text = "0";
             this.txtPdo1Data3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // txtPdo1Data4
@@ -378,7 +374,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data4.Name = "txtPdo1Data4";
             this.txtPdo1Data4.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data4.TabIndex = 12;
-            this.txtPdo1Data4.Text = "255";
+            this.txtPdo1Data4.Text = "0";
             this.txtPdo1Data4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // txtPdo1Data5
@@ -388,7 +384,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data5.Name = "txtPdo1Data5";
             this.txtPdo1Data5.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data5.TabIndex = 13;
-            this.txtPdo1Data5.Text = "255";
+            this.txtPdo1Data5.Text = "0";
             this.txtPdo1Data5.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // txtPdo1Data6
@@ -398,7 +394,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data6.Name = "txtPdo1Data6";
             this.txtPdo1Data6.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data6.TabIndex = 14;
-            this.txtPdo1Data6.Text = "255";
+            this.txtPdo1Data6.Text = "0";
             this.txtPdo1Data6.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // txtPdo1Data7
@@ -408,7 +404,7 @@ namespace CanOpenSlaveTest
             this.txtPdo1Data7.Name = "txtPdo1Data7";
             this.txtPdo1Data7.Size = new System.Drawing.Size(60, 25);
             this.txtPdo1Data7.TabIndex = 15;
-            this.txtPdo1Data7.Text = "255";
+            this.txtPdo1Data7.Text = "0";
             this.txtPdo1Data7.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
             // 
             // btnSendTpdo1
@@ -446,7 +442,7 @@ namespace CanOpenSlaveTest
             this.lblPeriodTime.AutoSize = true;
             this.lblPeriodTime.Location = new System.Drawing.Point(350, 98);
             this.lblPeriodTime.Name = "lblPeriodTime";
-            this.lblPeriodTime.Size = new System.Drawing.Size(77, 15);
+            this.lblPeriodTime.Size = new System.Drawing.Size(75, 15);
             this.lblPeriodTime.TabIndex = 19;
             this.lblPeriodTime.Text = "周期时间:";
             // 
@@ -473,7 +469,7 @@ namespace CanOpenSlaveTest
             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.Size = new System.Drawing.Size(75, 15);
             this.lblContinuousDuration.TabIndex = 22;
             this.lblContinuousDuration.Text = "持续时间:";
             // 
@@ -491,7 +487,7 @@ namespace CanOpenSlaveTest
             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.Size = new System.Drawing.Size(99, 15);
             this.lblContinuousDurationUnit.TabIndex = 24;
             this.lblContinuousDurationUnit.Text = "秒(0=无限制)";
             // 
@@ -500,7 +496,7 @@ namespace CanOpenSlaveTest
             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.Size = new System.Drawing.Size(75, 15);
             this.lblRuleDuration.TabIndex = 25;
             this.lblRuleDuration.Text = "规则时长:";
             // 
@@ -518,7 +514,7 @@ 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(62, 15);
+            this.lblRuleDurationUnit.Size = new System.Drawing.Size(99, 15);
             this.lblRuleDurationUnit.TabIndex = 27;
             this.lblRuleDurationUnit.Text = "秒(0=无限制)";
             // 
@@ -527,12 +523,12 @@ namespace CanOpenSlaveTest
             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, 320); // 调整位置
+            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, 210); // 调整尺寸
+            this.txtLog.Size = new System.Drawing.Size(770, 210);
             this.txtLog.TabIndex = 3;
             // 
             // m_updateTimer
@@ -880,12 +876,18 @@ namespace CanOpenSlaveTest
             AppendLog("⚠ Byte" + index.ToString() + " 值无效: '" + text + "',使用默认值0");
             return 0;
         }
-        
+
+        bool NextValue = false;
+        byte[] pdoData = new byte[8];
         /// <summary>
         /// 根据规则发送TPDO1(第一字节自增)
         /// </summary>
         private void SendRuleBasedTpdo1()
         {
+            if(m_ruleSendCounter==0)
+            {
+                m_ruleSendStartTime = DateTime.Now;
+            }
             // 检查是否达到运行时长限制
             if (m_ruleSendDurationMs > 0)
             {
@@ -900,7 +902,7 @@ namespace CanOpenSlaveTest
             try
             {
                 // 构建PDO数据 - 第一个字节自增
-                byte[] pdoData = new byte[8];
+
                 pdoData[0] = (byte)(m_ruleSendCounter % 256);  // 第一个字节自增
                 pdoData[1] = ParseByteValue(txtPdo1Data1.Text, 1);
                 pdoData[2] = ParseByteValue(txtPdo1Data2.Text, 2);
@@ -908,14 +910,16 @@ namespace CanOpenSlaveTest
                 pdoData[4] = ParseByteValue(txtPdo1Data4.Text, 4);
                 pdoData[5] = ParseByteValue(txtPdo1Data5.Text, 5);
                 pdoData[6] = ParseByteValue(txtPdo1Data6.Text, 6);
-                pdoData[7] = ParseByteValue(txtPdo1Data7.Text, 7);
-                
+                //pdoData[7] = ParseByteValue(txtPdo1Data7.Text, 7);
+                pdoData[7] = NextValue ? (byte)0 : (byte)1;
+
                 // 发送PDO
                 m_slaveDevice.SendTpdo(1, pdoData);
+                NextValue = !NextValue;
                 
                 // 计数器自增
                 m_ruleSendCounter++;
-                
+
                 // 输出日志
                 string dataStr = BitConverter.ToString(pdoData).Replace("-", " ");
                 AppendLog("→ 规则发送 #" + m_ruleSendCounter.ToString() + ", Byte0=" + ((m_ruleSendCounter - 1) % 256).ToString() + ", Data: [" + dataStr + "]");
@@ -1009,12 +1013,13 @@ namespace CanOpenSlaveTest
             }
             
             string timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
+            Console.WriteLine("[" + timestamp + "] " + message + "\r\n");
             // 使用字符串拼接而非插值,确保兼容性
-            txtLog.AppendText("[" + timestamp + "] " + message + "\r\n");
+            //txtLog.AppendText("[" + timestamp + "] " + message + "\r\n");
             
             // 自动滚动到底部
-            txtLog.SelectionStart = txtLog.Text.Length;
-            txtLog.ScrollToCaret();
+            //txtLog.SelectionStart = txtLog.Text.Length;
+            //txtLog.ScrollToCaret();
         }
         
         protected override void OnFormClosing(FormClosingEventArgs e)