C3032
2026-01-08 116ed6b584bbdb40c5b65e7cb57e039b6ae57800
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using LB_SmartVisionCommon;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_VisionProcesses.Communicators.SiemensS7
{
    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["型号"];
                variable = CommunicatorConnections["变量地址"].ToString();
                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
            {
                if (plc!=null)
                {
                    if (string.IsNullOrEmpty(variable))
                    {
                        variable = CommunicatorConnections["变量地址"].ToString();
                    }
                    plc.Write(variable, message);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            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);
            }
        }
    }
}