From 4583eb27bb19a984a517c4d6234c024eada905f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83?= <15095123350@163.com> Date: Mon, 24 Jun 2024 11:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E7=A0=81=E6=9E=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Khd.Core.Wcs/MainCentralControl.cs | 14 +-- src/Khd.Core.Wcs/Wcs/FiveFloorCTU.cs | 56 ++++++++---- src/Khd.Core.Wpf/Form/FormBoardT.xaml.cs | 62 ++++++++++--- src/Khd.Core.Wpf/Global/PlcConfig.cs | 6 +- src/Khd.Core.Wpf/Khd.Core.Wpf.csproj | 1 + src/Khd.Core.Wpf/Scan/SerialPortHelper.cs | 103 ++++++++++++++++++++++ src/Khd.Core.Wpf/SystemData.cs | 55 ++++++++++++ 7 files changed, 258 insertions(+), 39 deletions(-) create mode 100644 src/Khd.Core.Wpf/Scan/SerialPortHelper.cs create mode 100644 src/Khd.Core.Wpf/SystemData.cs diff --git a/src/Khd.Core.Wcs/MainCentralControl.cs b/src/Khd.Core.Wcs/MainCentralControl.cs index df08b63..ef0d880 100644 --- a/src/Khd.Core.Wcs/MainCentralControl.cs +++ b/src/Khd.Core.Wcs/MainCentralControl.cs @@ -101,19 +101,19 @@ namespace Khd.Core.Wcs //FiveFloorLine fiveFloorLine = new(_host, StaticData.PlcDic[0], FiveFloorLineEquip.floorNo.Value, FiveFloorLineEquip.equipNo); //fiveFloorLine.StartPoint(); - ////五层CTU - //var FiveFloorCTUEquip = StaticData.BaseEquip.Where(t => t.floorNo == 5 && t.equipType == 6).First(); - //FiveFloorCTU fiveFloorCTU = new(_host, StaticData.PlcDic[0], FiveFloorCTUEquip.floorNo.Value, FiveFloorCTUEquip.equipNo); - //fiveFloorCTU.StartPoint(); + //五层CTU + var FiveFloorCTUEquip = StaticData.BaseEquip.Where(t => t.floorNo == 5 && t.equipType == 6).First(); + FiveFloorCTU fiveFloorCTU = new(_host, StaticData.PlcDic[0], FiveFloorCTUEquip.floorNo.Value, FiveFloorCTUEquip.equipNo); + fiveFloorCTU.StartPoint(); ////五层AGV //var FifthFloorAgvEquip = StaticData.BaseEquip.Where(t => t.floorNo == 5 && t.equipType == 5).First(); //FiveFloorAGV fifthFloorAGV = new(_host, StaticData.PlcDic[0], FifthFloorAgvEquip.floorNo.Value, FifthFloorAgvEquip.equipNo); //fifthFloorAGV.StartPoint(); - var FifthFloorBearAgvEquip = StaticData.BaseEquip.Where(t => t.floorNo == 5 && t.equipType == 11).First(); - FiveFloorBearAgv fiveFloorBearAgv = new(_host, StaticData.PlcDic[0], FifthFloorBearAgvEquip.floorNo.Value, FifthFloorBearAgvEquip.equipNo); - fiveFloorBearAgv.StartPoint(); + //var FifthFloorBearAgvEquip = StaticData.BaseEquip.Where(t => t.floorNo == 5 && t.equipType == 11).First(); + //FiveFloorBearAgv fiveFloorBearAgv = new(_host, StaticData.PlcDic[0], FifthFloorBearAgvEquip.floorNo.Value, FifthFloorBearAgvEquip.equipNo); + //fiveFloorBearAgv.StartPoint(); } catch (Exception ex) { diff --git a/src/Khd.Core.Wcs/Wcs/FiveFloorCTU.cs b/src/Khd.Core.Wcs/Wcs/FiveFloorCTU.cs index 59eba9b..e50ac2d 100644 --- a/src/Khd.Core.Wcs/Wcs/FiveFloorCTU.cs +++ b/src/Khd.Core.Wcs/Wcs/FiveFloorCTU.cs @@ -167,7 +167,7 @@ namespace Khd.Core.Wcs.Wcs new() { positionCode = wcsCmd.nextPointNo, - type = "05" + type = "00" }, }, ctnrTyp = "1", @@ -213,12 +213,12 @@ namespace Khd.Core.Wcs.Wcs new() { positionCode = wcsCmd.currPointNo, - type = "0" + type = "05" }, new() { positionCode = wcsCmd.nextPointNo, - type = "0" + type = "05" }, }, ctnrTyp = "1", @@ -246,19 +246,45 @@ namespace Khd.Core.Wcs.Wcs { if (wcsCmd.cmdStatus == 3) { - var agvTask = new RequestAGVTaskDto + var canOut = this._plc.Read(""); + if (canOut != null && Convert.ToInt64(canOut) == 1) { - reqCode = StaticData.SnowId.NextId().ToString(), - taskCode = wcsCmd.taskCode - }; - string message = JsonConvert.SerializeObject(agvTask); - string result = HttpHelper.HttpPost("http://172.16.12.24:8182/rcms/services/rest/hikRpcService/continueTask", message); - ReponseMessage? reponseMessage = JsonConvert.DeserializeObject(result); - if (reponseMessage != null && reponseMessage.message == "成功") - { - wcsCmd.cmdStatus = 4; - dbContext.Update(wcsCmd); - dbContext.SaveChanges(); + if (item.taskType == 30) + { + var agvTask = new + { + reqCode = StaticData.SnowId.NextId().ToString(), + taskCode = wcsCmd.taskCode, + type = 2 + }; + string message = JsonConvert.SerializeObject(agvTask); + string result = HttpHelper.HttpPost("http://172.16.12.24:8182/rcms/services/rest/hikRpcService/boxApplyPass", message); + ReponseMessage? reponseMessage = JsonConvert.DeserializeObject(result); + if (reponseMessage != null && reponseMessage.message == "成功") + { + wcsCmd.cmdStatus = 4; + dbContext.Update(wcsCmd); + dbContext.SaveChanges(); + } + } + else if(item.taskType ==29) + { + var agvTask = new + { + reqCode = StaticData.SnowId.NextId().ToString(), + taskCode = wcsCmd.taskCode, + type = 1 + }; + string message = JsonConvert.SerializeObject(agvTask); + string result = HttpHelper.HttpPost("http://172.16.12.24:8182/rcms/services/rest/hikRpcService/boxApplyPass", message); + ReponseMessage? reponseMessage = JsonConvert.DeserializeObject(result); + if (reponseMessage != null && reponseMessage.message == "成功") + { + wcsCmd.cmdStatus = 4; + dbContext.Update(wcsCmd); + dbContext.SaveChanges(); + } + } } } else if (wcsCmd.cmdStatus == 5) diff --git a/src/Khd.Core.Wpf/Form/FormBoardT.xaml.cs b/src/Khd.Core.Wpf/Form/FormBoardT.xaml.cs index 0d1ab31..4e45165 100644 --- a/src/Khd.Core.Wpf/Form/FormBoardT.xaml.cs +++ b/src/Khd.Core.Wpf/Form/FormBoardT.xaml.cs @@ -1,31 +1,21 @@ using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; -using Khd.Core.Plc.S7.Types; -using Masuit.Tools.Logging; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Win32; -using OfficeOpenXml; using System; using System.Collections.Generic; using System.Data; -using System.IO; using System.Linq; -using System.Text.RegularExpressions; -using System.Threading; using System.Windows; using System.Windows.Controls; -using System.Windows.Media; using System.Windows.Threading; -using Thrift.Protocol; -using Thrift.Server; -using Thrift.Transport; -using ThriftService; using Z.EntityFramework.Plus; -using Jc.SnowId; using Masuit.Tools; using System.Windows.Input; +using Khd.Core.Wpf.Scan; +using System.IO.Ports; +using System.Threading; namespace Khd.Core.Wpf.Form { @@ -42,6 +32,8 @@ namespace Khd.Core.Wpf.Form private DispatcherTimer ShowOrderMessage;//呈现PLC机柜信息 private object order_code; private int UpState;//对应上件站点的状态,0为良好 1为损坏 + private SerialPortModel serialPortModel; + private SerialPortHelper serialPortHelper; List basePlcpoints = new List(); object timerjilu; public class barcodeinfo @@ -55,6 +47,37 @@ namespace Khd.Core.Wpf.Form public List barcodeLsit = new List(); + private void ScanMessage() + { + while (true) + { + try + { + if (SystemData.isUpdate) + { + if (!string.IsNullOrEmpty(SystemData.message)) + { + Dispatcher.Invoke(() => + { + var focusedElement = FocusManager.GetFocusedElement(this); + if (focusedElement is TextBox textBox) + { + textBox.Text = SystemData.message; + SystemData.isUpdate = false; + } + }); + //检测到哪个文本框获取到了焦点 + } + } + } + catch + { + + } + Thread.Sleep(500); + } + } + private string selectedId; //FormShowSelect formSelect; // public static int WeekNo = CommonHelper.WeekOfYear(DateTime.Now, new System.Globalization.CultureInfo("zh-CN")); @@ -79,6 +102,18 @@ namespace Khd.Core.Wpf.Form MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight; WindowState = WindowState.Maximized; } + serialPortModel = new SerialPortModel(); + serialPortModel.PortName = "COM7"; + serialPortModel.BaudRate = 9600; + serialPortModel.DataBits = 8; + serialPortModel.Parity = Parity.None; + serialPortModel.StopBits = StopBits.One; + serialPortHelper = new SerialPortHelper(); + serialPortHelper.OpenMyConn(serialPortModel); + + Thread scanThread = new Thread(ScanMessage); + scanThread.IsBackground = true; + scanThread.Start(); //加载dategrid信息 //LoadMaterial_GetMessage(""); //连接PLC判断 @@ -375,7 +410,6 @@ namespace Khd.Core.Wpf.Form MessageBox.Show("请输入有效的数字"); } } - /// /// 料箱扫描 /// diff --git a/src/Khd.Core.Wpf/Global/PlcConfig.cs b/src/Khd.Core.Wpf/Global/PlcConfig.cs index 7d63cd7..8cc45ba 100644 --- a/src/Khd.Core.Wpf/Global/PlcConfig.cs +++ b/src/Khd.Core.Wpf/Global/PlcConfig.cs @@ -34,8 +34,8 @@ namespace Khd.Core.Wpf public static int UpFlagNum { get; set; } } public static class Global - { - public static JcSnowId SnowId = new JcSnowId(); -} + { + public static JcSnowId SnowId = new JcSnowId(); + } } diff --git a/src/Khd.Core.Wpf/Khd.Core.Wpf.csproj b/src/Khd.Core.Wpf/Khd.Core.Wpf.csproj index 69c62cc..c6ac65f 100644 --- a/src/Khd.Core.Wpf/Khd.Core.Wpf.csproj +++ b/src/Khd.Core.Wpf/Khd.Core.Wpf.csproj @@ -141,6 +141,7 @@ + diff --git a/src/Khd.Core.Wpf/Scan/SerialPortHelper.cs b/src/Khd.Core.Wpf/Scan/SerialPortHelper.cs new file mode 100644 index 0000000..2954b05 --- /dev/null +++ b/src/Khd.Core.Wpf/Scan/SerialPortHelper.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Khd.Core.Wpf.Scan +{ + /// + /// 串口帮助类 + /// + public class SerialPortHelper + { + /// + /// 定义委托 + /// + /// + public delegate void ShowMesgDelegate(string info); + + public string message = ""; + //缓冲信息 + public string ReceiveData = ""; + private SerialPort myCom = new SerialPort();//串口对象 + /// + /// 串口属性 + /// + public SerialPort MyCom + { + get + { + return myCom; + } + set + { + myCom = value; + } + } + + private byte myReceiveByte = 0;//接收字节 + private byte[] bData = new byte[1024];//接收的字节数组 + private int index = 0; + + /// + /// 打开串口 + /// + /// + public void OpenMyConn(SerialPortModel entiry) + { + if (myCom.IsOpen) + { + myCom.Close();//关闭 + } + myCom.PortName = entiry.PortName;//串口名称 + myCom.BaudRate = entiry.BaudRate;//波特率 + myCom.DataBits = entiry.DataBits;//数据位 + myCom.StopBits = entiry.StopBits;//停止位 + myCom.Parity = entiry.Parity;//校验位 + myCom.ReceivedBytesThreshold = 1;//设置串口缓冲区的字节数 + myCom.DataReceived += MyCom_DataReceived;//串口接收数据事件 + myCom.Open();//打开串口 + } + /// + /// 串口接收事件 + /// + /// + /// + private void MyCom_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + //读取串口缓冲区的字节数据,分两次传递结果 + byte[] result = new byte[MyCom.BytesToRead]; + MyCom.Read(result, 0, MyCom.BytesToRead); + ReceiveData += Encoding.UTF8.GetString(result); + if (ReceiveData.Contains("\r")) + { + message = ReceiveData; + SystemData.message = message; + ReceiveData = ""; + } + SystemData.isUpdate = true; + } + + /// + /// 关闭串口 + /// + public void CloseMycom() + { + if (MyCom.IsOpen) + { + MyCom.Close();//关闭 + } + } + } + + public class SerialPortModel + { + public string PortName { get; set; } + public int BaudRate { get; set; } + public int DataBits { get; set; } + public Parity Parity { get; set; } + public StopBits StopBits { get; set; } + } +} diff --git a/src/Khd.Core.Wpf/SystemData.cs b/src/Khd.Core.Wpf/SystemData.cs new file mode 100644 index 0000000..3d8da34 --- /dev/null +++ b/src/Khd.Core.Wpf/SystemData.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Khd.Core.Wpf +{ + public class SystemData + { + public static string _message { get; set; } + + public static object _lock = new object(); + + public static string message + { + get + { + lock (_lock) + { + return _message; + } + } + set + { + lock (_lock) + { + _message = value; + } + } + } + + public static bool _isUpdate { get; set; } + + public static object _updateLock = new object(); + + public static bool isUpdate + { + get + { + lock (_updateLock) + { + return _isUpdate; + } + } + set + { + lock (_updateLock) + { + _isUpdate = value; + } + } + } + } +}