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

master
liuwf 4 months ago
parent 1260124a30
commit 6f6907d55e

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

@ -195,30 +195,36 @@ namespace SlnMesnac.TouchSocket
/// <returns></returns>
public int ChangeSerialSignal(byte[] data,int len)
{
if (data == null || len < 0)
try
{
return -1;
}
len = Math.Min(len, data.Length);
byte[] CheckByte = new byte[len];
Array.Copy(data, 0, CheckByte, 0, len);
if (data == null || len < 0)
{
return -1;
}
len = Math.Min(len, data.Length);
byte[] CheckByte = new byte[len];
Array.Copy(data, 0, CheckByte, 0, len);
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 };
byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
byte[] release = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x00, (byte)0xB9, (byte)0xD4 };
byte[] trigger = new byte[] { (byte)0x01, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0xFF, (byte)0xF9, (byte)0x94 };
if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release))
{
if (CheckByte.Length == release.Length && CheckByte.SequenceEqual(release))
{
return 0;
}
else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger))
{
return 0;
}
else if (CheckByte.Length == trigger.Length && CheckByte.SequenceEqual(trigger))
{
return 1;
}
return 1;
}
// 异常
return -1;
// 异常
return -1;
}catch(Exception ex)
{
return -1;
}
}
}

Loading…
Cancel
Save