yanjinhui
2025-02-28 880766ff5ccc98f1def11cd3c6e15e860f143ada
ÏîÄ¿´úÂë/WIDESEAWCS_Server Õýʽ/WIDESEAWCS_Tasks/SerialPort/SerialPortTestJob.cs
ÎļþÃû´Ó ÏîÄ¿´úÂë/WIDESEAWCS_Server Õýʽ/WIDESEAWCS_Tasks/SerialPort/SerialPortTestService.cs ÐÞ¸Ä
@@ -1,38 +1,54 @@
using System.IO.Ports;
using System.Text;
using Quartz;
using WIDESEA_ISerialPortService;
using WIDESEAWCS_Model.Models.SerialPort;
namespace WIDESEA_SerialPortTestService
namespace WIDESEAWCS_Tasks
{
    public class SerialPortTestService
    public class SerialPortTestJob:IJob
    {
        // åœ¨ SerialPortTestService ç±»ä¸­æ·»åР坹 ISerialPortService çš„引用
        private readonly ISerialPortService _serialPortService;
        public readonly string _port;
        public SerialPort SerialPort { get; set; } =null!; //表示要打开的串口,可为空
        // æž„造函数
        public SerialPortTestService(string port, ISerialPortService _serialPortService)
        public SerialPortTestJob(ISerialPortService serialPortService)
        {
            SerialPort = new SerialPort(port);
            _serialPortService = serialPortService;
            _port = "COM3";
            SerialPort = new SerialPort(_port);
            //串口接收到数据时,SerialPort ä¼šè‡ªåŠ¨è§¦å‘ DataReceived äº‹ä»¶
            SerialPort.DataReceived += OnDataReceived; //接收串口发出的数据
            //SerialPort.DataReceived += OnDataReceived; //接收串口发出的数据
        }
        private CancellationTokenSource _cancellationTokenSource;
        private Task _sendTask;
        private int commandType = 2;
        public SerialPort SerialPort { get; set; } = null!; //表示要打开的串口,可为空
        //// äº‹ä»¶ - ç”¨äºŽé€šçŸ¥æ•°æ®æŽ¥æ”¶åˆ°
        //public event Action<string> DataReceivedEvent;
        // å‘½ä»¤å­—符串
        private string command0 = "";  // ä¸å‘送命令
        private string command1 = "824070241JNT\r"; //发送信号
        private string command2 = "824070241JNT,0004000,+0001000[+TOL],-0001000[-TOL]\r"; //设值
        private string command2 = "824070241JNT,0005000,+0001000[+TOL],-0001000[-TOL]\r"; //设值
        public Task Execute(IJobExecutionContext context)
        {
            OpenSerialPort();
            SerialPort.DataReceived += OnDataReceived;
            return Task.CompletedTask;
        }
        // ç”¨äºŽå­˜å‚¨æŽ¥æ”¶åˆ°çš„æ•°æ®
        private List<string> receivedDataList = new List<string>();
@@ -112,7 +128,7 @@
           receivedDataList.Add(receivedData);
           
@@ -160,5 +176,7 @@
                }
            });
        }
    }
}