change-条码绑定扫码器,扫码枪方法由一个一个传改成两个一起传

dev
liuwf 10 months ago
parent 8fbb2ad83d
commit 3da5303554

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>

@ -25,11 +25,13 @@ namespace Aucma.Core.CodeBinding.Business
// 箱壳扫码器ip
private static string SNScannerIp = allScanners.First(x => x.Name == "sn扫码器").Ip;
private static string SNCode = string.Empty;
private static string MESCode = string.Empty;
/// <summary>
/// code1扫码信息刷新
/// </summary>
/// <param name="Code1"></param>
public delegate void BindingReceiveCode(string code, string ip);
public delegate void BindingReceiveCode(string MesCode, string SnCode);
public static event BindingReceiveCode? BindingReceiveCodeEvent;
#region 单例实现
@ -103,13 +105,22 @@ namespace Aucma.Core.CodeBinding.Business
if (str.Substring(0, 1) == "B")
{
// mes条码
MESCode = str;
RefreshMaterialCodeStrEvent?.Invoke(str, MesScannerIp);
}
else
{
//SN码
SNCode = str;
RefreshMaterialCodeStrEvent?.Invoke(str,SNScannerIp);
}
if(!string.IsNullOrEmpty(MESCode) && !string.IsNullOrEmpty(SNCode))
{
// 委托
BindingReceiveCodeEvent?.Invoke(MESCode, SNCode);
SNCode = string.Empty;
MESCode = string.Empty;
}
sb.Clear();
}

@ -32,6 +32,10 @@ namespace Aucma.Core.CodeBinding.Business
public static MvCodeReader LinerDevice = null;
public static DateTime ShellLiveTime = DateTime.Now;
public static DateTime LinerLiveTime = DateTime.Now;
private static string MesCode = string.Empty;
private static string SNCode = string.Empty;
#endregion
@ -40,7 +44,7 @@ namespace Aucma.Core.CodeBinding.Business
#region 条码绑定上位机委托事件
/// <summary>
/// 刷新扫码器状态
@ -56,8 +60,15 @@ namespace Aucma.Core.CodeBinding.Business
/// code1扫码信息刷新
/// </summary>
/// <param name="Code1"></param>
public delegate void BindingReceiveCode(string code, string ip);
public static event BindingReceiveCode? BindingReceiveCodeEvent;
public delegate void ReceiveCode(string code, string ip);
public static event ReceiveCode? ReceiveCodeEvent;
/// <summary>
/// 条码绑定
/// </summary>
/// <param name="Code1"></param>
public delegate void BindingCode(string mesCode, string snCode);
public static event BindingCode? BindingCodeEvent;
/// <summary>
/// NoRead通知
@ -91,6 +102,8 @@ namespace Aucma.Core.CodeBinding.Business
log.Info(DateTime.Now + ":SN--->No Read 处理");
//更新扫码器存活时间
ShellLiveTime = DateTime.Now;
SNCode = string.Empty;
MesCode = string.Empty;
}
for (Int32 i = 0; i < stBcrResult.nCodeNum; i++)
{
@ -106,12 +119,14 @@ namespace Aucma.Core.CodeBinding.Business
string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode);
Console.WriteLine(DateTime.Now + ":SN扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
log.Info(DateTime.Now + ":SN扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
//更新扫码器存活时间
ShellLiveTime = DateTime.Now;
BindingReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), ShellScannerIp);
// 业务处理
//更新扫码器存活时间
ShellLiveTime = DateTime.Now;
SNCode = strCode.Trim().TrimEnd('\0');
ReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), ShellScannerIp);
}
}
@ -127,51 +142,73 @@ namespace Aucma.Core.CodeBinding.Business
public static void LinerImageCallbackFunc(IntPtr pData, IntPtr pstFrameInfoEx2, IntPtr pUser)
{
var stFrameInfo = (MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)Marshal.PtrToStructure(pstFrameInfoEx2, typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2));
try
{
var stFrameInfo = (MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)Marshal.PtrToStructure(pstFrameInfoEx2, typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2));
MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2 stBcrResult = (MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)Marshal.PtrToStructure(stFrameInfo.UnparsedBcrList.pstCodeListEx2, typeof(MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2));
MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2 stBcrResult = (MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)Marshal.PtrToStructure(stFrameInfo.UnparsedBcrList.pstCodeListEx2, typeof(MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2));
// Console.WriteLine("CodeNum[" + Convert.ToString(stBcrResult.nCodeNum) + "]");
if (stBcrResult.nCodeNum == 0)
{
NoReadEvent?.Invoke(LinerScannerIp);
Console.WriteLine(DateTime.Now + ":mes--->No Read 处理");
log.Info(DateTime.Now + ":mes--->No Read 处理");
// 更新存活时间
LinerLiveTime = DateTime.Now;
}
for (Int32 i = 0; i < stBcrResult.nCodeNum; i++)
{
bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode);
if (bIsValidUTF8)
// Console.WriteLine("CodeNum[" + Convert.ToString(stBcrResult.nCodeNum) + "]");
if (stBcrResult.nCodeNum == 0)
{
string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
Console.WriteLine(DateTime.Now + ":Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
NoReadEvent?.Invoke(LinerScannerIp);
Console.WriteLine(DateTime.Now + ":mes--->No Read 处理");
log.Info(DateTime.Now + ":mes--->No Read 处理");
// 更新存活时间
LinerLiveTime = DateTime.Now;
SNCode = string.Empty;
MesCode = string.Empty;
}
else
for (Int32 i = 0; i < stBcrResult.nCodeNum; i++)
{
string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode);
Console.WriteLine(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
log.Info(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode);
if (bIsValidUTF8)
{
string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
Console.WriteLine(DateTime.Now + ":Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
}
else
{
string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode);
Console.WriteLine(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
log.Info(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
// 更新存活时间
LinerLiveTime = DateTime.Now;
// 业务处理
BindingReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), LinerScannerIp);
MesCode = strCode.Trim().TrimEnd('\0');
ReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), LinerScannerIp);
if (!string.IsNullOrEmpty(MesCode) && !string.IsNullOrEmpty(SNCode))
{
BindingCodeEvent?.Invoke(MesCode, SNCode);
MesCode = string.Empty;
SNCode = string.Empty;
}
}
}
}
MvCodeReader.MV_CODEREADER_OCR_INFO_LIST stOcrInfo = (MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)Marshal.PtrToStructure(stFrameInfo.UnparsedOcrList.pstOcrList, typeof(MvCodeReader.MV_CODEREADER_OCR_INFO_LIST));
MvCodeReader.MV_CODEREADER_OCR_INFO_LIST stOcrInfo = (MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)Marshal.PtrToStructure(stFrameInfo.UnparsedOcrList.pstOcrList, typeof(MvCodeReader.MV_CODEREADER_OCR_INFO_LIST));
Console.WriteLine("ocrAllNum[" + Convert.ToString(stOcrInfo.nOCRAllNum) + "]");
for (int i = 0; i < stOcrInfo.nOCRAllNum; i++)
Console.WriteLine("ocrAllNum[" + Convert.ToString(stOcrInfo.nOCRAllNum) + "]");
for (int i = 0; i < stOcrInfo.nOCRAllNum; i++)
{
string strOcrCharCode = Encoding.UTF8.GetString(stOcrInfo.stOcrRowInfo[i].chOcr);
Console.WriteLine("Get OcrInfo:" + "ocrNum[" + i.ToString() + "], ocrLen[" + Convert.ToString(stOcrInfo.stOcrRowInfo[i].nOcrLen) + "], ocrChar[" + strOcrCharCode.Trim().TrimEnd('\0') + "]");
}
}
catch (Exception ex)
{
string strOcrCharCode = Encoding.UTF8.GetString(stOcrInfo.stOcrRowInfo[i].chOcr);
Console.WriteLine("Get OcrInfo:" + "ocrNum[" + i.ToString() + "], ocrLen[" + Convert.ToString(stOcrInfo.stOcrRowInfo[i].nOcrLen) + "], ocrChar[" + strOcrCharCode.Trim().TrimEnd('\0') + "]");
log.Info("mes扫码器回调函数异常:" + ex.Message.ToString());
}
finally
{
MesCode = string.Empty;
SNCode = string.Empty;
}
}
public static void Shell()
{

@ -54,10 +54,10 @@ namespace Aucma.Core.CodeBinding.ViewModels
// 过点数据表物料完成记录MaterialCompletion
private readonly IMaterialCompletionServices? _iMaterialCompletionServices;
// 静态变量存code1
private static string code1Str = string.Empty;
// private static string code1Str = string.Empty;
// 静态变量存code2
private static string code2Str = string.Empty;
// private static string code2Str = string.Empty;
//配置文件扫码器列表
private static List<ScannerModel> allScanners = Appsettings.app<ScannerModel>("ScannerServer").ToList();
public IndexPageViewModel()
@ -70,10 +70,12 @@ namespace Aucma.Core.CodeBinding.ViewModels
_printBarCodeServices = App.ServiceProvider.GetService<IPrintBarCodeServices>();
_iMaterialCompletionServices = App.ServiceProvider.GetService<IMaterialCompletionServices>();
Business.MvCodeHelper.BindingReceiveCodeEvent += ReceiveCode;
GunBusiness.RefreshMaterialCodeStrEvent += ReceiveCode;
Business.MvCodeHelper.ReceiveCodeEvent += ReceiveCode;
Business.MvCodeHelper.BindingCodeEvent += BindingCode;
Business.MvCodeHelper.NoReadEvent += ReceiveNoRead;
GunBusiness.BindingReceiveCodeEvent += BindingCode;
GunBusiness.RefreshMaterialCodeStrEvent += ReceiveCode;
LoadData();
@ -104,29 +106,29 @@ namespace Aucma.Core.CodeBinding.ViewModels
{
string tempCode1 ;
string tempCode2;
// 实时绑定条码
Task.Run(() =>
{
while(true)
{
try
{
if (!string.IsNullOrEmpty(code1Str) && !string.IsNullOrEmpty(code2Str))
{
tempCode1 = code1Str;
tempCode2 = code2Str;
code1Str = string.Empty;
code2Str = string.Empty;
BindingCode(tempCode1, tempCode2);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Thread.Sleep(500);
}
});
//// 实时绑定条码
//Task.Run(() =>
//{
// while(true)
// {
// try
// {
// if (!string.IsNullOrEmpty(code1Str) && !string.IsNullOrEmpty(code2Str))
// {
// tempCode1 = code1Str;
// tempCode2 = code2Str;
// code1Str = string.Empty;
// code2Str = string.Empty;
// BindingCode(tempCode1, tempCode2);
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.ToString());
// }
// Thread.Sleep(500);
// }
//});
// 心跳线程
Task.Run(() =>
@ -449,11 +451,11 @@ namespace Aucma.Core.CodeBinding.ViewModels
{
log.Info("MES条码NoRead:");
// 全局变量赋值
code1Str = string.Empty ;
code2Str = string.Empty;
// code1Str = string.Empty ;
// code2Str = string.Empty;
// 页面扫码信息清空
Code1 = code1Str;
Code2 = code2Str;
Code1 = string.Empty;
Code2 = string.Empty;
Code1Time = string.Empty ;
Code2Time = string.Empty ;
SendPlcStop();
@ -462,11 +464,11 @@ namespace Aucma.Core.CodeBinding.ViewModels
else if(model.Id == 2)
{
RefreshAndWriteLog("SN条码未扫描到,NoRead报警,线体停止,请重新扫描!");
code1Str = string.Empty;
code2Str = string.Empty;
// code1Str = string.Empty;
// code2Str = string.Empty;
// 页面扫码信息清空
Code1 = code1Str;
Code2 = code2Str;
Code1 = string.Empty;
Code2 = string.Empty;
Code1Time = string.Empty;
Code2Time = string.Empty;
SendPlcStop();
@ -487,7 +489,7 @@ namespace Aucma.Core.CodeBinding.ViewModels
{
log.Info("扫描到MES条码:" + codeStr);
// 全局变量赋值SN码扫描后使用
code1Str = codeStr;
// code1Str = codeStr;
RefreshCode1(codeStr);
}
else
@ -495,7 +497,7 @@ namespace Aucma.Core.CodeBinding.ViewModels
log.Info("扫描到成品条码:" + codeStr);
// 1.刷新界面条码信息
// 全局变量赋值,mes条码扫描后使用
code2Str = codeStr;
// code2Str = codeStr;
RefreshCode2(codeStr);
}
@ -554,8 +556,8 @@ namespace Aucma.Core.CodeBinding.ViewModels
/// <summary>
/// 条码绑定
/// </summary>
/// <param name="code1"></param>
/// <param name="code2"></param>
/// <param name="code1">MES码</param>
/// <param name="code2">SN码</param>
public void BindingCode(string code1,string code2)
{
try
@ -656,8 +658,8 @@ namespace Aucma.Core.CodeBinding.ViewModels
}
finally
{
code1Str = string.Empty;
code2Str = string.Empty;
// code1Str = string.Empty;
// code2Str = string.Empty;
}
}
@ -705,7 +707,7 @@ namespace Aucma.Core.CodeBinding.ViewModels
var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("条码绑定Plc"));
if (obj != null)
{
log.Info("停止信号D7102写1");
log.Info("下发放行,停止信号D7102写0");
// 停止信号复位
obj.plc.WriteInt16("D7102", "0");
// 报警信号复位

Loading…
Cancel
Save