zhengqifeng
2025-12-30 e254b1f4efa18b3c84ce75e9da2aeff64a1c7cbe
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autofac.Core;
using HslCommunication;
using HslCommunication.Core;
using Microsoft.VisualBasic.FileIO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using SixLabors.ImageSharp.PixelFormats;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class PLCJob : JobBase, IJob
    {
        private readonly IBoxingService _boxingService;
        private readonly IBoxingDetailService _boxingDetailService;
        private readonly IFormulaService _formulaService;
        private readonly IFormulaDetailService _formulaDetailService;
        private readonly IProcessInfoService _processInfoService;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        public PLCJob(IBoxingService boxingService, IBoxingDetailService boxingDetailService, IFormulaService formulaService, IFormulaDetailService formulaDetailService, IProcessInfoService processInfoService, IUnitOfWorkManage unitOfWorkManage)
        {
            _boxingService = boxingService;
            _boxingDetailService = boxingDetailService;
            _formulaService = formulaService;
            _formulaDetailService = formulaDetailService;
            _processInfoService = processInfoService;
            _unitOfWorkManage = unitOfWorkManage;
        }
        private readonly string set = "0x16,0x4D,0x0D,0x30,0x34,0x30,0x31,0x44,0x30,0x35,0x2E";//主机发送设置扫描模式为主机模式的命令,扫描器返回接收到的命令和<ACK>(0x06)后,将扫描模式更改为主机模式
        private readonly string Text = "开启扫码";//主机发送设置扫描模式为主机模式的命令,扫描器返回接收到的命令和<ACK>(0x06)后,将扫描模式更改为主机模式
        private readonly string command = "0x16,0x54,0x0D";//发送开始解码命令;"0x16,0x55,0x0D"发送停止解码命令
        public Task Execute(IJobExecutionContext context)
        {
 
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null)
            {
                if (value is OtherDevice)
                {
                    OtherDevice device = (OtherDevice)value;
                    try
                    {
 
                        var Heart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolHeart);
                        var EMG = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolEMG);
                        var AutoExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolAutoExecuting);
                        var OnlineExecuting = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolOnlineExecuting);
                        var Error = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolError);
                        if (Heart && !EMG && AutoExecuting && OnlineExecuting && !Error)
                        {
                            #region 上线扫码
                            HandleOnlineScan(device);
                            #endregion
 
                            #region 压装扫码
                            HandlePressScan(device);
                            #endregion
 
                            #region 检测扫码
                            HandleDetectScan(device);
                            #endregion
 
                            #region 下线扫码
                            HandleOfflineScan(device);
 
                            #endregion
                        }
 
                    }
                    catch (Exception ex)
                    {
                        WriteError(device.DeviceName, "错误", ex);
                    }
                }
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 上线扫码
        /// </summary>
        /// <param name="device"></param>
        private void HandleOnlineScan(OtherDevice device)
        {
            var Location1ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation1ScanStart);
            var Location1ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation1ScanDone);
            if (Location1ScanStart && !Location1ScanDone)//控制扫码枪扫描托盘码,根据托盘码查询组盘数据,判断托盘绑定成品是否已绑定所有所需零件
            {
                #region 串口连接
                SerialPortCommunicator portCommunicator1 = new SerialPortCommunicator("COM1", 9600, "上线扫码枪");
                bool IsConnected1 = false;
                SerialPortCommunicator portCommunicator2 = new SerialPortCommunicator("COM2", 9600, "上线扫码枪");
                bool IsConnected2 = false;
                if (!IsConnected1) IsConnected1 = portCommunicator1.Connect();
                if (!IsConnected2) IsConnected2 = portCommunicator2.Connect();
                byte[] dataToSend1 = Encoding.ASCII.GetBytes(command);//转成字节数组
                portCommunicator1.Write(dataToSend1);
                string receiveData = portCommunicator2.ToString(Encoding.UTF8);
                //if (receiveData == "0x16,0x54,0x0D")
                //{
                //    Console.WriteLine(receiveData);
                byte[] dataToSend2 = Encoding.ASCII.GetBytes("PL106");//转成字节数组
                portCommunicator2.Write(dataToSend2);
                //}
                string PalletCode = portCommunicator1.ToString(Encoding.UTF8);
                using (var uow = _unitOfWorkManage.CreateUnitOfWork())
                {
                    try
                    {
                        if (PalletCode.IsNotEmptyOrNull())
                        {
                            var boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
                            if (boxing == null)
                            {
                                WriteError(device.DeviceName, "空托盘");
                                return;
                            }
                            List<Dt_BoxingDetail> dt_BoxingDetails = _boxingDetailService.Repository.QueryData(x => x.BoxingId == boxing.Id);
                            Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == boxing.ProductCode);
                            if (dt_Formula == null)
                            {
                                WriteError(device.DeviceName, "无成品配方");
                                return;
                            }
                            List<Dt_FormulaDetail> dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == dt_Formula.Id);
                            //比较零件是否齐全
                            if (!_boxingDetailService.IsComponentCodesEqual(dt_BoxingDetails, dt_FormulaDetails))
                            {
                                WriteError(device.DeviceName, "零件有问题,请处理");
                                return;
                            }
                        }
                        //提交事务
                        uow.Commit();
 
                        IsConnected1 = !portCommunicator1.Disconnect();
                        IsConnected2 = !portCommunicator2.Disconnect();
                        #endregion
                        device.SetValue(W_PLCDBName.wboolLocation1ScanDone, true);
                        
 
                    }
                    catch (Exception ex)
                    {
                        WriteError(device.DeviceName, "上线扫码流程执行异常", ex);
                    }
                }
            }
            else if (!Location1ScanStart && Location1ScanDone)
            {
                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, false);
            }
        }
 
        /// <summary>
        /// 压装扫码
        /// </summary>
        /// <param name="device"></param>
        private void HandlePressScan(OtherDevice device)
        {
            var Location2ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation2ScanStart);
            var Location2ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation2ScanDone);
            if (Location2ScanStart && !Location2ScanDone)//控制扫码枪扫描托盘码,根据托盘码查询托盘绑定成品加工所需数据,写入PLC
            {
                #region MyRegion
                #region 串口连接
                SerialPortCommunicator portCommunicator17 = new SerialPortCommunicator("COM17", 9600, "压装扫码枪");
                bool IsConnected17 = false;
                SerialPortCommunicator portCommunicator18 = new SerialPortCommunicator("COM18", 9600, "压装扫码枪");
                bool IsConnected18 = false;
                if (!IsConnected17) IsConnected17 = portCommunicator17.Connect();
                if (!IsConnected18) IsConnected18 = portCommunicator18.Connect();
                byte[] dataToSend1 = Encoding.ASCII.GetBytes(command);//转成字节数组
                portCommunicator17.Write(dataToSend1);
                string receiveData = portCommunicator18.ToString(Encoding.UTF8);
                byte[] dataToSend2 = Encoding.ASCII.GetBytes("PL106");//转成字节数组
                portCommunicator18.Write(dataToSend2);
                //}
                string PalletCode = portCommunicator17.ToString(Encoding.UTF8);
                using (var uow = _unitOfWorkManage.CreateUnitOfWork())
                {
                    try
                    {
                        if (PalletCode.IsNotEmptyOrNull())
                        {
                            Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
                            Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_Boxing.ProductCode);
                            //写入PLC
                            device.SetValue(W_PLCDBName.wrealProductLength, dt_Formula.ProductLength);
                            device.SetValue(W_PLCDBName.wrealProductWidth, dt_Formula.ProductWidth);
                            device.SetValue(W_PLCDBName.wrealProductHeight, dt_Formula.ProductHeight);
                            device.SetValue(W_PLCDBName.wrealScrewDownsetDistance, dt_Formula.ScrewDownsetDistance);
                            device.SetValue(W_PLCDBName.wrealScrewTorqueOutput, dt_Formula.ScrewTorqueOutput);
                            device.SetValue(W_PLCDBName.wDintAutoScrewOn, dt_Formula.DintAutoScrewOn);
 
                            //压装扫码完成
                            device.SetValue(W_PLCDBName.wboolLocation2ScanDone, true);
 
                            //接收数据
                            float PressHeight = device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectPressHeight);
                            float ScrewTorque = device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectScrewTorque);
                            float ScrewAngle = device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectScrewAngle);
 
                            //添加到数据库
                            Dt_ProcessInfo dt_ProcessInfo = new Dt_ProcessInfo()
                            {
                                PalletCode = PalletCode,
                                ProductCode = dt_Formula.ProductCode,
                                ProductName = dt_Formula.ProductName,
                                PressHeight = PressHeight,
                                ScrewTorque = ScrewTorque,
                                ScrewAngle = ScrewAngle
 
                            };
                            _processInfoService.Repository.AddData(dt_ProcessInfo);
                        }
                        //提交事务
                        uow.Commit();
 
                        IsConnected17 = !portCommunicator17.Disconnect();
                        IsConnected18 = !portCommunicator18.Disconnect();
                        #endregion
                        #endregion
 
                    }
                    catch (Exception ex)
                    {
                        WriteError(device.DeviceName, "压装扫码流程执行异常", ex);
                    }
                }
            }
            else if (!Location2ScanStart && Location2ScanDone)
            {
                device.SetValue(W_PLCDBName.wboolLocation2ScanDone, false);
            }
        }
 
        /// <summary>
        /// 检测扫码
        /// </summary>
        /// <param name="device"></param>
        private void HandleDetectScan(OtherDevice device)
        {
            var Location3ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation3ScanStart);
            var Location3ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation3ScanDone);
            if (Location3ScanStart && !Location3ScanDone)
            {
                #region MyRegion
                #region 串口连接
                SerialPortCommunicator portCommunicator19 = new SerialPortCommunicator("COM19", 9600, "检测扫码枪");
                bool IsConnected19 = false;
                SerialPortCommunicator portCommunicator20 = new SerialPortCommunicator("COM20", 9600, "检测扫码枪");
                bool IsConnected20 = false;
                if (!IsConnected19) IsConnected19 = portCommunicator19.Connect();
                if (!IsConnected20) IsConnected20 = portCommunicator20.Connect();
                byte[] dataToSend1 = Encoding.ASCII.GetBytes(command);//转成字节数组
                portCommunicator19.Write(dataToSend1);
                string receiveData = portCommunicator20.ToString(Encoding.UTF8);
                byte[] dataToSend2 = Encoding.ASCII.GetBytes("PL106");//转成字节数组
                portCommunicator20.Write(dataToSend2);
                //}
                string PalletCode = portCommunicator19.ToString(Encoding.UTF8);
 
                using (var uow = _unitOfWorkManage.CreateUnitOfWork())
                {
                    try
                    {
                        if (PalletCode.IsNotEmptyOrNull())
                        {
                            Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
                            Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_Boxing.ProductCode);
                            //写入PLC
                            device.SetValue(W_PLCDBName.wrealXDirectionDistance1, dt_Formula.XDirectionDistance1);
                            device.SetValue(W_PLCDBName.wrealYDirectionHeight1, dt_Formula.YDirectionHeight1);
                            device.SetValue(W_PLCDBName.wrealXDirectionDistance2, dt_Formula.XDirectionDistance2);
                            device.SetValue(W_PLCDBName.wrealYDirectionHeight2, dt_Formula.YDirectionHeight2);
                            device.SetValue(W_PLCDBName.wrealXDirectionDistance3, dt_Formula.XDirectionDistance3);
                            device.SetValue(W_PLCDBName.wrealYDirectionHeight3, dt_Formula.YDirectionHeight3);
 
                            //检测扫码完成
                            device.SetValue(W_PLCDBName.wboolLocation3ScanDone, true);
 
                            //接收数据
                            float Height1 = device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectHeight1);
                            float Height2 = device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectHeight2);
                            float Height3 = device.GetValue<R_PLCDBName, float>(R_PLCDBName.rrealDetectHeight3);
 
                            Dt_ProcessInfo dt_ProcessInfo = _processInfoService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
                            if (dt_ProcessInfo == null)
                            {
                                WriteInfo(device.DeviceName, "成品未压装");
                                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, true);
                                return;
                            }
                            dt_ProcessInfo.Height1 = Height1;
                            dt_ProcessInfo.Height2 = Height2;
                            dt_ProcessInfo.Height3 = Height3;
                            _processInfoService.Repository.UpdateData(dt_ProcessInfo);
 
                        }
                        //提交事务
                        uow.Commit();
 
                        IsConnected19 = !portCommunicator19.Disconnect();
                        IsConnected20 = !portCommunicator20.Disconnect();
                        #endregion
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        WriteError(device.DeviceName, "检测扫码流程执行异常", ex);
                    }
                }
            }
            else if (!Location3ScanStart && Location3ScanDone)
            {
                device.SetValue(W_PLCDBName.wboolLocation3ScanDone, false);
            }
 
        }
 
        /// <summary>
        /// 下线扫码
        /// </summary>
        /// <param name="device"></param>
        private void HandleOfflineScan(OtherDevice device)
        {
            var Location4ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation4ScanStart);
            var Location4ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation4ScanDone);
            if (Location4ScanStart && !Location4ScanDone)//控制扫码枪扫描托盘码,根据托盘码查询组盘数据,判断托盘绑定成品是否已绑定所有所需零件
            {
                SerialPortCommunicator portCommunicator21 = new SerialPortCommunicator("COM21", 9600, "下线扫码枪");
                bool IsConnected21 = false;
                SerialPortCommunicator portCommunicator22 = new SerialPortCommunicator("COM22", 9600, "下线扫码枪");
                bool IsConnected22 = false;
                if (!IsConnected21) IsConnected21 = portCommunicator21.Connect();
                if (!IsConnected22) IsConnected22 = portCommunicator22.Connect();
                byte[] dataToSend1 = Encoding.ASCII.GetBytes(command);//转成字节数组
                portCommunicator21.Write(dataToSend1);
                string receiveData = portCommunicator22.ToString(Encoding.UTF8);
                byte[] dataToSend2 = Encoding.ASCII.GetBytes("PL106");//转成字节数组
                portCommunicator22.Write(dataToSend2);
                //}
                string PalletCode = portCommunicator21.ToString(Encoding.UTF8);
                using (var uow = _unitOfWorkManage.CreateUnitOfWork())
                {
                    try
                    {
                        if (PalletCode.IsNotEmptyOrNull())
                        {
                            Dt_Boxing dt_Boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
                            _boxingService.Repository.DeleteData(dt_Boxing);
                            List<Dt_BoxingDetail> dt_BoxingDetails = _boxingDetailService.Repository.QueryData(x => x.BoxingId == dt_Boxing.Id);
                            foreach (Dt_BoxingDetail dt_BoxingDetail in dt_BoxingDetails)
                            {
                                _boxingDetailService.Repository.DeleteDataById(dt_BoxingDetail.Id);
                            }
                            uow.Commit();
                            //下线扫码完成
                            device.SetValue(W_PLCDBName.wboolLocation4ScanDone, true);
                        }
                        IsConnected21 = !portCommunicator21.Disconnect();
                        IsConnected22 = !portCommunicator22.Disconnect();
                    }
                    catch (Exception ex)
                    {
                        WriteError(device.DeviceName, "下线扫码流程执行异常", ex);
                    }
                }
            }
            else if (!Location4ScanStart && Location4ScanDone)
            {
                device.SetValue(W_PLCDBName.wboolLocation2ScanDone, false);
            }
        }
    }
}