1
dengjunjie
2025-12-25 3db3b2244255ca7eadc4babe1cc4430bc22cb46b
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
using Autofac.Core;
using HslCommunication;
using HslCommunication.Core;
using Microsoft.VisualBasic.FileIO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using Quartz;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core.Helper;
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;
        public PLCJob(IBoxingService boxingService, IBoxingDetailService boxingDetailService, IFormulaService formulaService, IFormulaDetailService formulaDetailService, IProcessInfoService processInfoService)
        {
            _boxingService = boxingService;
            _boxingDetailService = boxingDetailService;
            _formulaService = formulaService;
            _formulaDetailService = formulaDetailService;
            _processInfoService = processInfoService;
        }
        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"发送停止解码命令
        SerialPortCommunicator portCommunicator1 = new SerialPortCommunicator("COM1", 9600, "上线扫码枪");
        static bool IsConnected1 = false;
        SerialPortCommunicator portCommunicator2 = new SerialPortCommunicator("COM2", 9600, "上线扫码枪");
        static bool IsConnected2 = false;
        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 上线扫码
                            var Location1ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation1ScanStart);
                            var Location1ScanDone = device.GetValue<W_PLCDBName, bool>(W_PLCDBName.wboolLocation1ScanDone);
                            if (Location1ScanStart && !Location1ScanDone)//控制扫码枪扫描托盘码,根据托盘码查询组盘数据,判断托盘绑定成品是否已绑定所有所需零件
                            {
                                #region MyRegion
                                #region 串口连接
                                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);
                                if (PalletCode.IsNotEmptyOrNull())
                                {
                                    var boxing = _boxingService.Repository.QueryFirst(x => x.PalletCode == PalletCode);
                                }
                                IsConnected1 = !portCommunicator1.Disconnect();
                                IsConnected2 = !portCommunicator2.Disconnect();
                                #endregion
                                #endregion
 
                                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, true);
                            }
                            else if (!Location1ScanStart && Location1ScanDone)
                            {
                                device.SetValue(W_PLCDBName.wboolLocation1ScanDone, false);
                            }
                            #endregion
 
 
                            #region 压装扫码
                            var Location2ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation2ScanStart);
                            if (Location2ScanStart)//控制扫码枪扫描托盘码,根据托盘码查询托盘绑定成品加工所需数据,写入PLC
                            {
 
                            }
                            #endregion
 
                            #region 检测扫码
                            var Location3ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation3ScanStart);
                            #endregion
 
                            #region 下线扫码
                            var Location4ScanStart = device.GetValue<R_PLCDBName, bool>(R_PLCDBName.rboolLocation4ScanStart);
                            #endregion
                        }
 
 
                        //var a = _boxingService.Repository.QueryFirst(x => x.PalletCode == "1232123");
                        //WriteInfo(device.DeviceName, "infoLog");
 
                        //WriteDebug(device.DeviceName, "debugLog");
                    }
                    catch (Exception ex)
                    {
                        WriteError(device.DeviceName, "错误", ex);
                    }
                }
            }
            return Task.CompletedTask;
        }
    }
 
 
}