|
|
|
@ -10,19 +10,21 @@ using static System.Runtime.CompilerServices.RuntimeHelpers;
|
|
|
|
|
using HighWayIot.Log4net;
|
|
|
|
|
using HighWayIot.Config;
|
|
|
|
|
using System.Security.Policy;
|
|
|
|
|
|
|
|
|
|
namespace Aucma.Core.Scanner
|
|
|
|
|
{
|
|
|
|
|
public class MvCodeHelper
|
|
|
|
|
{
|
|
|
|
|
private AppConfig appConfig = AppConfig.Instance;
|
|
|
|
|
private static AppConfig appConfig = AppConfig.Instance;
|
|
|
|
|
public static bool m_bGrabbing = true;
|
|
|
|
|
private LogHelper logHelper = LogHelper.Instance;
|
|
|
|
|
#region 委托事件
|
|
|
|
|
public delegate void RefreshMaterialCodeStr(string storeCode, string materialCodeStr);
|
|
|
|
|
public event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 日志信息刷新
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
public delegate void RefreshLogMessage(string message);
|
|
|
|
|
public static event RefreshLogMessage RefreshLogMessageEvent;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// 获取到的所有设备
|
|
|
|
@ -59,24 +61,25 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取并创建设备列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void DeviceListAcq()
|
|
|
|
|
public static void DeviceListAcq()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info("获取扫码器设备列表,进入DeviceListAcq()方法");
|
|
|
|
|
RefreshLogMessageEvent?.Invoke("获取扫码器设备列表,进入DeviceListAcq()方法");
|
|
|
|
|
|
|
|
|
|
System.GC.Collect();
|
|
|
|
|
m_stDeviceList.nDeviceNum = 0;
|
|
|
|
|
// 获取设备列表
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_EnumDevices_NET(ref m_stDeviceList, MvCodeReader.MV_CODEREADER_GIGE_DEVICE);
|
|
|
|
|
if (0 != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info("获取扫码器列表失败,扫码器错误码:" + nRet);
|
|
|
|
|
RefreshLogMessageEvent?.Invoke("获取扫码器列表失败,扫码器错误码:" + nRet);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (0 == m_stDeviceList.nDeviceNum)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info("获取扫码器数量为0,请检查扫码器连接:");
|
|
|
|
|
RefreshLogMessageEvent?.Invoke("获取扫码器数量为0,请检查扫码器连接:");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -93,33 +96,32 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
|
|
|
|
|
// 获取ip
|
|
|
|
|
string ip = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24) + "." + ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16) + "." + ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8) + "." + (stGigEDeviceInfo.nCurrentIp & 0x000000ff);
|
|
|
|
|
// Console.WriteLine($"打印扫码设备信息,下标:{i};IP:{ip}");
|
|
|
|
|
logHelper.Info("扫码器设备[" + i + "],ip:" + ip);
|
|
|
|
|
// Console.WriteLine($"打印扫码设备信息,下标:{i};IP:{ip}");
|
|
|
|
|
Console.Write("扫码器设备[" + i + "],ip:" + ip);
|
|
|
|
|
// 创建第i个设备
|
|
|
|
|
stDevInfo = (MvCodeReader.MV_CODEREADER_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MvCodeReader.MV_CODEREADER_DEVICE_INFO));
|
|
|
|
|
nRet = m_cMyDevice.MV_CODEREADER_CreateHandle_NET(ref stDevInfo);//创建设备
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("创建第" + i + "个扫码器设备失败,ip:" + ip);
|
|
|
|
|
Console.WriteLine("创建第" + i + "个扫码器设备失败,ip:" + ip);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
logHelper.Info("创建第" + i + "个扫码器设备成功,ip:" + ip);
|
|
|
|
|
}
|
|
|
|
|
// 打开设备
|
|
|
|
|
nRet = m_cMyDevice.MV_CODEREADER_OpenDevice_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
m_cMyDevice.MV_CODEREADER_DestroyHandle_NET();
|
|
|
|
|
logHelper.Error("Device open fail!");
|
|
|
|
|
Console.WriteLine("Device open fail!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RefreshLogMessageEvent?.Invoke("创建并打开第" + i + "个扫码器设备成功,ip:" + ip);
|
|
|
|
|
//设置触发模式
|
|
|
|
|
nRet = m_cMyDevice.MV_CODEREADER_SetEnumValue_NET("TriggerMode", (uint)MvCodeReader.MV_CODEREADER_TRIGGER_MODE.MV_CODEREADER_TRIGGER_MODE_ON);
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("Set TriggerMode On Fail!");
|
|
|
|
|
Console.WriteLine("设置触发模式失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//添加到集合
|
|
|
|
|
m_cMyDevices.Add(m_cMyDevice, ip);
|
|
|
|
|
}
|
|
|
|
@ -127,14 +129,14 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("获取和创建设备异常:" + ex);
|
|
|
|
|
Console.WriteLine("获取和创建设备异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 光电触发扫码器接收条码处理业务
|
|
|
|
|
public void StartGrab()
|
|
|
|
|
public static void StartGrab()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -166,7 +168,7 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
{
|
|
|
|
|
// string strCode = System.Text.Encoding.Default.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
logHelper.Info("bIsValidUTF8:: Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
|
|
|
|
|
// logHelper.Info("bIsValidUTF8:: Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
@ -176,7 +178,7 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
Array.Copy(stBcrResult.stBcrInfoEx2[i].chCode, buffer, 22);
|
|
|
|
|
}
|
|
|
|
|
string strCode = Encoding.GetEncoding("UTF-8").GetString(buffer).Trim().TrimEnd('\0');
|
|
|
|
|
logHelper.Info("相机ip:" + hashmap.Value + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]");
|
|
|
|
|
RefreshLogMessageEvent?.Invoke("相机ip:" + hashmap.Value + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]");
|
|
|
|
|
if (!string.IsNullOrEmpty(strCode))
|
|
|
|
|
{
|
|
|
|
|
// 获取到条码处理业务
|
|
|
|
@ -199,18 +201,18 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("扫码异常:" + ex);
|
|
|
|
|
Console.WriteLine("扫码异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 关闭所有设备
|
|
|
|
|
public void CloseAllDevice()
|
|
|
|
|
public static void CloseAllDevice()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info("开始关闭所有设备");
|
|
|
|
|
Console.WriteLine("开始关闭所有设备");
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_OK;
|
|
|
|
|
// 关闭所有已打开相机
|
|
|
|
|
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices)
|
|
|
|
@ -219,14 +221,14 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_StopGrabbing_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备ip:" + hashmap.Value + "停止抓图失败");
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "停止抓图失败");
|
|
|
|
|
Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
|
|
|
|
|
}
|
|
|
|
|
// ch:关闭设备 | en:Close device
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_CloseDevice_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备ip:" + hashmap.Value + "关闭失败");
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -234,14 +236,14 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_DestroyHandle_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备ip:" + hashmap.Value + "销毁失败");
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "销毁失败");
|
|
|
|
|
}
|
|
|
|
|
logHelper.Error("设备ip:" + hashmap.Value + "关闭成功!");
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "关闭成功!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备关闭异常:" + ex);
|
|
|
|
|
Console.WriteLine("设备关闭异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -258,7 +260,7 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_StopGrabbing_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备ip:" + hashmap.Value + "停止抓图失败");
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "停止抓图失败");
|
|
|
|
|
Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -266,13 +268,13 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_CloseDevice_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备ip:" + hashmap.Value + "关闭失败");
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("设备关闭异常:" + ex);
|
|
|
|
|
Console.WriteLine("设备关闭异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|