//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; // } // } // } // } //}