yanjinhui
2025-03-07 aeb32ca2cc420266734c782df01b27be617e6943
ÏîÄ¿´úÂë/WIDESEAWCS_Server Õýʽ/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs
@@ -9,6 +9,7 @@
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class SerialPortJob : JobBase, IJob
    {
        public enum CommandType
@@ -17,20 +18,23 @@
            Get = 1,
            Set = 2,
        }
        private SerialPort serialPort;
        private CommandType commandType = CommandType.None;
        private string commandGet = "";//读值
        private string commandSet = "";//设值
        private string setOK = "";
        private string getOK = "";
        private static SerialPort serialPort;
        private static CommandType commandType = CommandType.None;
        private string commandGet = "824070241JNT\r";//读值
        private string commandSet = "824070241JNT,0005000,+0001000[+TOL],-0001000[-TOL]\r";//设值
        private string setOK = "ParseOK";
        private string getOK = "82407024103";
        private string getError = "82407024104";
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                if (serialPort == null) { serialPort = new SerialPort("COM1"); serialPort.DataReceived += SerialPort_DataReceived;}
                if (serialPort == null) { serialPort = new SerialPort("COM3"); serialPort.DataReceived += SerialPort_DataReceived; }
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                    serialPort.Write(commandSet);//打开串口时先设值
                    commandType = CommandType.Set;
@@ -45,7 +49,11 @@
                        break;
                }
            }
            catch (Exception ex) { }            return Task.CompletedTask;
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return Task.CompletedTask;
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
@@ -61,6 +69,14 @@
                else if (data.Contains(getOK) && commandType == CommandType.Get)
                {
                    commandType = CommandType.Set;
                }
                else if (data.Contains(getError) && commandType == CommandType.Get)
                {
                    commandType = CommandType.Set;
                }
            }
        }