| | |
| | | /*using System.IO.Ports; |
| | | using System.Text; |
| | | using Quartz; |
| | | using WIDESEA_ISerialPortService; |
| | | using WIDESEAWCS_Model.Models; |
| | | //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; |
| | | // } |
| | | |
| | | |
| | | namespace WIDESEAWCS_Tasks |
| | | { |
| | | public class SerialPortTestJob:IJob |
| | | { |
| | | // å¨ SerialPortTestService ç±»ä¸æ·»å 对 ISerialPortService çå¼ç¨ |
| | | private readonly ISerialPortService _serialPortService; |
| | | public readonly string _port; |
| | | public SerialPort SerialPort { get; set; } =null!; //è¡¨ç¤ºè¦æå¼ç串å£;å¯ä¸ºç©º |
| | | |
| | | // æé 彿° |
| | | public SerialPortTestJob(ISerialPortService serialPortService) |
| | | { |
| | | _serialPortService = serialPortService; |
| | | _port = "COM3"; |
| | | // 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 = ""; |
| | | |
| | | SerialPort = new SerialPort(_port); |
| | | // 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 |
| | | |
| | | //䏲壿¥æ¶å°æ°æ®æ¶ï¼SerialPort ä¼èªå¨è§¦å DataReceived äºä»¶ |
| | | //SerialPort.DataReceived += OnDataReceived; //æ¥æ¶ä¸²å£ååºçæ°æ® |
| | | } |
| | | |
| | | private CancellationTokenSource _cancellationTokenSource; |
| | | private Task _sendTask; |
| | | private int commandType = 2; |
| | | |
| | | // å½ä»¤å符串 |
| | | private string command0 = ""; // ä¸åéå½ä»¤ |
| | | private string command1 = "824070241JNT\r"; //åéä¿¡å· |
| | | private string command2 = "824070241JNT;0005000;+0001000[+TOL];-0001000[-TOL]\r"; //è®¾å¼ |
| | | |
| | | public Task Execute(IJobExecutionContext context) |
| | | { |
| | | |
| | | //OpenSerialPort(); |
| | | SerialPort.DataReceived += OnDataReceived; |
| | | |
| | | return Task.CompletedTask; |
| | | } |
| | | // try |
| | | // { |
| | | // if (serialPort == null) { serialPort = new SerialPort("COM3"); serialPort.DataReceived += SerialPort_DataReceived; } |
| | | // if (!serialPort.IsOpen) |
| | | // { |
| | | // serialPort.Open(); |
| | | |
| | | |
| | | // ç¨äºå卿¥æ¶å°çæ°æ® |
| | | private List<string> receivedDataList = new List<string>(); |
| | | |
| | | |
| | | // 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 = ""; |
| | | |
| | | // æå¼ä¸²å£å¹¶å¼å§æç»åéä¿¡å· |
| | | public void OpenSerialPort() |
| | | { |
| | | if (!SerialPort.IsOpen) |
| | | { |
| | | SerialPort.Open(); |
| | | StartSendingData();//å¼å¯åå°å¾ªç¯ |
| | | } |
| | | } |
| | | // var com = _nJCommandsRepository.QueryFirst(x => x.ID == 3); |
| | | // if (com != null) |
| | | // { |
| | | // getOK = string.Format("{0}03", com.DeviceNum);// "82407024103" |
| | | |
| | | // å
³é串å£å¹¶åæ¢åéä¿¡å· |
| | | public void CloseSerialPort() |
| | | { |
| | | if (SerialPort.IsOpen) |
| | | { |
| | | SerialPort.Close(); |
| | | } |
| | | } |
| | | // getError = string.Format("{0}04", com.DeviceNum);// "82407024104" |
| | | // } |
| | | // else |
| | | // { |
| | | // throw new Exception("æªæ¾å°æå®çå½ä»¤æ°æ®ï¼"); |
| | | // } |
| | | // #endregion |
| | | |
| | | // åéæ°æ®ä¸æ¬¡ |
| | | public void SendData(string data) |
| | | { |
| | | if (SerialPort.IsOpen) |
| | | { |
| | | byte[] bytesToSend = Encoding.ASCII.GetBytes(data); |
| | | SerialPort.Write(bytesToSend; 0; bytesToSend.Length);//å°æ°æ®åéå°ä¸²å£ |
| | | } |
| | | } |
| | | // 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. |
| | | |
| | | // è·å串å£ç¶æ |
| | | public bool GetSerialPortStatus() |
| | | { |
| | | return SerialPort.IsOpen; |
| | | } |
| | | |
| | | // è·åæ¥æ¶å°çæææ°æ® |
| | | public List<string> GetReceivedData() |
| | | { |
| | | return receivedDataList; |
| | | } |
| | | |
| | | // å¤çæ¥æ¶å°çæ°æ® |
| | | //æ¯å½ä¸²å£æ¶å°æ°æ®æ¶ï¼ä¼è§¦åæ¤æ¹æ³ |
| | | private void OnDataReceived(object sender; SerialDataReceivedEventArgs e) |
| | | { |
| | | //ä»ä¸²å£æ¥æ¶æ°æ®ï¼å¹¶å°æ¥æ¶å°çæ°æ®è½¬æ¢ä¸ºå符串 |
| | | byte[] buffer = new byte[1024];//ä¸ä¸ªåèæ°ç» bufferï¼å¤§å°ä¸º 1024 åè |
| | | int bytesRead = SerialPort.Read(buffer; 0; buffer.Length);//ä»ä¸²å£æµ SerialPort ä¸è¯»åæ°æ®å¹¶å°å
¶åå
¥ buffer æ°ç»ä¸ã |
| | | string receivedData = Encoding.ASCII.GetString(buffer; 0; bytesRead);//å°æ¥æ¶å°çåèæ°æ®è½¬æ¢æä¸ä¸ªå符串 |
| | | |
| | | //è¿éåä¸ä¸ªæ¹æ³ç¨äºå¤ææ¯æ¬¡receivedDatæ¯å¦æ»¡è¶³æ¡ä»¶ |
| | | //æªå设å¤ç¼å·åç俩为æ¯å¦ä¸º03ï¼03æåå°±è¿è¡ä¸æ¥ |
| | | string deviceCodePostfix = receivedData.Substring(9; 2); // ç´¢å¼ä» 9 å¼å§ï¼é¿åº¦ä¸º 2ï¼å³æå "04" |
| | | // æåä»ç¬¬24ä½åçæ°æ®ï¼ä¾å¦ "0008000"ï¼ |
| | | string dataAfter24th = receivedData.Substring(13; 7); // ä»ç¬¬23ä½å¼å§æå7个å符ï¼å¾å° "0008000" |
| | | if (deviceCodePostfix == "03") |
| | | { |
| | | //æå
¥æ°æ® |
| | | Dt_TorqueOp torqueOp = new Dt_TorqueOp |
| | | { |
| | | TorqueSize = dataAfter24th |
| | | |
| | | }; |
| | | |
| | | _serialPortService.AddSerialPort(torqueOp); |
| | | |
| | | //receivedDataList.Add(receivedData); |
| | | |
| | | |
| | | } |
| | | // commandType = CommandType.Set; |
| | | // } |
| | | // else if (data.Contains(getError) && commandType == CommandType.Get) |
| | | // { |
| | | // commandType = CommandType.Set; |
| | | |
| | | |
| | | receivedDataList.Add(receivedData); |
| | | |
| | | |
| | | |
| | | |
| | | // å¤çå½ä»¤åæ¢é»è¾ |
| | | if (commandType == 2 && receivedData.Contains("ParseOK")) |
| | | { |
| | | commandType = 1; |
| | | } |
| | | else if (commandType == 1 && (receivedData.Contains("82407024103") || receivedData.Contains("82407024104"))) |
| | | { |
| | | commandType = 2; |
| | | } |
| | | } |
| | | |
| | | // å¯å¨åå°ä»»å¡ï¼æ¯é䏿®µæ¶é´åéä¿¡å· |
| | | private void StartSendingData() |
| | | { |
| | | _cancellationTokenSource = new CancellationTokenSource(); |
| | | _sendTask = Task.Run(() => |
| | | { |
| | | while (SerialPort.IsOpen) |
| | | { |
| | | string command = command0; |
| | | //å
夿commandType |
| | | if (commandType == 1) |
| | | { |
| | | command = command1; |
| | | } |
| | | else if (commandType == 2) |
| | | { |
| | | command = command2; |
| | | } |
| | | |
| | | |
| | | //åæ£æ¥ command å符串æ¯å¦ä¸ºç©ºæ null |
| | | if (!string.IsNullOrEmpty(command)) |
| | | { |
| | | byte[] dataToSend = Encoding.ASCII.GetBytes(command);//å°command转æåèæ°ç» |
| | | //ï¼ä¸ä¸ªåæ°çå«ä¹ï¼1æ°ç»ï¼2ä»ç¬¬0ä½å¼å§æªåï¼3æ¯è¦åéçåèæ°ï¼è¿é表示æ´ä¸ªåèæ°ç»çé¿åº¦ï¼ |
| | | SerialPort.Write(dataToSend; 0; dataToSend.Length);//æåå串å£åéæ°æ® |
| | | |
| | | } |
| | | |
| | | Thread.Sleep(1000); // æ¯é1ç§åé䏿¬¡å½ä»¤ |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | } |
| | | }*/ |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | //} |