using Admin.Core.Common.Helper; using Admin.Core.IService; using Admin.Core.Model; using Aucma.Core.HwPLc; using log4net; using Microsoft.Extensions.DependencyInjection; using NetTaste; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using static NPOI.HSSF.Util.HSSFColor; namespace Aucma.Core.OldBoxFoam.Business { public class CollectionFoamMachine { private readonly log4net.ILog log = LogManager.GetLogger(typeof(CollectionFoamMachine)); private System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); private readonly IBoxLastShotRecordServices? _lastShotRecordServices = App.ServiceProvider.GetService(); public void startCollect() { try { Task.Run(() => { Thread.Sleep(5000); var obj_1 = PlcHelper.siemensList.FirstOrDefault(d => d.Id == 5); var obj_2 = PlcHelper.siemensList.FirstOrDefault(d => d.Id == 6); ReadFillingRecord(obj_1, obj_2); }); }catch(Exception ex) { log.Error($"发泡机参数采集异常:{ex.Message}"); } } /// /// 通过PLC读取注料温度和压力 /// /// /// /// private List ReadTempAndHpByPlc(PlcModel obj_1,PlcModel obj_2) { List boxLastShots = new List(); if (obj_1 != null) { #region 1#发泡机参数 byte[] info = obj_1.plc.Read("VD600", 50); var temp1 = byteToFloat(info.Skip(0).Take(4).ToArray()).ToString("0.00"); var temp2 = byteToFloat(info.Skip(4).Take(4).ToArray()).ToString("0.00"); var temp3 = byteToFloat(info.Skip(20).Take(4).ToArray()).ToString("0.00"); var temp4 = byteToFloat(info.Skip(24).Take(4).ToArray()).ToString("0.00"); byte[] phInfo = obj_1.plc.Read("VD200", 100); var pressure1 = byteToFloat(phInfo.Skip(0).Take(4).ToArray()).ToString("0.00"); var pressure2 = byteToFloat(phInfo.Skip(20).Take(4).ToArray()).ToString("0.00"); var pressure3 = byteToFloat(phInfo.Skip(40).Take(4).ToArray()).ToString("0.00"); var pressure4 = byteToFloat(phInfo.Skip(60).Take(4).ToArray()).ToString("0.00"); #endregion boxLastShots.Add(new BoxLastShotRecord() { ProductLineCode = "CX_01", StationNumber = "1105", System = "1", SystemStatus = 1, PolTemp = temp1.ToString(), IsoTemp = temp3.ToString(), GunCode = "A1", PolHp = pressure1.ToString(), IsoHp = pressure3.ToString(), CreateTime = DateTime.Now, }); boxLastShots.Add(new BoxLastShotRecord() { ProductLineCode = "CX_01", StationNumber = "1105", System = "1", SystemStatus = 1, PolTemp = temp1.ToString(), IsoTemp = temp3.ToString(), GunCode = "B1", PolHp = pressure2.ToString(), IsoHp = pressure4.ToString(), CreateTime = DateTime.Now, }); } if (obj_2 != null) { #region 2#发泡机参数 byte[] info = obj_1.plc.Read("VD600", 50); var temp1 = byteToFloat(info.Skip(0).Take(4).ToArray()).ToString("0.00"); var temp2 = byteToFloat(info.Skip(4).Take(4).ToArray()).ToString("0.00"); var temp3 = byteToFloat(info.Skip(20).Take(4).ToArray()).ToString("0.00"); var temp4 = byteToFloat(info.Skip(24).Take(4).ToArray()).ToString("0.00"); byte[] phInfo = obj_1.plc.Read("VD200", 100); var pressure1 = byteToFloat(phInfo.Skip(0).Take(4).ToArray()).ToString("0.00"); var pressure2 = byteToFloat(phInfo.Skip(20).Take(4).ToArray()).ToString("0.00"); var pressure3 = byteToFloat(phInfo.Skip(40).Take(4).ToArray()).ToString("0.00"); var pressure4 = byteToFloat(phInfo.Skip(60).Take(4).ToArray()).ToString("0.00"); #endregion boxLastShots.Add(new BoxLastShotRecord() { ProductLineCode = "CX_01", StationNumber = "1105", System = "2", SystemStatus = 1, PolTemp = temp1.ToString(), IsoTemp = temp3.ToString(), GunCode = "A2", PolHp = pressure1.ToString(), IsoHp = pressure3.ToString(), CreateTime = DateTime.Now, }); boxLastShots.Add(new BoxLastShotRecord() { ProductLineCode = "CX_01", StationNumber = "1105", System = "2", SystemStatus = 1, PolTemp = temp1.ToString(), IsoTemp = temp3.ToString(), GunCode = "B2", PolHp = pressure2.ToString(), IsoHp = pressure4.ToString(), CreateTime = DateTime.Now, }); } _lastShotRecordServices.AddAsync(boxLastShots); return boxLastShots; } private void ReadFillingRecord(PlcModel obj_1, PlcModel obj_2) { Task.Run(() => { while (true) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>开始读取1#发泡结束信号"); if (obj_1 != null) { if (obj_1.plc.ReadBool("I3.1")) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>1#发泡机A1枪结束信号"); ReadParamData(obj_1, "1", "A1"); Thread.Sleep(6000); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>1#发泡机A1枪结束逻辑处理完成"); } else if (obj_1.plc.ReadBool("I4.1")) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>1#发泡机B1枪结束信号"); ReadParamData(obj_1, "1", "B1"); Thread.Sleep(6000); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>1#发泡机B1枪结束逻辑处理完成"); } } Thread.Sleep(1000); } }); Task.Run(() => { while (true) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>开始读取2#发泡结束信号"); if (obj_2 != null) { if (obj_2.plc.ReadBool("I3.1")) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>2#发泡机A2枪结束信号"); ReadParamData(obj_2, "2", "A2"); Thread.Sleep(6000); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>2#发泡机B2枪结束逻辑处理完成"); } else if (obj_2.plc.ReadBool("I4.1")) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>2#发泡机B2枪结束信号"); ReadParamData(obj_2, "2", "B2"); Thread.Sleep(6000); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.ss")}===>2#发泡机B2枪结束逻辑处理完成"); } } Thread.Sleep(1000); } }); } /// /// 读取注料记录参数 /// /// /// /// private void ReadParamData(PlcModel obj,string systemCode,string gunCode) { try { lock (string.Empty) { Thread.Sleep(500); if (obj != null) { byte[] info = obj.plc.Read("VW1060", 40); var gun = asciiEncoding.GetString(info.Skip(2).Take(1).ToArray()).Replace("\0", "").Trim('\n'); var station = short.Parse(StringChange.bytesToHexStr(info.Skip(3).Take(1).ToArray(), 1), System.Globalization.NumberStyles.HexNumber); var setWeight = byteToFloat(info.Skip(4).Take(4).ToArray()).ToString("0.00"); var realWeight = byteToFloat(info.Skip(8).Take(4).ToArray()).ToString("0.00"); var mixingRatio = byteToFloat(info.Skip(12).Take(4).ToArray()).ToString("0.00"); var polHp = byteToFloat(info.Skip(16).Take(4).ToArray()).ToString("0.00"); var isoHp = byteToFloat(info.Skip(20).Take(4).ToArray()).ToString("0.00"); var hp = byteToFloat(info.Skip(24).Take(4).ToArray()).ToString("0.00"); var carryGunTime = short.Parse(StringChange.bytesToHexStr(info.Skip(28).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber); var closeGunTime = short.Parse(StringChange.bytesToHexStr(info.Skip(30).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber); string outPutAddress = $"{(gun == "A" ? "VW18" : "VW28")}{(station * 2).ToString().PadLeft(2, '0')}"; //log.Info($"读取{gun}枪{station}工位产量地址:{outPutAddress}"); var outPut = obj.plc.ReadInt16(outPutAddress); byte[] tempInfo = obj.plc.Read("VD600", 50); var temp1 = byteToFloat(tempInfo.Skip(0).Take(4).ToArray()).ToString("0.00"); var temp2 = byteToFloat(tempInfo.Skip(4).Take(4).ToArray()).ToString("0.00"); var temp3 = byteToFloat(tempInfo.Skip(20).Take(4).ToArray()).ToString("0.00"); var temp4 = byteToFloat(tempInfo.Skip(24).Take(4).ToArray()).ToString("0.00"); bool statusFlag_1 = obj.plc.ReadBool("Q0.1"); bool statusFlag_2 = obj.plc.ReadBool("Q0.2"); bool statusFlag_3 = obj.plc.ReadBool("Q0.3"); int statusFlag = 0; if (statusFlag_1) { statusFlag = 3; }else if(statusFlag_2) { statusFlag = 1; }else if(statusFlag_3) { statusFlag = 2; } //Console.WriteLine($"{systemCode}#状态:{statusFlag}"); var shotRecord = new BoxLastShotRecord() { ProductLineCode = "CX_01", StationNumber = "1105", System = systemCode, SystemStatus = statusFlag, PourNu = station.ToString(), GunCode = gunCode, SetWeight = setWeight, PourWeight = realWeight, PourRatio = mixingRatio, PolTemp = temp1.ToString(), IsoTemp = temp3.ToString(), PolHp = polHp.ToString(), IsoHp = isoHp.ToString(), HydrPress = hp, MixpistOn = carryGunTime.ToString(), MixpistOff = closeGunTime.ToString(), TotalYield = outPut.ToString(), CreateTime = DateTime.Now, }; _lastShotRecordServices.AddAsync(shotRecord); } } }catch(Exception ex) { log.Error($"注料记录数据采集异常:{ex.Message}"); } } /// /// byte数组转换为float /// private float byteToFloat(byte[] list) { float result = 0.00f; Array.Reverse(list); result = BitConverter.ToSingle(list, 0); return result; } } }