From 0b617e22e15bca0b3dbaebdc55015cc2792f6840 Mon Sep 17 00:00:00 2001
From: C3031 <shitiangui@lanpucloud.cn>
Date: 星期二, 06 一月 2026 16:15:01 +0800
Subject: [PATCH] 增加S7通讯界面
---
LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs | 81 +++++++++++++++++++++++++++++++++++++++-
1 files changed, 79 insertions(+), 2 deletions(-)
diff --git a/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs b/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
index f9fac67..b82d523 100644
--- a/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
+++ b/LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
@@ -1,4 +1,6 @@
-锘縰sing System;
+锘縰sing LB_SmartVisionCommon;
+using S7.Net;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +8,82 @@
namespace LB_VisionProcesses.Communicators.SiemensS7
{
- internal class SiemensLBS7
+ public class SiemensLBS7 : BaseCommunicator
{
+ private Plc plc;
+ public string variable = string.Empty;
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="name"></param>
+ public SiemensLBS7(string name = "瑗块棬瀛怱7")
+ {
+ CommunicatorConnections.Add("鍦板潃", "127.0.0.1");
+ CommunicatorConnections.Add("绔彛", "1");
+ CommunicatorConnections.Add("鍨嬪彿", S7.Net.CpuType.S71500);
+ CommunicatorBrand = CommunicatorBrand.SiemensS7;
+ CommunicatorName = name;
+ }
+ public override bool Connect()
+ {
+ try
+ {
+ string IP = CommunicatorConnections["鍦板潃"].ToString();
+ short slot;
+ short.TryParse(CommunicatorConnections["绔彛"].ToString(), out slot);
+ S7.Net.CpuType cpuType = (CpuType)CommunicatorConnections["鍨嬪彿"];
+ plc = new Plc(cpuType, IP, 0, slot);
+ plc.Open();
+ return true;
+
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ public override bool Disconnect()
+ {
+ try
+ {
+ plc?.Close();
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ public override bool SendMessage(string message)
+ {
+ try
+ {
+ plc.Write(variable, message);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ public override void Dispose()
+ {
+ try
+ {
+ AsyncLogHelper.Info($"Device:[{CommunicatorName}],Dispose()");
+
+ plc = null;
+
+ // Suppress finalization.
+ GC.SuppressFinalize(this);
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"Device:[{CommunicatorName}],Dispose(),Error" + ex);
+ }
+ }
}
}
--
Gitblit v1.9.3