C3031
2026-01-05 0b617e22e15bca0b3dbaebdc55015cc2792f6840
LB_VisionProcesses/Communicators/SiemensS7/SiemensLBS7.cs
@@ -1,4 +1,6 @@
using System;
using 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 = "西门子S7")
        {
            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);
            }
        }
    }
}