分支自 SuZhouGuanHong/TaiYuanTaiZhong

PCS
dengjunjie
2023-12-13 113d1d4262d8f9e78a9d92123713c41669ad6c87
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.FreeDB;
using WIDESEA_Core.Utilities;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.Services.WCS.DianJieChiServer
{
    public class ParentServer
    {
        FreeDB freeDB = new FreeDB();
        private static List<PLCInfo> plcInfos;
 
        public ParentServer()
        {
            if (plcInfos == null)
            {
                string config = freeDB.DataBase.Ado.QuerySingle<string>("select config from dt_equipmentinfo where equipment_name='母车1'");
                plcInfos = JsonConvert.DeserializeObject<List<PLCInfo>>(config);
            }
        }
 
        /// <summary>
        /// 获取PLC信息
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetParentInfo()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //长链接断开后,会自动默认使用短连接
                var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
                if (client == null || !client.IsConnected)
                {
                    return content.Error($"PLC{"母车1"}未连接");
                }
                GetParent(client);
                content.Data = new { info = plcInfos };
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 获取母车信息
        /// </summary>
        /// <param name="client"></param>
        private void GetParent(PLCClient client)
        {
            foreach (var item in plcInfos)
            {
                if (item.dbAddress == "V230" || item.dbAddress == "V270")
                {
                    item.value = client.Read<short>(item.dbAddress);
                    continue;
                }
                if (item.showType.Contains("bool"))
                {
                    item.value = client.Read<bool>(item.dbAddress);
                }
                else if (item.showType == "int")
                {
                    item.value = client.Read<int>(item.dbAddress);
                }
            }
        }
 
        #region 操作母车
        /// <summary>
        /// 启动母车
        /// </summary>
        /// <returns></returns>
        public WebResponseContent Run_Parent(int point)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
                if (client == null || !client.IsConnected)
                {
                    return content.Error($"PLC{"母车1"}未连接");
                }
                var move_error1 = client.Read<short>("V230");//伺服1报警
                var move_error2 = client.Read<short>("V270");//伺服2报警
 
                if (move_error1 != 0)
                {
                    throw new Exception($"伺服1报警码{move_error1},无法启动");
                }
                if (move_error2 != 0)
                {
                    throw new Exception($"伺服2报警码{move_error2},无法启动");
                }
 
                client.Write("V1010.0", point);//目的地址
                Thread.Sleep(1500);
                client.Write("V1006.0", true);//启动命令
 
                WriteLog.Write_Log("手动操作", "母车", "启动母车" + point.ToString());
                content.OK("启动成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "母车", "启动母车异常" + point.ToString(), ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 停止母车
        /// </summary>
        /// <returns></returns>
        public WebResponseContent Stop_Parent()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
                if (client == null || !client.IsConnected)
                {
                    return content.Error($"PLC{"母车1"}未连接");
                }
 
                client.Write("V1006.0", false);//启动命令
 
                WriteLog.Write_Log("手动操作", "母车", "停止母车");
                content.OK("停止成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "母车", "停止母车异常", ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 复位母车伺服的报警
        /// </summary>
        /// <returns></returns>
        public WebResponseContent ResetMotherMove()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
                if (client == null || !client.IsConnected)
                {
                    return content.Error($"PLC{"母车1"}未连接");
                }
                client.Write("V1006.2", true);
                Thread.Sleep(2000);
                client.Write("V1006.2", false);
 
                WriteLog.Write_Log("手动操作", "母车", "复位伺服报警");
                content.OK("伺服复位成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "母车", "复位伺服报警异常", ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 母车置零
        /// </summary>
        /// <returns></returns>
        public WebResponseContent SetZeroToMotherCar()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
                if (client == null || !client.IsConnected)
                {
                    return content.Error($"PLC{"母车1"}未连接");
                }
 
                //一个置零信号,先发1、间隔3秒再发个0
                client.Write("V1006.1", true);
                WriteLog.Write_Log("手动操作", "母车", "母车置零,写入1");
                Thread.Sleep(2000);
                client.Write("V1006.1", false);
                WriteLog.Write_Log("手动操作", "母车", "母车置零,写入0");
 
                content.OK("母车置零成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "母车", "母车置零异常", ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
        #endregion
    }
}