From 1d92f653e2bf1174d5d1d101ee0bbbd2103ecf4a Mon Sep 17 00:00:00 2001 From: yanjinhui <3306209981@qq.com> Date: 星期三, 12 三月 2025 08:34:41 +0800 Subject: [PATCH] 通信 --- 项目代码/WIDESEAWCS_Server 正式/WIDESEAWCS_Tasks/SerialPort/SerialPortJob.cs | 236 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 199 insertions(+), 37 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/SerialPortJob.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/SerialPortJob.cs" index 40462f4..72412de 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/SerialPortJob.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/SerialPortJob.cs" @@ -1,68 +1,230 @@ 锘縰sing System; using System.Collections.Generic; +using System.Drawing.Imaging; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; +using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; +using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using Quartz; +using SqlSugar; +using StackExchange.Profiling.Internal; +using WIDESEA_ISerialPortRepository; +using WIDESEA_SerialPortRepository; +using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; +using WIDESEAWCS_QuartzJob.DTO; +using WIDESEAWCS_QuartzJob.StackerCrane.Enum; +using WIDESEAWCS_Tasks.ConveyorLineJob; +using WIDESEAWCS_Tasks.StackerCraneJob; namespace WIDESEAWCS_Tasks { - public class SerialPortJob : JobBase, IJob + [DisallowConcurrentExecution] + public class SerialPortJob : JobBase, IJob, IDisposable { + public void Dispose() + { + GC.SuppressFinalize(this); + } + public enum CommandType { None = 0, Get = 1, Set = 2, } - private SerialPort serialPort; - private CommandType commandType = CommandType.None; - private string commandGet = "";//璇诲�� - private string commandSet = "";//璁惧�� - private string setOK = ""; - private string getOK = ""; + + public enum CommandResult + { + SetOK = 0, + GetOK = 1, + GetError = 2, + } + private readonly IPutakeRepository _putakeRepository; + private readonly IProcessRepository _processRepository; + private readonly ITorqueOpRepository _orqueOpRepository; + public SerialPortJob(IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository) + { + _putakeRepository = putakeRepository; + _processRepository = processRepository; + _orqueOpRepository = torqueOpRepository; + } + public Task Execute(IJobExecutionContext context) { try { - if (serialPort == null) { serialPort = new SerialPort("COM1"); serialPort.DataReceived += SerialPort_DataReceived;} - if (!serialPort.IsOpen) + SerialPortDevice serialPortDevice = (SerialPortDevice)context.JobDetail.JobDataMap.Get("JobParams"); + if (serialPortDevice != null) { - serialPort.Open(); + List<DeviceProDTO> deviceProDTOs = serialPortDevice.DeviceProDTOs; + foreach (var item in deviceProDTOs) + { + if (item.DeviceProParamName != CommandType.Get.ToString() && item.DeviceProParamName != CommandType.Set.ToString()) + { + DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set)); + if (deviceProtocolDetail != null) + { + //鎴戣鍓嶇杩斿洖缁欐垜涓�涓粍鍒紝鍜屼换鍔d + //鍏堟煡鐘舵�佸拰缁勶紝鎸夋椂闂撮檷搴� + string group = "鐢垫皵"; + + + //杩欓噷鍒ゆ柇缁勫埆涓変釜鐪嬫槸閭d釜锛屽氨鏌ラ偅涓伐鑹鸿〃鐨勫�� + if (group=="鐢垫皵") + { + + //鍏堢湅宸ヨ壓琛ㄤ腑姝ラ涓�涓鎵殑鍊兼湁澶氬皯涓� 鍐嶆煡鐪婦t_TorqueOp琛ㄤ腑鏄惁鏈夎浠诲姟鍙穒d鍜屾楠ゆ�绘暟鏄惁婊¤冻宸ヨ壓琛� + // 鏌ヨ鎵�鏈夊伐鑹烘暟鎹紝鎸夌収姝ラ鍙� SetpNum 閫掑鎺掑簭 + var processList = _processRepository.QueryData(x => x.CraftType == group) + .OrderBy(x => x.SetpNum) + .ToList(); - serialPort.Write(commandSet);//鎵撳紑涓插彛鏃跺厛璁惧�� - commandType = CommandType.Set; + foreach (var process in processList) + { + float torqueone = process.TorqueOne;//璇诲埌鍊间簡 + float torquetow = process.TorqueTwo; + if (torqueone!=0) + { + serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (torqueone * 1000).ToString().PadLeft(7, '0')) + "\r"); + } + if (torquetow != 0) + { + serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (torquetow * 1000).ToString().PadLeft(7, '0')) + "\r"); + } + + // 璁$畻褰撳墠姝ラ鎵�闇�鐨� Torque 鎬绘暟 + int requiredTorqueCount = process.TorqueOneQuantity + process.TorqueTwoQuantity; + + // 鏌ヨ褰撳墠姝ラ鐨� TorqueOp 璁板綍锛屽尮閰� ProcessSte鍜岀粍锛屾鏌ユ槸鍚﹁揪鍒拌姹� + int torqueCount = _orqueOpRepository.QueryData(x => x.ProcessSte == process.SetpNum&& x.GroupOp== group).Count(); + + if (torqueCount < requiredTorqueCount) + { + // 濡傛灉鏁伴噺涓嶆弧瓒筹紝鍋滄璇诲彇 + + break; + } + //绗﹀悎瑕佹眰锛岀户缁鍙栦笅涓�姝� + + } + + } + if (group == "鏈烘") + { + + } + if (group == "鍦版矡") + { + + } + //鎵撳紑涓插彛鏃跺厛璁惧�� + item.DeviceProParamName = CommandType.Set.ToString(); + } + } + + if (CommandType.Get.ToString() == item.DeviceProParamName) + { + DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Get)); + //鍙戦�佽澶囧彿 + if (deviceProtocolDetail != null) + { + serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//鎵撳紑涓插彛鏃跺厛璁惧�� + } + } + else if (CommandType.Set.ToString() == item.DeviceProParamName) + { + DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set)); + //璁惧�� + if (deviceProtocolDetail != null) + { + serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//鎵撳紑涓插彛鏃跺厛璁惧�� + } + } + + + + if (serialPortDevice.Communicator.Buffers.Count > 0) + { + string? receiveData = serialPortDevice.Communicator.ToString(Encoding.Default); + if (!string.IsNullOrEmpty(receiveData)) + { + Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + receiveData); + + if (item.DeviceProParamName == CommandType.Set.ToString()) + { + DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.SetOK)); + //parseok + if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue)) + { + item.DeviceProParamName = CommandType.Get.ToString(); + + + + + } + } + else if (item.DeviceProParamName == CommandType.Get.ToString()) + { + DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetOK)); + //03鎴愬姛 + + if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue)) + { + //杩欓噷鎴愬姛涔嬪悗锛岃瀛樺偍鍒扮湅Dt_TorqueOp锛堝瓨浠诲姟鍙凤紝缁勶紝鍊硷級 + string group = ""; + if (group == "鐢垫皵") + { + var putake = _putakeRepository.QueryData(x => x.Pustatus == 2 && x.Grouptype == group).OrderByDescending(x => x.Dispatchtime).FirstOrDefault();//姣忔閮芥嬁鏈�鏃╃殑閭f潯 + var punjid = putake.ID; + var Nj = new Dt_TorqueOp() + { + TakeId = putake.Njtakeid, + GroupOp = group, + //ProcessSte= processList + TorqueSize = receiveData + + }; + _orqueOpRepository.AddData(Nj); + + } + if (group == "鏈烘") + { + + } + if (group == "鍦版矡") + + + item.DeviceProParamName = CommandType.Set.ToString(); + + } + } + else if (item.DeviceProParamName == CommandType.Get.ToString()) + { + DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError)); + if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue)) + { + item.DeviceProParamName = CommandType.Set.ToString(); + + + + } + } + } + } + } + } - switch (commandType) - { - case CommandType.Get: - serialPort.Write(commandGet); - break; - case CommandType.Set: - serialPort.Write(commandSet); - break; - } + } - catch (Exception ex) { } return Task.CompletedTask; - } - private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) - { - byte[] buffers = new byte[1024]; - while (serialPort.BytesToRead > 0) + catch (Exception ex) { - 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) - { - commandType = CommandType.Set; - } + WriteError("CommonConveyorLineJob", "test", ex); } + WriteDebug("CommonConveyorLineJob", "test"); + return Task.CompletedTask; } } } -- Gitblit v1.9.3