From d3ae2e67d8237e8474c7b88f526619550608b933 Mon Sep 17 00:00:00 2001
From: yanjinhui <3306209981@qq.com>
Date: 星期四, 13 三月 2025 21:51:20 +0800
Subject: [PATCH] 通信

---
 项目代码/WIDESEAWCS_Server 正式/WIDESEAWCS_Tasks/SerialPort/SerialPortTestJob.cs |  292 +++++++++++++++++++++++++--------------------------------
 1 files changed, 129 insertions(+), 163 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortTestJob.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortTestJob.cs"
index 352f942..7ed38b0 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortTestJob.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_Tasks/SerialPort/SerialPortTestJob.cs"
@@ -1,177 +1,143 @@
-锘縰sing System.IO.Ports;
-using System.Text;
-using Quartz;
-using WIDESEA_ISerialPortService;
-using WIDESEAWCS_Model.Models.SerialPort;
+锘�//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
 
-            //涓插彛鎺ユ敹鍒版暟鎹椂锛孲erialPort 浼氳嚜鍔ㄨЕ鍙� 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琛ㄤ腑锛屽苟涓斿湪杩欏垽鏂璽orqueop琛ㄤ腑鐨勬暟鎹畇ize鍜屾楠わ紝鏄惁婊¤冻宸ヨ壓琛ㄦ墍瑕佹眰鐨�
+//                    //_orqueOpRepository.
 
-        // 鑾峰彇涓插彛鐘舵��
-        public bool GetSerialPortStatus()
-        {
-            return SerialPort.IsOpen;
-        }
-
-        // 鑾峰彇鎺ユ敹鍒扮殑鎵�鏈夋暟鎹�
-        public List<string> GetReceivedData()
-        {
-            return receivedDataList;
-        }
-
-        // 澶勭悊鎺ユ敹鍒扮殑鏁版嵁
-        //姣忓綋涓插彛鏀跺埌鏁版嵁鏃讹紝浼氳Е鍙戞鏂规硶
-        private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
-        {
-            //浠庝覆鍙f帴鏀舵暟鎹紝骞跺皢鎺ユ敹鍒扮殑鏁版嵁杞崲涓哄瓧绗︿覆
-            byte[] buffer = new byte[1024];//涓�涓瓧鑺傛暟缁� buffer锛屽ぇ灏忎负 1024 瀛楄妭
-            int bytesRead = SerialPort.Read(buffer, 0, buffer.Length);//浠庝覆鍙f祦 SerialPort 涓鍙栨暟鎹苟灏嗗叾瀛樺叆 buffer 鏁扮粍涓��
-            string receivedData = Encoding.ASCII.GetString(buffer, 0, bytesRead);//灏嗘帴鏀跺埌鐨勫瓧鑺傛暟鎹浆鎹㈡垚涓�涓瓧绗︿覆
-
-            //杩欓噷鍐欎竴涓柟娉曠敤浜庡垽鏂瘡娆eceivedDat鏄惁婊¤冻鏉′欢
-            //鎴彇璁惧缂栧彿鍚庣殑淇╀负鏄惁涓�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;
-                    //鍏堝垽鏂璫ommandType
-                    if (commandType == 1)
-                    {
-                        command = command1;
-                    }
-                    else if (commandType == 2)
-                    {
-                        command = command2;
-                    }
-
-
-                    //鍐嶆鏌� command 瀛楃涓叉槸鍚︿负绌烘垨 null
-                    if (!string.IsNullOrEmpty(command))
-                    {
-                        byte[] dataToSend = Encoding.ASCII.GetBytes(command);//灏哻ommand杞垚瀛楄妭鏁扮粍
-                        //锛堜笁涓弬鏁扮殑鍚箟锛�1鏁扮粍锛�2浠庣0浣嶅紑濮嬫埅鍙栵紝3鏄鍙戦�佺殑瀛楄妭鏁帮紝杩欓噷琛ㄧず鏁翠釜瀛楄妭鏁扮粍鐨勯暱搴︼級
-                        SerialPort.Write(dataToSend, 0, dataToSend.Length);//鏈�鍚庡悜涓插彛鍙戦�佹暟鎹�
-
-                    }
-
-                    Thread.Sleep(1000); // 姣忛殧1绉掑彂閫佷竴娆″懡浠�
-                }
-            });
-        }
-
-       
-    }
-}
\ No newline at end of file
+//                }
+//            }
+//        }
+//    }
+//}

--
Gitblit v1.9.3