//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(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(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