change-修改相机拍照处理流程,增加光电模块触发信号

master
liuwf 4 months ago
parent 1260124a30
commit 6f6907d55e

@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging;
using SlnMesnac.Common; using SlnMesnac.Common;
using SlnMesnac.Config; using SlnMesnac.Config;
using SlnMesnac.Model.domain; using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Plc; using SlnMesnac.Plc;
using SlnMesnac.Repository.service; using SlnMesnac.Repository.service;
using SlnMesnac.Repository.service.Impl; using SlnMesnac.Repository.service.Impl;
@ -37,6 +38,7 @@ namespace SlnMesnac.Business
public PaddleOCRSharp.PaddleOCREngine engine = null; public PaddleOCRSharp.PaddleOCREngine engine = null;
private GunHelper gunHelper = GunHelper.Instance; private GunHelper gunHelper = GunHelper.Instance;
private Queue<BarCodeModel> consumeQueue = new Queue<BarCodeModel>();
private static LogoBusiness instance; private static LogoBusiness instance;
// 存放照片路径 // 存放照片路径
@ -45,7 +47,7 @@ namespace SlnMesnac.Business
public static string LogPath = System.Environment.CurrentDirectory + "/Logs/"; public static string LogPath = System.Environment.CurrentDirectory + "/Logs/";
/// <summary> /// <summary>
/// 存储海康相机识别结果 /// 接收海康相机识别结果
/// </summary> /// </summary>
private static bool HikCameraResult = false; private static bool HikCameraResult = false;
#region 委托定义 刷新界面扫描信息 #region 委托定义 刷新界面扫描信息
@ -86,43 +88,29 @@ namespace SlnMesnac.Business
} }
public void test()
{
Task.Run(async() =>
{
Thread.Sleep(5000);
TcpClient _tcpClient = new TcpClient();
_tcpClient.Setup(new TouchSocketConfig().SetRemoteIPHost($"127.0.0.1:7024"));
_tcpClient.Connect();
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
{
FilterFunc = response =>
{
return true;
}
});
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
var reciveBuffer =await waitClient.SendThenResponseAsync(release);
});
}
/// <summary>
/// 接收光电模块信号
/// </summary>
/// <param name="ip"></param>
/// <param name="flag"></param>
private void SerialSignalPush(string ip, int flag) private void SerialSignalPush(string ip, int flag)
{ {
// 光电触发拍照 // 光电触发拍照
if (flag == 1) if (flag == 1)
{ {
CameraHandler();
} }
else //光电释放 else //光电释放暂不使用
{ {
} }
} }
/// <summary>
/// 接受相机识别结果
/// </summary>
/// <param name="result"></param>
private void ReceiveCameraResult(string result) private void ReceiveCameraResult(string result)
{ {
logger.LogInformation($"相机返回结果:{result}"); logger.LogInformation($"相机返回结果:{result}");
@ -154,22 +142,17 @@ namespace SlnMesnac.Business
/// <param name="ip"></param> /// <param name="ip"></param>
public async void BarCodeHandler(string materialCodeStr, string ip) public async void BarCodeHandler(string materialCodeStr, string ip)
{ {
//打开灯光
lightHelper.SendData("OPEN");
#region 复位报警灯
// 声光电报警复位
gunHelper.SendData("OK");
#endregion
logger.LogInformation("相机流程开始:");
// 传入照片
byte[] ImageData = null;
// 压缩后的图片
byte[] compressedImageData = null;
try try
{ {
logger.LogInformation("扫码流程开始:");
#region 初始操作:复位海康上次结果,删除海康上次保存文件 #region 初始操作:复位海康上次结果,删除海康上次保存文件
HikCameraResult = false; HikCameraResult = false;
FileHelper.DeleteAllPictures(config.CameraFilePath); FileHelper.DeleteAllPictures(config.CameraFilePath);
// 保证队列一条数据
consumeQueue.Clear();
// 声光电报警复位
gunHelper.SendData("OK");
#endregion #endregion
bool judge = FoamtJudge(materialCodeStr); bool judge = FoamtJudge(materialCodeStr);
@ -180,10 +163,8 @@ namespace SlnMesnac.Business
} }
logger.LogInformation($"扫描到箱体码:{materialCodeStr}"); logger.LogInformation($"扫描到箱体码:{materialCodeStr}");
// 1.立即触发相机拍照
tcpServer.SendCommand(config.CameraIP, "START");
//2.根据箱体码查询型号根据型号判断是否需要校验LOGO //1.根据箱体码查询型号根据型号判断是否需要校验LOGO
ProductModel mode = logoConfigService.GetMaterialTypeByBarCode(materialCodeStr); ProductModel mode = logoConfigService.GetMaterialTypeByBarCode(materialCodeStr);
if(mode == null) if(mode == null)
{ {
@ -192,59 +173,93 @@ namespace SlnMesnac.Business
return; return;
} }
LogoConfig logoConfig = logoConfigService.GetByMaterialType(mode.MaterialCode); LogoConfig logoConfig = logoConfigService.GetByMaterialType(mode.MaterialCode);
// 海康校验结果
bool hikFlag = false;
if (logoConfig.IsChecked == 0) if (logoConfig.IsChecked == 0)
{ {
// 不需要校验 // 不需要校验
hikFlag = true;
RefreshMessageEvent?.Invoke("LOGO+PCI无需识别下发放行"); RefreshMessageEvent?.Invoke("LOGO+PCI无需识别下发放行");
logger.LogInformation($"箱体码:{materialCodeStr}Logo无需识别下发放行"); logger.LogInformation($"箱体码:{materialCodeStr}Logo无需识别下发放行");
RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(), logoConfig.MaterialName, true);
#region 更新数据库
LogoIdentify record = new LogoIdentify();
record.ProductCode = materialCodeStr;
record.MaterialType = logoConfig.MaterialType;
record.MaterialName = logoConfig.MaterialName;
record.isChecked = logoConfig.IsChecked;
record.Result = 1;
record.RecordTime = DateTime.Now;
record.ProductLine = config.ProductLine;
logoIdentifyService.InsertRecord(record);
#endregion
RefreshDataGridEvent?.Invoke();
} }
else else
{ {
// 需要检验但是还没设置配方 // 需要检验但是还没设置配方
if(logoConfig.CheckKind == 0) if(logoConfig.CheckKind == 0)
{ {
// 不需要校验
hikFlag = false;
Warning($"箱体码:{materialCodeStr}LOGO+PCI需要识别但是未设置PCI模版"); Warning($"箱体码:{materialCodeStr}LOGO+PCI需要识别但是未设置PCI模版");
return;
} }
else else
{ {
#region 拍照处理流程 //需要检测
Thread.Sleep(int.Parse(config.SleepStr)); //打开灯光
lightHelper.SendData("OPEN");
BarCodeModel barCodeModel = new BarCodeModel();
barCodeModel.BarCode = materialCodeStr;
barCodeModel.logoConfig = logoConfig;
consumeQueue.Enqueue(barCodeModel);
logger.LogInformation($"箱体码:{materialCodeStr},入队等待识别");
}
}
}
catch (Exception ex)
{
Warning($"BarCodeHandler异常,识别Logo失败,原因:{ex.Message},箱体条码:{materialCodeStr}");
}
}
/// <summary>
/// 相机流程处理
/// </summary>
/// <returns></returns>
private async void CameraHandler()
{
try
{
logger.LogInformation("相机流程开始:");
//照片
byte[] ImageData = null;
// 压缩后的图片
byte[] compressedImageData = null;
//从队列取出条码
if (consumeQueue.Count < 0) return;
bool hikFlag = false;
BarCodeModel barCodeModel = consumeQueue.Dequeue();
// Thread.Sleep(int.Parse(config.SleepStr));
// 1.触发相机拍照 // 1.触发相机拍照
tcpServer.SendCommand(config.CameraIP, logoConfig.CheckKind.ToString()); tcpServer.SendCommand(config.CameraIP, barCodeModel.logoConfig.CheckKind.ToString());
// 需要校验
// 2.等待接收海康结果 // 2.等待接收海康结果
hikFlag = await JudgeIsSuccessAsync(); hikFlag = await JudgeIsSuccessAsync();
if (hikFlag) if (hikFlag)
{ {
//校验成功放行
RefreshMessageEvent?.Invoke("LOGO+PCI识别成功下发放行"); RefreshMessageEvent?.Invoke("LOGO+PCI识别成功下发放行");
logger.LogInformation($"箱体码:{materialCodeStr}Logo识别成功下发放行"); logger.LogInformation($"箱体码:{barCodeModel.BarCode}Logo识别成功下发放行");
} }
else else
{ {
//校验失败禁止放行
WarningAndStop($"LOGO+PCI识别失败禁止放行"); WarningAndStop($"LOGO+PCI识别失败禁止放行");
} }
#endregion
}
}
// 刷新界面、刷新图片,照片按照日期存储本地 // 刷新界面、刷新图片,照片按照日期存储本地
RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(), logoConfig.MaterialName, hikFlag); #region
RefreshBoxInfoEvent?.Invoke(barCodeModel.BarCode, DateTime.Now.ToString(), barCodeModel.logoConfig.MaterialName, hikFlag);
//需要检测并且设置过配方的条码等待照片
if(logoConfig.IsChecked == 1 && logoConfig.CheckKind != 0)
{
await Task.Run(() => await Task.Run(() =>
{ {
Thread.Sleep(int.Parse(config.PictureSleep)); Thread.Sleep(int.Parse(config.PictureSleep));
@ -253,24 +268,20 @@ namespace SlnMesnac.Business
{ {
// 图片压缩 // 图片压缩
compressedImageData = FileHelper.CompressImageData(ImageData, 20); compressedImageData = FileHelper.CompressImageData(ImageData, 20);
FileHelper.SaveImage(compressedImageData, materialCodeStr + ".jpg", PicturePath); FileHelper.SaveImage(compressedImageData, barCodeModel.BarCode + ".jpg", PicturePath);
RefreshPictureEvent?.Invoke(ImageData); RefreshPictureEvent?.Invoke(ImageData);
} }
else
{
// WarningAndStop($"读取相机保存照片文件夹失败,请检查文件夹:{config.CameraFilePath}是否存在");
}
}); });
} #endregion
#region 更新数据库 #region 更新数据库
LogoIdentify record = new LogoIdentify(); LogoIdentify record = new LogoIdentify();
record.ProductCode = materialCodeStr; record.ProductCode = barCodeModel.BarCode;
record.MaterialType = logoConfig.MaterialType; record.MaterialType = barCodeModel.logoConfig.MaterialType;
record.MaterialName = logoConfig.MaterialName; record.MaterialName = barCodeModel.logoConfig.MaterialName;
record.isChecked = logoConfig.IsChecked; record.isChecked = barCodeModel.logoConfig.IsChecked;
record.Result = hikFlag ? 1 : 0; record.Result = hikFlag == true ? 1 : 0;
record.RecordTime = DateTime.Now; record.RecordTime = DateTime.Now;
record.ProductLine = config.ProductLine; record.ProductLine = config.ProductLine;
if (compressedImageData != null) if (compressedImageData != null)
@ -280,23 +291,21 @@ namespace SlnMesnac.Business
logoIdentifyService.InsertRecord(record); logoIdentifyService.InsertRecord(record);
#endregion #endregion
RefreshDataGridEvent?.Invoke(); RefreshDataGridEvent?.Invoke();
}catch(Exception ex)
}
catch (Exception ex)
{ {
Warning($"BarCodeHandler异常,识别Logo失败,原因:{ex.Message},箱体条码:{materialCodeStr}"); Warning($"相机流程异常,原因:{ex.Message}");
} }
finally finally
{ {
lightHelper.SendData("CLOSE");
#region 复位海康上次结果,删除海康上次保存文件 #region 复位海康上次结果,删除海康上次保存文件、关闭灯光
HikCameraResult = false; HikCameraResult = false;
FileHelper.DeleteAllPictures(config.CameraFilePath); FileHelper.DeleteAllPictures(config.CameraFilePath);
lightHelper.SendData("CLOSE");
#endregion #endregion
} }
}
}
/// <summary> /// <summary>
@ -319,7 +328,7 @@ namespace SlnMesnac.Business
{ {
return true; return true;
} }
// 检查是否超过两秒 // 检查是否超
if (stopwatch.ElapsedMilliseconds > 2500) if (stopwatch.ElapsedMilliseconds > 2500)
{ {
return false; return false;
@ -555,5 +564,35 @@ namespace SlnMesnac.Business
} }
#endregion #endregion
#region 测试用例
/// <summary>
/// 模拟光电发送byte测试接收处理
/// </summary>
public void testPostByte()
{
Task.Run(async () =>
{
Thread.Sleep(5000);
TcpClient _tcpClient = new TcpClient();
_tcpClient.Setup(new TouchSocketConfig().SetRemoteIPHost($"127.0.0.1:7024"));
_tcpClient.Connect();
var waitClient = _tcpClient.CreateWaitingClient(new WaitingOptions()
{
FilterFunc = response =>
{
return true;
}
});
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
var reciveBuffer = await waitClient.SendThenResponseAsync(release);
});
}
#endregion
} }
} }

@ -38,7 +38,7 @@ namespace SlnMesnac.Model.domain
public int IsChecked { get; set; } public int IsChecked { get; set; }
/// <summary> /// <summary>
/// 是否需要检测0不需要1需要 /// 检测类型
/// </summary> /// </summary>
[SugarColumn(ColumnName = "CHECK_KIND")] [SugarColumn(ColumnName = "CHECK_KIND")]
public int CheckKind { get; set; } public int CheckKind { get; set; }

@ -0,0 +1,15 @@
using SlnMesnac.Model.domain;
using System;
namespace SlnMesnac.Model.dto
{
public class BarCodeModel
{
//扫描到的条码
public string? BarCode { get; set; }
//条码对应的识别配置
public LogoConfig? logoConfig { get; set; }
}
}

@ -194,6 +194,8 @@ namespace SlnMesnac.TouchSocket
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public int ChangeSerialSignal(byte[] data,int len) public int ChangeSerialSignal(byte[] data,int len)
{
try
{ {
if (data == null || len < 0) if (data == null || len < 0)
{ {
@ -219,6 +221,10 @@ namespace SlnMesnac.TouchSocket
// 异常 // 异常
return -1; return -1;
}catch(Exception ex)
{
return -1;
}
} }
} }

Loading…
Cancel
Save