分支自 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using Newtonsoft.Json;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.FreeDB;
using WIDESEA_Core.Utilities;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS
{
    /// <summary>
    /// PLC显示信息配置
    /// </summary>
    public class PLCInfo
    {
        /// <summary>
        /// 所属设备类型
        /// </summary>
        public string eqType { get; set; }
 
        /// <summary>
        /// 信号中文名称
        /// </summary>
        public string io_name { get; set; }
 
        /// <summary>
        /// 显示的数据类型
        /// </summary>
        public string showType { get; set; }
 
        /// <summary>
        /// 数据地址
        /// </summary>
        public string dbAddress { get; set; }
 
        /// <summary>
        /// 值
        /// </summary>
        public object value { get; set; }
    }
 
    /// <summary>
    /// 子车参数配置
    /// </summary>
    public class EqConfig
    {
        /// <summary>
        /// 配置名称
        /// </summary>
        public string name { get; set; }
 
        /// <summary>
        /// 信号地址
        /// </summary>
        public string dbAddress { get; set; }
 
        /// <summary>
        /// 值
        /// </summary>
        public int value { get; set; }
 
        /// <summary>
        /// 获取配置字典
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, short> GetConfigDic(List<EqConfig> configs)
        {
            Dictionary<string, short> config_dic = new Dictionary<string, short>();
            foreach (EqConfig config in configs)
            {
                config_dic.Add(config.dbAddress, (short)config.value);
            }
            return config_dic;
        }
    }
 
    public class ChilderServer
    {
        /*
            //1、显示基本信息
            //2、单独控制子车(显示实时位置)
            //3、单独控制龙门(显示实时位置)
            //3、单独控制升降(显示实时位置)
            //4、单独控制夹爪(显示夹爪状态)
            //5联动运行(现场调试)
         */
 
        FreeDB freeDB = new FreeDB();
        private static List<PLCInfo> plcInfos;
 
        public ChilderServer()
        {
            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 GetChidlerInfo()
        {
            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"}未连接");
                }
                GetChilderInfo(client);
                content.Data = new { info = plcInfos };
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 获取子车参数
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetChidlerParm()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string config = freeDB.DataBase.Ado.QuerySingle<string>("select eqconfig from dt_equipmentinfo where equipment_name='子车1'");
                content.Data = config;
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        #region 子车操作
        public void Remark()
        {
            //运行流程
            //1、启动子车、连接子车PLC(刚启动时子车所有状态为初始状态)
            //2、子车、龙门、升降、夹爪全部给使能。所有初始速度全部加载好
            //3、点击回原点
            //4、(设置为自动状态)依次控制子车、龙门、升降、夹爪运行
 
 
            //完成后3种操作
            //1、原地处理。所有操作不动。
            //处理完成后
            //处理完成或下降到之前的位置,
            //松开夹爪。
            //然后升降机回位。
            //龙门还要回到原点
 
            //2、龙门回到原点
            //升降运动到指定位置(原点)
            //人工处理故障板
            //人工处理完成
            //提起故障板
            //龙门回到提故障板的位置
 
            //3、龙门回到原点
            //升降运动到指定位置(原点)
            //子车回原点
            //人工处理故障板
            //人工处理完成
            //在送回
        }
 
 
        /// <summary>
        /// 回原点
        /// </summary>
        /// <returns></returns>
        public WebResponseContent ToBasePoint()
        {
            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 configDic = GetEqConfig();
                client.Write("66", true);//子车伺服使能1
                Thread.Sleep(1000);
                client.Write("67", true);//子车伺服使能2
                Thread.Sleep(1000);
                client.Write("209", true);//龙门使能1
                Thread.Sleep(1000);
                client.Write("210", true); //龙门使能2
                Thread.Sleep(1000);
                client.Write("237", true);//升降使能
                Thread.Sleep(1000);
                client.Write("212", true);//夹紧使能
                Thread.Sleep(1000);
 
                client.Write("1036", configDic["1036"]);//子车行走速度
                client.Write("1038", configDic["1038"]);//子车手动速度
                client.Write("1040", configDic["1040"]); //子车扫码枪慢速定位速度
                client.Write("1044", configDic["1044"]);//龙门行走速度
                client.Write("1046", configDic["1046"]);//龙门手动速度
                client.Write("1048", configDic["1048"]);//龙门回零速度
                client.Write("1050", configDic["1050"]);//升降行走速度
                client.Write("1052", configDic["1052"]);//升降手动速度
                client.Write("1054", configDic["1054"]);//升降回零速度
                client.Write("1056", configDic["1056"]);//夹紧行走速度
                client.Write("1058", configDic["1058"]);//夹紧手动速度
                client.Write("1060", configDic["1060"]);//夹紧回零速度
                client.Write("1042", configDic["1042"]);//子车回零速度
 
                Thread.Sleep(500);
                client.Write("241", true);//请求回原点信号
 
                WriteLog.Write_Log("手动操作", "子车", "手动回原点成功", configDic);
                content.OK("回原点成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "子车", "手动回原点异常", ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 启动子车
        /// </summary>
        /// <returns></returns>
        public WebResponseContent Run_Child(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"}未连接");
                }
 
                WriteRun(client, 1010, point, "64");
 
                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_Child()
        {
            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("242", true);
 
                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 Reset_Child()
        {
            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("244", true);
                Thread.Sleep(2000);
                client.Write("244", false);
 
                WriteLog.Write_Log("手动操作", "子车", "复位子车成功");
                content.OK("复位成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "子车", "复位子车异常", ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 启动龙门
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public WebResponseContent Run_Gantry(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"}未连接");
                }
 
                WriteRun(client, 1028, point, "208");
 
                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>
        /// <param name="point"></param>
        /// <returns></returns>
        public WebResponseContent Run_Lifting(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"}未连接");
                }
 
                WriteRun(client, 1032, point, "211");
 
                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>
        /// <param name="point"></param>
        /// <returns></returns>
        public WebResponseContent Colse_Handle(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"}未连接");
                }
                //夹紧夹爪
                client.Write("213", true);
 
                WriteLog.Write_Log("手动操作", "子车", "夹紧夹爪");
                content.OK("操作成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "子车", "夹紧夹爪异常" + point.ToString(), ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 松开夹爪
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public WebResponseContent Open_Handle(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"}未连接");
                }
                //松开夹爪
                client.Write("214", true);
 
                WriteLog.Write_Log("手动操作", "子车", "松开夹爪");
                content.OK("操作成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "子车", "松开夹爪异常" + point.ToString(), ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 更改自动状态
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public WebResponseContent ChangeAutoState(string state)
        {
            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"}未连接");
                }
 
                if (state == "手动")
                {
                    client.Write("233", true); //手动状态
                }
                else if (state == "自动")
                {
                    client.Write("233", false); //自动状态
                }
 
                WriteLog.Write_Log("手动操作", "子车", "更改设备手自动状态为:" + state + "成功");
                content.OK("操作成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "子车", "更改设备手自动状态为:" + state + "异常", ex.Message);
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 修改子车参数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public WebResponseContent SetChilderParm(List<EqConfig> configs)
        {
            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 configDic = EqConfig.GetConfigDic(configs);
 
                client.Write("1036", configDic["1036"]);//子车行走速度
                client.Write("1038", configDic["1038"]);//子车手动速度
                client.Write("1040", configDic["1040"]); //子车扫码枪慢速定位速度
                client.Write("1044", configDic["1044"]);//龙门行走速度
                client.Write("1046", configDic["1046"]);//龙门手动速度
                client.Write("1048", configDic["1048"]);//龙门回零速度
                client.Write("1050", configDic["1050"]);//升降行走速度
                client.Write("1052", configDic["1052"]);//升降手动速度
                client.Write("1054", configDic["1054"]);//升降回零速度
                client.Write("1056", configDic["1056"]);//夹紧行走速度
                client.Write("1058", configDic["1058"]);//夹紧手动速度
                client.Write("1060", configDic["1060"]);//夹紧回零速度
                client.Write("1042", configDic["1042"]);//子车回零速度
 
                string configJson = JsonConvert.SerializeObject(configs);
                int res = freeDB.DataBase.Ado.ExecuteNonQuery("update dt_equipmentinfo set eqconfig=@eqconfig where equipment_name='子车1'", new { eqconfig = configJson });
 
                WriteLog.Write_Log("手动操作", "子车", "修改子车参数成功", configs);
                content.OK("操作成功!");
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("手动操作", "子车", "修改子车参数异常", new { 内容 = configs, 错误 = ex.Message });
                content.Error(ex.Message);
            }
            return content;
        }
 
 
        /// <summary>
        /// 一键运行子车
        /// </summary>
        /// <returns></returns>
        public WebResponseContent RunChilder()
        {
            WebResponseContent content = new WebResponseContent();
            return content.Error("开发调试中……");
 
            ChilderCarJob.CurrentState = "子车开始";
            ChilderCarJob.childerPoint = 1200;
            ChilderCarJob.gantryPoint = 1500;
            return content.OK("启动成功");
        }
        #endregion
 
        /// <summary>
        /// 写入设备运行
        /// </summary>
        /// <param name="address">位置地址</param>
        /// <param name="point">位置值</param> 
        /// <param name="runAddress">启动地址</param> 
        private void WriteRun(PLCClient client, int address, int point, string runAddress)
        {
            client.Write(runAddress, false);//复位启动命令
            Thread.Sleep(1000);
 
            int startPoint = point / 1000;
            int endPoint = point % 1000;
            string address1 = address.ToString();
            string address2 = (address + 1).ToString();
 
            client.Write(address1, (short)startPoint);
            client.Write(address2, (short)endPoint);//写入目的地址
 
            var point1 = client.Read<short>(address1);
            var point2 = client.Read<short>(address2);
            if (startPoint != point1)
            {
                client.Write(address1, (short)startPoint);
                WriteLog.Write_Log("手动操作", "子车", $"启动子车值不一致写入{startPoint},复读{point1}");
            }
            if (endPoint != point2)
            {
                client.Write(address2, (short)endPoint);
                WriteLog.Write_Log("手动操作", "子车", $"启动子车值不一致写入{endPoint},复读{point2}");
            }
 
            Thread.Sleep(1000);//必须延时
            client.Write(runAddress, true);//写入启动命令
        }
 
        /// <summary>
        /// 读取子车信息
        /// </summary>
        /// <param name="client"></param>
        private void GetChilderInfo(PLCClient client)
        {
            foreach (var item in plcInfos)
            {
                if (item.showType.Contains("bool"))
                {
                    item.value = client.Read<bool>(item.dbAddress);
                }
                else if (item.showType == "int")
                {
                    var address = int.Parse(item.dbAddress) + 1;
                    item.value = client.Read<short>(item.dbAddress) * 1000 + client.Read<short>(address + "");
                }
            }
        }
 
        /// <summary>
        /// 获取子车配置
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, short> GetEqConfig()
        {
            string configJson = freeDB.DataBase.Ado.QuerySingle<string>("select eqconfig from dt_equipmentinfo where equipment_name='子车1'");
            var configList = JsonConvert.DeserializeObject<List<EqConfig>>(configJson);
            var config = EqConfig.GetConfigDic(configList);
            return config;
        }
    }
}