diff --git a/Aucma.Scada.HikRobot/MvCodeHelper.cs b/Aucma.Scada.HikRobot/MvCodeHelper.cs index 86d0c5fd..33b1471f 100644 --- a/Aucma.Scada.HikRobot/MvCodeHelper.cs +++ b/Aucma.Scada.HikRobot/MvCodeHelper.cs @@ -32,28 +32,28 @@ namespace Aucma.Core.Scanner public static MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST m_stDeviceList = new MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST(); // (成功创建)连接上的设备和其ip(string)集合 - public static Dictionary m_cMyDevices = new Dictionary(); + public static Dictionary m_cMyDevices = new Dictionary(); #region 设备连接状态 /// /// 获取不到任务设备即连接失败 /// /// - public static bool ConnectionStatus(string ip) + public static bool ConnectionStatus(string ip) { // 遍历所有已打开相机 - foreach (KeyValuePair hashmap in m_cMyDevices) + foreach (var hashmap in m_cMyDevices) { - if (ip.Equals(hashmap.Value)) + if (ip.Equals(hashmap.Key)) { return true; } } - // 没有连接上,重新获取并创建设备 - Task.Run(() => - { - DeviceListAcq(); - }); + //// 没有连接上,重新获取并创建设备 + //Task.Run(() => + //{ + // DeviceListAcq(); + //}); return false; } #endregion @@ -113,7 +113,7 @@ namespace Aucma.Core.Scanner { Console.WriteLine("创建第" + i + "个扫码器设备失败,ip:" + ip); return; - } + } // 打开设备 nRet = m_cMyDevice.MV_CODEREADER_OpenDevice_NET(); if (MvCodeReader.MV_CODEREADER_OK != nRet) @@ -124,14 +124,14 @@ namespace Aucma.Core.Scanner } 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) - { - Console.WriteLine("设置触发模式失败"); - return; - } + //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) + //{ + // Console.WriteLine("设置触发模式失败"); + // return; + //} //添加到集合 - m_cMyDevices.Add(m_cMyDevice, ip); + m_cMyDevices.Add(ip, m_cMyDevice); } } } @@ -142,7 +142,7 @@ namespace Aucma.Core.Scanner } #endregion - + #region 光电触发扫码器接收条码处理业务 public static void StartGrab() { @@ -156,50 +156,53 @@ namespace Aucma.Core.Scanner IntPtr pstFrameInfoEx2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2))); Marshal.StructureToPtr(stFrameInfoEx2, pstFrameInfoEx2, false); - foreach (KeyValuePair hashmap in m_cMyDevices) + foreach (var hashmap in m_cMyDevices) { - nRet = hashmap.Key.MV_CODEREADER_StartGrabbing_NET(); - while (m_bGrabbing) + Task.Run(() => { - // 光电触发了有图像 - nRet = hashmap.Key.MV_CODEREADER_GetOneFrameTimeoutEx2_NET(ref pData, pstFrameInfoEx2, 1000); - // ch:获取一帧图像 | en:Get one image - if (MvCodeReader.MV_CODEREADER_OK == nRet) + nRet = hashmap.Value.MV_CODEREADER_StartGrabbing_NET(); + while (m_bGrabbing) { - 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) + // 光电触发了有图像 + nRet = hashmap.Value.MV_CODEREADER_GetOneFrameTimeoutEx2_NET(ref pData, pstFrameInfoEx2, 1000); + // ch:获取一帧图像 | en:Get one image + if (MvCodeReader.MV_CODEREADER_OK == nRet) { - 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); - // logHelper.Info("bIsValidUTF8:: Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); - } - else + 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) { - byte[] buffer = new byte[22]; - if (stBcrResult.stBcrInfoEx2[i].chCode.Length > 0) + bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode); + if (bIsValidUTF8) { - Array.Copy(stBcrResult.stBcrInfoEx2[i].chCode, buffer, 22); + // 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') + "]"); } - string strCode = Encoding.GetEncoding("UTF-8").GetString(buffer).Trim().TrimEnd('\0'); - RefreshLogMessageEvent?.Invoke("相机ip:" + hashmap.Value + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]"); - if (!string.IsNullOrEmpty(strCode) && lastCode!=strCode) + else { - - RefreshMaterialCodeStrEvent?.Invoke(strCode,hashmap.Value); - lastCode = strCode; - + 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'); + RefreshLogMessageEvent?.Invoke("相机ip:" + hashmap.Key + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]"); + if (!string.IsNullOrEmpty(strCode) && lastCode != strCode) + { + + RefreshMaterialCodeStrEvent?.Invoke(strCode, hashmap.Key); + lastCode = strCode; + + } } } } - } - Thread.Sleep(500); + Thread.Sleep(500); - } + } + }); } } @@ -219,17 +222,17 @@ namespace Aucma.Core.Scanner Console.WriteLine("开始关闭所有设备"); int nRet = MvCodeReader.MV_CODEREADER_OK; // 关闭所有已打开相机 - foreach (KeyValuePair hashmap in m_cMyDevices) + foreach (var hashmap in m_cMyDevices) { // ch:停止抓图 | en:Stop grab image - nRet = hashmap.Key.MV_CODEREADER_StopGrabbing_NET(); + nRet = hashmap.Value.MV_CODEREADER_StopGrabbing_NET(); if (MvCodeReader.MV_CODEREADER_OK != nRet) { Console.WriteLine("设备ip:" + hashmap.Value + "停止抓图失败"); Console.WriteLine("Stop grabbing failed{0:x8}", nRet); } // ch:关闭设备 | en:Close device - nRet = hashmap.Key.MV_CODEREADER_CloseDevice_NET(); + nRet = hashmap.Value.MV_CODEREADER_CloseDevice_NET(); if (MvCodeReader.MV_CODEREADER_OK != nRet) { Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败"); @@ -237,7 +240,7 @@ namespace Aucma.Core.Scanner } // ch:销毁设备 | en:Destroy device - nRet = hashmap.Key.MV_CODEREADER_DestroyHandle_NET(); + nRet = hashmap.Value.MV_CODEREADER_DestroyHandle_NET(); if (MvCodeReader.MV_CODEREADER_OK != nRet) { Console.WriteLine("设备ip:" + hashmap.Value + "销毁失败"); @@ -255,7 +258,7 @@ namespace Aucma.Core.Scanner #endregion #region 关闭指定设备 - public void CloseDevice(KeyValuePair hashmap) + public void CloseDevice(KeyValuePair hashmap) { try {