yanjinhui
2025-03-25 308e1a141a5d89fd9d2f97cb0d7bcb258188ef02
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
//using System;
//using System.Collections.Generic;
//using System.IO.Ports;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
//using Quartz;
//using WIDESEA_ISerialPortRepository;
//using WIDESEA_SerialPortRepository;
//using WIDESEAWCS_Model.Models;
//using WIDESEAWCS_QuartzJob;
 
//namespace WIDESEAWCS_Tasks
//{
//    [DisallowConcurrentExecution]
//    public class SerialPortJob : JobBase, IJob
//    {
//        //dispatchinfo
//        //1    测试 OtherDevice WIDESEAWCS_Tasks SerialPortJob    1    NULL NULL    NULL    1    2025-02-19 14:55:02.167    NULL NULL
//        public enum CommandType
//        {
//            None = 0,
//            Get = 1,
//            Set = 2,
//        }
 
//        private readonly IPutakeRepository _putakeRepository;
//        private readonly INJCommandsRepository _nJCommandsRepository;
//        private readonly IProcessRepository _processRepository;
//        private readonly ITorqueOpRepository _orqueOpRepository;
//        public SerialPortJob(IPutakeRepository putakeRepository, INJCommandsRepository nJCommandsRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository)
//        {
//            _putakeRepository = putakeRepository;
//            _nJCommandsRepository = nJCommandsRepository;
//            _processRepository = processRepository;
//            _orqueOpRepository = torqueOpRepository;
//        }
 
 
 
//        private static SerialPort serialPort;
//        private static CommandType commandType = CommandType.None;
//        //private string commandGet = "824070241JNT\r";//读值
//        //private string commandSet = "824070241JNT,0012500,+0001000[+TOL],-0001000[-TOL]\r";//设值
//        private string setOK = "ParseOK";
//        //private string getOK = "82407024103";
//        //private string getError = "82407024104";
//        public Task Execute(IJobExecutionContext context)
//        {
//            #region 先查到这条命令
//            string commandSet = "";
//            string commandGet = "";
//            string getOK = "";
//            string getError = "";
 
//            var com = _nJCommandsRepository.QueryFirst(x => x.ID == 3);
//            if (com != null)
//            {
//                commandSet = string.Format("{0}JNT,{1},{2},{3}\r", com.DeviceNum, com.TorqueValue, com.TorqueMax, com.TorqueMin);//"824070241JNT,0005000,+0001000[+TOL],-0001000[-TOL]\r"
//                commandGet = string.Format("{0}\t", com.DeviceNum);// "824070241JNT\r"
//                getOK = string.Format("{0}03", com.DeviceNum);// "82407024103"
//                getError = string.Format("{0}04", com.DeviceNum);// "82407024104"
//            }
//            else
//            {
//                throw new Exception("未找到指定的命令数据!");
//            }
//            #endregion
 
//            try
//            {
//                if (serialPort == null) { serialPort = new SerialPort("COM3"); serialPort.DataReceived += SerialPort_DataReceived; }
//                if (!serialPort.IsOpen)
//                {
//                    serialPort.Open();
 
 
 
//                    serialPort.Write(commandSet);//打开串口时先设值
//                    commandType = CommandType.Set;
//                }
//                switch (commandType)
//                {
//                    case CommandType.Get://读值
//                        serialPort.Write(commandGet);
//                        break;
//                    case CommandType.Set://设值
//                        serialPort.Write(commandSet);
//                        break;
//                }
//            }
//            catch (Exception ex)
//            {
//                throw new Exception(ex.Message);
//            }
//            return Task.CompletedTask;
//        }
//        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
//        {
//            # region 先查到这条命令
//            string getError = "";
//            string getOK = "";
 
//            var com = _nJCommandsRepository.QueryFirst(x => x.ID == 3);
//            if (com != null)
//            {
//                getOK = string.Format("{0}03", com.DeviceNum);// "82407024103"
 
//                getError = string.Format("{0}04", com.DeviceNum);// "82407024104"
//            }
//            else
//            {
//                throw new Exception("未找到指定的命令数据!");
//            }
//            #endregion
 
//            byte[] buffers = new byte[1024];
//            while (serialPort.BytesToRead > 0)
//            {
//                serialPort.Read(buffers, 0, serialPort.BytesToRead > buffers.Length ? buffers.Length : serialPort.BytesToRead);
//                string data = Encoding.Default.GetString(buffers);
//                if (data.Contains(setOK) && commandType == CommandType.Set)
//                {
//                    commandType = CommandType.Get;
//                }
//                else if (data.Contains(getOK) && commandType == CommandType.Get) //成功就保存到数据库中
//                {
//                    //这里如果成了就存到数据库中的torqueop表中,并且在这判断torqueop表中的数据size和步骤,是否满足工艺表所要求的
//                    //_orqueOpRepository.
 
//                    commandType = CommandType.Set;
//                }
//                else if (data.Contains(getError) && commandType == CommandType.Get)
//                {
//                    commandType = CommandType.Set;
 
 
//                }
//            }
//        }
//    }
//}
#region
//if (group == "电气")
//{
//    //获取当前任务号  //IPutakeServer里面的待执行的第一条任务;
//    var take = _putakeRepository.QueryData(x => x.Grouptype == group)
//        .OrderBy(x => x.Dispatchtime)
//        .FirstOrDefault();
//    var takeid = take?.Njtakeid;//拿到任务号
 
//    //调用Getcircuit拿到当前步骤
//    var process = _processServer.Getcircuit(group, takeid);
 
//    if (process != null && process.Status)
//    {
//        var list = JsonConvert.SerializeObject(process.Data);
//        var data = JsonConvert.DeserializeObject<ProcessData>(list);
 
//        if (data?.proNow != null)
//        {
 
//            int setpNum = data.proNow.SetpNum;
//            int sum = data.proNow.TorqueSum;
//            double torqueone = data.proNow.TorqueOne;
//            double torquetwo = data.proNow.TorqueTwo;
 
//            int onequantity = data.proNow.TorqueOneQuantity;
//            int towquantity = data.proNow.TorqueTwoQuantity;
 
 
//            //判断op表中这个值是否有该条任务的数据
//            var op = _orqueOpRepository.QueryData(x => x.TakeId == takeid && x.GroupOp == group && x.ProcessSte == setpNum);
//            if (op.Any())//判断是否有数据
//            {
//                //找到了就要对比现在op表中有多少条,是否和工艺表中的目标一致
 
//                // 计算第一个扭矩的数量(在 ±1 误差范围内)//Math.Abs(5.2 - 4.5) = 0.7 // 计入 Math.Abs(6.0 - 4.5) = 1.5 不计入
//                var oponecount = op.Count(x => Math.Abs(x.TorqueSize - torqueone) <= 1);
 
//                int times = 0;
//                if (op.Count() > onequantity)
//                {
//                    times = onequantity;//如果第一条以及满足就移除onequantity数量的数据
//                }
//                else
//                {
//                    times = op.Count();//没有满足就移除当前的数据数据
//                }
 
//                for (int i = 0; i < times; i++)
//                {
//                    //移除第一次值的影响(以为有可能俩个目标值相近而产生误判)
//                    op.RemoveAt(0); //移除 op 列表的 第一个元素(即最早存入的记录)
 
//                }
 
//                // 计算第二个扭矩的数量(在 ±1 误差范围内)
//                var optowcount = op.Count(x => Math.Abs(x.TorqueSize - torquetwo) <= 1);
//                sum = oponecount + optowcount;
//                //第一种,第一个扭力值没有扭完
//                if (oponecount < onequantity && torqueone != 0)
//                {
//                    //设值
//                    //var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
//                    //        .Replace("[setNum]", (torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
//                    var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
//                              .Replace("[setNum]", Math.Round(torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
//                    //格式化成整数
 
//                    serialPortDevice.Communicator.Write(com);
//                    //WriteDebug("写入", com);
//                }
//                //第二种,第一个值扭完了,第二个没有扭完
//                if (oponecount == onequantity && optowcount < towquantity && torquetwo != 0)
//                {
//                    //设值
//                    var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
//                            .Replace("[setNum]", Math.Round(torquetwo * 1000).ToString().PadLeft(7, '0')) + "\r";
//                    serialPortDevice.Communicator.Write(com);
//                }
//            }
//            //这里这个任务第一次执行,op表中肯定是没有数据的,所有第一次就要把工艺表中的值给它
//            else //防止步骤一中torqueone的值为0导致报错
//            {
//                //设值
//                var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue
//                        .Replace("[setNum]", Math.Round(torqueone * 1000).ToString().PadLeft(7, '0')) + "\r";
//                serialPortDevice.Communicator.Write(com);
//            }
 
//        }
//        else
//        {
//            Console.WriteLine("proNow 为空");
//        }
//    }
//    else
//    {
//        Console.WriteLine($"Getcireit接口错误:{process?.Message}");
//    }
//}
 
#endregion
 
 
#region 根据不同的group存值
//if (group == "电气")
//{
//    var take = _putakeRepository.QueryData(x => x.Grouptype == group)
//                        .OrderBy(x => x.Dispatchtime)
//                        .FirstOrDefault();
//    var takeid = take?.Njtakeid;//拿到任务号
 
//    //调用Getcircuit拿到当前步骤
//    var process = _processServer.Getcircuit(group, takeid);
 
//    if (process != null && process.Status)
//    {
//        var list = JsonConvert.SerializeObject(process.Data);
//        var dataz = JsonConvert.DeserializeObject<ProcessData>(list);
 
//        if (dataz?.proNow != null)
//        {
 
//            int setpNum = dataz.proNow.SetpNum;
//            int sum = dataz.proNow.TorqueSum;
//            double torqueone = dataz.proNow.TorqueOne;
//            int onequantity = dataz.proNow.TorqueOneQuantity;
//            int towquantity = dataz.proNow.TorqueTwoQuantity;
//            double torquetwo = dataz.proNow.TorqueTwo;
//            Dt_TorqueOp Addop = new Dt_TorqueOp()
//            {
//                DeviceCode = devicecode,
//                TakeId = takeid,
//                GroupOp = group,
//                ProcessSte = setpNum,
//                TorqueSize = float.TryParse(torqueValue, out float torque) ? torque : 0.0f, // 这里进行字符串到float的转换
//                CreateDate = DateTime.Now,
//            };
//            _orqueOpRepository.AddData(Addop);
//        }
//    }
 
 
//    item.DeviceProParamName = CommandType.None.ToString();
//}
 
#endregion