change-扫码器键值对主键调换

collectionStore
liuwf 11 months ago
parent ffcbace19f
commit bb7d41c102

@ -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(); 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>(); public static Dictionary<string, MvCodeReader> m_cMyDevices = new Dictionary<string, MvCodeReader>();
#region 设备连接状态 #region 设备连接状态
/// <summary> /// <summary>
/// 获取不到任务设备即连接失败 /// 获取不到任务设备即连接失败
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static bool ConnectionStatus(string ip) public static bool ConnectionStatus(string ip)
{ {
// 遍历所有已打开相机 // 遍历所有已打开相机
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices) foreach (var hashmap in m_cMyDevices)
{ {
if (ip.Equals(hashmap.Value)) if (ip.Equals(hashmap.Key))
{ {
return true; return true;
} }
} }
// 没有连接上,重新获取并创建设备 //// 没有连接上,重新获取并创建设备
Task.Run(() => //Task.Run(() =>
{ //{
DeviceListAcq(); // DeviceListAcq();
}); //});
return false; return false;
} }
#endregion #endregion
@ -124,14 +124,14 @@ namespace Aucma.Core.Scanner
} }
RefreshLogMessageEvent?.Invoke("创建并打开第" + i + "个扫码器设备成功,ip:" + ip); RefreshLogMessageEvent?.Invoke("创建并打开第" + i + "个扫码器设备成功,ip:" + ip);
//设置触发模式 //设置触发模式
nRet = m_cMyDevice.MV_CODEREADER_SetEnumValue_NET("TriggerMode", (uint)MvCodeReader.MV_CODEREADER_TRIGGER_MODE.MV_CODEREADER_TRIGGER_MODE_ON); //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) //if (MvCodeReader.MV_CODEREADER_OK != nRet)
{ //{
Console.WriteLine("设置触发模式失败"); // Console.WriteLine("设置触发模式失败");
return; // return;
} //}
//添加到集合 //添加到集合
m_cMyDevices.Add(m_cMyDevice, ip); m_cMyDevices.Add(ip, m_cMyDevice);
} }
} }
} }
@ -156,50 +156,53 @@ namespace Aucma.Core.Scanner
IntPtr pstFrameInfoEx2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(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); Marshal.StructureToPtr(stFrameInfoEx2, pstFrameInfoEx2, false);
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices) foreach (var hashmap in m_cMyDevices)
{ {
nRet = hashmap.Key.MV_CODEREADER_StartGrabbing_NET(); Task.Run(() =>
while (m_bGrabbing)
{ {
// 光电触发了有图像 nRet = hashmap.Value.MV_CODEREADER_StartGrabbing_NET();
nRet = hashmap.Key.MV_CODEREADER_GetOneFrameTimeoutEx2_NET(ref pData, pstFrameInfoEx2, 1000); while (m_bGrabbing)
// 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)); // 光电触发了有图像
// 分配条码内存空间 nRet = hashmap.Value.MV_CODEREADER_GetOneFrameTimeoutEx2_NET(ref pData, pstFrameInfoEx2, 1000);
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)); // ch:获取一帧图像 | en:Get one image
for (int i = 0; i < stBcrResult.nCodeNum; ++i) if (MvCodeReader.MV_CODEREADER_OK == nRet)
{ {
bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode); stFrameInfoEx2 = (MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)Marshal.PtrToStructure(pstFrameInfoEx2, typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2));
if (bIsValidUTF8) // 分配条码内存空间
{ 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));
// string strCode = System.Text.Encoding.Default.GetString(stBcrResult.stBcrInfoEx2[i].chCode); for (int i = 0; i < stBcrResult.nCodeNum; ++i)
string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
// logHelper.Info("bIsValidUTF8:: Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
}
else
{ {
byte[] buffer = new byte[22]; bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode);
if (stBcrResult.stBcrInfoEx2[i].chCode.Length > 0) 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'); else
RefreshLogMessageEvent?.Invoke("相机ip:" + hashmap.Value + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]");
if (!string.IsNullOrEmpty(strCode) && lastCode!=strCode)
{ {
byte[] buffer = new byte[22];
RefreshMaterialCodeStrEvent?.Invoke(strCode,hashmap.Value); if (stBcrResult.stBcrInfoEx2[i].chCode.Length > 0)
lastCode = strCode; {
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("开始关闭所有设备"); Console.WriteLine("开始关闭所有设备");
int nRet = MvCodeReader.MV_CODEREADER_OK; int nRet = MvCodeReader.MV_CODEREADER_OK;
// 关闭所有已打开相机 // 关闭所有已打开相机
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices) foreach (var hashmap in m_cMyDevices)
{ {
// ch:停止抓图 | en:Stop grab image // 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) if (MvCodeReader.MV_CODEREADER_OK != nRet)
{ {
Console.WriteLine("设备ip:" + hashmap.Value + "停止抓图失败"); Console.WriteLine("设备ip:" + hashmap.Value + "停止抓图失败");
Console.WriteLine("Stop grabbing failed{0:x8}", nRet); Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
} }
// ch:关闭设备 | en:Close device // ch:关闭设备 | en:Close device
nRet = hashmap.Key.MV_CODEREADER_CloseDevice_NET(); nRet = hashmap.Value.MV_CODEREADER_CloseDevice_NET();
if (MvCodeReader.MV_CODEREADER_OK != nRet) if (MvCodeReader.MV_CODEREADER_OK != nRet)
{ {
Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败"); Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败");
@ -237,7 +240,7 @@ namespace Aucma.Core.Scanner
} }
// ch:销毁设备 | en:Destroy device // ch:销毁设备 | en:Destroy device
nRet = hashmap.Key.MV_CODEREADER_DestroyHandle_NET(); nRet = hashmap.Value.MV_CODEREADER_DestroyHandle_NET();
if (MvCodeReader.MV_CODEREADER_OK != nRet) if (MvCodeReader.MV_CODEREADER_OK != nRet)
{ {
Console.WriteLine("设备ip:" + hashmap.Value + "销毁失败"); Console.WriteLine("设备ip:" + hashmap.Value + "销毁失败");
@ -255,7 +258,7 @@ namespace Aucma.Core.Scanner
#endregion #endregion
#region 关闭指定设备 #region 关闭指定设备
public void CloseDevice(KeyValuePair<MvCodeReader, string> hashmap) public void CloseDevice(KeyValuePair<MvCodeReader, string> hashmap)
{ {
try try
{ {

Loading…
Cancel
Save