From e2483160a90f6415b90267047288df1be4f14a27 Mon Sep 17 00:00:00 2001 From: liuwf Date: Thu, 7 Mar 2024 19:02:02 +0800 Subject: [PATCH] =?UTF-8?q?change-=E6=88=90=E5=93=81=E4=B8=8B=E7=BA=BF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=AB=E7=A0=81=E6=9E=AA=EF=BC=8C=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=89=AB=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Business/GunBusiness.cs | 140 ++++++++++++++++++ .../Business/offLineBusiness.cs | 62 ++++---- Aucma.Core.ProductOffLine/Config/AppConfig.cs | 10 +- .../ViewModels/LoginViewModel.cs | 2 +- .../ViewModels/MainWindowViewModel.cs | 33 ++--- .../Views/ChooseDirectionWindow.xaml | 31 ++-- .../Views/ChooseDirectionWindow.xaml.cs | 30 +++- .../Views/IndexPageView.xaml | 2 +- Aucma.Core.ProductOffLine/Views/Login.xaml | 2 +- Aucma.Core.ProductOffLine/Views/Login.xaml.cs | 5 + 10 files changed, 253 insertions(+), 64 deletions(-) create mode 100644 Aucma.Core.ProductOffLine/Business/GunBusiness.cs diff --git a/Aucma.Core.ProductOffLine/Business/GunBusiness.cs b/Aucma.Core.ProductOffLine/Business/GunBusiness.cs new file mode 100644 index 00000000..b00d2cf5 --- /dev/null +++ b/Aucma.Core.ProductOffLine/Business/GunBusiness.cs @@ -0,0 +1,140 @@ +using Admin.Core.Common; +using Aucma.Core.ProductOffLine.Config; +using Aucma.Core.Scanner; +using log4net; +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Aucma.Core.ProductOffLine.Business +{ + /// + /// 扫码枪补扫 + /// + public class GunBusiness + { + private static AppConfig appConfig = AppConfig.Instance; + private static SerialPort serialPort = new SerialPort(); + + private static List allScanners = Appsettings.app("ScannerServer").ToList(); + // 内胆扫码器ip + private static string LinerScannerIp = allScanners.First(x => x.Name == "内侧扫码器1").Ip; + // 箱壳扫码器ip + private static string ShellScannerIp = allScanners.First(x => x.Name == "外侧扫码器2").Ip; + + #region 单例实现 + private static readonly GunBusiness lazy = new GunBusiness(); + + /// + /// 扫码委托 + /// + /// + /// + public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip); + public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; + + public static GunBusiness Instance + { + get + { + return lazy; + } + } + #endregion + + + private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(GunBusiness)); + + + //初始化串口并启动接收数据 + public static void InstanceSerialPort3() + { + try + { + string port = System.IO.Ports.SerialPort.GetPortNames().FirstOrDefault(); + //实例化串行端口 + + //端口名 注:因为使用的是USB转RS232 所以去设备管理器中查看一下虚拟com口的名字 + serialPort.PortName = port;// portName; + //波特率 + serialPort.BaudRate = 115200; + //奇偶校验 + serialPort.Parity = Parity.None; + //停止位 + serialPort.StopBits = StopBits.One; + //数据位 + serialPort.DataBits = 8; + //忽略null字节 + serialPort.DiscardNull = true; + + //接收事件 + serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived); + + //开启串口 + serialPort.Open(); + } + catch (Exception ex) + { + logHelper.Error(ex.Message.ToString()); + } + } + + private static void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) + { + Thread.Sleep(50); + int nums = serialPort.BytesToRead; + byte[] receiveBytes = new byte[nums]; + serialPort.Read(receiveBytes, 0, nums); + + StringBuilder sb = new StringBuilder(); + + string str = Encoding.ASCII.GetString(receiveBytes); + string ip = DirectionToIP(); + if (!string.IsNullOrEmpty(ip)) + { + + RefreshMaterialCodeStrEvent?.Invoke(str,ip); + int direction = -1; + appConfig.GunDirection = direction.ToString(); + } + sb.Clear(); + } + + /// + /// 将用户选择的放行方向转换为对应位置的扫码器ip + /// + /// + private static string DirectionToIP() + { + try + { + + // 外侧方向转换为外侧扫码器ip + if (int.Parse(appConfig.GunDirection) == 0) + { + return ShellScannerIp; + } + // 内侧方向转换为内侧扫码器ip + else if (int.Parse(appConfig.GunDirection) == 1) + { + return LinerScannerIp; + } + else + { + return ""; + } + } + catch (Exception ex) + { + return ""; + } + + + } + + } +} diff --git a/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs b/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs index a6c3b68a..bc1d4965 100644 --- a/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs +++ b/Aucma.Core.ProductOffLine/Business/offLineBusiness.cs @@ -109,8 +109,10 @@ namespace Aucma.Core.ProductOffLine.Business public OffLineBusiness() { MvCodeHelper.RefreshMaterialCodeStrEvent += MaterialBarScan; + GunBusiness.RefreshMaterialCodeStrEvent += MaterialBarScan; ChooseDirectionWindow.SendPlcPassEvent += DownLoadPassFlag; - test(); + + //test(); } #region 变量定义 /// @@ -122,6 +124,15 @@ namespace Aucma.Core.ProductOffLine.Business public void test() { + + //Task.Run(() => + //{ + + // Thread.Sleep(5000); + + + //}); + // private static readonly log4net.ILog log = LogManager.GetLogger(typeof(OffLineBusiness)); // string result = _productOffLineServices.QueryChecked("1520870000099E2V0004"); // string result = _productOffLineServices.QueryChecked("1530080CD0098E1V1714"); @@ -129,33 +140,28 @@ namespace Aucma.Core.ProductOffLine.Business // string strSave = _productOffLineServices.SaveBarcodeInfo("1520870000099E2V0004", "ILS_SORT", result.Replace("0000",""), 1); // LogHelper.Info("成品码:1520870000099E2V0004"+strSave); // string tt = "Y@1104@16160030000000910780@000010034895@@ @000000@000000009000004899@BCD-160C,家电下乡@@BCD-160C@皓月白-家电下乡@161601300@160@1-00版@家电下乡产品@默认@2010-09-01"; - Task.Run(() => - { - //while (true) - //{ - Thread.Sleep(5000); - // string aa = appConfig.BarCodeAccount; - // string bb = appConfig.ProductlineCode; - // appConfig.BarCodeAccount = "123"; - //TempOffLineInfo1.QualityResult = "失败"; - //TempOffLineInfo1.ProductSNCode = "323222444"; //产品SN条码*1 - //TempOffLineInfo1.ProductScanTime = System.DateTime.Now; // 扫码时间*2 - //TempOffLineInfo1.MsgInfo = "条码质检失败:"; - //RefreshScanMateriaCodeEvent?.Invoke(TempOffLineInfo1); - // Random random = new Random(); - // MaterialBarScan("32160030000000912" + random.Next(100, 999), "10.10.92.141"); - - //Thread.Sleep(3000); - //Random random1 = new Random(); - //MaterialBarScan("99160030000000912" + random1.Next(100, 999), "10.10.92.141"); - //Thread.Sleep(3000); - //Random random2 = new Random(); - //MaterialBarScan("44160030000000912" + random2.Next(100, 999), "10.10.92.141"); - // } - - // Thread.Sleep(1000); - // MaterialBarScanEvent("33160030000000910780", "192.168.1.20"); - }); + + // string aa = appConfig.BarCodeAccount; + // string bb = appConfig.ProductlineCode; + // appConfig.BarCodeAccount = "123"; + //TempOffLineInfo1.QualityResult = "失败"; + //TempOffLineInfo1.ProductSNCode = "323222444"; //产品SN条码*1 + //TempOffLineInfo1.ProductScanTime = System.DateTime.Now; // 扫码时间*2 + //TempOffLineInfo1.MsgInfo = "条码质检失败:"; + //RefreshScanMateriaCodeEvent?.Invoke(TempOffLineInfo1); + // Random random = new Random(); + // MaterialBarScan("32160030000000912" + random.Next(100, 999), "10.10.92.141"); + + //Thread.Sleep(3000); + //Random random1 = new Random(); + //MaterialBarScan("99160030000000912" + random1.Next(100, 999), "10.10.92.141"); + //Thread.Sleep(3000); + //Random random2 = new Random(); + //MaterialBarScan("44160030000000912" + random2.Next(100, 999), "10.10.92.141"); + // } + + // Thread.Sleep(1000); + // MaterialBarScanEvent("33160030000000910780", "192.168.1.20"); } public void MaterialBarScan(string code, string scannerIp) diff --git a/Aucma.Core.ProductOffLine/Config/AppConfig.cs b/Aucma.Core.ProductOffLine/Config/AppConfig.cs index 7ee93185..0a14863f 100644 --- a/Aucma.Core.ProductOffLine/Config/AppConfig.cs +++ b/Aucma.Core.ProductOffLine/Config/AppConfig.cs @@ -52,6 +52,14 @@ namespace Aucma.Core.ProductOffLine.Config get { return iniHelper.IniReadValue("system", "LoginTeam"); } set { iniHelper.IniWriteValue("system", "LoginTeam", value); } } - + + /// + /// 扫码枪放行方向,0外侧,1内侧,-1都未选中 + /// + public string GunDirection + { + get { return iniHelper.IniReadValue("system", "GunDirection"); } + set { iniHelper.IniWriteValue("system", "GunDirection", value); } + } } } diff --git a/Aucma.Core.ProductOffLine/ViewModels/LoginViewModel.cs b/Aucma.Core.ProductOffLine/ViewModels/LoginViewModel.cs index 00938655..bbe0ee96 100644 --- a/Aucma.Core.ProductOffLine/ViewModels/LoginViewModel.cs +++ b/Aucma.Core.ProductOffLine/ViewModels/LoginViewModel.cs @@ -80,7 +80,7 @@ namespace Aucma.Core.ProductOffLine.ViewModels } - + /// /// 班组,二线白班,二线夜班 diff --git a/Aucma.Core.ProductOffLine/ViewModels/MainWindowViewModel.cs b/Aucma.Core.ProductOffLine/ViewModels/MainWindowViewModel.cs index 61d9846a..4c4d6c1d 100644 --- a/Aucma.Core.ProductOffLine/ViewModels/MainWindowViewModel.cs +++ b/Aucma.Core.ProductOffLine/ViewModels/MainWindowViewModel.cs @@ -58,26 +58,29 @@ namespace Aucma.Core.ProductOffLine.ViewModels Scanner2State(false); // Job_AllState_Quartz.RefreshStateEvent += RefreshStatus; - InitHikRobot(); + InitHikRobotAndGun(); + } - #region 开启海康扫码器 - public void InitHikRobot() + #region 开启海康扫码器和扫码枪 + public void InitHikRobotAndGun() { try { Task.Run(() => { Thread.Sleep(2000); - - //MvCodeHelper1.DeviceListAcq();//获取创建设备 - // MvCodeHelper1.StartGrab(); // 开启触发扫码接收数据 + + Business.MvCodeHelper.Shell(); }); Task.Run(() => { Thread.Sleep(4000); + // 启动扫码枪服务 + GunBusiness.InstanceSerialPort3(); + //更新plc状态 var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("成品下线Plc")); if (obj.plc.IsConnected) @@ -88,29 +91,17 @@ namespace Aucma.Core.ProductOffLine.ViewModels { PlcState(false); } - //MvCodeHelper1.DeviceListAcq();//获取创建设备 - // MvCodeHelper1.StartGrab(); // 开启触发扫码接收数据 + Business.MvCodeHelper.Liner(); }); - // 心跳线程 - //Task.Run(() => - //{ - // while (true) - // { - // //扫码器心跳监测并重连 - // MvCodeHelper.JudgeShellIsConnectedAndReConnect(); - // Thread.Sleep(2000); - // } - //}); - + } catch (Exception ex) { - //MvCodeHelper.CloseAllDevice(); - InitHikRobot(); + log.Error("InitHikRobotAndGun()开启海康扫码器和扫码枪方法异常" + ex.Message.ToString()); } } #endregion diff --git a/Aucma.Core.ProductOffLine/Views/ChooseDirectionWindow.xaml b/Aucma.Core.ProductOffLine/Views/ChooseDirectionWindow.xaml index dbec5e51..21e5d454 100644 --- a/Aucma.Core.ProductOffLine/Views/ChooseDirectionWindow.xaml +++ b/Aucma.Core.ProductOffLine/Views/ChooseDirectionWindow.xaml @@ -5,15 +5,28 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Aucma.Core.ProductOffLine.Views" mc:Ignorable="d" FontFamily="Microsoft YaHei" WindowStartupLocation="CenterScreen" - Title="选择放行方向" Height="300" Width="300"> + Title="选择放行方向" Height="500" Width="700"> + - - - - - - public partial class ChooseDirectionWindow : Window { - + private AppConfig appConfig = AppConfig.Instance; + + /// /// 下发plc放行信号,传进来扫码器编号,分垛方向A或者B,返回plc反馈结果 /// scannerNo == 1内侧,scannerNo == 2外侧 @@ -35,6 +39,16 @@ namespace Aucma.Core.ProductOffLine.Views public ChooseDirectionWindow() { InitializeComponent(); + // 外侧 + if (int.Parse(appConfig.GunDirection) == 0) + { + SelectedMaterialType.SelectedIndex = 0; + } + // 内侧 + if (int.Parse(appConfig.GunDirection) == 1) + { + SelectedMaterialType.SelectedIndex = 1; + } } private void LeftButton_Click(object sender, RoutedEventArgs e) @@ -73,5 +87,17 @@ namespace Aucma.Core.ProductOffLine.Views } } } + + + private void Button_Click(object sender, RoutedEventArgs e) + { + MessageBoxResult result = MessageBox.Show("确认保存吗?", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question); + if (result == MessageBoxResult.Yes) + { + Console.WriteLine(SelectedMaterialType.SelectedIndex); + appConfig.GunDirection = SelectedMaterialType.SelectedIndex.ToString(); + this.Close(); + } + } } } diff --git a/Aucma.Core.ProductOffLine/Views/IndexPageView.xaml b/Aucma.Core.ProductOffLine/Views/IndexPageView.xaml index 54fbb4e2..a0027be3 100644 --- a/Aucma.Core.ProductOffLine/Views/IndexPageView.xaml +++ b/Aucma.Core.ProductOffLine/Views/IndexPageView.xaml @@ -216,7 +216,7 @@ -