|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
using log4net;
|
|
|
|
|
using MvCodeReaderSDKNet;
|
|
|
|
|
using NPOI.SS.Formula.Functions;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
@ -47,7 +48,7 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
// 获取到的所有设备
|
|
|
|
|
public static MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST m_stDeviceList = new MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST();
|
|
|
|
|
|
|
|
|
|
// 连接上的设备和其ip(string)集合
|
|
|
|
|
// (成功创建)连接上的设备和其ip(string)集合
|
|
|
|
|
public static Dictionary<MvCodeReader, string> m_cMyDevices = new Dictionary<MvCodeReader, string>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -66,20 +67,20 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 没有连接上,重新获取并打开设备
|
|
|
|
|
// 没有连接上,重新获取并创建设备
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
DeviceListAcqAndOpen();
|
|
|
|
|
DeviceListAcq();
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 获取并打开设备列表
|
|
|
|
|
#region 获取并创建设备列表
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取设备列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void DeviceListAcqAndOpen()
|
|
|
|
|
public static void DeviceListAcq()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -102,7 +103,7 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
|
|
|
|
|
MvCodeReader m_cMyDevice = new MvCodeReader();
|
|
|
|
|
|
|
|
|
|
//打开所有设备
|
|
|
|
|
//创建所有设备
|
|
|
|
|
for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
|
|
|
|
|
{
|
|
|
|
|
MvCodeReader.MV_CODEREADER_DEVICE_INFO stDevInfo = (MvCodeReader.MV_CODEREADER_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MvCodeReader.MV_CODEREADER_DEVICE_INFO));
|
|
|
|
@ -115,7 +116,7 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
string ip = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24) + "." + ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16) + "." + ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8) + "." + (stGigEDeviceInfo.nCurrentIp & 0x000000ff);
|
|
|
|
|
// Console.WriteLine($"打印扫码设备信息,下标:{i};IP:{ip}");
|
|
|
|
|
log.Info("扫码器设备[" + i + "],ip:" + ip);
|
|
|
|
|
// 打开第i个设备
|
|
|
|
|
// 创建第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)
|
|
|
|
@ -124,7 +125,10 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
log.Info("创建第" + i + "个扫码器设备成功,ip:" + ip);
|
|
|
|
|
//添加到集合
|
|
|
|
|
m_cMyDevices.Add(m_cMyDevice, ip);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
nRet = m_cMyDevice.MV_CODEREADER_OpenDevice_NET();//打开设备
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
@ -155,12 +159,14 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
}
|
|
|
|
|
log.Info("*****************************************************************");
|
|
|
|
|
log.Info("第" + i + "个扫码器设备创建,打开,开启连续抓图设置成功!ip:" + ip);
|
|
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("获取打开设备异常:" + ex);
|
|
|
|
|
log.Error("获取和创建设备异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
@ -251,6 +257,125 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region (先不使用此方法,待测试) 通过plc触发单个扫码器接收条码处理业务
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据plc信号地址区分扫码器Ip,再获取指定扫码器条码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ip"></param>
|
|
|
|
|
public async void StartGrabByPlc(string ip)
|
|
|
|
|
{
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_OK;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices)
|
|
|
|
|
{
|
|
|
|
|
// 1.找到指定相机 hashmap.Key
|
|
|
|
|
if (hashmap.Value == ip)
|
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
// 2.打开相机开启扫描
|
|
|
|
|
int nRet = hashmap.Key.MV_CODEREADER_OpenDevice_NET();//打开设备
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
log.Error("打开扫码器设备失败,销毁设备,ip:" + ip);
|
|
|
|
|
hashmap.Key.MV_CODEREADER_DestroyHandle_NET();
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
// ch:设置触发模式为off || en:set trigger mode as off
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != hashmap.Key.MV_CODEREADER_SetEnumValue_NET("TriggerMode", 0))
|
|
|
|
|
{
|
|
|
|
|
log.Error("设置扫码器设备触发模式失败,ip:" + ip);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// ch:开启抓图 | en:start grab
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_StartGrabbing_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
log.Error("设置扫码器设备开启抓图失败,ip:" + ip);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 3.开启抓图
|
|
|
|
|
int nCount = 0;
|
|
|
|
|
IntPtr pData = IntPtr.Zero;
|
|
|
|
|
MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2 stFrameInfoEx2 = new MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2();
|
|
|
|
|
IntPtr pstFrameInfoEx2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)));
|
|
|
|
|
Marshal.StructureToPtr(stFrameInfoEx2, pstFrameInfoEx2, false);
|
|
|
|
|
|
|
|
|
|
while (nCount++!=10)
|
|
|
|
|
{
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_GetOneFrameTimeoutEx2_NET(ref pData, pstFrameInfoEx2, 1000);
|
|
|
|
|
// ch:获取一帧图像 | en:Get one image
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK == nRet)
|
|
|
|
|
{
|
|
|
|
|
stFrameInfoEx2 = (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(stFrameInfoEx2.UnparsedBcrList.pstCodeListEx2, typeof(MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2));
|
|
|
|
|
for (int i = 0; i < stBcrResult.nCodeNum; ++i)
|
|
|
|
|
{
|
|
|
|
|
bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
if (bIsValidUTF8)
|
|
|
|
|
{
|
|
|
|
|
// string strCode = System.Text.Encoding.Default.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
log.Info("bIsValidUTF8:: Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[22];
|
|
|
|
|
if (stBcrResult.stBcrInfoEx2[i].chCode.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(stBcrResult.stBcrInfoEx2[i].chCode, buffer, 22);
|
|
|
|
|
}
|
|
|
|
|
string strCode = Encoding.GetEncoding("UTF-8").GetString(buffer).Trim().TrimEnd('\0');
|
|
|
|
|
log.Info("相机ip:" + hashmap.Value + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]");
|
|
|
|
|
if (!string.IsNullOrEmpty(strCode))
|
|
|
|
|
{
|
|
|
|
|
// 处理业务
|
|
|
|
|
// 扫码器1,处理扫码器1的业务
|
|
|
|
|
if (ip.Equals(Appsettings.app("Middleware", "Scanner1", "Ip")))
|
|
|
|
|
{
|
|
|
|
|
#region 条码绑定业务处理
|
|
|
|
|
ReceiveCode1Event?.Invoke(strCode);
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
else if (ip.Equals(Appsettings.app("Middleware", "Scanner2", "Ip"))) // 扫码器2,处理扫码器2的业务
|
|
|
|
|
{
|
|
|
|
|
#region 条码绑定业务处理
|
|
|
|
|
ReceiveCode2Event?.Invoke(strCode);
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
// 获取到条码就跳出扫码
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(800);
|
|
|
|
|
}
|
|
|
|
|
if (nCount == 10)
|
|
|
|
|
{
|
|
|
|
|
log.Error("扫码器扫码失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
log.Error("扫码异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 关闭设备
|
|
|
|
|
public static void CloseDevice()
|
|
|
|
|
{
|
|
|
|
@ -393,3 +518,8 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|