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
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
using logtxtWrite;
using System.Threading;
namespace iWareSda_QQJF.OPCService
{
    public class SS7 : PlcS7
    {
        public string heartAdd = "";//心跳DB地址
        int oldHeart = 10;//记录前一秒心跳值(也不知道为啥默认是10)
        int lostHeartTimes = 10;//掉线次数(一开始让它掉线直接重连)
        static CpuType plcType = (CpuType)Enum.Parse(typeof(CpuType), "S71200");
 
        public SS7(string ip, short rack, short slot,string HeartDb, string name = "", object tag = null)
            : base(plcType, ip, rack, slot)
        {
            this.OpenReadHeart(HeartDb);
        }
 
 
        ///// <summary>初始化S7连接
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public static SS7 s7Init(string IP,string add)
        //{
        //    try
        //    {
        //        SS7 newPLC = new SS7(IP, Convert.ToInt16("0"), Convert.ToInt16("1"), add);
        //        //var code = newPLC.Open();
        //        return newPLC;
 
        //    }
        //    catch (Exception )
        //    {
        //        logtxt.txtWrite("类名:FormMain/函数名:deviceOpcInit/ OPC初始化失败:"+IP+"初始化异常", 2);
        //    }
        //    return null;
        //}
 
 
        /// <summary>
        /// S7写入(暂时写这里)
        /// </summary>
        /// <param name="add"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool WriteValuePoint(string add, object value)
        {
            if (this.IsConnected)
            {
                object o = new object();
                //bool
                var t = value.GetType();
                if (value.GetType() == typeof(bool))
                {
                    //byte MyData = Convert.ToByte(value);
                    int myData = Convert.ToInt32(value);
                    o = this.Write(add, myData);
                }
                //浮点
                if (value.GetType() == typeof(double) || value.GetType() == typeof(float))
                {
                    double MyData = Convert.ToDouble(value);
                    o = this.Write(add, MyData.ConvertToUInt());
 
                }
                //整数
                else if (value.GetType() == typeof(Int32) || value.GetType() == typeof(short))
                {
                    short MyData = Convert.ToInt16(value);
                    o = this.Write(add, MyData.ConvertToUshort());
                }
                //双整数
                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 o.ToString();
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// S7读取
        /// </summary>
        /// <param name="add"></param>
        /// <param name="type"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public object ReadValuePoint(string add)
        {
 
            object MyPlcData = this.Read(add);
            return MyPlcData;
 
        }
 
        /// <summary>
        /// 自检在线
        /// </summary>
        public void IsOnline()
        {
            while (true)
            {
                Thread.Sleep(1000);
                if (!string.IsNullOrEmpty(heartAdd))
                {
                    //int heart = Convert.ToInt32(this.ReadValuePoint(heartAdd));
                    //if (heart == oldHeart)
                    //{
                    //    //掉线次数+1
                    //    lostHeartTimes++;
                    //}
                    //else
                    //{
                    //    lostHeartTimes = 0;
                    //}
                    if (!this.IsConnected)
                    {
                        //连续掉线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();
 
        }
    }
}