mengshunguo 1 неделя назад
Родитель
Сommit
6a1c7f38c8
1 измененных файлов с 255 добавлено и 0 удалено
  1. 255 0
      SCADA_DAQ/Customer/Machines/SampleMachine.cs

+ 255 - 0
SCADA_DAQ/Customer/Machines/SampleMachine.cs

@@ -3,13 +3,19 @@ using SCADA;
 using SCADA.CommonLib;
 using SCADA.CommonLib.Data.DIL;
 using SCADA.Drive;
+using SCADA_DAQ.Customer.Models;
 using SCADA_DAQ.Plugin.Core.Service;
 using SCADA_DAQ.Plugin.Machine.Device;
 using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
+using System.IO.Ports;
+using System.IO;
 using System.Linq;
 using System.Runtime.Remoting.Services;
+using System.Text;
+using System.Threading;
 using System.Windows;
 
 namespace SCADA_DAQ.Customer.Machines
@@ -25,6 +31,8 @@ namespace SCADA_DAQ.Customer.Machines
         /// </summary>
         public ObservableCollection<Alarm> AlarmCollection { get; set; } = new ObservableCollection<Alarm>();
 
+        public event EventHandler<TestFinishEventArgs> TestFinish;
+
         /// <summary>
         /// 
         /// </summary>
@@ -52,12 +60,259 @@ namespace SCADA_DAQ.Customer.Machines
             if (RedundancyService?.RedundancyState == RedundancyStates.Standby) { return; }  //如果是待机模式就不执行下面的逻辑
             base.RegReadValueChanged(sender, e);
             var reg = (RegInfo)sender;
+            if (reg.Comment.Variable == "步骤" && reg.Value == 5)
+            {
+                Thread.Sleep(2000);
+                ReadInfo("71");
+            }
+
             //if (reg.Comment.Variable == "Sys_1minPlus")
             //{
             //    Log.Info($"寄存器变化:{reg.Comment.Variable},value:{reg.Value}");
             //}
         }
 
+        public async void ReadInfo(string uid)
+        {
+            var rfidInfo = Env.DAL.App_RFID_Binding.GetData<RFIDBindingModel>(t => t.UID == uid)?.FirstOrDefault();
+            if (int.TryParse(rfidInfo?.StationCode, out var station))
+            {
+                List<byte> data = null;
+                for (int i = 0; i < 2; i++)
+                {
+
+
+                    using (var serialcomm = new SerialPort()
+                    {
+                        PortName = CustomerEnv.CustomerConfig.SerialModel.PortName,
+                        BaudRate = CustomerEnv.CustomerConfig.SerialModel.BaudRate,
+                        DataBits = CustomerEnv.CustomerConfig.SerialModel.DataBits,
+                        StopBits = CustomerEnv.CustomerConfig.SerialModel.StopBits,
+                        Parity = CustomerEnv.CustomerConfig.SerialModel.Parity,
+                        ReadTimeout = CustomerEnv.CustomerConfig.SerialModel.Timeout,
+                        ReadBufferSize = 102400
+                    })
+                    {
+                        var readeBuff = new List<byte>();
+                        var isReadSuccess = false;
+                        var now = DateTime.UtcNow;
+                        serialcomm.Open();
+                        try
+                        {
+                            serialcomm.Write(new byte[] { 0x00, (byte)station, 0x17, 0x01 }, 0, 4); //收到数据
+                            while ((DateTime.UtcNow - now).TotalMilliseconds < CustomerEnv.CustomerConfig.SerialModel.Timeout)
+                            {
+                                while (serialcomm.BytesToRead > 0)
+                                {
+                                    var buff = new byte[serialcomm.BytesToRead];
+                                    serialcomm.Read(buff, 0, buff.Length);
+                                    readeBuff.AddRange(buff);
+                                }
+                                if (readeBuff.Count > 4)
+                                {
+                                    if (readeBuff[0] == 0xAA && readeBuff[1] == 0xBB &&
+                                        readeBuff[readeBuff.Count - 2] == 0xFF && readeBuff[readeBuff.Count - 1] == 0xFF)
+                                    {
+                                        isReadSuccess = true;
+                                        break;
+                                    }
+                                }
+                                Thread.Sleep(100);
+                            }
+                        }
+                        catch (Exception)
+                        {
+
+
+                        }
+
+                        if (isReadSuccess)
+                        {
+                            data = readeBuff;
+                            serialcomm.Write(new byte[] { 0x00, (byte)station, 0x17, 0x02 }, 0, 4); //收到数据
+                            break;
+                        }
+
+                        if (data == null)
+                        {
+
+                            if (readeBuff.Count <= 0)
+                            {
+                                Log.Warn($"工位:{rfidInfo.StationCode},第{i + 1}次数据读取失败");
+                                if (i == 1)
+                                {
+                                    string strInfo = $"工位:{rfidInfo.StationCode},第{i + 1}次数据读取失败";
+                                    var filePath = $"NotreceivedNGData\\{rfidInfo.StationCode}_{DateTime.Now.ToString("yyyyMMddhhmmss")}.txt";
+                                    SCADA.CommonLib.Helper.FileHelper.EnsureDirectoryExist(filePath);
+                                    using (var fs = new FileStream(filePath, FileMode.Create))
+                                    {
+                                        var str = Encoding.UTF8.GetBytes(strInfo);
+                                        fs.Write(str, 0, str.Length);
+                                    }
+                                }
+                            }
+                            else
+                            {
+                                var filePath = $"NgData\\{rfidInfo.UID}_{i + 1}_{DateTime.Now.ToString("yyyyMMddhhmmss")}.txt";
+                                Log.Warn($"第{i + 1}次读取收到数据了数据,但是缺少结尾,数据已输出到:{filePath}");
+                                SCADA.CommonLib.Helper.FileHelper.EnsureDirectoryExist(filePath);
+                                using (var fs = new FileStream(filePath, FileMode.Create))
+                                {
+                                    var str = readeBuff.ByteToHexStr();
+                                    fs.Write(Encoding.ASCII.GetBytes(str).ToArray(), 0, str.Length);
+                                }
+                            }
+                            serialcomm.Write(new byte[] { 0x00, (byte)station, 0x17, 0x03 }, 0, 4); //未收到数
+                        }
+                        Thread.Sleep(1000);
+                        serialcomm.Dispose();
+                    }
+                }
+
+                if (data?.Count > 0)
+                {
+                    if ((data.Count - 6 - 2) % 6 != 0)
+                    {
+                        int secondFFIndex = 0;
+                        List<byte> dataByteList = null;
+                        try //有出现两段重复报文数据,提取第一段报文数据
+                        {
+                            for (int i = 0; i < data.Count; i++)
+                            {
+                                if (i + 3 <= data.Count - 1 && data[i] == 255 && data[i + 1] == 255 && data[i + 2] == 0 && data[i + 3] == 23)
+                                {
+                                    secondFFIndex = i + 1;
+                                    break;
+                                }
+                            }
+                            dataByteList = data.Take(secondFFIndex + 1).ToList();
+                        }
+                        catch (Exception)
+                        {
+
+                        }
+
+                        if ((dataByteList.Count - 6 - 2) % 6 == 0)
+                        {
+                            data = dataByteList ?? data;
+                        }
+                        else
+                        {
+                            string strInfo = $"工位:{rfidInfo.StationCode}, 数据长度不正确";
+                            Log.Warn(strInfo);
+                            var filePath = $"LengthNGData\\{rfidInfo.StationCode}_{DateTime.Now.ToString("yyyyMMddhhmmss")}.txt";
+                            SCADA.CommonLib.Helper.FileHelper.EnsureDirectoryExist(filePath);
+                            using (var fs = new FileStream(filePath, FileMode.Create))
+                            {
+                                var str = data.ByteToHexStr();
+                                fs.Write(Encoding.ASCII.GetBytes(str).ToArray(), 0, str.Length);
+                            }
+                        }
+                    }
+                    if ((data.Count - 6 - 2) % 6 == 0)
+                    {
+                        var points = new List<ProcessData>();
+                        data = data.Skip(6).Take(data.Count - 8).ToList();
+                        for (int i = 0; i < data.Count; i += 6)
+                        {
+                            var model = new ProcessData();
+                            model.DataDecode(data.ToArray(), i);
+                            points.Add(model);
+                        }
+                        //if (points.Count <= 90 && CustomerEnv.CustomerConfig.SetPower.IsSimulationDta) //当时长不够时进行模拟数据赋值
+                        //{
+                        //    points = ProcessData.SimulationData();
+                        //}
+
+                        #region 测试结果判断
+                        var MinNGNumber = points.Where(point => point.Power < CustomerEnv.CustomerConfig.SetPower.MinPower
+                       && point.Power != 0).ToList().Count();
+                        var MaxNGNumber = points.Where(point => point.Power > CustomerEnv.CustomerConfig.SetPower.MaxPower
+                        && point.Power != 0).ToList().Count();
+                        var okNumber = points.Where(point => point.Power >= CustomerEnv.CustomerConfig.SetPower.MinPower
+                        && point.Power <= CustomerEnv.CustomerConfig.SetPower.MaxPower).ToList().Count();
+                        var testResult = TestResult.NG;
+                        if (MinNGNumber < CustomerEnv.CustomerConfig.SetPower.MinNGNo && MaxNGNumber < CustomerEnv.CustomerConfig.SetPower.MaxNGNo &&
+                            okNumber >= CustomerEnv.CustomerConfig.SetPower.OKNo && rfidInfo.Barcode?.Length == 22)
+                        {
+                            testResult = TestResult.OK;
+                        }
+                        #endregion
+
+                        var product = new ProductModel()
+                        {
+                            Barcode = rfidInfo.Barcode,
+                            RFID = rfidInfo.UID,
+                            StationCode = rfidInfo.StationCode,
+                            ProcessDatas = points,
+                            RunTime = points.Count,
+                            CreateTime = DateTime.Now,
+                            TestResult = testResult,
+                            RunTimeStr = $"{points.Count} S",
+                        };
+
+                        Log.Info($"RFID{uid},产品条码{rfidInfo.Barcode},收到{product?.ProcessDatas?.Count}");
+                        TestFinish?.Invoke(this, new TestFinishEventArgs() { Product = product });
+
+                        if (rfidInfo.Barcode != null && rfidInfo.Barcode != "" && product.ProcessDatas.Count > 0 && CustomerEnv.MESAPITokenExpired > DateTime.Now &&
+                            CustomerEnv.MESServerConnectStatus && CustomerEnv.MESAPIToken != null && CustomerEnv.CustomerConfig.SetPower.MESUpload)
+                        {
+                            try
+                            {
+                                var result = CustomerEnv.MESUploadProductData(product);
+                                if (result.IsError)
+                                {
+                                    Log.Info($"产品{product.Barcode}过程数据上传MES失败,报错信息:{result.Message}");
+                                }
+                                else
+                                {
+                                    product.IsSynced = true;
+                                    Log.Info($"产品{product.Barcode}过程数据上传成功");
+                                }
+                            }
+                            catch (Exception)
+                            {
+                                Log.Warn("调取MES接口时发生错误");
+                            }
+                        }
+
+                        Env.DAL.Insert(product);
+                        foreach (var item in points)
+                        {
+                            item.ReportId = product.ID;
+                        }
+                        Env.DAL.Insert(points);
+                    }
+                }
+                else
+                {
+                    string strInfo = $"工位:{rfidInfo.StationCode}, 在获取测试数据时没有收到有效数据";
+                    Log.Warn(strInfo);
+                    var filePath = $"ThreeNGData\\{rfidInfo.StationCode}_{DateTime.Now.ToString("yyyyMMddhhmmss")}.txt";
+                    SCADA.CommonLib.Helper.FileHelper.EnsureDirectoryExist(filePath);
+                    using (var fs = new FileStream(filePath, FileMode.Create))
+                    {
+                        var str = Encoding.UTF8.GetBytes(strInfo);
+                        fs.Write(str, 0, str.Length);
+                    }
+                }
+                //CustomerEnv.RFIDBindingList.RemoveAll(i => i.StationCode == uid);
+            }
+
+            else
+            {
+                Log.Info($"RFID {uid} 维护的站号信息不正确");
+            }
+            if (rfidInfo != null)
+            {
+                Env.DAL.App_RFID_Binding.Update(rfidInfo, t => new
+                {
+                    t.UID
+                }, t => new { t.Barcode });   //解绑条码
+            }
+        }
+
+
         /// <summary>
         /// 扫描完成事件
         /// </summary>