2329 lines
112 KiB
C#
2329 lines
112 KiB
C#
using JinYuan.Helper;
|
||
using JinYuan.MES.Enums;
|
||
using JinYuan.Models;
|
||
using JinYuan.VirtualDataLibrary;
|
||
using Language;
|
||
using PLCCommunication;
|
||
using PLCCommunication.Common;
|
||
using PLCCommunication.Common.DataConvert;
|
||
using SqlSugar;
|
||
using SqlSugar.Extensions;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Runtime.InteropServices;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace JinYuan.ControlCenters
|
||
{
|
||
public partial class ControlCenter
|
||
{
|
||
public object FileLock = new object();//文件锁
|
||
|
||
|
||
#region 上料工位(数据处理)
|
||
/// <summary>
|
||
/// 上料工位A
|
||
/// </summary>
|
||
//上料解析
|
||
private async Task FeedingStationA(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
var sw1 = new Stopwatch();
|
||
string logName = "A进站";
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] 开始处理", logName);
|
||
|
||
if (bArrays.IsSuccess && bArrays.Content != null)
|
||
{
|
||
try
|
||
{
|
||
sw.Start();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 数据有效, 数据长度:{bArrays.Content.Length}", logName);
|
||
|
||
#region 数据初始化
|
||
//解析数据
|
||
List<AGearEntity> list = new List<AGearEntity>();//实体
|
||
List<short> listRes = GetList<short>(8, 2);//默认为1,良品
|
||
List<bool> listMark = GetList<bool>(8, false);//有料标志
|
||
List<string> listCode = GetList<string>(8, "");//电芯码
|
||
List<string> listRemark = GetList<string>(8, "");//备注
|
||
List<string> listFlag = GetList<string>(8, "0");//是否上传标志
|
||
List<string> listResult = GetList<string>(8, "OK");//结果
|
||
List<string> listMesage = GetList<string>(8, "");//mesMesage
|
||
List<MesResType> listMesResType = GetList<MesResType>(8, MesResType.B);//mesType
|
||
#endregion
|
||
|
||
#region 数据解析
|
||
sw1.Start();
|
||
// 从D2021开始
|
||
byte[] bytes = bArrays.Content.ByteReverse();//高低位转换
|
||
int validCount = 0;
|
||
|
||
//有料标志
|
||
for (int i = 0; i < listMark.Count; i++)
|
||
{
|
||
listMark[i] = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2)) == 1 ? true : false;// 从D2021开始
|
||
if (listMark[i])
|
||
{
|
||
validCount++;
|
||
#region 数据读取
|
||
//条码长度
|
||
int codeLength = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (20 * 2));// 从D2041开始
|
||
if (codeLength > 0)
|
||
{
|
||
//条码
|
||
listCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 50) + (79 * 2), codeLength, System.Text.Encoding.ASCII).// 从D2100开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
else
|
||
{
|
||
listCode[i] = "ERROR";
|
||
}
|
||
#endregion
|
||
|
||
#region 数据保存
|
||
AGearEntity m = new AGearEntity();
|
||
m.BarCode = listCode[i];
|
||
m.ChannelNo = (i + 1).ToString();
|
||
m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
list.Add(m);
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
CommonMethods.AddDataMonitorLog(0, "进站上料工位A有料标志为0".Translated() + "通道号".Translated() + $":{i}");
|
||
}
|
||
}
|
||
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 解析完成, 耗时{sw1.ElapsedMilliseconds}ms, 有料数量:{validCount}, 条码:{string.Join(",", listCode.Where(x => !string.IsNullOrEmpty(x)))}", logName);
|
||
#endregion
|
||
|
||
#region 上传MES
|
||
|
||
sw1.Restart();
|
||
|
||
if (list.Count > 0)
|
||
{
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
if (CommonMethods.mesConfig.isConnected)
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] 开始上传MES, 数量:{list.Count}", logName);
|
||
|
||
var allMesMessages = new List<string>();
|
||
var allMesResTypes = new List<MesResType>();
|
||
var allFlags = new List<string>();
|
||
foreach (var item in list)
|
||
{
|
||
// 每次只上传一个条码(封装为List)
|
||
var singleCodeList = new List<string> { item.BarCode };
|
||
|
||
var (success1, mesage1, mesResType1) = await CommonMethods.hbgMes.ManyCellInStationAsync(
|
||
CommonMethods.mesConfig.ProductInStation,
|
||
CommonMethods.mesConfig.siteCode,
|
||
CommonMethods.mesConfig.lineCode,
|
||
CommonMethods.mesConfig.equipNum,
|
||
CommonMethods.mesConfig.MaterialCode,
|
||
CommonMethods.mesConfig.mesUserName,
|
||
singleCodeList); // 单条码列表
|
||
if (success1)
|
||
{
|
||
// 收集结果而不是覆盖
|
||
allMesMessages.AddRange(mesage1);
|
||
allMesResTypes.AddRange(mesResType1);
|
||
allFlags.Add("1");
|
||
}
|
||
else
|
||
{
|
||
// 收集失败结果
|
||
allMesMessages.AddRange(mesage1);
|
||
allMesResTypes.AddRange(mesResType1);
|
||
allFlags.Add("2");
|
||
// 记录失败状态
|
||
listRes = GetList<short>(8, 3);
|
||
listRemark = GetList<string>(8, "");
|
||
if (mesage1.Count > 0 && mesage1[0].Contains("一个或多个错误"))
|
||
{
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short); // 反馈PLC结果
|
||
}
|
||
}
|
||
}
|
||
// 将收集的结果赋给主列表
|
||
listMesage = allMesMessages;
|
||
listMesResType = allMesResTypes;
|
||
listFlag = allFlags;
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES上传完成, 成功:{allFlags.Count(x => x == "1")}, 失败:{allFlags.Count(x => x == "2")}", logName);
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES断连", logName);
|
||
listRes = GetList<short>(8, 3);
|
||
listRemark = GetList<string>(8, "MES断连".Translated());
|
||
listFlag = GetList<string>(8, "2");
|
||
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short);//反馈MES结果
|
||
}
|
||
}
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES上传耗时:{sw1.ElapsedMilliseconds}ms", logName);
|
||
#endregion
|
||
|
||
#region 数据组合
|
||
sw1.Restart();
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
if (String.IsNullOrEmpty(list[i].BarCode) || list[i].BarCode.Trim() == "ERROR" || !IsAlphaNumeric(list[i].BarCode))
|
||
{
|
||
listRes[i] = 2;
|
||
listResult[i] = "NG";
|
||
listRemark[i] = "电芯码不良".Translated();
|
||
listFlag[i] = "2";
|
||
}
|
||
else
|
||
{
|
||
int codeIndex = i;//listCode.Count - i - 1;
|
||
listResult[i] = "OK,";
|
||
listRemark[i] = "良品,".Translated();
|
||
listRes[i] = 1;
|
||
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
switch (listMesResType[codeIndex])
|
||
{
|
||
case MesResType.A:// 停线,设备报警停机
|
||
listResult[i] += "MES NG";
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 3;//10
|
||
listFlag[i] = "2";
|
||
break;
|
||
case MesResType.B:// 排出
|
||
listResult[i] += "MES NG";
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 3;//2
|
||
listFlag[i] = "2";
|
||
break;
|
||
case MesResType.C:// 警示
|
||
case MesResType.D:// 条码上传OK
|
||
listResult[i] += "MES OK";//MES认为是可以当成良品流下去的
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 1;
|
||
listFlag[i] = "1";
|
||
break;
|
||
default:
|
||
listResult[i] += "MES NG";
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 3;//2
|
||
listFlag[i] = "2";
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
list[i].Result = listResult[i].TrimStart(',').TrimEnd(',');
|
||
list[i].Remark = listRemark[i].TrimStart(',').TrimEnd(',');
|
||
list[i].Flag = listFlag[i];
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 数据组合耗时:{sw1.ElapsedMilliseconds}ms", logName);
|
||
#endregion
|
||
|
||
#region 数据反馈&上传
|
||
|
||
sw1.Restart();
|
||
_ = Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
await SaveFeedingDataAsync(list);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"{logName} SaveFeedingDataAsync 异常{ex.Message}", "SysErrorLog");
|
||
}
|
||
}
|
||
);
|
||
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 存储耗时:{sw1.ElapsedMilliseconds}ms", logName);
|
||
|
||
sw1.Restart();
|
||
JYResult result = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[1].VarAddress, listRes.ToArray(), PLC.DataType.ArrShort);//判断结果写入PLC
|
||
if (result != null)
|
||
{
|
||
string strres = "进站A工位发送给PLC".Translated() + $",[{pf.VarList[1].VarAddress}]:{string.Join(",", listRes)}";
|
||
CommonMethods.AddDataMonitorLog(0, strres);
|
||
LogHelper.Instance.WriteError($"logName 工位发送给PLC:{strres}", "SysErrorLog");
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 反馈PLC,耗时{sw1.ElapsedMilliseconds}ms, 结果:{string.Join(",", listRes)}", logName);
|
||
|
||
|
||
sw1.Restart();
|
||
bool writeResult = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 清0耗时:{sw1.ElapsedMilliseconds}ms, 结果:{writeResult}", logName);
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"[{logName}] 异常:{ex.Message}", "SysErrorLog");
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
}
|
||
sw.Stop();
|
||
long cost = sw.ElapsedMilliseconds;
|
||
LogHelper.Instance.WriteLog($"[{logName}] 总耗时: {cost}ms", logName);
|
||
CommonMethods.AddDataMonitorLog(0, "卷芯进站A共耗时".Translated() + $":{cost}ms");
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] 数据无效", logName);
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上料工位B
|
||
/// </summary>
|
||
//上料解析
|
||
private async Task FeedingStationB(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
string logName = "B进站";
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] 开始处理", logName);
|
||
if (bArrays.IsSuccess && bArrays.Content != null)
|
||
{
|
||
try
|
||
{
|
||
sw.Start();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 数据有效, 数据长度:{bArrays.Content.Length}", logName);
|
||
|
||
#region 数据初始化
|
||
//解析数据
|
||
List<AGearEntity> list = new List<AGearEntity>();//实体
|
||
List<short> listRes = GetList<short>(8, 2);//默认为1,良品
|
||
List<bool> listMark = GetList<bool>(8, false);//有料标志
|
||
List<string> listCode = GetList<string>(8, "");//电芯码
|
||
List<string> listRemark = GetList<string>(8, "");//备注
|
||
List<string> listFlag = GetList<string>(8, "0");//是否上传标志
|
||
List<string> listResult = GetList<string>(8, "OK");//结果
|
||
List<string> listMesage = GetList<string>(8, "");//mesMesage
|
||
List<MesResType> listMesResType = GetList<MesResType>(8, MesResType.B);//mesType
|
||
#endregion
|
||
|
||
#region 数据解析
|
||
// 从D3021开始
|
||
byte[] bytes = bArrays.Content.ByteReverse();//高低位转换
|
||
int validCount = 0;
|
||
|
||
//有料标志
|
||
for (int i = 0; i < listMark.Count; i++)
|
||
{
|
||
listMark[i] = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2)) == 1 ? true : false;// 从D3021开始
|
||
if (listMark[i])
|
||
{
|
||
validCount++;
|
||
#region 数据读取
|
||
//条码长度
|
||
int codeLength = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (20 * 2));// 从D3041开始
|
||
if (codeLength > 0)
|
||
{
|
||
//条码
|
||
listCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 50) + (79 * 2), codeLength, System.Text.Encoding.ASCII).// 从D3100开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
else
|
||
{
|
||
listCode[i] = "ERROR";
|
||
}
|
||
#endregion
|
||
|
||
#region 数据保存
|
||
AGearEntity m = new AGearEntity();
|
||
m.BarCode = listCode[i];
|
||
m.ChannelNo = (i + 1).ToString();
|
||
m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
list.Add(m);
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
CommonMethods.AddDataMonitorLog(0, "进站上料工位B有料标志为0".Translated() + "通道号".Translated() + $":{i}");
|
||
}
|
||
}
|
||
LogHelper.Instance.WriteLog($"[{logName}] 解析完成, 有料数量:{validCount}, 条码:{string.Join(",", listCode.Where(x => !string.IsNullOrEmpty(x)))}", logName);
|
||
#endregion
|
||
|
||
#region 上传MES
|
||
var sw1 = new Stopwatch();
|
||
sw1.Start();
|
||
|
||
if (list.Count > 0)
|
||
{
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
if (CommonMethods.mesConfig.isConnected)
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] 开始上传MES, 数量:{list.Count}", logName);
|
||
|
||
var allMesMessages = new List<string>();
|
||
var allMesResTypes = new List<MesResType>();
|
||
var allFlags = new List<string>();
|
||
foreach (var item in list)
|
||
{
|
||
// 每次只上传一个条码(封装为List)
|
||
var singleCodeList = new List<string> { item.BarCode };
|
||
|
||
var (success1, mesage1, mesResType1) = await CommonMethods.hbgMes.ManyCellInStationAsync(
|
||
CommonMethods.mesConfig.ProductInStation,
|
||
CommonMethods.mesConfig.siteCode,
|
||
CommonMethods.mesConfig.lineCode,
|
||
CommonMethods.mesConfig.equipNum,
|
||
CommonMethods.mesConfig.MaterialCode,
|
||
CommonMethods.mesConfig.mesUserName,
|
||
singleCodeList); // 单条码列表
|
||
if (success1)
|
||
{
|
||
// 收集结果而不是覆盖
|
||
allMesMessages.AddRange(mesage1);
|
||
allMesResTypes.AddRange(mesResType1);
|
||
allFlags.Add("1");
|
||
}
|
||
else
|
||
{
|
||
// 收集失败结果
|
||
allMesMessages.AddRange(mesage1);
|
||
allMesResTypes.AddRange(mesResType1);
|
||
allFlags.Add("2");
|
||
// 记录失败状态
|
||
listRes = GetList<short>(8, 3);
|
||
listRemark = GetList<string>(8, "");
|
||
if (mesage1.Count > 0 && mesage1[0].Contains("一个或多个错误"))
|
||
{
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short); // 反馈PLC结果
|
||
}
|
||
}
|
||
}
|
||
// 将收集的结果赋给主列表
|
||
listMesage = allMesMessages;
|
||
listMesResType = allMesResTypes;
|
||
listFlag = allFlags;
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES上传完成, 成功:{allFlags.Count(x => x == "1")}, 失败:{allFlags.Count(x => x == "2")}", logName);
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES断连", logName);
|
||
listRes = GetList<short>(8, 3);
|
||
listRemark = GetList<string>(8, "MES断连".Translated());
|
||
listFlag = GetList<string>(8, "2");
|
||
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short);//反馈MES结果
|
||
}
|
||
}
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES上传耗时:{sw1.ElapsedMilliseconds}ms", logName);
|
||
#endregion
|
||
|
||
#region 数据组合
|
||
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
if (string.IsNullOrEmpty(list[i].BarCode) || list[i].BarCode.Trim() == "ERROR" || !IsAlphaNumeric(list[i].BarCode))
|
||
{
|
||
listRes[i] = 2;
|
||
listResult[i] = "NG";
|
||
listRemark[i] = "电芯码不良".Translated();
|
||
listFlag[i] = "2";
|
||
}
|
||
else
|
||
{
|
||
int codeIndex = i;
|
||
listResult[i] = "OK,";
|
||
listRes[i] = 1;
|
||
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
switch (listMesResType[codeIndex])
|
||
{
|
||
case MesResType.A:// 停线,设备报警停机
|
||
listResult[i] += "MES NG";
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 3;//10
|
||
listFlag[i] = "2";
|
||
break;
|
||
case MesResType.B:// 排出
|
||
listResult[i] += "MES NG";
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 3;//2
|
||
listFlag[i] = "2";
|
||
break;
|
||
case MesResType.C:// 警示
|
||
case MesResType.D:// 条码上传OK
|
||
listResult[i] += "MES OK";//MES认为是可以当成良品流下去的
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 1;
|
||
listFlag[i] = "1";
|
||
break;
|
||
default:
|
||
listResult[i] += "MES NG";
|
||
listRemark[i] += listMesage[codeIndex];
|
||
listRes[i] = 3;//2
|
||
listFlag[i] = "2";
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
list[i].Result = listResult[i].TrimStart(',').TrimEnd(',');
|
||
list[i].Remark = listRemark[i].TrimStart(',').TrimEnd(',');
|
||
list[i].Flag = listFlag[i];
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 数据反馈&上传
|
||
//读取结果显示
|
||
//数据存储 -异步处理
|
||
_ = Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
await SaveFeedingDataAsync(list);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"SaveFeedingDataAsync 异常{ex.Message}", logName);
|
||
}
|
||
}
|
||
);
|
||
|
||
//写入·结果反馈
|
||
JYResult result = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[1].VarAddress, listRes.ToArray(), PLC.DataType.ArrShort);//判断结果写入PLC
|
||
if (result != null)
|
||
{
|
||
string strres = "进站B工位发送给PLC".Translated() + $",[{pf.VarList[1].VarAddress}]:{string.Join(",", listRes)}";
|
||
CommonMethods.AddDataMonitorLog(0, strres);
|
||
}
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] 反馈PLC结果:{string.Join(",", listRes)}", logName);
|
||
|
||
bool writeResult = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
LogHelper.Instance.WriteLog($"[{logName}] 清0结果:{writeResult}", logName);
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"[{logName}] 异常:{ex.Message}", logName);
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
}
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 处理完成, 总耗时:{sw.ElapsedMilliseconds}ms", logName);
|
||
CommonMethods.AddDataMonitorLog(0, "卷芯进站B共耗时".Translated() + $":{sw.Elapsed.TotalMilliseconds}ms");
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] 数据无效", logName);
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
}
|
||
}
|
||
|
||
#region 保存上料数据
|
||
/// <summary>
|
||
/// 保存上料数据到数据
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
private async Task SaveFeedingDataAsync(List<AGearEntity> list)
|
||
{
|
||
var sw = new Stopwatch();
|
||
sw.Start();
|
||
//数据的保存
|
||
try
|
||
{
|
||
if (list != null && list.Count > 0)
|
||
{
|
||
var sw1 = new Stopwatch();
|
||
sw1.Start();
|
||
bool b = await CommonMethods.db.AddReturnBoolAsync<AGearEntity>(list);
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"SaveFeedingDataAsync AddReturnBoolAsync 耗时{sw1.ElapsedMilliseconds}", "A进站");
|
||
sw1.Restart();
|
||
//数据显示
|
||
CommonMethods.ShowFeedingDelegate.Invoke(list);
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"SaveFeedingDataAsync ShowFeedingDelegate 耗时{sw1.ElapsedMilliseconds}", "A进站");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"卷芯上料保存处理异常:{ex}", "SysErrorLog");
|
||
}
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteLog($"SaveFeedingDataAsync 耗时{sw.ElapsedMilliseconds}", "A进站");
|
||
}
|
||
#endregion
|
||
|
||
#endregion
|
||
|
||
#region 下料工位(数据处理)
|
||
/// <summary>
|
||
/// 电芯出站A
|
||
/// </summary>
|
||
/// <param name="bArrays"></param>
|
||
/// <param name="plcModel"></param>
|
||
private async Task BlankingStationA(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
var sw1 = new Stopwatch();
|
||
string logName = "A出站";
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] 开始处理", logName);
|
||
|
||
if (bArrays.IsSuccess && bArrays.Content != null)
|
||
{
|
||
try
|
||
{
|
||
sw.Start();
|
||
LogHelper.Instance.WriteLog($"【出站A】开始处理, 时间={DateTime.Now:HH:mm:ss.fff} 数据有效, 数据长度:{bArrays.Content.Length}", logName);
|
||
|
||
#region 初始化数据
|
||
sw1.Restart();
|
||
|
||
List<BGearEntity> list = new List<BGearEntity>();
|
||
List<short> listRes = GetList<short>(8, 2);//默认为1,良品
|
||
List<bool> listMark = GetList<bool>(8, false);//有料标志
|
||
List<string> listCode = GetList<string>(8, "");//电芯码
|
||
List<string> listRemark = GetList<string>(8, "");//备注
|
||
List<string> listFlag = GetList<string>(8, "0");//是否上传标志
|
||
List<string> listResult = GetList<string>(8, "OK");//结果
|
||
List<string> listMesMesage = GetList<string>(8, "");//mesMesage
|
||
List<string> listrfid = GetList<string>(8, "");//RFID
|
||
List<string> listJetCode = GetList<string>(8, "");//喷码
|
||
List<MesResType> listMesResType = GetList<MesResType>(8, MesResType.B);//mesType
|
||
|
||
List<DataTurnover> list_battery411_2Entity = new List<DataTurnover>();
|
||
|
||
//ushort listRes = 1; // 默认为1,良品
|
||
string lst_RFIDCodes = ""; // 托杯码
|
||
string lst_BarCodes = ""; // 条码
|
||
string lst_JetCode = "";// 喷码
|
||
|
||
string windSpeed;// 风速
|
||
string barCodeLength;// 电芯码长度
|
||
string barCodeWidth;// 电芯码宽度
|
||
string gbCodeBottomToCoverPlateHeight;// GB码底部距盖板高度
|
||
string midDiameter;// 中间直径
|
||
string insulationFilmToBottemHeight1;// 绝缘膜距底部高度1
|
||
string insulationFilmToBottemHeight2;// 绝缘膜距底部高度2
|
||
string insulationFilmToBottemHeight3;// 绝缘膜距底部高度3
|
||
string insulationFilmToBottemHeight4;// 绝缘膜距底部高度4
|
||
string insulationFilmToBottemHeight5;// 包膜对齐度1
|
||
string insulationFilmToBottemHeight6;// 包膜对齐度2
|
||
string topiameter;// 顶部直径
|
||
string lowiameter;// 底部直径
|
||
|
||
int firstTrueIndex = 0;//通道号
|
||
int station = 0;// 工位
|
||
int stationA1 = 0; //滚标位
|
||
int ccdAfterCoatedAlignmentTestResult = 0; // 包膜后对齐度检测结果
|
||
int ccdAfterCoatedApperanceTestResult = 0; // 贴膜后外观检测结果
|
||
int ccdAfterCoatedApperanceTestReson = 0; // 贴膜后外观NG
|
||
string shuttleActuator = ""; // 穿梭动子
|
||
string cEDistance = "";// Ce 距离
|
||
string cEHeight = "";// Ce 高
|
||
string cEWidth = "";// Ce 宽
|
||
|
||
int[] ccdTestResult = new int[2];//ccd检测结果
|
||
|
||
string result = "";//总结果
|
||
string info = "";//NG详情
|
||
string flag = "";//是否上传mes
|
||
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"【出站A】数据初始化耗时: {sw1.ElapsedMilliseconds}ms", logName);
|
||
#endregion
|
||
|
||
#region 数据解析
|
||
byte[] bytes = bArrays.Content.ByteReverse();
|
||
int validCount = 0;
|
||
sw1.Restart();
|
||
for (int i = 0; i < listMark.Count; i++)
|
||
{
|
||
listMark[i] = ShortLib.GetShortFromByteArray(bArrays.Content, i * 2) == 1 ? true : false;// 从D4021开始
|
||
if (listMark[i])
|
||
{
|
||
validCount++;
|
||
#region 数据读取
|
||
//条码长度
|
||
int codeLength = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (20 * 2));// 从D4041开始
|
||
if (codeLength > 0)
|
||
{
|
||
//条码
|
||
listCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 50) + (79 * 2), codeLength, System.Text.Encoding.ASCII).// 从D4100开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
else { listCode[i] = "ERROR"; }
|
||
|
||
bool jetCodeMark = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (579 * 2)) == 1 ? true : false;// 从D4600开始
|
||
if (jetCodeMark)
|
||
{
|
||
// 从PLC获取
|
||
int jetCodeLength = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (587 * 2));// 从D4608开始
|
||
if (jetCodeLength > 0)
|
||
{
|
||
listJetCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 50) + (629 * 2), jetCodeLength, System.Text.Encoding.ASCII).// 从D4650开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
}
|
||
// 从PLC获取RFID
|
||
int RFIDCodeLength = 9;
|
||
if (RFIDCodeLength > 0)
|
||
{
|
||
listrfid[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 60) + (1079 * 2), RFIDCodeLength, System.Text.Encoding.ASCII).// 从D5100开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
|
||
windSpeed = GetValueFromByteArray(bArrays.Content, "windSpeed", i * 4);// 风速 D4850 829
|
||
barCodeLength = GetValueFromByteArray(bArrays.Content, "barCodeLength", i * 4);// 二维码长度 D4866 845
|
||
barCodeWidth = GetValueFromByteArray(bArrays.Content, "barCodeWidth", i * 4);// 二维码宽度 D4882 861
|
||
gbCodeBottomToCoverPlateHeight = GetValueFromByteArray(bArrays.Content, "gbCodeBottomToCoverPlateHeight", i * 4);// GB码底部距盖板高度 D4898 877
|
||
midDiameter = GetValueFromByteArray(bArrays.Content, "midDiameter", i * 4);// 中间直径 D4914 893
|
||
insulationFilmToBottemHeight1 = GetValueFromByteArray(bArrays.Content, "insulationFilmToBottemHeight1", i * 6 * 2 * 2);// 绝缘膜距底部高度1 D4930 909
|
||
insulationFilmToBottemHeight2 = GetValueFromByteArray(bArrays.Content, "insulationFilmToBottemHeight2", i * 6 * 2 * 2);// 绝缘膜距底部高度2 D4932 911
|
||
insulationFilmToBottemHeight3 = GetValueFromByteArray(bArrays.Content, "insulationFilmToBottemHeight3", i * 6 * 2 * 2);// 绝缘膜距底部高度3 D4934 913
|
||
insulationFilmToBottemHeight4 = GetValueFromByteArray(bArrays.Content, "insulationFilmToBottemHeight4", i * 6 * 2 * 2);// 绝缘膜距底部高度4 D4936 915
|
||
insulationFilmToBottemHeight5 = GetValueFromByteArray(bArrays.Content, "insulationFilmToBottemHeight5", i * 6 * 2 * 2);// 包膜对齐度1 D4938 917
|
||
insulationFilmToBottemHeight6 = GetValueFromByteArray(bArrays.Content, "insulationFilmToBottemHeight6", i * 6 * 2 * 2);// 包膜对齐度2 D4940 919
|
||
station = int.Parse(GetValueFromByteArray(bArrays.Content, "station", i * 4));// 工位 D5026 1005
|
||
ccdAfterCoatedAlignmentTestResult = int.Parse(GetValueFromByteArray(bArrays.Content, "ccdAfterCoatedAlignmentTestResult", i * 2)); // 包膜后对齐度检测结果 D5042 1021
|
||
ccdAfterCoatedApperanceTestResult = int.Parse(GetValueFromByteArray(bArrays.Content, "ccdAfterCoatedApperanceTestResult", i * 2)); // 贴膜后外观检测结果 D5050 1029
|
||
ccdAfterCoatedApperanceTestReson = int.Parse(GetValueFromByteArray(bArrays.Content, "ccdAfterCoatedApperanceTestReson", i * 2)); // 贴膜后外观NG D5058 1037
|
||
topiameter = GetValueFromByteArray(bArrays.Content, "topiameter", i * 4);// 顶部直径
|
||
lowiameter = GetValueFromByteArray(bArrays.Content, "lowiameter", i * 4);// 底部直径
|
||
shuttleActuator = GetValueFromByteArray(bArrays.Content, "shuttleActuator", i * 2);// 穿梭动子
|
||
|
||
stationA1 = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (1369 * 2)); // D5390~5397
|
||
cEHeight = GetValueFromByteArray(bArrays.Content, "cEHeight", (i * 4));//5400 CE宽
|
||
cEWidth = GetValueFromByteArray(bArrays.Content, "cEWidth", (i * 4));// 5416 CE高
|
||
cEDistance = GetValueFromByteArray(bArrays.Content, "cEDistance", (i * 4));// 5432 CE距离
|
||
|
||
result = GetValueFromByteArray(bArrays.Content, "result", i * 2);// 总结果 D5058 1037
|
||
info = GetValueFromByteArray(bArrays.Content, "info", i * 2); // NG详情 D5066 1045
|
||
|
||
lst_RFIDCodes = listrfid[i];//StringLib.GetStringFromByteArrayByEncoding(bytes, 0, 39, Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim(); // RFID D3200-3219
|
||
lst_BarCodes = listCode[i];
|
||
lst_JetCode = listJetCode[i];
|
||
|
||
firstTrueIndex = listMark.IndexOf(true);
|
||
#endregion
|
||
|
||
#region 数据保存
|
||
BGearEntity m = new BGearEntity
|
||
{
|
||
ChannelNo = (i + 1).ToString(),
|
||
TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
|
||
RFID = listrfid[i],
|
||
BarCode = listCode[i],
|
||
JetCode = listJetCode[i],
|
||
WindSpeed = windSpeed,
|
||
BarCodeLength = barCodeLength,
|
||
BarCodeWidth = barCodeWidth,
|
||
GBCodeBottomToCoverPlateHeight = gbCodeBottomToCoverPlateHeight,
|
||
MidDiameter = midDiameter,
|
||
InsulationFilmToBottemHeight1 = insulationFilmToBottemHeight1,
|
||
InsulationFilmToBottemHeight2 = insulationFilmToBottemHeight2,
|
||
InsulationFilmToBottemHeight3 = insulationFilmToBottemHeight3,
|
||
InsulationFilmToBottemHeight4 = insulationFilmToBottemHeight4,
|
||
InsulationFilmToBottemHeight5 = insulationFilmToBottemHeight5,
|
||
InsulationFilmToBottemHeight6 = insulationFilmToBottemHeight6,
|
||
TOPiameter = topiameter,
|
||
LOWiameter = lowiameter,
|
||
ShuttleActuator = shuttleActuator,
|
||
|
||
CCDAfterCoatedAlignmentTestResult = ccdAfterCoatedAlignmentTestResult == 1 ? "OK" : "NG",
|
||
CCDAfterCoatedApperanceTestResult = ccdAfterCoatedApperanceTestResult == 1 ? "OK" : "NG",
|
||
CCDAfterCoatedApperanceTestReson = ccdAfterCoatedApperanceTestResult == 2 ? "UI003" : "",
|
||
Station = station == 1 ? "A" : "B",
|
||
stationA1 = stationA1 == 1 ? "1" : "2",
|
||
cEHeight = cEHeight,
|
||
cEDistance = cEDistance,
|
||
cEWidth =cEWidth,
|
||
|
||
Flag = listFlag[i],
|
||
Flag1 = "2",
|
||
Result = result,
|
||
Info = info,
|
||
MaterialCode = CommonMethods.mesConfig.MaterialCode,
|
||
};
|
||
list_battery411_2Entity.Add(new DataTurnover()
|
||
{
|
||
equipNum = CommonMethods.mesConfig.equipNum,
|
||
Time = DateTime.Now,
|
||
containerCode = listrfid[i],
|
||
identification = listCode[i],
|
||
message = "OK"
|
||
});
|
||
list.Add(m);
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
CommonMethods.AddDataMonitorLog(0, "出站下料工位A有料标志为0".Translated() + "通道号".Translated() + $":{i}");
|
||
}
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 解析完成,耗时:{sw1.ElapsedMilliseconds}ms, 有料数量:{validCount}, 条码:{string.Join(",", listCode.Where(x => !string.IsNullOrEmpty(x)))}", logName);
|
||
#endregion
|
||
|
||
#region 数据上传
|
||
|
||
sw1.Restart();
|
||
|
||
if (list.Count > 0)
|
||
{
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
if (CommonMethods.mesConfig.isConnected)
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] 开始上传MES, 数量:{list.Count}", logName);
|
||
|
||
var tempMesMessages = new List<string>();
|
||
var tempMesResTypes = new List<MesResType>();
|
||
var tempFlags = new List<string>();
|
||
foreach (var item in list)
|
||
{
|
||
var sw3 = new Stopwatch();
|
||
sw3.Start();
|
||
// 创建单个电池的列表
|
||
var singleBatteryList = new List<BGearEntity> { item };
|
||
|
||
// 调用MES接口(每次只传一个电池)
|
||
var (success, mesage, mesResType) = await CommonMethods.hbgMes.ManyCellOutStationAsync(
|
||
CommonMethods.mesConfig.ProductOutStation,
|
||
CommonMethods.mesConfig.siteCode,
|
||
CommonMethods.mesConfig.lineCode,
|
||
CommonMethods.mesConfig.equipNum,
|
||
CommonMethods.mesConfig.MaterialCode,
|
||
CommonMethods.mesConfig.ModelName,
|
||
CommonMethods.mesConfig.mesUserName,
|
||
singleBatteryList,
|
||
1,
|
||
CommonMethods.mesConfig.materialLotCodeLocator);
|
||
if (success)
|
||
{
|
||
// 收集成功结果
|
||
tempMesMessages.AddRange(mesage);
|
||
tempMesResTypes.AddRange(mesResType);
|
||
tempFlags.Add("1");
|
||
}
|
||
else
|
||
{
|
||
// 收集失败结果
|
||
tempMesMessages.AddRange(mesage);
|
||
tempMesResTypes.AddRange(mesResType);
|
||
tempFlags.Add("2");
|
||
|
||
// 记录失败状态
|
||
listRes = GetList<short>(8, 3);
|
||
listRemark = GetList<string>(8, "");
|
||
if (mesage.Count > 0 && mesage[0].Contains("一个或多个错误"))
|
||
{
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short); // 反馈PLC结果
|
||
}
|
||
}
|
||
sw3.Stop();
|
||
LogHelper.Instance.WriteLog($"A出站 {item.BarCode} 上传mes单次耗时: {sw3.ElapsedMilliseconds},", logName);
|
||
}
|
||
// 将收集的结果赋给主列表
|
||
listMesMesage = tempMesMessages;
|
||
listMesResType = tempMesResTypes;
|
||
listFlag = tempFlags;
|
||
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES上传完成, 成功:{tempFlags.Count(x => x == "1")}, 失败:{tempFlags.Count(x => x == "2")}", logName);
|
||
var sw2 = new Stopwatch();
|
||
sw2.Start();
|
||
|
||
_ = Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
await UpProducResultParamAsync(list);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"UpProducResultParamAsync 异常{ex.Message}", logName);
|
||
}
|
||
}
|
||
);
|
||
sw2.Stop();
|
||
LogHelper.Instance.WriteLog($"A出站上传结果参数耗时: {sw2.ElapsedMilliseconds},", logName);
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES断连", logName);
|
||
listRes = GetList<short>(8, 3);
|
||
listRemark = GetList<string>(8, "MES断连".Translated());
|
||
listFlag = GetList<string>(8, "2");
|
||
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short);//反馈MES结果
|
||
}
|
||
}
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] MES 上传总耗时:{sw1.ElapsedMilliseconds}ms", logName);
|
||
#endregion
|
||
|
||
#region 数据组合
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
if (String.IsNullOrEmpty(listCode[i]) || listCode[i].Trim() == "ERROR" || listCode[i].Trim().ToUpper() == "NOREAD" || listCode[i].Trim().ToUpper() == "NG" || !IsAlphaNumeric(listCode[i]))
|
||
{
|
||
listRes[i] = 2;
|
||
listResult[i] = "NG";
|
||
listRemark[i] = "电芯码不良".Translated();
|
||
listFlag[i] = "2";
|
||
}
|
||
else
|
||
{
|
||
string strMessage = "";
|
||
ccdTestResult[0] = list[i].CCDAfterCoatedAlignmentTestResult == "OK" ? 1 : 2;
|
||
ccdTestResult[1] = list[i].CCDAfterCoatedApperanceTestResult == "OK" ? 1 : 2;
|
||
//short a = (short)GetResult(ccdTestResult, ref strErr);
|
||
int ngMessage = Convert.ToInt32(list[i].Info);
|
||
strMessage = (string)GetFinalResult(ngMessage,ref strMessage);
|
||
listResult[i] = list[i].Result == "2" ? "OK" : "NG";
|
||
list[i].Info = strMessage;
|
||
listRes[i] = 1;
|
||
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
// 本地判断OK,MES判断OK D代表MES OK
|
||
if (listResult[i] == "OK" && listMesResType[i] == MesResType.D)
|
||
{
|
||
listResult[i] += ",MES OK";
|
||
listRemark[i] += "良品".Translated();
|
||
listRemark[i] += $"{listMesMesage[i]}";
|
||
listRes[i] = 1;
|
||
listFlag[i] = "1";
|
||
}
|
||
// 本地判断OK,MES判断NG
|
||
else if (listResult[i] == "OK" && listMesResType[i] != MesResType.D)
|
||
{
|
||
switch (listMesResType[i])
|
||
{
|
||
case MesResType.A: // 停线,设备报警停机
|
||
listResult[i] += ",MES NG";
|
||
listRemark[i] += $"{listMesMesage[i]}";
|
||
listRes[i] = 3;//10
|
||
listFlag[i] = "2";
|
||
break;
|
||
case MesResType.B: // MES不良
|
||
listResult[i] += ",MES NG";
|
||
listRemark[i] += $"{listMesMesage[i]}";
|
||
listRes[i] = 3;//9
|
||
listFlag[i] = "2";
|
||
break;
|
||
case MesResType.C: // 警示
|
||
listResult[i] += ",MES OK"; // MES认为是可以当成良品流下去的
|
||
listRemark[i] += $"Alarm:{listMesMesage[i]}";
|
||
listRes[i] = 1;
|
||
listFlag[i] = "1";
|
||
break;
|
||
case MesResType.D: // 条码上传OK
|
||
listResult[i] += ",MES OK";
|
||
listRemark[i] += $"{listMesMesage[i]}";
|
||
listRes[i] = 1;
|
||
listFlag[i] = "1";
|
||
break;
|
||
default:
|
||
listResult[i] += ",MES NG";
|
||
listRemark[i] += $"{listMesMesage[i]}";
|
||
listRes[i] = 3;
|
||
listFlag[i] = "2";
|
||
break;
|
||
}
|
||
}
|
||
// 本地判断NG,MES判断OK
|
||
else if (listResult[i] == "NG" && listMesResType[i] == MesResType.D)
|
||
{
|
||
listResult[i] += ",MES OK";
|
||
listRemark[i] += $"{listMesMesage[i]}";
|
||
//listRes[i] = 1;
|
||
listRes[i] = 99;
|
||
listFlag[i] = "1";
|
||
}
|
||
// 本地判断NG,MES判断NG
|
||
else if (listResult[i] == "NG" && listMesResType[i] != MesResType.D)
|
||
{
|
||
listResult[i] += ",MES NG";
|
||
listRemark[i] += $"{listMesResType[i].ToString()},{listMesMesage[i]}";
|
||
listRes[i] = 3;//9
|
||
listFlag[i] = "2";
|
||
}
|
||
// 其他情况
|
||
else
|
||
{
|
||
listResult[i] += ",MES NG";
|
||
listRemark[i] += listMesMesage[i];
|
||
listRes[i] = 3;
|
||
listFlag[i] = "2";
|
||
}
|
||
}
|
||
}
|
||
list[i].Result = listResult[i].TrimStart(',').TrimEnd(',');
|
||
list[i].Remark = listRemark[i].TrimStart(',').TrimEnd(',');
|
||
list[i].Flag = listFlag[i];
|
||
}
|
||
#endregion
|
||
|
||
#region 数据保存&上传
|
||
|
||
sw1.Restart();
|
||
if (list_battery411_2Entity.Any())
|
||
{
|
||
await SaveBlankingData2SQL(list_battery411_2Entity);
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"A出站远端存储耗时: {sw1.ElapsedMilliseconds}ms", logName);
|
||
|
||
//数据存储 - 异步处理
|
||
sw1.Restart();
|
||
_ = Task.Run(async () =>
|
||
{
|
||
try
|
||
{
|
||
await SaveBlankingDataAsync(list);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"SaveBlankingDataAsync 异常{ex.Message}", logName);
|
||
}
|
||
});
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"A出站本地存储耗时: {sw1.ElapsedMilliseconds}", logName);
|
||
|
||
sw1.Restart();
|
||
if (pf.IsFeedbackPlc)
|
||
{
|
||
//写入·结果反馈
|
||
JYResult result1 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[1].VarAddress, listRes.ToArray(), PLC.DataType.ArrShort);//判断结果写入PLC
|
||
LogHelper.Instance.WriteLog($"[{logName}] 反馈PLC结果:{string.Join(",", listRes)}", logName);
|
||
if (result1 != null)
|
||
{
|
||
string strres = "出站A工位发送给PLC".Translated() + $",[{pf.VarList[1].VarAddress}]:{string.Join(",", listRes)}";
|
||
CommonMethods.AddDataMonitorLog(0, strres);
|
||
}
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"反馈结果给PLC 耗时: {sw1.ElapsedMilliseconds}ms", logName);
|
||
|
||
sw1.Restart();
|
||
bool writeResult = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
|
||
if (!writeResult)
|
||
{
|
||
int count = 0;
|
||
bool success = false;
|
||
do
|
||
{
|
||
success = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
count++;
|
||
LogHelper.Instance.WriteLog($"[{logName}] 清0失败第{count}次重试结果:{success}", logName);
|
||
}
|
||
while (count < 3 && success == false);
|
||
}
|
||
sw1.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 清0, 耗时{sw1.ElapsedMilliseconds}ms,结果:{writeResult}", logName);
|
||
|
||
|
||
#endregion
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"[{logName}] 异常:{ex.Message}", logName);
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
}
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteLog($"[{logName}] 处理完成, 总耗时:{sw.ElapsedMilliseconds}ms", logName);
|
||
// 输出上料数据处理耗时:
|
||
CommonMethods.AddDataMonitorLog(0, "电芯出站A共耗时".Translated() + $":{sw.Elapsed.TotalMilliseconds}ms");
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteLog($"[{logName}] 数据无效", logName);
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||
}
|
||
}
|
||
|
||
#region//上传托杯 电芯码绑定关系 到 数据库
|
||
private async Task<bool> SaveBlankingData2SQL(List<DataTurnover> list)
|
||
{
|
||
var res = false;
|
||
try
|
||
{
|
||
if (list != null && list.Count > 0)
|
||
{
|
||
res = await CommonMethods.dbServer.AddReturnBoolAsync<DataTurnover>(list);
|
||
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteError($"电芯出站数据上传远程数据库的List为空", "SysErrorLog");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"电芯出站数据上传远程数据库保存异常:{ex}", "SysErrorLog");
|
||
}
|
||
return res;
|
||
|
||
}
|
||
# endregion
|
||
|
||
#region 保存下料数据
|
||
/// <summary>
|
||
/// 保存下料数据到数据
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
private async Task SaveBlankingDataAsync(List<BGearEntity> list)
|
||
{
|
||
var sw = new Stopwatch();
|
||
|
||
//数据的保存
|
||
try
|
||
{
|
||
if (list != null && list.Count > 0)
|
||
{
|
||
//数据显示
|
||
sw.Restart();
|
||
bool b = await CommonMethods.db.AddReturnBoolAsync<BGearEntity>(list);
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteError($"AddReturnBoolAsync 耗时 {sw.ElapsedMilliseconds}ms", "SysErrorLog");
|
||
|
||
//数据显示
|
||
sw.Restart();
|
||
CommonMethods.ShowBlankingDelegate.Invoke(list);
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteError($"ShowBlankingDelegate 耗时 {sw.ElapsedMilliseconds}ms", "SysErrorLog");
|
||
|
||
sw.Restart();
|
||
// 2. 保存到临时表(新增代码)
|
||
if (b) // 确保主表保存成功后再保存到临时表
|
||
{
|
||
// 创建临时表实体列表
|
||
var tempList = list.Select(b => new Temprfidtable
|
||
{
|
||
// 复制所有属性
|
||
ChannelNo = b.ChannelNo,
|
||
TestTime = b.TestTime,
|
||
RFID = b.RFID,
|
||
BarCode = b.BarCode,
|
||
JetCode = b.JetCode,
|
||
WindSpeed = b.WindSpeed,
|
||
BarCodeLength = b.BarCodeLength,
|
||
BarCodeWidth = b.BarCodeWidth,
|
||
GBCodeBottomToCoverPlateHeight = b.GBCodeBottomToCoverPlateHeight,
|
||
MidDiameter = b.MidDiameter,
|
||
InsulationFilmToBottemHeight1 = b.InsulationFilmToBottemHeight1,
|
||
InsulationFilmToBottemHeight2 = b.InsulationFilmToBottemHeight2,
|
||
InsulationFilmToBottemHeight3 = b.InsulationFilmToBottemHeight3,
|
||
InsulationFilmToBottemHeight4 = b.InsulationFilmToBottemHeight4,
|
||
InsulationFilmToBottemHeight5 = b.InsulationFilmToBottemHeight5,
|
||
InsulationFilmToBottemHeight6 = b.InsulationFilmToBottemHeight6,
|
||
CCDAfterCoatedAlignmentTestResult = b.CCDAfterCoatedAlignmentTestResult,
|
||
CCDAfterCoatedApperanceTestResult = b.CCDAfterCoatedApperanceTestResult,
|
||
Station = b.Station,
|
||
Flag = b.Flag,
|
||
Flag1 = b.Flag1,
|
||
Result = b.Result,
|
||
Info = b.Info,
|
||
MaterialCode = b.MaterialCode
|
||
}).ToList();
|
||
|
||
// 保存到临时表
|
||
await CommonMethods.db.AddReturnBoolAsync<Temprfidtable>(tempList);
|
||
}
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteError($"AddReturnBoolAsync 耗时 {sw.ElapsedMilliseconds}ms", "SysErrorLog");
|
||
|
||
sw.Restart();
|
||
//数据保存本地
|
||
string strData = string.Empty;//数据字符串,本地保存
|
||
foreach (BGearEntity m in list)
|
||
{
|
||
foreach (var item in m.GetType().GetProperties())
|
||
{
|
||
strData += item.GetValue(m) + ",";
|
||
}
|
||
strData = strData.TrimEnd(',');
|
||
BlankingDataSaveLocal(strData);
|
||
}
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteError($"BlankingDataSaveLocal 耗时 {sw.ElapsedMilliseconds}ms", "SysErrorLog");
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteError($"电芯出站数据List为空", "SysErrorLog");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"电芯出站数据保存异常:{ex}", "SysErrorLog");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 电池结果判断
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private ushort GetResult(int[] res, ref string strErr)
|
||
{
|
||
strErr = "";
|
||
int resNum = 0;
|
||
int resNum1 = 0;
|
||
int resultNum = 0;
|
||
|
||
for (int i = 0; i < res.Length; i++)
|
||
{
|
||
if (i == 0)
|
||
{
|
||
switch (res[i])
|
||
{
|
||
case 1:
|
||
resNum = 1;
|
||
break;
|
||
case 2:
|
||
resNum = 2;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
else if (i == 1)
|
||
{
|
||
switch (res[i])
|
||
{
|
||
case 1:
|
||
resNum1 = 1;
|
||
break;
|
||
case 2:
|
||
resNum1 = 2;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (resNum == 1 && resNum1 == 1) //OK
|
||
{
|
||
resultNum = 1;
|
||
strErr = "CCD检测结果OK";
|
||
}
|
||
else if (resNum == 2 && resNum1 == 1) //对齐度NG
|
||
{
|
||
resultNum = 2;
|
||
strErr = "包膜后对齐度检测结果不良".Translated();
|
||
}
|
||
else if (resNum == 1 && resNum1 == 2) //外观检测NG
|
||
{
|
||
resultNum = 2;
|
||
strErr = "贴膜后外观检测结果不良".Translated();
|
||
}
|
||
else if (resNum == 2 && resNum1 == 2)
|
||
{
|
||
strErr = "包膜后对齐度检测结果不良,".Translated() + "贴膜后外观检测结果不良".Translated();
|
||
resultNum = 2;
|
||
}
|
||
else
|
||
{
|
||
strErr += "CCD反馈" + resultNum.ToString() + "检测结果未知".Translated();
|
||
resultNum = 2;
|
||
}
|
||
strErr = strErr.TrimEnd(',');
|
||
return (ushort)resultNum;
|
||
}
|
||
private String GetFinalResult(int res, ref string strErr)
|
||
{
|
||
strErr = "";
|
||
switch (res)
|
||
{
|
||
case 1:
|
||
strErr = "OK";
|
||
break;
|
||
case 2:
|
||
strErr = "上料扫码NG";
|
||
break;
|
||
case 3:
|
||
strErr = "上位机喷码NG";
|
||
break;
|
||
case 4:
|
||
strErr = "喷码扫码NG";
|
||
break;
|
||
case 6:
|
||
strErr = "飞拍NG";
|
||
break;
|
||
case 7:
|
||
strErr = "MES出站NG";
|
||
break;
|
||
case 8:
|
||
strErr = "MES进站NG";
|
||
break;
|
||
case 9:
|
||
strErr = "CCD1_留白异常";
|
||
break;
|
||
case 10:
|
||
strErr = "CCD1_对齐度异常";
|
||
break;
|
||
case 11:
|
||
strErr = "CCD1_ ce码异常 ";
|
||
break;
|
||
case 12:
|
||
strErr = "CCD1_二维码异常";
|
||
break;
|
||
case 13:
|
||
strErr = "CCD2_留白异常";
|
||
break;
|
||
case 14:
|
||
strErr = "CCD2_对齐度异常";
|
||
break;
|
||
case 15:
|
||
strErr = "CCD2_ ce码异常";
|
||
break;
|
||
case 16:
|
||
strErr = "CCD2_二维码异常";
|
||
break;
|
||
case 17:
|
||
strErr = "CCD1_算法NG";
|
||
break;
|
||
case 18:
|
||
strErr = "CCD2_算法NG";
|
||
break;
|
||
case 19:
|
||
strErr = "顶部直径NG";
|
||
break;
|
||
case 20:
|
||
strErr = "中间直径NG";
|
||
break;
|
||
case 21:
|
||
strErr = "底部直径NG";
|
||
break;
|
||
case 22:
|
||
strErr = "飞拍算法NG";
|
||
break;
|
||
case 23:
|
||
strErr = "CCD1数据NG";
|
||
break;
|
||
case 24:
|
||
strErr = "CCD2数据NG";
|
||
break;
|
||
case 25:
|
||
strErr = "CCD1_气泡NG_异物NG";
|
||
break;
|
||
case 26:
|
||
strErr = "CCD2_气泡NG_异物NG";
|
||
break;
|
||
case 27:
|
||
strErr = "CCD1_重叠区NG";
|
||
break;
|
||
case 28:
|
||
strErr = "CCD1_褶皱NG";
|
||
break;
|
||
case 29:
|
||
strErr = "CCD1_翘起NG";
|
||
break;
|
||
case 30:
|
||
strErr = "CCD2_重叠区NG";
|
||
break;
|
||
case 31:
|
||
strErr = "CCD2_褶皱NG";
|
||
break;
|
||
case 32:
|
||
strErr = "CCD2_翘起NG";
|
||
break;
|
||
case 33:
|
||
strErr = "CCD1_破损NG";
|
||
break;
|
||
case 34:
|
||
strErr = "CCD2_破损NG";
|
||
break;
|
||
}
|
||
return (String)strErr;
|
||
}
|
||
|
||
#region 结果数据上传
|
||
/// <summary>
|
||
/// 结果数据上传
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
private async Task UpProducResultParamAsync(List<BGearEntity> listBger)
|
||
{
|
||
var sw = Stopwatch.StartNew();
|
||
if (listBger == null || listBger.Count == 0) return;
|
||
|
||
var tasks = listBger
|
||
.Where(m => m.BarCode != "NG" && m.BarCode != "ERROR")
|
||
.Select(async m =>
|
||
{
|
||
// 并行调用 MES
|
||
var (success, message) = await CommonMethods.hbgMes.ProducResultParamAsync(
|
||
CommonMethods.mesConfig.ProductResultParams,
|
||
CommonMethods.mesConfig.siteCode,
|
||
CommonMethods.mesConfig.lineCode,
|
||
CommonMethods.mesConfig.equipNum,
|
||
CommonMethods.mesConfig.MaterialCode,
|
||
CommonMethods.mesConfig.mesUserName,
|
||
m, MesUploadType.DD);
|
||
|
||
if (success)
|
||
{
|
||
m.Flag1 = "1";
|
||
}
|
||
});
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteLog($"UpProducResultParamAsync ProducResultParamAsync 耗时{sw.ElapsedMilliseconds}ms", "A出站");
|
||
|
||
sw.Restart();
|
||
|
||
var successCodes = listBger
|
||
.Where(m => m.Flag1 == "1")
|
||
.Select(m => m.BarCode)
|
||
.ToList();
|
||
if (successCodes.Any())
|
||
{
|
||
await CommonMethods.db.UpdateAsync<BGearEntity>(
|
||
it => it.Flag1 == "1",
|
||
it => successCodes.Contains(it.BarCode) // 条件
|
||
);
|
||
}
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteLog($"UpProducResultParamAsync UpdateAsync 耗时{sw.ElapsedMilliseconds}ms", "A出站");
|
||
|
||
sw.Restart();
|
||
// 等待所有任务完成
|
||
await Task.WhenAll(tasks);
|
||
sw.Stop();
|
||
LogHelper.Instance.WriteLog($"UpProducResultParamAsync WhenAll 耗时{sw.ElapsedMilliseconds}ms", "A出站");
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 定时补传
|
||
/// <summary>
|
||
/// 定时补传
|
||
/// </summary>
|
||
//public void ReUpProducResultParam()
|
||
//{
|
||
// try
|
||
// {
|
||
// List<BGearEntity> list = CommonMethods.db.QuerySqlList<BGearEntity>(CommonMethods.dBSQL.GetOutFlag1Sql("2"));
|
||
// if (CommonMethods.sysConfig.MesModeSwitching && CommonMethods.mesConfig.isConnected)
|
||
// {
|
||
// UpProducResultParam(list);
|
||
// }
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// LogHelper.Instance.WriteError($"结果数据补传失败, 原因: {ex}", "系统报错");
|
||
// //throw new Exception(ex.ToString());
|
||
// }
|
||
//}
|
||
#endregion
|
||
|
||
#endregion
|
||
|
||
#region 喷码工位(数据处理)
|
||
/// <summary>
|
||
/// 喷码工位A
|
||
/// </summary>
|
||
private async Task JettingStationA(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
if (bArrays.IsSuccess && bArrays.Content != null)
|
||
{
|
||
try
|
||
{
|
||
sw.Start();
|
||
|
||
//解析数据
|
||
List<AGearEntity> list = new List<AGearEntity>();//实体
|
||
List<short> listRes = GetList<short>(4, 2);//默认为1,良品
|
||
List<string> listJetCode = GetList<string>(4, "");//喷码
|
||
List<bool> listMark = GetList<bool>(4, false);//有料标志
|
||
List<string> listCode = GetList<string>(4, "");//电芯码
|
||
|
||
// 从D7021开始
|
||
#region 数据解析
|
||
byte[] bytes = bArrays.Content.ByteReverse();//高低位转换
|
||
//有料标志
|
||
for (int i = 0; i < listMark.Count; i++)
|
||
{
|
||
listMark[i] = ShortLib.GetShortFromByteArray(bArrays.Content, i * 2) == 1 ? true : false;// 从D7021开始
|
||
if (listMark[i])
|
||
{
|
||
//条码长度
|
||
int codeLength = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (20 * 2));// 从D7041开始
|
||
if (codeLength > 0)
|
||
{
|
||
//条码
|
||
listCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 50) + (79 * 2), codeLength, System.Text.Encoding.ASCII).// 从D7100开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
else
|
||
{
|
||
listCode[i] = "ERROR";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
CommonMethods.AddDataMonitorLog(0, "喷码工位A有料标志为0".Translated() + "通道号".Translated() + $":{i}");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 清空喷码机缓存区
|
||
//下发喷码前重新连接喷码机,下发完成后断开
|
||
CommonMethods.jetClientA.SetTCPParams(IPAddress.Parse("192.168.101.26"), 10086);
|
||
CommonMethods.jetClientA.ConnectWithRetry();
|
||
JetReturnResult jetReturn = new JetReturnResult();
|
||
JetKey jetKey = new JetKey();
|
||
jetKey.KEY = 66;//清空缓存数据命令
|
||
CommonMethods.jetClientA.SendAndGetRes(JsonHelper.EntityToJson(jetKey), ref jetReturn);
|
||
if (jetReturn.RS == "" || jetReturn.RS == "ERROR")
|
||
{
|
||
//listRes[i] = 3;
|
||
LogHelper.Instance.WriteError($"喷码A清空缓存区失败", "喷码A");
|
||
}
|
||
#endregion
|
||
|
||
#region 数据组合
|
||
Random random = new Random();
|
||
for (int i = 0; i < listCode.Count; i++)
|
||
{
|
||
if (listMark[i])
|
||
{
|
||
if (listCode[i].Contains("_")) { listJetCode[i] = $"TEXT{random.Next(100000, 999999)}"; }
|
||
else if (String.IsNullOrEmpty(listCode[i]) || listCode[i].Trim() == "ERROR" || !IsAlphaNumeric(listCode[i]))
|
||
{
|
||
listRes[i] = 2;
|
||
listJetCode[i] = "";
|
||
}
|
||
else
|
||
{
|
||
//喷码
|
||
listJetCode[i] = listCode[i];
|
||
//更新进站喷码字段数据
|
||
AGearEntity m = new AGearEntity();
|
||
m.BarCode = listCode[i];
|
||
m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
m.JetCode = listJetCode[i];//滚标机喷码工序喷的是GB码
|
||
list.Add(m);
|
||
|
||
#region 获取MES下发喷码 取消
|
||
//上传MES
|
||
//if (CommonMethods.sysConfig.MesModeSwitching && CommonMethods.mesConfig.isConnected && false)
|
||
//{
|
||
// List<string> codeList = new List<string>();
|
||
// codeList.Add(listCode[i]);
|
||
// var (success, mesDMCCode, mesReason) = await CommonMethods.hbgMes.GetDMCJetCodeAsync(CommonMethods.mesConfig.DMCJetCodeURL, CommonMethods.mesConfig.equipNum, codeList);
|
||
// if (success) //调用接口成功
|
||
// {
|
||
// //喷码
|
||
// m.JetCode = listJetCode[i] = mesDMCCode[i];
|
||
|
||
// }
|
||
// else
|
||
// {
|
||
// listRes[i] = 4;
|
||
// m.Remark += $",{mesReason[i]}";
|
||
// }
|
||
//}
|
||
//else
|
||
//{
|
||
// listRes[i] = 4;
|
||
// m.Remark = "MES断连";
|
||
// m.Flag = "0";
|
||
// CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short);//反馈MES结果
|
||
//}
|
||
|
||
#endregion
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 喷码下发喷码机
|
||
for (int i = listCode.Count - 1; i >= 0; i--)
|
||
{
|
||
if (CommonMethods.jetClientA.Connected)
|
||
{
|
||
//喷码下发喷码机
|
||
//json组合
|
||
JetData jetData = new JetData();
|
||
jetData.MID = 1;
|
||
jetData.SID = 1;
|
||
jetData.VAL = listJetCode[i];
|
||
JetVisible jetVisible = new JetVisible();
|
||
jetVisible.MID = 2;
|
||
if (listJetCode[i] != "")
|
||
jetVisible.VAL = true;
|
||
else
|
||
jetVisible.VAL = false;
|
||
List<JetData> listJetData = new List<JetData>();
|
||
List<JetVisible> listJetVisible = new List<JetVisible>();
|
||
listJetData.Add(jetData);
|
||
listJetVisible.Add(jetVisible);
|
||
|
||
JetClass jet = new JetClass();
|
||
jet.KEY = 82;//喷码下发喷码机缓存命令
|
||
jet.DATA = listJetData;
|
||
jet.VISIBLE = listJetVisible;
|
||
string json = JsonHelper.EntityToJson(jet);
|
||
CommonMethods.jetClientA.SendAndGetRes(json, ref jetReturn);
|
||
if (jetReturn.RS == "" || jetReturn.RS == "ERROR")
|
||
{
|
||
listRes[i] = 3;
|
||
LogHelper.Instance.WriteError($"喷码A条码:{listJetCode}下发失败,返回结果:{jetReturn.RS}", "喷码A");
|
||
}
|
||
else { listRes[i] = 1; }
|
||
LogHelper.Instance.WriteError($"喷码json: {json}", "喷码A"); //临时日志,可取消
|
||
}
|
||
else
|
||
{
|
||
listRes[i] = 5;
|
||
LogHelper.Instance.WriteError($"喷码机A断连,喷码下发失败", "喷码A");
|
||
}
|
||
}
|
||
CommonMethods.jetClientA.Close();
|
||
//Thread.Sleep(100);
|
||
#endregion
|
||
|
||
#region 数据反馈&保存
|
||
//写入·结果反馈
|
||
JYResult result = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[1].VarAddress, listRes.ToArray(), PLC.DataType.ArrShort);//判断结果写入PLC
|
||
if (result != null)
|
||
{
|
||
string strres = "喷码A工位发送给PLC".Translated() + $",[{pf.VarList[1].VarAddress}]:{string.Join(",", listRes)}";
|
||
CommonMethods.AddDataMonitorLog(0, strres);
|
||
}
|
||
|
||
//喷码下发plc
|
||
//for (int j = 0; j < listJetCode.Count; j++)
|
||
//{
|
||
// JYResult result1 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[j + 3].VarAddress, listJetCode[j], PLC.DataType.String);
|
||
// if (result1 != null)
|
||
// {
|
||
// string strres = $"喷码A工位发送给PLC喷码{j + 1},[{pf.VarList[j + 3].VarAddress}]:{listJetCode[j]}";
|
||
// LogHelper.Instance.WriteLog(strres);
|
||
// }
|
||
//}
|
||
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
|
||
|
||
//#region 喷码下发数量与电芯有料数量比较
|
||
//if (CommonMethods.jetClientA.Connected)
|
||
//{
|
||
// jetKey = new JetKey();
|
||
// jetKey.KEY = 65;//查询未打印缓存数据个数
|
||
// string js = JsonHelper.EntityToJson(jetKey);
|
||
// int cacheCount = 0;
|
||
// CommonMethods.jetClientA.SendAndGetRes(jetKey.KEY, js, ref jetReturn, ref cacheCount);
|
||
// //if (jetReturn.DATA != listCode.Count.ToString())//list.Count
|
||
// //{
|
||
// // LogHelper.Instance.WriteError($"喷码A下发数量与电芯有料数量不一致,条码:{string.Join(",", listCode)},,喷码数量:{jetReturn.DATA}", "SysErrorLog");
|
||
// //}
|
||
//}
|
||
//#endregion
|
||
|
||
//数据存储 -异步处理
|
||
//lock (this)
|
||
//{
|
||
// SaveJetCodeData(list);
|
||
//}
|
||
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"喷码线程A数据处理:{ex}", "喷码A");
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
|
||
CommonMethods.jetClientA.Close();
|
||
}
|
||
sw.Stop();
|
||
////输出上料数据处理耗时:
|
||
CommonMethods.AddDataMonitorLog(0, "卷芯喷码A获取/下发共耗时".Translated() + $":{sw.Elapsed.TotalMilliseconds}ms");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 喷码工位B D8000
|
||
/// </summary>
|
||
private async Task JettingStationB(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
if (bArrays.IsSuccess && bArrays.Content != null)
|
||
{
|
||
try
|
||
{
|
||
sw.Start();
|
||
|
||
//解析数据
|
||
List<AGearEntity> list = new List<AGearEntity>();//实体
|
||
List<short> listRes = GetList<short>(4, 2);//默认为1,良品
|
||
List<string> listJetCode = GetList<string>(4, "");//喷码
|
||
List<bool> listMark = GetList<bool>(4, false);//有料标志
|
||
List<string> listCode = GetList<string>(4, "");//电芯码
|
||
|
||
// 从D8021开始
|
||
#region 数据解析
|
||
byte[] bytes = bArrays.Content.ByteReverse();//高低位转换
|
||
|
||
//有料标志
|
||
for (int i = 0; i < listMark.Count; i++)
|
||
{
|
||
listMark[i] = ShortLib.GetShortFromByteArray(bArrays.Content, i * 2) == 1 ? true : false;// 从D8021开始
|
||
if (listMark[i])
|
||
{
|
||
//条码长度
|
||
int codeLength = ShortLib.GetShortFromByteArray(bArrays.Content, (i * 2) + (20 * 2));// 从D8041开始
|
||
if (codeLength > 0)
|
||
{
|
||
//条码
|
||
listCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, (i * 50) + (79 * 2), codeLength, System.Text.Encoding.ASCII).// 从D8100开始
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
else
|
||
{
|
||
listCode[i] = "ERROR";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
CommonMethods.AddDataMonitorLog(0, "喷码工位B有料标志为0".Translated() + "通道号".Translated() + $":{i}");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 清空喷码机缓存区
|
||
CommonMethods.jetClientB.SetTCPParams(IPAddress.Parse("192.168.100.27"), 10086);
|
||
CommonMethods.jetClientB.ConnectWithRetry();
|
||
JetReturnResult jetReturn = new JetReturnResult();
|
||
JetKey jetKey = new JetKey();
|
||
jetKey.KEY = 66;//清空缓存数据命令
|
||
CommonMethods.jetClientB.SendAndGetRes(JsonHelper.EntityToJson(jetKey), ref jetReturn);
|
||
if (jetReturn.RS == "" || jetReturn.RS == "ERROR")
|
||
{
|
||
//listRes[i] = 3;
|
||
LogHelper.Instance.WriteError($"喷码B清空缓存区失败", "喷码B");
|
||
}
|
||
#endregion
|
||
|
||
#region 数据组合
|
||
Random random = new Random();
|
||
for (int i = 0; i < listCode.Count; i++)
|
||
{
|
||
|
||
if (listMark[i])
|
||
{
|
||
if (listCode[i].Contains("_")) { listJetCode[i] = $"TEXT{random.Next(100000, 999999)}"; }
|
||
else if (String.IsNullOrEmpty(listCode[i]) || listCode[i].Trim() == "ERROR" || !IsAlphaNumeric(listCode[i]))
|
||
{
|
||
listRes[i] = 2;
|
||
listJetCode[i] = "";
|
||
}
|
||
else
|
||
{
|
||
//喷码
|
||
listJetCode[i] = listCode[i];
|
||
//更新进站喷码字段数据
|
||
AGearEntity m = new AGearEntity();
|
||
m.BarCode = listCode[i];
|
||
m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
m.JetCode = listJetCode[i];//滚标机喷码工序喷的是GB码
|
||
list.Add(m);
|
||
|
||
#region 获取MES下发喷码 取消
|
||
//上传MES
|
||
//if (CommonMethods.sysConfig.MesModeSwitching && CommonMethods.mesConfig.isConnected && false)
|
||
//{
|
||
// List<string> codeList = new List<string>();
|
||
// codeList.Add(listCode[i]);
|
||
// var (success, mesDMCCode, mesReason) = await CommonMethods.hbgMes.GetDMCJetCodeAsync(CommonMethods.mesConfig.DMCJetCodeURL, CommonMethods.mesConfig.equipNum, codeList);
|
||
// if (success) //调用接口成功
|
||
// {
|
||
// //喷码
|
||
// m.JetCode = listJetCode[i] = mesDMCCode[i];
|
||
|
||
// }
|
||
// else
|
||
// {
|
||
// listRes[i] = 4;
|
||
// m.Remark += $",{mesReason[i]}";
|
||
// }
|
||
//}
|
||
//else
|
||
//{
|
||
// listRes[i] = 4;
|
||
// m.Remark = "MES断连";
|
||
// m.Flag = "0";
|
||
// CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue("D80", 1, PLC.DataType.Short);//反馈MES结果
|
||
//}
|
||
|
||
#endregion
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 喷码下发喷码机
|
||
for (int i = listCode.Count - 1; i >= 0; i--)
|
||
{
|
||
if (CommonMethods.jetClientB.Connected)
|
||
{
|
||
//喷码下发喷码机
|
||
//json组合
|
||
JetData jetData = new JetData();
|
||
jetData.MID = 1;
|
||
jetData.SID = 1;
|
||
jetData.VAL = listJetCode[i];
|
||
//
|
||
JetVisible jetVisible = new JetVisible();
|
||
jetVisible.MID = 2;
|
||
if (listJetCode[i] != "")
|
||
jetVisible.VAL = true;
|
||
else
|
||
jetVisible.VAL = false;
|
||
List<JetData> listJetData = new List<JetData>();
|
||
List<JetVisible> listJetVisible = new List<JetVisible>();
|
||
listJetData.Add(jetData);
|
||
listJetVisible.Add(jetVisible);
|
||
|
||
JetClass jet = new JetClass();
|
||
jet.KEY = 82;//喷码下发喷码机缓存命令
|
||
jet.DATA = listJetData;
|
||
jet.VISIBLE = listJetVisible;
|
||
|
||
|
||
string json = JsonHelper.EntityToJson(jet);
|
||
CommonMethods.jetClientB.SendAndGetRes(json, ref jetReturn);
|
||
if (jetReturn.RS == "" || jetReturn.RS == "ERROR")
|
||
{
|
||
listRes[i] = 3;
|
||
LogHelper.Instance.WriteError($"喷码B条码:{listJetCode}下发失败,返回结果:{jetReturn.RS}", "喷码B");
|
||
}
|
||
else { listRes[i] = 1; }
|
||
LogHelper.Instance.WriteError($"喷码json: {json}", "喷码B"); //临时日志,可取消
|
||
}
|
||
else
|
||
{
|
||
listRes[i] = 5;
|
||
LogHelper.Instance.WriteError($"喷码机B断连,喷码下发失败", "喷码B");
|
||
}
|
||
}
|
||
CommonMethods.jetClientB.Close();
|
||
//Thread.Sleep(100);
|
||
#endregion
|
||
|
||
#region 数据反馈&保存
|
||
//写入·结果反馈
|
||
JYResult result = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[1].VarAddress, listRes.ToArray(), PLC.DataType.ArrShort);//判断结果写入PLC
|
||
if (result != null)
|
||
{
|
||
string strres = "喷码B工位发送给PLC".Translated() + $",[{pf.VarList[1].VarAddress}]:{string.Join(",", listRes)}";
|
||
CommonMethods.AddDataMonitorLog(0, strres);
|
||
}
|
||
|
||
//喷码下发plc
|
||
//for (int j = 0; j < listJetCode.Count; j++)
|
||
//{
|
||
// JYResult result1 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[j + 3].VarAddress, listJetCode[j], PLC.DataType.String);
|
||
// if (result1 != null)
|
||
// {
|
||
// string strres = $"喷码B工位发送给PLC喷码{j + 1},[{pf.VarList[j + 3].VarAddress}]:{listJetCode[j]}";
|
||
// LogHelper.Instance.WriteLog(strres);
|
||
// }
|
||
//}
|
||
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
|
||
|
||
//#region 喷码下发数量与电芯有料数量比较
|
||
//if (CommonMethods.jetClientB.Connected)
|
||
//{
|
||
// jetKey = new JetKey();
|
||
// jetKey.KEY = 65;//查询未打印缓存数据个数
|
||
// string js = JsonHelper.EntityToJson(jetKey);
|
||
// CommonMethods.jetClientB.SendAndGetRes(jetKey.KEY, js, ref jetReturn, ref cacheCount);
|
||
// //if (jetReturn.DATA != listCode.Count.ToString())//list.Count
|
||
// //{
|
||
// // LogHelper.Instance.WriteError($"喷码B下发数量与电芯有料数量不一致,条码:{string.Join(",", listCode)},,喷码数量:{jetReturn.DATA}", "SysErrorLog");
|
||
// //}
|
||
//}
|
||
//#endregion
|
||
|
||
//数据存储 -异步处理
|
||
|
||
//SaveJetCodeData(list);
|
||
|
||
|
||
#endregion
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"喷码线程B数据处理:{ex}", "喷码B");
|
||
CommonMethods.jetClientB.Close();
|
||
CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
|
||
}
|
||
sw.Stop();
|
||
////输出上料数据处理耗时:
|
||
CommonMethods.AddDataMonitorLog(0, "卷芯喷码B获取/下发共耗时".Translated() + $":{sw.Elapsed.TotalMilliseconds}ms");
|
||
}
|
||
}
|
||
|
||
|
||
#region 保存上料数据
|
||
/// <summary>
|
||
/// 保存喷码数据到数据
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
private async void SaveJetCodeData(List<AGearEntity> list)
|
||
{
|
||
//数据的保存
|
||
try
|
||
{
|
||
if (list != null && list.Count > 0)
|
||
{
|
||
foreach (var m in list)
|
||
{
|
||
int a = await CommonMethods.db.UpdateSingleAsync(m, it => new AGearEntity() { JetCode = m.JetCode }, it => it.BarCode == m.BarCode);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"喷码数据保存处理异常:{ex}", "SysErrorLog");
|
||
}
|
||
}
|
||
#endregion
|
||
#endregion
|
||
|
||
#region 复投工位
|
||
private async Task ReInStation(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
if (bArrays.IsSuccess)//&& bArrays.Content != null
|
||
{
|
||
try
|
||
{
|
||
sw.Start();
|
||
|
||
#region 数据解析
|
||
//byte[] bytes = bArrays.Content.ByteReverse();//高低位转换
|
||
int codeListLen = CommonMethods.tempCodeList.Count;
|
||
if (codeListLen >= 8)
|
||
{
|
||
//复投条码下发plc
|
||
for (int i = 0; i < 8; i++)
|
||
{
|
||
string tempCode = CommonMethods.tempCodeList.Dequeue();
|
||
JYResult result1 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(
|
||
pf.VarList[i + 3].VarAddress,
|
||
tempCode,
|
||
PLC.DataType.String
|
||
);
|
||
CommonMethods.AddDataMonitorLog(0, string.Format("卷芯复投电芯{0}, 电芯:{1}", i, tempCode));
|
||
if (result1 != null)
|
||
{
|
||
string strres = $"复投工位发送给PLC电芯码{i + 1},[{pf.VarList[i + 3].VarAddress}]:{tempCode}";
|
||
//LogHelper.Instance.WriteLog(strres);
|
||
LogHelper.Instance.WriteError(strres, "系统报错");
|
||
}
|
||
}
|
||
}
|
||
else if (codeListLen > 0 && codeListLen < 8)
|
||
{
|
||
//复投条码下发plc
|
||
for (int i = 0; i < codeListLen; i++)
|
||
{
|
||
string tempCode = CommonMethods.tempCodeList.Dequeue();
|
||
JYResult result1 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(
|
||
pf.VarList[i + 3].VarAddress,
|
||
tempCode,
|
||
PLC.DataType.String
|
||
);
|
||
if (result1 != null)
|
||
{
|
||
string strres = $"复投工位发送给PLC电芯码{i + 1},[{pf.VarList[i + 3].VarAddress}]:{tempCode}";
|
||
//LogHelper.Instance.WriteLog(strres);
|
||
LogHelper.Instance.WriteError(strres, "系统报错");
|
||
}
|
||
}
|
||
}
|
||
//else
|
||
//{
|
||
// CommonMethods.plcDevices[0].WriteInt16("D9052", 0);//plc恢复扫码信号
|
||
//}
|
||
|
||
#endregion
|
||
|
||
#region 数据反馈
|
||
// Thread.Sleep(5000);
|
||
// CommonMethods.plcDevices[0].WriteInt16("D9053", 0);//触发复位
|
||
if (CommonMethods.tempCodeList.Count == 0) { CommonMethods.plcDevices[0].WriteInt16("D9052", 0); }//plc恢复扫码信号
|
||
#endregion
|
||
|
||
sw.Stop();
|
||
|
||
////输出复投数据处理耗时:
|
||
CommonMethods.AddDataMonitorLog(0, "卷芯复投共耗时".Translated() + $":{sw.Elapsed.TotalMilliseconds}ms");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
CommonMethods.AddDataMonitorLog(2, "批量复投工位异常:".Translated() + ex.Message);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 结构件料盘工位(数据处理)
|
||
/// <summary>
|
||
/// 结构件料盘工位
|
||
/// </summary>
|
||
/// <param name="resByte"></param>
|
||
/// <param name="plcModel"></param>
|
||
///
|
||
|
||
private async Task MaterialStation(JYResult<byte[]> bArrays, object plcModel)
|
||
{
|
||
PlcGroup pf = plcModel as PlcGroup;
|
||
var sw = new Stopwatch();
|
||
if (bArrays.IsSuccess && bArrays.Content != null)
|
||
{
|
||
|
||
sw.Restart();
|
||
#region 初始化数据
|
||
List<DGearEntity> list = new List<DGearEntity>();//实体
|
||
List<string> listMaterialCode = GetList<string>(1, "");//料盘码
|
||
List<short> listRes = GetList<short>(1, 1);//默认为1,良品
|
||
#endregion
|
||
|
||
#region 数据解析
|
||
byte[] bytes = bArrays.Content.ByteReverse();
|
||
for (int i = 0; i < listMaterialCode.Count; i++)
|
||
{
|
||
listMaterialCode[i] = StringLib.GetStringFromByteArrayByEncoding(bytes, i * 40, 39, System.Text.Encoding.ASCII).
|
||
Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
|
||
}
|
||
#endregion
|
||
|
||
for (int i = 0; i < listMaterialCode.Count; i++)
|
||
{
|
||
DGearEntity m = new DGearEntity();
|
||
try
|
||
{
|
||
switch (pf.GroupName)
|
||
{
|
||
case "结构件料盘B":
|
||
m.ChannelNo = 1;
|
||
break;
|
||
case "结构件料盘D":
|
||
m.ChannelNo = 2;
|
||
break;
|
||
}
|
||
m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
m.MaterialCode = listMaterialCode[i];
|
||
m.MaterialCodes = CommonMethods.mesConfig.MaterialCode;
|
||
m.EquipNum = CommonMethods.mesConfig.equipNum;
|
||
m.Flag = 0;
|
||
if (String.IsNullOrEmpty(m.MaterialCode) || m.MaterialCode.Trim() == "ERROR" || m.MaterialCode.Trim() == "NG")
|
||
{
|
||
m.MaterialCode = "NG";
|
||
m.Result = "NG";
|
||
m.Remark = "料盘码不良";
|
||
listRes[i] = 2;
|
||
}
|
||
else
|
||
{
|
||
m.Result = "OK";
|
||
m.Remark = "良品";
|
||
|
||
#region MES对接
|
||
if (CommonMethods.sysConfig.MesModeSwitching)
|
||
{
|
||
var (mesRes, Mesage, mesResType) = await CommonMethods.hbgMes.RawMaterialStationAsync(CommonMethods.mesConfig.MaterialInputInfo,
|
||
CommonMethods.mesConfig.MaterialCode, CommonMethods.mesConfig.equipNum, m.MaterialCode);
|
||
|
||
if (mesRes) //调用接口成功
|
||
{
|
||
m.Flag = 1;
|
||
//本地判断OK,MES判断OK
|
||
if (m.Result == "OK" && mesResType == MesResType.D)
|
||
{
|
||
m.Result = "OK";
|
||
m.Remark = m.Remark + Mesage;
|
||
listRes[i] = 1;
|
||
}
|
||
//本地判断OK,MES判断NG
|
||
else if (m.Result == "OK" && mesResType != MesResType.D)
|
||
{
|
||
switch (mesResType)
|
||
{
|
||
case MesResType.B:
|
||
m.Result = "MES NG";
|
||
m.Remark = $"{Mesage}";
|
||
listRes[i] = 9;
|
||
break;
|
||
default:
|
||
m.Result = "MES NG";
|
||
m.Remark = $"{Mesage}";
|
||
listRes[i] = 9;
|
||
break;
|
||
}
|
||
}
|
||
//其他情况
|
||
else
|
||
{
|
||
m.Result = " MES NG";
|
||
m.Remark = Mesage;
|
||
listRes[i] = 9;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
m.Result = " MES NG";
|
||
m.Remark = Mesage;
|
||
listRes[i] = 9;
|
||
if (Mesage.Contains("接口调用失败"))
|
||
{
|
||
listRes[i] = 10;
|
||
CommonMethods.AddLog(false, $"原材料上传失败,请联系IT人员检查MES网络或服务器");
|
||
LogHelper.Instance.WriteLog($"原材料上传失败,请联系IT人员检查MES网络或服务器", "SysErrorLog");
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
list.Add(m);
|
||
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"结构件料盘线程数据解析:{ex}", "系统报错");
|
||
}
|
||
}
|
||
JYResult result1 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[2].VarAddress, listRes.ToArray(), PLC.DataType.ArrShort);//判断结果写入PLC
|
||
|
||
JYResult result3 = CommonMethods.plcDevices[pf.PlcNum - 1].WriteValue(pf.VarList[0].VarAddress, 0);//置位
|
||
//数据存储 -异步处理
|
||
|
||
await SaveMaterialData(list);
|
||
|
||
sw.Stop();
|
||
////输出上料数据处理耗时:
|
||
CommonMethods.AddDataMonitorLog(0, $"结构件料盘线程共耗时:{sw.Elapsed.TotalMilliseconds}ms");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 结构件料盘保存
|
||
/// </summary>
|
||
/// <param name="list"></param>
|
||
private async Task SaveMaterialData(List<DGearEntity> list)
|
||
{
|
||
//数据的保存
|
||
try
|
||
{
|
||
if (list != null && list.Count > 0)
|
||
{
|
||
string strRes = "";
|
||
foreach (DGearEntity m in list)
|
||
{
|
||
if (m.MaterialCode != "NG")
|
||
{
|
||
bool b1 = await CommonMethods.db.AddReturnBoolAsync<DGearEntity>(m);
|
||
strRes = b1 ? ",本存Yes" : ",本存No";
|
||
}
|
||
else
|
||
{
|
||
strRes = ",不存储";
|
||
}
|
||
m.Remark = m.Remark + strRes;
|
||
}
|
||
//数据显示
|
||
CommonMethods.ShowMaterialDelegate.Invoke(list);
|
||
|
||
}
|
||
else
|
||
{
|
||
LogHelper.Instance.WriteError($"结构件料盘数据List为空", "SysErrorLog");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"结构件料盘保存处理异常:{ex}", "SysErrorLog");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 创建空的数组List
|
||
public List<T> GetList<T>(int num, T data)
|
||
{
|
||
List<T> list = new List<T>();
|
||
for (int i = 0; i < num; i++)
|
||
{
|
||
list.Add(data);
|
||
}
|
||
return list;
|
||
|
||
}
|
||
|
||
public List<MesResType> GetMesType(int Num)
|
||
{
|
||
List<MesResType> mesResTypes = new List<MesResType>();
|
||
for (int i = 0; i < Num; i++)
|
||
{
|
||
mesResTypes.Add(MesResType.UnKnow);
|
||
}
|
||
return mesResTypes;
|
||
}
|
||
|
||
/// <summary>
|
||
/// PLC内批量写\0
|
||
/// </summary>
|
||
/// <param name="Num"></param>
|
||
/// <returns></returns>
|
||
public string GetString_0(int Num)
|
||
{
|
||
string ff = "";
|
||
for (int i = 0; i < Num; i++)
|
||
{
|
||
ff = ff.Insert(ff.Length, "\0");
|
||
}
|
||
return ff;
|
||
}
|
||
|
||
public string GetBarCodeNum(int Num)
|
||
{
|
||
string CodeNo = "";
|
||
int CodeLenth = Num.ToString().Length;
|
||
|
||
switch (CodeLenth)
|
||
{
|
||
case 1:
|
||
return CodeNo = $"00000{Num}";
|
||
case 2:
|
||
return CodeNo = $"0000{Num}";
|
||
case 3:
|
||
return CodeNo = $"000{Num}";
|
||
case 4:
|
||
return CodeNo = $"00{Num}";
|
||
case 5:
|
||
return CodeNo = $"0{Num}";
|
||
default:
|
||
return CodeNo = $"{Num}";
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 检测数据是否只包含字母加数字
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public bool IsAlphaNumeric(string input)
|
||
{
|
||
// 使用正则表达式检查是否只包含字母和数字
|
||
return Regex.IsMatch(input, "^[a-zA-Z0-9]+$");
|
||
|
||
}
|
||
|
||
#region 方法
|
||
/// <summary>
|
||
/// 下料数据保存本地
|
||
/// </summary>
|
||
/// <param name="strData"></param>
|
||
private void BlankingDataSaveLocal(string strData)
|
||
{
|
||
#region
|
||
string savePath = string.Format(@"{0}\{1}\{2}\{3}\", @"D:\LocalData", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
|
||
bool acquiredLock = false;
|
||
Monitor.TryEnter(FileLock, ref acquiredLock);
|
||
|
||
if (!Directory.Exists(savePath))
|
||
{
|
||
Directory.CreateDirectory(savePath);
|
||
}
|
||
try
|
||
{
|
||
if (acquiredLock)
|
||
{
|
||
string strFileName = System.DateTime.Now.ToString("yyyyMMdd") + ".csv";//yyyyMMdd.csv
|
||
|
||
if (!File.Exists(savePath + strFileName)) //如果文件不存在,创建文件,并保存到本地
|
||
{
|
||
string strProlnfoFileHead = string.Empty;
|
||
foreach (var item in CommonMethods.m_DBCtrl.HeaderTextOut)
|
||
{
|
||
strProlnfoFileHead += item + ",";
|
||
}
|
||
strProlnfoFileHead = strProlnfoFileHead.TrimEnd(',');
|
||
|
||
strProlnfoFileHead = strProlnfoFileHead.Substring(strProlnfoFileHead.IndexOf(',') + 1);
|
||
|
||
StreamWriter sw = new StreamWriter(savePath + strFileName, true, Encoding.GetEncoding("GB2312"));
|
||
sw.WriteLine(strProlnfoFileHead);
|
||
sw.WriteLine(strData);
|
||
|
||
sw.Flush();
|
||
sw.Close();
|
||
sw.Dispose();
|
||
}
|
||
else
|
||
{
|
||
StreamWriter sw = new StreamWriter(savePath + strFileName, true, Encoding.GetEncoding("GB2312"));
|
||
sw.WriteLine(strData);
|
||
|
||
sw.Flush();
|
||
sw.Close();
|
||
sw.Dispose();
|
||
}
|
||
Monitor.Exit(FileLock);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Instance.WriteError($"下料数据保存异常:{ex}", "SysErrorLog");
|
||
if (acquiredLock)
|
||
{ Monitor.Exit(FileLock); }
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
} |