qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
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
using Castle.Core.Internal;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_WCS.EquipBaseInfo;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.SchedulerExecute.AGV
{
    public partial class AGVSchedulerExecute
    {
        private static int _readSendAGVTaskSignalso = 0;
        private static bool isTrue1 = false;
        private static string name1 = "";
 
        public static void SendAGVTask1(PLCClient plcClient)
        {
            //if (Interlocked.Exchange(ref _readSendAGVTaskSignalso, 1) == 0)
            //{
            try
            {
                if (plcClient==null)
                {
                    return;
                }
                VOLContext context = new VOLContext();
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                int TaskInteractive = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));//1收到
                int TaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()));//0初始/1下发/2取消/3更改
                int resetTaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveW.ToString()));
                int resetTaskInteractiver = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveR.ToString()));
                if (isTrue1 && name1 == plcClient.PLCName)
                {
                    if (TaskInteractiveW != 0 || resetTaskInteractiveW != 0 || TaskInteractive != 0)
                    {
                        plcClient.WriteValue(TaskDBName.resetTaskInteractiveW.ToString(), 1);
                        Task.Delay(2000).Wait();
                        resetTaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveW.ToString()));
                        if (resetTaskInteractiveW == 1)
                        {
                            plcClient.WriteValue(TaskDBName.resetTaskInteractiveW.ToString(), 0);
                            plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                            Task.Delay(2000).Wait();
                        }
                        return;
                    }
                    else
                    {
                        isTrue1 = false;
                        name1 = "";
                    }
                }
                if (resetTaskInteractiver == 1)
                {
                    plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                    for (int i = 0; i < 5; i++)
                    {
                        Thread.Sleep(300);
                        var agvnumber = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()));
                        if (agvnumber != 0)
                        {
                            plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (0 == TaskInteractive && TaskInteractiveW == 0)
                {
                    dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_taskstate == AGVTaskStateEnum.Create.ToString() && r.agv_code == plcClient.PLCName && r.agv_tasktype.Contains(AGVTaskTypeEnum.TaskType_Outbound.ToString())).OrderBy(r => r.agv_createtime).OrderByDescending(r => r.agv_grade).FirstOrDefault();
                    if (null != agvTask)
                    {
                        //写入任务信息
                        string rel = AGVBaseInfo.TaskWrite(plcClient, agvTask, AGVJobEnum.newTaskEnum);
                        /*2023.8.2新增 给驰众写入卷绕长度*/
                        if (agvTask?.agv_toaddress.Contains("JR") ?? false)
                        {
                            plcClient.SiemensPLCClient.SiemensS7NetClient.Write("DB1000.122", agvTask.size);
                            if (!string.IsNullOrEmpty(agvTask.JRunm))
                            {
                                plcClient.SiemensPLCClient.SiemensS7NetClient.Write("DB1000.78.0", agvTask.JRunm);
                                WriteLog.Info("SendAGVTask").Write("给AGV写入任务卷绕信息," + agvTask.size + agvTask.JRunm + DateTime.Now, "SendAGVTask");
                            }
                            else
                            {
                                WriteLog.Info("SendAGVTask").Write("给AGV写入任务卷绕信息,值为空"  + DateTime.Now, "SendAGVTask");
                            }
                            
                        }
                        #region
                        //if (agvTask.agv_fromaddress.Contains("JJK") && agvTask.agv_toaddress.Contains("TBHCJ"))
                        //{
                        //    string barcode = "TB-" + agvTask.agv_materbarcode.Split('*')[0].Substring(agvTask.agv_materbarcode.Split('*')[0].Length - 1, 1);
                        //    plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), barcode);
                        //    WriteLog.Info("materBarcode1").Write(agvTask.agv_tasknum+barcode + DateTime.Now, "materBarcode1");
                        //    for (int i = 0; i < 10; i++)
                        //    {
                        //        Thread.Sleep(1000);
                        //        string newbarcode = plcClient.ReadBarcodeValue(TaskDBName.materBarcode.ToString()).ToString();
                        //        if (newbarcode != barcode)
                        //        {
                        //            plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(),  barcode);
                        //            WriteLog.Info("materBarcode2").Write(agvTask.agv_tasknum + barcode  +newbarcode+ DateTime.Now, "materBarcode2");
                        //        }
                        //        else
                        //        {
                        //            break;
                        //        }
                        //    }
                        //}
                        //else
                        //{
                        //    plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), "0");
                        //    for (int i = 0; i < 10; i++)
                        //    {
                        //        Thread.Sleep(1000);
                        //        string newbarcode = plcClient.ReadBarcodeValue(TaskDBName.materBarcode.ToString()).ToString();
                        //        WriteLog.Info("materBarcode3").Write(agvTask.agv_tasknum  + newbarcode + DateTime.Now, "materBarcode3");
                        //        if (newbarcode!="0")
                        //        {
                        //          string barcode3= plcClient.WriteBarcodeValue(TaskDBName.materBarcode.ToString(), "0").ToString();
                        //            WriteLog.Info("materBarcode4").Write(agvTask.agv_tasknum + barcode3 + DateTime.Now, "materBarcode4");
                        //        }
                        //        else
                        //        {
                        //            break;
                        //        }
                        //    }
                        //}
                        #endregion
                        Thread.Sleep(1000);
                        string taskId = plcClient.ReadValue(TaskDBName.taskID.ToString()).ToString();
                        if (taskId != agvTask.agv_tasknum)
                        {
                            isTrue1 = true;
                            name1 = plcClient.PLCName;
                            WriteLog.Info("SendAGVTask").Write("给AGV写入任务失败," + agvTask.agv_tasknum + DateTime.Now, "SendAGVTask");
                            throw new Exception(string.Format("给AGV写入任务失败,任务号{0}", agvTask.agv_tasknum));
                        }
                        if (string.IsNullOrEmpty(rel))
                        {
                            //写入任务确认1
                            plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 1);
                            Thread.Sleep(1000);
                            //读取AGV信号
                            int ReadTask_1_OK = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));
                            if (ReadTask_1_OK == 1)
                            {
                                //写入WCS确认信号0
                                plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                                Thread.Sleep(2000);
                                //读取AGV信号是否为0
                                int ReadTask_0_OK = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));
                                if (ReadTask_0_OK == 0)
                                {
                                    agvTask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString();
                                    agvTask.agv_realesstime = DateTime.Now;
                                    agvtaskRepository.Update(agvTask, true);
                                    // 上传AGV运行数据 by xiaoyang
                                    SendMESTask.SendMesTask(agvTask, 0);
                                }
                                else
                                {
                                    isTrue1 = true;
                                    name1 = plcClient.PLCName;
                                    throw new Exception(string.Format("给AGV写入任务确认0失败,任务号{0}", agvTask.agv_tasknum));
                                }
                            }
                            else if (ReadTask_1_OK == 11)
                            {
                                plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                                Thread.Sleep(2000);
                                int ReadTask_0_Error = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));
                                if (ReadTask_0_Error == 0)
                                {
                                    agvTask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString();
                                    agvTask.agv_realesstime = DateTime.Now;
                                    agvtaskRepository.Update(agvTask, true);
                                }
                                else
                                {
                                    isTrue1 = true;
                                    name1 = plcClient.PLCName;
                                    throw new Exception(string.Format("给AGV写入任务异常确认0失败,任务号{0}", agvTask.agv_tasknum));
                                }
                            }
                            else
                            {
                                isTrue1 = true;
                                name1 = plcClient.PLCName;
                                throw new Exception(string.Format("给AGV写入任务确认1失败,任务号{0}", agvTask.agv_tasknum));
                            }
                        }
                        else
                        {
                            isTrue1 = true;
                            name1 = plcClient.PLCName;
                            throw new Exception(string.Format("给AGV写入任务失败,任务号{0}" + rel, agvTask.agv_tasknum));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                StackTrace sta = new StackTrace(ex, true);
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = sta.GetFrame(0);
                WriteLog.Info("SendAGVTask").Write(ex.Message+"行号"+ sf.GetFileLineNumber(), "SendAGVTask");
            }
            finally
            {
                // WriteLog.Info("SendAGVTask").Write(plcClient.PLCName+"\t"+DateTime.Now, "SendAGVTask");
                Interlocked.Exchange(ref _readSendAGVTaskSignalso, 0);
            }
        }
    }
}