diff --git a/Aucma.Core.BoxFoam/Business/CollectionFoamMachine.cs b/Aucma.Core.BoxFoam/Business/CollectionFoamMachine.cs new file mode 100644 index 00000000..1e624859 --- /dev/null +++ b/Aucma.Core.BoxFoam/Business/CollectionFoamMachine.cs @@ -0,0 +1,255 @@ +using Admin.Core.Common.Helper; +using Admin.Core.IService; +using Admin.Core.Model; +using Aucma.Core.HwPLc; +using HwCommunication.Core; +using NetTaste; +using NPOI.Util; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Documents; + +namespace Aucma.Core.BoxFoam.Business +{ + /// + /// 采集发泡机数据 + /// + public class CollectionFoamMachine + { + private static System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); + + private static BoxLastShotRecord tempLastRecord = null; + public CollectionFoamMachine() + { + + } + public async void startCollect() + { + Task.Run(() => + { + Thread.Sleep(6000); + var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamMachinePlc")); + // ReadEquipSystem(obj, "DB50.388"); //采集1系统 + // ReadEquipSystem(obj, "DB50.642"); //采集2系统 + // ReadGunData(obj, "DB50.896"); //A1枪数据 + ReadGunData(obj, "DB50.1084"); //A1枪数据 + ReadGunData(obj, "DB50.1272"); //A1枪数据 + ReadGunData(obj, "DB50.1460"); //A1枪数据 + }); + } + + /// + /// 采集设备系统参数 + /// + /// + static void ReadEquipSystem(HwPLc.PlcModel obj,string startStr) + { + if (obj == null && !obj.plc.IsConnected) return; + if (obj.plc.IsConnected) + { + byte[] info = obj.plc.Read(startStr, 170); + int temp1 = judgeStatus(ByteArrayToBinary(info.Skip(0).Take(1).ToArray())); + int temp2 = judgeStatus(ByteArrayToBinary(info.Skip(1).Take(1).ToArray())); + float temp3 = byteToFloat(info.Skip(2).Take(4).ToArray()); + + float temp4 = byteToFloat(info.Skip(14).Take(4).ToArray()); + float temp5 = byteToFloat(info.Skip(18).Take(4).ToArray()); + float temp6 = byteToFloat(info.Skip(22).Take(4).ToArray()); + + float temp7 = byteToFloat(info.Skip(34).Take(4).ToArray()); + float temp8 = byteToFloat(info.Skip(38).Take(4).ToArray()); + float temp9 = byteToFloat(info.Skip(42).Take(4).ToArray()); + + float temp10 = byteToFloat(info.Skip(82).Take(4).ToArray()); + + float temp11 = byteToFloat(info.Skip(94).Take(4).ToArray()); + float temp12 = byteToFloat(info.Skip(98).Take(4).ToArray()); + float temp13 = byteToFloat(info.Skip(102).Take(4).ToArray()); + + float temp14 = byteToFloat(info.Skip(114).Take(4).ToArray()); + float temp15 = byteToFloat(info.Skip(118).Take(4).ToArray()); + float temp16 = byteToFloat(info.Skip(122).Take(4).ToArray()); + + float temp17 = byteToFloat(info.Skip(162).Take(4).ToArray()); + float temp18 = byteToFloat(info.Skip(166).Take(4).ToArray()); + + } + } + + /// + /// 每一枪数据 ---A1,B1,A2,B2 + /// + /// + static void ReadGunData(HwPLc.PlcModel obj, string startStr) + { + if (obj == null && !obj.plc.IsConnected) return; + if (obj.plc.IsConnected) + { + + byte[] info = obj.plc.Read(startStr, 88); + + float temp1 = byteToFloat(info.Skip(0).Take(4).ToArray()); + float temp2 = byteToFloat(info.Skip(4).Take(4).ToArray()); + float temp3 = byteToFloat(info.Skip(8).Take(4).ToArray()); + float temp4 = byteToFloat(info.Skip(12).Take(4).ToArray()); + float temp5 = byteToFloat(info.Skip(16).Take(4).ToArray()); + float temp6 = byteToFloat(info.Skip(20).Take(4).ToArray()); + float temp7 = byteToFloat(info.Skip(24).Take(4).ToArray()); + float temp8 = byteToFloat(info.Skip(28).Take(4).ToArray()); + + short temp9 = short.Parse(StringChange.bytesToHexStr(info.Skip(36).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber); + int temp10 = int.Parse(StringChange.bytesToHexStr(info.Skip(38).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber); + float temp11 = byteToFloat(info.Skip(42).Take(4).ToArray()); + float temp12 = byteToFloat(info.Skip(46).Take(4).ToArray()); + float temp13 = byteToFloat(info.Skip(50).Take(4).ToArray()); + float temp14 = byteToFloat(info.Skip(54).Take(4).ToArray()); + int temp15 = int.Parse(StringChange.bytesToHexStr(info.Skip(58).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber); + int temp16 = int.Parse(StringChange.bytesToHexStr(info.Skip(62).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber); + float temp17 = byteToFloat(info.Skip(66).Take(4).ToArray()); + float temp18 = byteToFloat(info.Skip(70).Take(4).ToArray()); + int temp19 = int.Parse(StringChange.bytesToHexStr(info.Skip(74).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber); + int temp20 = int.Parse(StringChange.bytesToHexStr(info.Skip(78).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber); + int temp21 = int.Parse(StringChange.bytesToHexStr(info.Skip(82).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber); + + int temp22 = judgeStatus(ByteArrayToBinary(info.Skip(87).Take(1).ToArray())); + } + } + + /// + /// 采集最后一枪数据 + /// + /// + public async Task CollectLast() + { + var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("Plc1")); + if (obj != null && obj.plc.IsConnected) + { + BoxLastShotRecord lastShotRecord = new BoxLastShotRecord(); + byte[] info = obj.plc.ReadBytes("DB50.896", (ushort)88); + IByteTransform byteTransform = new RegularByteTransform(); + + lastShotRecord.System = "1"; + lastShotRecord.GunCode = "A1"; + lastShotRecord.GunCode = "A1"; + #region 赋值 + lastShotRecord.PolTemp = Encoding.ASCII.GetString(info.Skip(0).Take(4).ToArray()); + lastShotRecord.PolHp = StringChange.bytesToHexStr(info.Skip(4).Take(4).ToArray(), 4); + lastShotRecord.PolVol = StringChange.bytesToHexStr(info.Skip(8).Take(4).ToArray(), 4); + lastShotRecord.PolUsage = StringChange.bytesToHexStr(info.Skip(12).Take(4).ToArray(), 4); + lastShotRecord.IsoTemp = StringChange.bytesToHexStr(info.Skip(16).Take(4).ToArray(), 4); + lastShotRecord.IsoHp = StringChange.bytesToHexStr(info.Skip(20).Take(4).ToArray(), 4); + lastShotRecord.IsoVol = StringChange.bytesToHexStr(info.Skip(24).Take(4).ToArray(), 4); + lastShotRecord.IsoUsage = StringChange.bytesToHexStr(info.Skip(28).Take(4).ToArray(), 4); + lastShotRecord.PourNu = StringChange.bytesToHexStr(info.Skip(36).Take(2).ToArray(), 2); + lastShotRecord.SetTime = StringChange.bytesToHexStr(info.Skip(38).Take(4).ToArray(), 4); + lastShotRecord.SetWeight = StringChange.bytesToHexStr(info.Skip(42).Take(4).ToArray(), 4); + lastShotRecord.SetRatio = StringChange.bytesToHexStr(info.Skip(46).Take(4).ToArray(), 4); + lastShotRecord.PourWeight = StringChange.bytesToHexStr(info.Skip(50).Take(4).ToArray(), 4); + lastShotRecord.PourRatio = StringChange.bytesToHexStr(info.Skip(54).Take(4).ToArray(), 4); + lastShotRecord.MpTime = StringChange.bytesToHexStr(info.Skip(58).Take(4).ToArray(), 4); + lastShotRecord.HpTime = StringChange.bytesToHexStr(info.Skip(62).Take(4).ToArray(), 4); + lastShotRecord.HydrPress = StringChange.bytesToHexStr(info.Skip(66).Take(4).ToArray(), 4); + lastShotRecord.HydrTemp = StringChange.bytesToHexStr(info.Skip(70).Take(4).ToArray(), 4); + lastShotRecord.MixpistOn = StringChange.bytesToHexStr(info.Skip(74).Take(4).ToArray(), 4); + lastShotRecord.MixpistOff = StringChange.bytesToHexStr(info.Skip(78).Take(4).ToArray(), 4); + lastShotRecord.TotalYield = StringChange.bytesToHexStr(info.Skip(82).Take(4).ToArray(), 4); + lastShotRecord.PourEnd = StringChange.bytesToHexStr(info.Skip(87).Take(1).ToArray(), 1); + lastShotRecord.CreateTime = DateTime.Now; + #endregion + + //if (tempLastRecord == null) + //{ + // //数据库查 + // tempLastRecord = _lastShotRecordServices.Query(x => x.System == lastShotRecord.System && x.ProductLineCode == lastShotRecord.ProductLineCode && x.GunCode == lastShotRecord.GunCode).OrderByDescending(x => x.CreateTime).FirstOrDefault(); + //} + //if (tempLastRecord == null || !tempLastRecord.MixpistOff.Equals(lastShotRecord.MixpistOff)) + //{ + // // 刷新页面 + // RefreshLastShotDataDelegateEvent?.Invoke(lastShotRecord); + // //保存 + // _lastShotRecordServices.AddAsync(lastShotRecord); + // tempLastRecord = lastShotRecord; + //} + } + } + + #region 字符工具转换方法 + /// byte[]转十进制ascii码 + /// + /// + static string ByteArrayToString(byte[] bytes) + { + string result = string.Empty; + foreach (byte b in bytes) + { + int decimalValue = Convert.ToInt32(b); // 将字节转换为十进制值 + result += decimalValue; + } + return result; + } + /// + /// byte[]转二进制 + /// + /// + static string ByteArrayToBinary(byte[] bytes) + { + StringBuilder binaryString = new StringBuilder(); + + foreach (byte b in bytes) + { + binaryString.Append(Convert.ToString(b, 2).PadLeft(8, '0')); + } + + // Console.WriteLine(binaryString); + + string reversedStr = new string(binaryString.ToString().Reverse().ToArray()); + // Console.WriteLine(reversedStr); + + return reversedStr; + } + + /// + /// 根据二进制字符串确定夹具状态 1正常生产 2暂停生产 + /// + static int judgeStatus(string str1) + { + int item1 = 0; + if (str1.Substring(0, 1) == "1") + { + item1 = 1; // + } + else if (str1.Substring(1, 1) == "1") + { + item1 = 2; // + } + return item1; + } + + + /// + /// byte数组转换为float + /// + static float byteToFloat(byte[] list) + { + float result = 0.00f; + Array.Reverse(list); + result = BitConverter.ToSingle(list, 0); + return result; + + } + + #endregion + } + + +} + + + + +