|
|
|
@ -14,10 +14,35 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
{
|
|
|
|
|
public class MvCodeHelper
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static LogHelper logHelper = LogHelper.Instance;
|
|
|
|
|
private static AppConfig appConfig = AppConfig.Instance;
|
|
|
|
|
public static bool m_bGrabbing = true;
|
|
|
|
|
private static string SheellLastCode;
|
|
|
|
|
// 箱壳扫码器ip
|
|
|
|
|
private static string ShellScannerIp = appConfig.foamHikRobotIp;
|
|
|
|
|
#region 全局变量定义
|
|
|
|
|
|
|
|
|
|
public static DateTime ShellLiveTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 委托事件
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 刷新扫码器状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="materialCodeStr"></param>
|
|
|
|
|
/// <param name="ip"></param>
|
|
|
|
|
public delegate void RefreshState(bool flag);
|
|
|
|
|
public static event RefreshState RefreshStateEvent;
|
|
|
|
|
|
|
|
|
|
//NoRead事件通知
|
|
|
|
|
public delegate void MessageNoRead();
|
|
|
|
|
public static event MessageNoRead MessageNoReadEvent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public delegate void RefreshMaterialCodeStr(string materialCodeStr);
|
|
|
|
|
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
|
|
|
|
|
/// <summary>
|
|
|
|
@ -28,266 +53,224 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
public static event RefreshLogMessage RefreshLogMessageEvent;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// 获取到的所有设备
|
|
|
|
|
public static MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST m_stDeviceList = new MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST();
|
|
|
|
|
public static void ShellImageCallbackFunc(IntPtr pData, IntPtr pstFrameInfoEx2, IntPtr pUser)
|
|
|
|
|
{
|
|
|
|
|
var stFrameInfo = (MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)Marshal.PtrToStructure(pstFrameInfoEx2, typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2));
|
|
|
|
|
|
|
|
|
|
// (成功创建)连接上的设备和其ip(string)集合
|
|
|
|
|
public static Dictionary<MvCodeReader, string> m_cMyDevices = new Dictionary<MvCodeReader, string>();
|
|
|
|
|
MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2 stBcrResult = (MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)Marshal.PtrToStructure(stFrameInfo.UnparsedBcrList.pstCodeListEx2, typeof(MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2));
|
|
|
|
|
|
|
|
|
|
#region 设备连接状态
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取不到任务设备即连接失败
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool ConnectionStatus(string ip)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
// Console.WriteLine("CodeNum[" + Convert.ToString(stBcrResult.nCodeNum) + "]");
|
|
|
|
|
if (stBcrResult.nCodeNum == 0)
|
|
|
|
|
{
|
|
|
|
|
// 遍历所有已打开相机
|
|
|
|
|
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices)
|
|
|
|
|
//更新扫码器存活时间
|
|
|
|
|
ShellLiveTime = DateTime.Now;
|
|
|
|
|
Console.WriteLine("箱壳No Read 处理");
|
|
|
|
|
MessageNoReadEvent();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
for (Int32 i = 0; i < stBcrResult.nCodeNum; i++)
|
|
|
|
|
{
|
|
|
|
|
bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
if (bIsValidUTF8)
|
|
|
|
|
{
|
|
|
|
|
string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
Console.WriteLine("Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ip.Equals(hashmap.Value))
|
|
|
|
|
string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode);
|
|
|
|
|
Console.WriteLine(DateTime.Now + ":箱壳扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]");
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(SheellLastCode) || SheellLastCode != strCode.Trim().TrimEnd('\0'))
|
|
|
|
|
{
|
|
|
|
|
//更新扫码器存活时间
|
|
|
|
|
ShellLiveTime = DateTime.Now;
|
|
|
|
|
RefreshMaterialCodeStrEvent?.Invoke(strCode.Trim().TrimEnd('\0'));
|
|
|
|
|
SheellLastCode = strCode.Trim().TrimEnd('\0');
|
|
|
|
|
}
|
|
|
|
|
if (SheellLastCode == strCode.Trim().TrimEnd('\0'))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
Console.WriteLine("箱壳和上次扫描结果一样,不作处理");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// 没有连接上,重新获取并创建设备
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
DeviceListAcq();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
|
|
MvCodeReader.MV_CODEREADER_OCR_INFO_LIST stOcrInfo = (MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)Marshal.PtrToStructure(stFrameInfo.UnparsedOcrList.pstOcrList, typeof(MvCodeReader.MV_CODEREADER_OCR_INFO_LIST));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < stOcrInfo.nOCRAllNum; i++)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error(ex.Message.ToString());
|
|
|
|
|
string strOcrCharCode = Encoding.UTF8.GetString(stOcrInfo.stOcrRowInfo[i].chOcr);
|
|
|
|
|
Console.WriteLine("Get OcrInfo:" + "ocrNum[" + i.ToString() + "], ocrLen[" + Convert.ToString(stOcrInfo.stOcrRowInfo[i].nOcrLen) + "], ocrChar[" + strOcrCharCode.Trim().TrimEnd('\0') + "]");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 获取并创建设备列表
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取并创建设备列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void DeviceListAcq()
|
|
|
|
|
|
|
|
|
|
public static void Shell()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
ShellLiveTime = DateTime.Now;
|
|
|
|
|
MvCodeReader.cbOutputEx2delegate ImageCallback;
|
|
|
|
|
|
|
|
|
|
MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2 stFrameInfo = new MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2();
|
|
|
|
|
MvCodeReader device = new MvCodeReader();
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_OK;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
// RefreshLogMessageEvent?.Invoke("获取扫码器设备列表,进入DeviceListAcq()方法");
|
|
|
|
|
// 扫码器ip
|
|
|
|
|
string scannerIp = appConfig.foamHikRobotIp;
|
|
|
|
|
if (string.IsNullOrEmpty(scannerIp)) return;
|
|
|
|
|
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)
|
|
|
|
|
// ch:枚举设备 | en:Enum device
|
|
|
|
|
MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST stDevList = new MvCodeReader.MV_CODEREADER_DEVICE_INFO_LIST();
|
|
|
|
|
nRet = MvCodeReader.MV_CODEREADER_EnumDevices_NET(ref stDevList, MvCodeReader.MV_CODEREADER_GIGE_DEVICE);
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info("获取扫码器列表失败,扫码器错误码:" + nRet);
|
|
|
|
|
RefreshLogMessageEvent?.Invoke("获取扫码器列表失败,扫码器错误码:" + nRet);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
|
|
|
|
|
if (0 == m_stDeviceList.nDeviceNum)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// RefreshLogMessageEvent?.Invoke("获取扫码器数量为0,请检查扫码器连接:");
|
|
|
|
|
return;
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
Thread.Sleep(1000 * 10);
|
|
|
|
|
Console.WriteLine("Enum device failed:{0:x8}", nRet);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
|
|
|
|
|
if (0 == stDevList.nDeviceNum)
|
|
|
|
|
{
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
|
|
|
|
|
MvCodeReader m_cMyDevice = new MvCodeReader();
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
Thread.Sleep(1000 * 10);
|
|
|
|
|
|
|
|
|
|
//创建所有设备
|
|
|
|
|
for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MvCodeReader.MV_CODEREADER_DEVICE_INFO stDevInfo; // 通用设备信息
|
|
|
|
|
int nDevIndex = -1;
|
|
|
|
|
// ch:打印设备信息 | en:Print device info
|
|
|
|
|
for (Int32 i = 0; i < stDevList.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));
|
|
|
|
|
if (stDevInfo.nTLayerType == MvCodeReader.MV_CODEREADER_GIGE_DEVICE)
|
|
|
|
|
stDevInfo = (MvCodeReader.MV_CODEREADER_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MvCodeReader.MV_CODEREADER_DEVICE_INFO));
|
|
|
|
|
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_GIGE_DEVICE == stDevInfo.nTLayerType)
|
|
|
|
|
{
|
|
|
|
|
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(stDevInfo.SpecialInfo.stGigEInfo, 0);
|
|
|
|
|
MvCodeReader.MV_CODEREADER_GIGE_DEVICE_INFO stGigEDeviceInfo = (MvCodeReader.MV_CODEREADER_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MvCodeReader.MV_CODEREADER_GIGE_DEVICE_INFO));
|
|
|
|
|
|
|
|
|
|
// 获取ip
|
|
|
|
|
string ip = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24) + "." + ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16) + "." + ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8) + "." + (stGigEDeviceInfo.nCurrentIp & 0x000000ff);
|
|
|
|
|
// 只连接配置的扫码器
|
|
|
|
|
if (!scannerIp.Equals(ip)) continue;
|
|
|
|
|
// 创建第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)
|
|
|
|
|
MvCodeReader.MV_CODEREADER_GIGE_DEVICE_INFO stGigEDeviceInfo = (MvCodeReader.MV_CODEREADER_GIGE_DEVICE_INFO)MvCodeReader.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MvCodeReader.MV_CODEREADER_GIGE_DEVICE_INFO));
|
|
|
|
|
uint nIp1 = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24);
|
|
|
|
|
uint nIp2 = ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16);
|
|
|
|
|
uint nIp3 = ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8);
|
|
|
|
|
uint nIp4 = (stGigEDeviceInfo.nCurrentIp & 0x000000ff);
|
|
|
|
|
// Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
|
|
|
|
|
// Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
|
|
|
|
|
string ipStr = nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4;
|
|
|
|
|
if (ipStr == ShellScannerIp)
|
|
|
|
|
{
|
|
|
|
|
// Console.WriteLine("创建第" + i + "个扫码器设备失败,ip:" + ip);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 打开设备
|
|
|
|
|
nRet = m_cMyDevice.MV_CODEREADER_OpenDevice_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
m_cMyDevice.MV_CODEREADER_DestroyHandle_NET();
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
// Console.WriteLine("设置触发模式失败");
|
|
|
|
|
return;
|
|
|
|
|
nDevIndex = i;
|
|
|
|
|
Console.WriteLine("device IP :" + ipStr);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//添加到集合
|
|
|
|
|
m_cMyDevices.Add(m_cMyDevice, ip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("获取和创建扫码器设备异常:" + ex.Message.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
if (nDevIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("未找到箱壳扫码器");
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
Thread.Sleep(1000 * 30);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
stDevInfo = (MvCodeReader.MV_CODEREADER_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MvCodeReader.MV_CODEREADER_DEVICE_INFO));
|
|
|
|
|
|
|
|
|
|
#region 光电触发扫码器接收条码处理业务
|
|
|
|
|
public static void StartGrab()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_OK;
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices)
|
|
|
|
|
// ch:创建设备 | en:Create device
|
|
|
|
|
nRet = device.MV_CODEREADER_CreateHandle_NET(ref stDevInfo);
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_StartGrabbing_NET();
|
|
|
|
|
while (m_bGrabbing)
|
|
|
|
|
{
|
|
|
|
|
// 光电触发了有图像
|
|
|
|
|
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);
|
|
|
|
|
// logHelper.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');
|
|
|
|
|
// RefreshLogMessageEvent?.Invoke("相机ip:" + hashmap.Value + " Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode + "]");
|
|
|
|
|
if (!string.IsNullOrEmpty(strCode))
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info("扫码器扫描到条码:"+strCode);
|
|
|
|
|
// 获取到条码处理业务
|
|
|
|
|
RefreshMaterialCodeStrEvent?.Invoke(strCode);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
Console.WriteLine("Create device failed:{0:x8}", nRet);
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// ch:打开设备 | en:Open device
|
|
|
|
|
nRet = device.MV_CODEREADER_OpenDevice_NET();
|
|
|
|
|
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Open device failed:{0:x8}", nRet);
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("扫码异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(true);
|
|
|
|
|
// ch:注册回调函数 | en:Register image callback
|
|
|
|
|
ImageCallback = new MvCodeReader.cbOutputEx2delegate(ShellImageCallbackFunc);
|
|
|
|
|
nRet = device.MV_CODEREADER_RegisterImageCallBackEx2_NET(ImageCallback, IntPtr.Zero);
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Register image callback failed!");
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 关闭所有设备
|
|
|
|
|
public static void CloseAllDevice()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("开始关闭所有设备");
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_OK;
|
|
|
|
|
// 关闭所有已打开相机
|
|
|
|
|
foreach (KeyValuePair<MvCodeReader, string> hashmap in m_cMyDevices)
|
|
|
|
|
// ch:开启抓图 || en: start grab image
|
|
|
|
|
nRet = device.MV_CODEREADER_StartGrabbing_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
// ch:停止抓图 | en:Stop grab image
|
|
|
|
|
nRet = hashmap.Key.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();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败");
|
|
|
|
|
Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ch:销毁设备 | en:Destroy device
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_DestroyHandle_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
Console.WriteLine("Press enter to exit");
|
|
|
|
|
Console.WriteLine(DateTime.Now.ToString());
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000 * 60);
|
|
|
|
|
DateTime dateTime = DateTime.Now;
|
|
|
|
|
TimeSpan timeSpan = dateTime.Subtract(ShellLiveTime);
|
|
|
|
|
if (timeSpan.TotalMinutes >= 10)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "销毁失败");
|
|
|
|
|
Console.WriteLine("箱壳扫码器超时");
|
|
|
|
|
//CloseShell();
|
|
|
|
|
//Shell();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "关闭成功!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("设备关闭异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 关闭指定设备
|
|
|
|
|
public void CloseDevice(KeyValuePair<MvCodeReader, string> hashmap)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int nRet = MvCodeReader.MV_CODEREADER_OK;
|
|
|
|
|
// ch:停止抓图 | en:Stop grab image
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_StopGrabbing_NET();
|
|
|
|
|
}
|
|
|
|
|
// Console.ReadLine();
|
|
|
|
|
// 刷新扫码器状态
|
|
|
|
|
RefreshStateEvent?.Invoke(false);
|
|
|
|
|
// ch:停止抓图 | en:Stop grabbing
|
|
|
|
|
nRet = device.MV_CODEREADER_StopGrabbing_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "停止抓图失败");
|
|
|
|
|
Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
|
|
|
|
|
return;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ch:关闭设备 | en:Close device
|
|
|
|
|
nRet = hashmap.Key.MV_CODEREADER_CloseDevice_NET();
|
|
|
|
|
nRet = device.MV_CODEREADER_CloseDevice_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("设备ip:" + hashmap.Value + "关闭失败");
|
|
|
|
|
return;
|
|
|
|
|
Console.WriteLine("Close device failed{0:x8}", nRet);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("设备关闭异常:" + ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ch:销毁设备 | en:Destroy device
|
|
|
|
|
nRet = device.MV_CODEREADER_DestroyHandle_NET();
|
|
|
|
|
if (MvCodeReader.MV_CODEREADER_OK != nRet)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Destroy device failed:{0:x8}", nRet);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while (false);
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
Shell();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 判断字符编码
|
|
|
|
@ -363,27 +346,6 @@ namespace Aucma.Core.Scanner
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 将Byte转换为结构体类型
|
|
|
|
|
//将Byte转换为结构体类型
|
|
|
|
|
public static object ByteToStruct(byte[] bytes, Type type)
|
|
|
|
|
{
|
|
|
|
|
int size = Marshal.SizeOf(type);
|
|
|
|
|
if (size > bytes.Length)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
//分配结构体内存空间
|
|
|
|
|
IntPtr structPtr = Marshal.AllocHGlobal(size);
|
|
|
|
|
//将byte数组拷贝到分配好的内存空间
|
|
|
|
|
Marshal.Copy(bytes, 0, structPtr, size);
|
|
|
|
|
//将内存空间转换为目标结构体
|
|
|
|
|
object obj = Marshal.PtrToStructure(structPtr, type);
|
|
|
|
|
//释放内存空间
|
|
|
|
|
Marshal.FreeHGlobal(structPtr);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|