ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace iWareSda_QQJF.OPCService
{
    public class HSLS7 : SiemensS7Net
    {
          public string heartAdd = "";//心跳DB地址
        public bool IsConnected=false;
        int oldHeart = 10;//记录前一秒心跳值(也不知道为啥默认是10)
        int lostHeartTimes = 10;//掉线次数(一开始让它掉线直接重连)
        static SiemensPLCS siemensPLCSelected = SiemensPLCS.S1200;
 
        public HSLS7(string ip, string HeartDb)
            : base(siemensPLCSelected, ip)
        {
            this.OpenReadHeart(HeartDb);
            OperateResult operateResult = this.ConnectServer();
        }
 
 
   
 
        /// <summary>
        /// S7写入(暂时写这里)
        /// </summary>
        /// <param name="add"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool WriteValuePoint(string add, object value)
        {
 
            OperateResult o = new OperateResult();
            //bool
            var t = value.GetType();
            if (value.GetType() == typeof(bool))
            {
                //byte MyData = Convert.ToByte(value);
                bool myData = Convert.ToBoolean(value);
                o = this.Write(add, myData);
            }
            //浮点
            else if (value.GetType() == typeof(double) || value.GetType() == typeof(float))
            {
                double MyData = Convert.ToDouble(value);
                o = this.Write(add, MyData);
 
            }
            //整数
            else if (value.GetType() == typeof(Int32) || value.GetType() == typeof(Int16))
            {
                short MyData = Convert.ToInt16(value);
                o = this.Write(add, MyData);
            }
            //双整数
            else if (value.GetType() == typeof(Int32))
            {
                int myData = Convert.ToInt32(value);
                o = this.Write(add, myData);
            }
            if (o == null)
            {
                return false;
            }
            //if (o.ToString() == "NoError")
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
           
            return true;
            //return o.ToString();
 
        }
 
        /// <summary>
        /// S7读取
        /// </summary>
        /// <param name="add"></param>
        /// <param name="type"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public object ReadValuePoint(string add,string type)
        {
            object MyPlcData = null;
            if (type == "int")
            {
                MyPlcData = this.ReadInt16(add).Content;
            }
            else if (type == "Dint")
            {
                MyPlcData = this.ReadInt32(add).Content;
            }
            else if (type == "bool")
            {
                MyPlcData = this.ReadBool(add).Content;
            }
            else if (type == "byte")
            {
                MyPlcData = this.ReadByte(add).Content;
            }
            return MyPlcData;
 
        }
 
        /// <summary>
        /// 自检在线
        /// </summary>
        public void IsOnline()
        {
            while (true)
            {
                Thread.Sleep(1000);
                if (!string.IsNullOrEmpty(heartAdd))
                {
                    int heart =this.ReadInt16(heartAdd).Content;
                    if (heart == oldHeart)
                    {
                        ////掉线次数+1
                        //lostHeartTimes++;
                        IsConnected = false;
                    }
                    else
                    {
                        IsConnected = true;
                        //lostHeartTimes = 0;
                    }
                    //if (!this.con)
                    //{
                    //    //连续掉线5次以上为掉线
                    //    this.Close();
                    //    //尝试重连
                    //    Thread.Sleep(100);
                    //    this.Open();
                       
                    //}
                    oldHeart = heart;
                }
            }
        }
 
        /// <summary>
        /// //开启自检在线线程
        /// </summary>
        /// <param name="add"></param>
        public void OpenReadHeart(string add)
        {
            heartAdd = add;
            Thread IsOnlineThread = new Thread(this.IsOnline);
            IsOnlineThread.Start();
 
        }
    }
}