yanjinhui
2025-03-11 6eaa4ce46292314450fb725f67f27774945865cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//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;
 
 
//                }
//            }
//        }
//    }
//}