From dc18021539bdf4ee176ec9d9b88eb7d9ad581600 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期五, 17 四月 2026 17:00:35 +0800
Subject: [PATCH] 新增心跳等常规信号配置&&修改相机轴读取配置等

---
 LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs |  148 +++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 131 insertions(+), 17 deletions(-)

diff --git a/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs b/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
index 2c8fbdd..080f3c5 100644
--- a/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
+++ b/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
@@ -13,10 +13,10 @@
     {
         private Plc plc;
         // 榛樿鍙橀噺鍦板潃
-        public string variable = "DB100.DBX0.1"; 
+        public string variable = "DB100.DBX0.1";
         // 鏁版嵁绫诲瀷
         private string dataType = "Bool";
-        
+
         // 缂撳瓨杩炴帴鍙傛暟
         private string ip = "127.0.0.1";
         private short rack = 0;
@@ -27,7 +27,7 @@
         {
             CommunicatorName = name;
             CommunicatorBrand = CommunicatorBrand.SiemensS7;
-            
+
             // 鍒濆鍖栭粯璁ゅ弬鏁�
             if (!CommunicatorConnections.Contains("鍦板潃")) CommunicatorConnections.Add("鍦板潃", "192.168.0.1");
             if (!CommunicatorConnections.Contains("鏈烘灦鍙�")) CommunicatorConnections.Add("鏈烘灦鍙�", "0");
@@ -35,7 +35,7 @@
             if (!CommunicatorConnections.Contains("鍨嬪彿")) CommunicatorConnections.Add("鍨嬪彿", CpuType.S71500);
             if (!CommunicatorConnections.Contains("鍙橀噺鍦板潃")) CommunicatorConnections.Add("鍙橀噺鍦板潃", "DB1.DBD0");
             if (!CommunicatorConnections.Contains("鏁版嵁绫诲瀷")) CommunicatorConnections.Add("鏁版嵁绫诲瀷", "String");
-            
+
             // 鍏煎鏃ч厤缃� "绔彛"
             if (CommunicatorConnections.Contains("绔彛"))
             {
@@ -43,7 +43,7 @@
             }
 
             // 璁剧疆榛樿蹇冭烦娑堟伅
-            strHeartbeat = "HEARTBEAT"; 
+            strHeartbeat = "HEARTBEAT";
         }
 
         public override bool Connect()
@@ -52,11 +52,11 @@
             {
                 // 鏇存柊鍙傛暟
                 if (CommunicatorConnections.Contains("鍦板潃")) ip = CommunicatorConnections["鍦板潃"].ToString();
-                
-                if (CommunicatorConnections.Contains("鏈烘灦鍙�")) 
+
+                if (CommunicatorConnections.Contains("鏈烘灦鍙�"))
                     short.TryParse(CommunicatorConnections["鏈烘灦鍙�"].ToString(), out rack);
-                
-                if (CommunicatorConnections.Contains("鎻掓Ы鍙�")) 
+
+                if (CommunicatorConnections.Contains("鎻掓Ы鍙�"))
                     short.TryParse(CommunicatorConnections["鎻掓Ы鍙�"].ToString(), out slot);
                 else if (CommunicatorConnections.Contains("绔彛"))
                     short.TryParse(CommunicatorConnections["绔彛"].ToString(), out slot);
@@ -68,7 +68,7 @@
                     else
                         Enum.TryParse(CommunicatorConnections["鍨嬪彿"].ToString(), out cpuType);
                 }
-                
+
                 if (CommunicatorConnections.Contains("鍙橀噺鍦板潃"))
                 {
                     variable = CommunicatorConnections["鍙橀噺鍦板潃"].ToString();
@@ -81,10 +81,10 @@
 
                 // 鍏抽棴鏃ц繛鎺�
                 plc?.Close();
-                
+
                 plc = new Plc(cpuType, ip, rack, slot);
                 plc.Open();
-                
+
                 if (plc.IsConnected)
                 {
                     bConnected = true;
@@ -137,7 +137,7 @@
 
             try
             {
-                variable= CommunicatorConnections.Contains("鍙橀噺鍦板潃") ? CommunicatorConnections["鍙橀噺鍦板潃"].ToString(): "DB100.DBX0.1";
+                variable = CommunicatorConnections.Contains("鍙橀噺鍦板潃") ? CommunicatorConnections["鍙橀噺鍦板潃"].ToString() : "DB100.DBX0.1";
                 string targetVar = variable;//"DB100.DBX0.1";
                 string strValue = message;
 
@@ -157,7 +157,7 @@
                 string currentDataType = CommunicatorConnections.Contains("鏁版嵁绫诲瀷") ? CommunicatorConnections["鏁版嵁绫诲瀷"].ToString() : "String";
 
                 // 鏍规嵁閰嶇疆鐨勬暟鎹被鍨嬭繘琛岃浆鎹�
-                try 
+                try
                 {
                     switch (currentDataType)
                     {
@@ -243,6 +243,120 @@
                 AsyncLogHelper.Error($"Device:[{CommunicatorName}] {Msg}");
                 return false;
             }
+        }
+
+        public object ReadSignal(SignalConfig signal)
+        {
+            if (plc == null || !plc.IsConnected)
+            {
+                AsyncLogHelper.Error("PLC 鏈繛鎺�");
+            }
+
+            string address = BuildAddress(signal);
+            if (signal.DataType.Equals("Real"))
+            {
+                object result = plc.Read(address);
+                byte[] bytes = BitConverter.GetBytes(Convert.ToUInt32(result));
+                float f = BitConverter.ToSingle(bytes, 0);
+                strReceiveMsg = f.ToString();
+            }
+            else if (signal.DataType.Equals("Bool"))
+            {
+                object result = plc.Read(address);
+                strReceiveMsg = result.ToString();
+            }
+            else if (signal.DataType.Equals("Int"))
+            {
+                object result = ((uint)plc.Read(address)).ConvertToInt();// ((uint)plc.Read("DB1.DBD60")).ConvertToInt();
+                strReceiveMsg = result.ToString();
+            }
+            return strReceiveMsg;
+        }
+
+        public void WriteSignal(SignalConfig signal, object value)
+        {
+            if (plc == null || !plc.IsConnected)
+            {
+                AsyncLogHelper.Error("PLC 鏈繛鎺�");
+            }
+
+            string address = BuildAddress(signal);
+            plc.Write(address, value);
+        }
+
+        public string BuildAddress(SignalConfig signal)
+        {
+            switch (signal.Area.ToUpper())
+            {
+                case "DB":
+                    {
+                        if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase))
+                        {
+                            return $"DB{signal.DbNumber}.DBX{signal.ByteOffset}.{signal.BitOffset}";
+                        }
+                        else
+                        {
+                            return $"DB{signal.DbNumber}.DBD{signal.ByteOffset}";
+                        }
+                    }
+                case "I":
+                    {
+                        if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase))
+                        {
+                            return $"I{signal.ByteOffset}.{signal.BitOffset}";
+                        }
+                        else
+                        {
+                            return $"IB{signal.ByteOffset}";
+                        }
+                    }
+                case "Q":
+                    {
+                        if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase))
+                        {
+                            return $"Q{signal.ByteOffset}.{signal.BitOffset}";
+                        }
+                        else
+                        {
+                            return $"QB{signal.ByteOffset}";
+                        }
+                    }
+                case "M":
+                    {
+                        if (signal.DataType.Equals("Bool", StringComparison.OrdinalIgnoreCase))
+                        {
+                            return $"M{signal.ByteOffset}.{signal.BitOffset}";
+                        }
+                        else
+                        {
+                            return $"MB{signal.ByteOffset}";
+                        }
+                    }
+                default:
+                    {
+                        AsyncLogHelper.Error($"涓嶆敮鎸佺殑瀛樺偍鍖�: {signal.Area}");
+                        return $"涓嶆敮鎸佺殑瀛樺偍鍖�: {signal.Area}";
+                    }
+            }
+        }
+
+        // 鍘熷鍦板潃璇诲啓锛堢敤浜庤皟璇曞櫒锛�
+        public object ReadRaw(string address)
+        {
+            if (plc == null || !plc.IsConnected)
+            {
+                AsyncLogHelper.Error("PLC 鏈繛鎺�");
+            }
+            return plc.Read(address);
+        }
+
+        public void WriteRaw(string address, object value)
+        {
+            if (plc == null || !plc.IsConnected)
+            {
+                AsyncLogHelper.Error("PLC 鏈繛鎺�");
+            }
+            plc.Write(address, value);
         }
 
         public override string ReceiveMsg()
@@ -360,16 +474,16 @@
                 return false;
             }
         }
-        
+
         public object Read(string address)
         {
             if (plc == null || !plc.IsConnected)
             {
                 return null;
             }
-             return plc.Read(address);
+            return plc.Read(address);
         }
-        
+
         public void Write(string address, object value)
         {
             if (plc != null && plc.IsConnected)

--
Gitblit v1.9.3