Browse Source

add wago simulator command error response

chenzk 2 months ago
parent
commit
850f4da864
1 changed files with 29 additions and 2 deletions
  1. 29 2
      CyberX8_Simulator/Devices/WagoSocketSimulator.cs

+ 29 - 2
CyberX8_Simulator/Devices/WagoSocketSimulator.cs

@@ -43,7 +43,7 @@ namespace CyberX8_Simulator.Devices
         /// </summary>
         private void InitializeData()
         {
-            AIShorts[3] = 0x1388;
+            AIShorts[3] = 0x1388; 
         }
 
         #region 功能方法
@@ -179,6 +179,12 @@ namespace CyberX8_Simulator.Devices
                 OnWriteMessage(data); //原消息返回
                 return;
             }
+            else 
+            {
+                short flag = byteTransform.TransInt16(data, 0);
+                byte channel = data[6];
+                OnWriteMessage(CreateError(flag, channel, command, 0x01)); //指令错误
+            }
         }
         /// <summary>
         /// 回复读数字量
@@ -226,7 +232,28 @@ namespace CyberX8_Simulator.Devices
             Array.Copy(values, 0, bytes, 9, values.Length);
             return bytes;
         }
+        /// <summary>
+        /// 错误回复
+        /// </summary>
+        /// <param name="flag"></param>
+        /// <param name="channel"></param>
+        /// <param name="command"></param>
+        /// <param name="error"></param>
+        /// <returns></returns>
+        private byte[] CreateError(short flag, byte channel, byte command, byte error)
+        {
+            byte[] bytes = new byte[9];
+            Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
+            bytes[2] = 0x00;
+            bytes[3] = 0x00;
+            bytes[4] = 0x00;
+            bytes[5] = 0x03;
+            bytes[6] = channel;
+            bytes[7] = (byte)(command | 0x80);
+            bytes[8] = error;
+            return bytes;
+        }
+
 
-       
     }
 }