public IEnumerable <CameraInfo> GetDevices() { var cameraInfos = new List <CameraInfo>(); var stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); if (MyCamera.MV_OK != MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList)) { return(cameraInfos); } for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { var stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { var stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (stGigEDeviceInfo.chManufacturerName == "Hikvision") { cameraInfos.Add(new CameraInfo(ECameraManufacturer.Hik, ECameraType.GIGE, stGigEDeviceInfo.chModelName, stGigEDeviceInfo.chSerialNumber)); } } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { var stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (stUsb3DeviceInfo.chManufacturerName == "Hikvision") { cameraInfos.Add(new CameraInfo(ECameraManufacturer.Hik, ECameraType.USB, stUsb3DeviceInfo.chModelName, stUsb3DeviceInfo.chSerialNumber)); } } } return(cameraInfos); }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static bool GetHikDeviceInfo(CInfo_Camera_Factory CameraInfo, out MyCamera.MV_CC_DEVICE_INFO stDevInfo) { stDevInfo = new MyCamera.MV_CC_DEVICE_INFO(); var stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); var nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { return(false); } for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); switch (CameraInfo.m_teinterface) { case EINTERFACE.eUSB: if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { var stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (CameraInfo.m_strmodel == stUsb3DeviceInfo.chModelName && CameraInfo.m_strserial == stUsb3DeviceInfo.chSerialNumber) { return(true); } } break; case EINTERFACE.eGIGE: if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { var stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (CameraInfo.m_strmodel == stGigEDeviceInfo.chModelName && CameraInfo.m_strserial == stGigEDeviceInfo.chSerialNumber) { return(true); } } break; } } return(false); }
private static bool GetHikDeviceInfo(CameraInfo cameraInfo, out MyCamera.MV_CC_DEVICE_INFO stDevInfo) { stDevInfo = new MyCamera.MV_CC_DEVICE_INFO(); var stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); var nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { return(false); } for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); switch (cameraInfo.CameraType) { case ECameraType.USB: if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { var stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (cameraInfo.ModelName == stUsb3DeviceInfo.chModelName && cameraInfo.SerialNo == stUsb3DeviceInfo.chSerialNumber) { return(true); } } break; case ECameraType.GIGE: if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { var stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (cameraInfo.ModelName == stGigEDeviceInfo.chModelName && cameraInfo.SerialNo == stGigEDeviceInfo.chSerialNumber) { return(true); } } break; } } return(false); }
private void cbDeviceList_SelectedIndexChanged(object sender, EventArgs e) { if (m_pDeviceList.nDeviceNum == 0) { ShowErrorMsg("No Device", 0); return; } MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); UInt32 nNetIp1 = (gigeInfo.nNetExport & 0xFF000000) >> 24; UInt32 nNetIp2 = (gigeInfo.nNetExport & 0x00FF0000) >> 16; UInt32 nNetIp3 = (gigeInfo.nNetExport & 0x0000FF00) >> 8; UInt32 nNetIp4 = (gigeInfo.nNetExport & 0x000000FF); // ch:显示IP | en:Display IP UInt32 nIp1 = (gigeInfo.nCurrentIp & 0xFF000000) >> 24; UInt32 nIp2 = (gigeInfo.nCurrentIp & 0x00FF0000) >> 16; UInt32 nIp3 = (gigeInfo.nCurrentIp & 0x0000FF00) >> 8; UInt32 nIp4 = (gigeInfo.nCurrentIp & 0x000000FF); lbTip.Text = "Notice: Recommend IP range (" + nNetIp1.ToString() + "." + nNetIp2.ToString() + "." + nNetIp3.ToString() + "." + "0" + "~" + nNetIp1.ToString() + "." + nNetIp2.ToString() + "." + nIp3.ToString() + "." + "255)"; tbIP.Text = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString(); // ch:显示掩码 | en:Display mask nIp1 = (gigeInfo.nCurrentSubNetMask & 0xFF000000) >> 24; nIp2 = (gigeInfo.nCurrentSubNetMask & 0x00FF0000) >> 16; nIp3 = (gigeInfo.nCurrentSubNetMask & 0x0000FF00) >> 8; nIp4 = (gigeInfo.nCurrentSubNetMask & 0x000000FF); tbMask.Text = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString(); // ch:显示网关 | en:Display gateway nIp1 = (gigeInfo.nDefultGateWay & 0xFF000000) >> 24; nIp2 = (gigeInfo.nDefultGateWay & 0x00FF0000) >> 16; nIp3 = (gigeInfo.nDefultGateWay & 0x0000FF00) >> 8; nIp4 = (gigeInfo.nDefultGateWay & 0x000000FF); tbDefaultWay.Text = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString(); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else { Console.Write("Not Support!\n"); break; } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en:Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:设置触发模式为on | en:Set trigger mode as on nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", 1); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Trigger Mode failed! {0:x8}", nRet); break; } // ch:设置触发源为Action1 | en:Set trigger source as Action1 nRet = device.MV_CC_SetEnumValue_NET("TriggerSource", 9); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Trigger Source failed! {0:x8}", nRet); break; } // ch:设置Action Device Key | en:Set Action Device Key nRet = device.MV_CC_SetIntValue_NET("ActionDeviceKey", g_DeviceKey); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Action Device Key failed! {0:x8}", nRet); break; } // ch:设置Action Group Key | en:Set Action Group Key nRet = device.MV_CC_SetIntValue_NET("ActionGroupKey", g_GroupKey); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Action Group Key failed! {0:x8}", nRet); break; } // ch:设置Action Group Mask | en:Set Action Group Mask nRet = device.MV_CC_SetIntValue_NET("ActionGroupMask", g_GroupMask); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Action Group Mask fail! {0:x8}", nRet); break; } // ch:获取包大小 || en: Get Payload Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet); break; } g_nPayloadSize = stParam.nCurValue; // ch:开启抓图 | en:start grab nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } Thread hActionCommandThreadHandle = new Thread(ActionCommandWorkThread); hActionCommandThreadHandle.Start(device); Thread hReceiveImageThreadHandle = new Thread(ReceiveImageWorkThread); hReceiveImageThreadHandle.Start(device); Console.WriteLine("Press enter to exit"); Console.ReadKey(); g_bExit = true; Thread.Sleep(1000); // ch:停止抓图 | en:Stop grab image nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
private bool DeviceListAcq() { System.GC.Collect(); m_stDeviceList.nDeviceNum = 0; int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref m_stDeviceList); if (0 != nRet) { StrErrorMsg = "枚举设备失败!"; return(false); } for (int i = 0; i < m_stDeviceList.nDeviceNum; i++) { MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE) { MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (gigeInfo.chUserDefinedName != "") { m_listUserDefinedName.Add(i, gigeInfo.chUserDefinedName); } else { StrErrorMsg = "没有给相机命名!" + "GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")"; return(false); } } } return(true); }
public override bool OpenCamera() { MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { WriteInfoLog("{this.Info.UserID} Camera Can not find device"); return(false); } if (0 == stDevList.nDeviceNum) { return(false); } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { //赋值结构体的一种方式 stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); //judge camera is gige if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); // ch:显示IP | en:Display IP //UInt32 nIp1 = (stGigEDeviceInfo.nCurrentIp & 0xFF000000) >> 24; //UInt32 nIp2 = (stGigEDeviceInfo.nCurrentIp & 0x00FF0000) >> 16; //UInt32 nIp3 = (stGigEDeviceInfo.nCurrentIp & 0x0000FF00) >> 8; //UInt32 nIp4 = (stGigEDeviceInfo.nCurrentIp & 0x000000FF); //string tempCameraIp = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString(); if (stGigEDeviceInfo.chUserDefinedName == Info.UserID) { nRet = camera.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { WriteInfoLog($"{this.Info.UserID} Camera Create device failed"); return(false); } // ch:打开设备 | en:Open device nRet = camera.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { WriteInfoLog($"{this.Info.UserID} Camera Open device failed"); return(false); } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = camera.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = camera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { WriteInfoLog($"{this.Info.UserID} Camera Warning: Set Packet Size failed"); } } else { WriteInfoLog($"{this.Info.UserID} Camera Warning: Get Packet Size failed"); } } // ch:设置采集连续模式 | en:Set Continues Aquisition Mode camera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2); // ch:工作在连续模式 | en:Acquisition On Continuous Mode camera.MV_CC_SetEnumValue_NET("TriggerMode", 0); // ch:连续模式 | en:Continuous ///绑定回调函数 nRet = camera.MV_CC_RegisterImageCallBackEx_NET(ImageCallback, IntPtr.Zero); if (MyCamera.MV_OK != nRet) { WriteInfoLog($"{this.Info.UserID} Camera Register image callback failed!"); return(false); } //注册掉线回调函数 nRet = camera.MV_CC_RegisterExceptionCallBack_NET(ReconnectCallbackFunc, IntPtr.Zero); if (MyCamera.MV_OK != nRet) { WriteInfoLog($"{this.Info.UserID} Camera Register exception callback failed!"); return(false); } bConnectOk = true; } } } return(true); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en:Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:设置触发模式为off || en:set trigger mode as off if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0)) { Console.WriteLine("Set TriggerMode failed!"); break; } // ch:开启抓图 | en:start grab nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } int nCount = 0; MyCamera.MV_FRAME_OUT FrameInfo = new MyCamera.MV_FRAME_OUT(); while (nCount++ != 100) { nRet = device.MV_CC_GetImageBuffer_NET(ref FrameInfo, 1000); // ch:获取一帧图像 | en:Get one image if (MyCamera.MV_OK == nRet) { Console.WriteLine("Get Image Buffer:" + "Width[" + Convert.ToString(FrameInfo.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.stFrameInfo.nHeight) + "] , FrameNum[" + Convert.ToString(FrameInfo.stFrameInfo.nFrameNum) + "]"); if (FrameInfo.pBufAddr != IntPtr.Zero) { nRet = device.MV_CC_FreeImageBuffer_NET(ref FrameInfo); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Free Image Buffer fail:{0:x8}", nRet); } } } else { Console.WriteLine("No data:{0:x8}", nRet); } } // ch:停止抓图 | en:Stop grab image nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
public MyCamera.MV_CC_DEVICE_INFO GetInfoByIP(string ip) { MyCamera.MV_CC_DEVICE_INFO stDevInfo = new MyCamera.MV_CC_DEVICE_INFO(); MyCamera.MV_CC_DEVICE_INFO_LIST stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();; int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDeviceList); if (nRet != MyCamera.MV_OK) { return(stDevInfo);; } for (Int32 i = 0; i < stDeviceList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); string ip1 = nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4; if (ip == ip1) { return(stDevInfo); } } } return(stDevInfo); }
//初始化设备 static public int InitCamera(out string CameraNames) { int CmaeraNum = 0; CameraNames = ""; int nRet = MyCamera.MV_OK; // MyCamera device = new MyCamera(); // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { return(0); } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 int DevCounter = 0; List <MyCamera.MV_CC_DEVICE_INFO> DevList = new List <MyCamera.MV_CC_DEVICE_INFO>(); // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); DevList.Add(stDevInfo); DevCounter++; } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); Int32 nDevIndex = Convert.ToInt32(Console.ReadLine()); stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); DevList.Sort((a, b) => ((MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(a.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO))).chUserDefinedName.CompareTo( ((MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(b.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO))).chUserDefinedName)); foreach (var b in DevList) { CameraNames += ((MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(b.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO))).chUserDefinedName + ","; } int minDevCounter = DevCounter < 4 ? DevCounter : 4; for (int i = 0; i < minDevCounter; i++) { stDevInfo = DevList[i]; nRet = devices[i].MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { continue; } nRet = devices[i].MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { continue; } // ch:设置触发模式为off || en:set trigger mode as off if (MyCamera.MV_OK != devices[i].MV_CC_SetEnumValue_NET("TriggerMode", 0)) { continue; } //设置曝光时间 float fValue = 5000; if (MyCamera.MV_OK != devices[i].MV_CC_SetExposureTime_NET(fValue)) { continue; } // ch:开启抓图 | en:start grab nRet = devices[i].MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); continue; } // ch:获取包大小 || en: Get Payload Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet = devices[i].MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet); continue; } nPayloadSize[i] = stParam.nCurValue; //int nCount = 0; pBufForDriver[i] = Marshal.AllocHGlobal((int)nPayloadSize[i]); CameraInitOK[i] = true; CmaeraNum++; } return(CmaeraNum); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; do { // ch:枚举设备 | en:Enum deivce MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } device = new MyCamera(); stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en: Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:设置触发模式为off || en:set trigger mode as off nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", 0); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set TriggerMode failed!"); break; } // ch:开启Event | en:Set Event of ExposureEnd On nRet = device.MV_CC_SetEnumValueByString_NET("EventSelector", "ExposureEnd"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set EventSelector failed!"); break; } nRet = device.MV_CC_SetEnumValueByString_NET("EventNotification", "On"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set EventNotification failed!"); break; } // ch:注册回调函数 | en:Register Event callback EventCallback = new MyCamera.cbEventdelegateEx(EventCallbackFunc); nRet = device.MV_CC_RegisterEventCallBackEx_NET("ExposureEnd", EventCallback, IntPtr.Zero); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Register event callback failed!"); break; } // ch:开启抓图 || en: Start grab image nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } Console.WriteLine("Push enter to exit"); Console.ReadLine(); // ch:停止抓图 | en:Stop grabbing nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
static void Main(string[] args) { device = new MyCamera(); int nRet = MyCamera.MV_OK; do { // 枚举设备 MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // 创建设备 nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // 打开设备 nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } //ch:读模式 |en:Read mode Console.WriteLine("Read to file"); g_nMode = 1; Thread hReadHandle = new Thread(FileAccessThread); hReadHandle.Start(); Thread.Sleep(5); Thread hReadProgressHandle = new Thread(FileAccessProgress); hReadProgressHandle.Start(); hReadProgressHandle.Join(); hReadHandle.Join(); if (MyCamera.MV_OK == g_nRet) { Console.WriteLine("File Access Read Success"); } Console.WriteLine(""); //ch:写模式 |en:Write mode Console.WriteLine("Write to file"); g_nMode = 2; Thread hWriteHandle = new Thread(FileAccessThread); hWriteHandle.Start(); Thread.Sleep(5); Thread hWriteProgressHandle = new Thread(FileAccessProgress); hWriteProgressHandle.Start(); hWriteProgressHandle.Join(); hWriteHandle.Join(); if (MyCamera.MV_OK == g_nRet) { Console.WriteLine("File Access Write Success"); } // 关闭设备 nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // 销毁设备 nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en:Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:设置触发模式为off || en:set trigger mode as off if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0)) { Console.WriteLine("Set TriggerMode failed!"); break; } // ch:获取包大小 || en: Get Payload Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet); break; } g_nPayloadSize = stParam.nCurValue; MyCamera.MV_CC_RECORD_PARAM stRecordPar = new MyCamera.MV_CC_RECORD_PARAM(); nRet = device.MV_CC_GetIntValue_NET("Width", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get Width failed: nRet {0:x8}", nRet); break; } stRecordPar.nWidth = (ushort)stParam.nCurValue; nRet = device.MV_CC_GetIntValue_NET("Height", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get Height failed: nRet {0:x8}", nRet); break; } stRecordPar.nHeight = (ushort)stParam.nCurValue; MyCamera.MVCC_ENUMVALUE stEnumValue = new MyCamera.MVCC_ENUMVALUE(); nRet = device.MV_CC_GetEnumValue_NET("PixelFormat", ref stEnumValue); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get Width failed: nRet {0:x8}", nRet); break; } stRecordPar.enPixelType = (MyCamera.MvGvspPixelType)stEnumValue.nCurValue; MyCamera.MVCC_FLOATVALUE stFloatValue = new MyCamera.MVCC_FLOATVALUE(); nRet = device.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stFloatValue); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get Float value failed: nRet {0:x8}", nRet); break; } // ch:帧率(大于1/16)fps | en:Frame Rate (>1/16)fps stRecordPar.fFrameRate = stFloatValue.fCurValue; // ch:码率kbps(128kbps-16Mbps) | en:Bitrate kbps(128kbps-16Mbps) stRecordPar.nBitRate = 1000; // ch:录像格式(仅支持AVI) | en:Record Format(AVI is only supported) stRecordPar.enRecordFmtType = MyCamera.MV_RECORD_FORMAT_TYPE.MV_FormatType_AVI; stRecordPar.strFilePath = "./Recording.avi"; nRet = device.MV_CC_StartRecord_NET(ref stRecordPar); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start Record failed: nRet {0:x8}", nRet); break; } // ch:开启抓图 | en:start grab nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } Thread hReceiveImageThreadHandle = new Thread(ReceiveImageWorkThread); hReceiveImageThreadHandle.Start(device); Console.WriteLine("Press enter to exit"); Console.ReadKey(); g_bExit = true; hReceiveImageThreadHandle.Join(); // ch:停止抓图 | en:Stop grab image nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:停止录像 | en:Stop record nRet = device.MV_CC_StopRecord_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop Record failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
// 枚举相机 public List <string> EnumDevices() { uint num = CameraNum(); List <string> allCameraInfos = new List <string>(); for (int i = 0; i < num; i++) { MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(deviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE) { MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (gigeInfo.chUserDefinedName != "") { allCameraInfos.Add("GEV: " + gigeInfo.chUserDefinedName); } else { allCameraInfos.Add("GEV: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName); } } else if (device.nTLayerType == MyCamera.MV_USB_DEVICE) { MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (usbInfo.chUserDefinedName != "") { allCameraInfos.Add("U3V: " + usbInfo.chUserDefinedName); } else { allCameraInfos.Add("U3V: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName); } } } return(allCameraInfos); }
/// <summary> /// 海康相机 /// </summary> /// <returns></returns> public static List <string> GetHikCameraListInfo() { List <string> cameralist = new List <string>(); try { MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { return(cameralist); } ///设备信息 MyCamera.MV_CC_DEVICE_INFO stDevInfo; for (int i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (!string.IsNullOrEmpty(stGigEDeviceInfo.chUserDefinedName)) { cameralist.Add(stGigEDeviceInfo.chUserDefinedName); } } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (!string.IsNullOrEmpty(stUsb3DeviceInfo.chUserDefinedName)) { cameralist.Add(stUsb3DeviceInfo.chUserDefinedName); } } } } catch (Exception e) { LogFileManager.Error("Camera", e.ToString()); } return(cameralist); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en:Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:设置触发模式为off || en:set trigger mode as off if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0)) { Console.WriteLine("Set TriggerMode failed!"); break; } // ch:获取包大小 || en: Get Payload Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet); break; } UInt32 nPayloadSize = stParam.nCurValue; // ch:开启抓图 | en:start grab nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } int nCount = 0; IntPtr pBufForDriver = Marshal.AllocHGlobal((int)nPayloadSize); IntPtr pBufForSaveImage = IntPtr.Zero; MyCamera.MV_FRAME_OUT_INFO_EX FrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX(); while (nCount++ != 10) { nRet = device.MV_CC_GetOneFrameTimeout_NET(pBufForDriver, nPayloadSize, ref FrameInfo, 1000); // ch:获取一帧图像 | en:Get one image if (MyCamera.MV_OK == nRet) { Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(FrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.nHeight) + "] , FrameNum[" + Convert.ToString(FrameInfo.nFrameNum) + "]"); if (pBufForSaveImage == IntPtr.Zero) { pBufForSaveImage = Marshal.AllocHGlobal((int)(FrameInfo.nHeight * FrameInfo.nWidth * 3 + 2048)); } MyCamera.MV_SAVE_IMAGE_PARAM_EX stSaveParam = new MyCamera.MV_SAVE_IMAGE_PARAM_EX(); stSaveParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Bmp; stSaveParam.enPixelType = FrameInfo.enPixelType; stSaveParam.pData = pBufForDriver; stSaveParam.nDataLen = FrameInfo.nFrameLen; stSaveParam.nHeight = FrameInfo.nHeight; stSaveParam.nWidth = FrameInfo.nWidth; stSaveParam.pImageBuffer = pBufForSaveImage; stSaveParam.nBufferSize = (uint)(FrameInfo.nHeight * FrameInfo.nWidth * 3 + 2048); stSaveParam.nJpgQuality = 80; nRet = device.MV_CC_SaveImageEx_NET(ref stSaveParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Save Image failed:{0:x8}", nRet); continue; } // ch:将图像数据保存到本地文件 | en:Save image data to local file byte[] data = new byte[stSaveParam.nImageLen]; Marshal.Copy(pBufForSaveImage, data, 0, (int)stSaveParam.nImageLen); FileStream pFile = null; try { pFile = new FileStream("frame" + nCount.ToString() + ".bmp", FileMode.Create); pFile.Write(data, 0, data.Length); } catch { Console.WriteLine("保存图像失败"); } finally { pFile.Close(); } continue; } else { Console.WriteLine("No data:{0:x8}", nRet); } } Marshal.FreeHGlobal(pBufForDriver); Marshal.FreeHGlobal(pBufForSaveImage); // ch:停止抓图 | en:Stop grab image nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
private void DeviceListAcq() { // ch:创建设备列表 | en:Create Device List System.GC.Collect(); cbDeviceList.Items.Clear(); m_stDeviceList.nDeviceNum = 0; int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_stDeviceList); if (0 != nRet) { ShowErrorMsg("Enumerate devices fail!", 0); return; } // ch:在窗体列表中显示设备名 | en:Display device name in the form list for (int i = 0; i < m_stDeviceList.nDeviceNum; i++) { MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE) { MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); if (gigeInfo.chUserDefinedName != "") { cbDeviceList.Items.Add("GEV: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")"); } else { cbDeviceList.Items.Add("GEV: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")"); } } else if (device.nTLayerType == MyCamera.MV_USB_DEVICE) { MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); if (usbInfo.chUserDefinedName != "") { cbDeviceList.Items.Add("U3V: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")"); } else { cbDeviceList.Items.Add("U3V: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")"); } } } // ch:选择第一项 | en:Select the first item if (m_stDeviceList.nDeviceNum != 0) { cbDeviceList.SelectedIndex = 0; } }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum deivce MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en: Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:获取触发模式的访问模式 | en:Get Access mode of trigger mode MyCamera.MV_XML_AccessMode pAccessMode = MyCamera.MV_XML_AccessMode.AM_NI; nRet = device.MV_XML_GetNodeAccessMode_NET("TriggerMode", ref pAccessMode); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get Access mode of trigger mode fail! nRet [0x%x]\n", nRet); } else { // ch:设置触发模式为off || en:set trigger mode as off nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", 0); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set TriggerMode failed!"); break; } } // ch:开启抓图 || en: start grab image nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } // ch:获取包大小 || en: Get Payload Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet); break; } UInt32 nPayloadSize = stParam.nCurValue; byte[] BufForDriver = new byte[nPayloadSize]; IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(BufForDriver, 0); MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX(); HTuple hObjectModel3D = null; HTuple hv_X = new HTuple(); HTuple hv_Y = new HTuple(); HTuple hv_Z = new HTuple(); Int32 j = 0; nRet = device.MV_CC_GetOneFrameTimeout_NET(pData, nPayloadSize, ref stFrameInfo, 1000); // 获取一帧图像 if (MyCamera.MV_OK == nRet) { Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(stFrameInfo.nHeight) + "] , FrameNum[" + Convert.ToString(stFrameInfo.nFrameNum) + "]"); switch (stFrameInfo.enPixelType) { case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32f: { float[] ConvertData = new float[nPayloadSize]; Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen); for (j = 0; j < stFrameInfo.nWidth; j++) { hv_X[j] = ConvertData[j * 3]; hv_Y[j] = ConvertData[j * 3 + 1]; hv_Z[j] = ConvertData[j * 3 + 2]; } } break; case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32: { Int32[] ConvertData = new Int32[nPayloadSize]; Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen); for (j = 0; j < stFrameInfo.nWidth; j++) { hv_X[j] = ConvertData[j * 3]; hv_Y[j] = ConvertData[j * 3 + 1]; hv_Z[j] = ConvertData[j * 3 + 2]; } } break; case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32f: { float[] ConvertData = new float[nPayloadSize]; Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen); for (j = 0; j < stFrameInfo.nWidth; j++) { hv_X[j] = ConvertData[j * 2]; hv_Y[j] = ConvertData[j * 2 + 1]; hv_Z[j] = 0.0; } } break; case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32: { Int32[] ConvertData = new Int32[nPayloadSize]; Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen); for (j = 0; j < stFrameInfo.nWidth; j++) { hv_X[j] = ConvertData[j * 2]; hv_Y[j] = ConvertData[j * 2 + 1]; hv_Z[j] = 0; } } break; case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32f: { float[] ConvertData = new float[nPayloadSize]; Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen); for (j = 0; j < stFrameInfo.nWidth; j++) { hv_X[j] = ConvertData[j * 2]; hv_Y[j] = 0.0; hv_Z[j] = ConvertData[j * 2 + 1]; } } break; case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32: { Int32[] ConvertData = new Int32[nPayloadSize]; Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen); for (j = 0; j < stFrameInfo.nWidth; j++) { hv_X[j] = ConvertData[j * 2]; hv_Y[j] = 0; hv_Z[j] = ConvertData[j * 2 + 1]; } } break; default: break; } } else { Console.WriteLine("No data{0:x8}", nRet); } // ch:停止抓图 | en:Stop grabbing nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } if (j == 0) { Console.Write("Probably not a 3D device!\n"); break; } Console.Write("*********************************************************************\n"); Console.Write("* 0.ply; 1.obj; *\n"); Console.Write("*********************************************************************\n"); Int32 nFileType = 0; Console.Write("Select FileType: "); try { nFileType = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } HOperatorSet.GenObjectModel3dFromPoints(hv_X, hv_Y, hv_Z, out hObjectModel3D); switch (nFileType) { case 0: { HOperatorSet.WriteObjectModel3d(hObjectModel3D, "ply", "./3DFile.ply", "invert_normals", "false"); } break; case 1: { HOperatorSet.WriteObjectModel3d(hObjectModel3D, "obj", "./3DFile.obj", "invert_normals", "false"); } break; default: Console.Write("Input error!\n"); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
static void Main(string[] args) { device = new MyCamera(); int nRet = MyCamera.MV_OK; do { // ch:枚举设备 | en:Enum deivce MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); uint nSepIp1 = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24); uint nSepIp2 = ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16); uint nSepIp3 = ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8); uint nSepIp4 = (stGigEDeviceInfo.nCurrentIp & 0x000000ff); Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName); Console.WriteLine("device IP :" + nSepIp1 + "." + nSepIp2 + "." + nSepIp3 + "." + nSepIp4); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en: Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:查询用户使用的模式 // Query the user for the mode to use. bool monitorMode = false; { string key = ""; // Ask the user to launch the multicast controlling application or the multicast monitoring application. Console.WriteLine("Start multicast sample in (c)ontrol or in (m)onitor mode? (c/m)\n"); do { key = Convert.ToString(Console.ReadLine()); }while ((key != "c") && (key != "m") && (key != "C") && (key != "M")); monitorMode = (key == "m") || (key == "M"); } // ch:打开设备 | en:Open device if (monitorMode) { nRet = device.MV_CC_OpenDevice_NET(MyCamera.MV_ACCESS_Monitor, 0); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } } else { nRet = device.MV_CC_OpenDevice_NET(MyCamera.MV_ACCESS_Control, 0); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE && false == monitorMode) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:指定组播ip | en:The specified multicast IP string strIp = "239.0.1.23"; var parts = strIp.Split('.'); int nIp1 = Convert.ToInt32(parts[0]); int nIp2 = Convert.ToInt32(parts[1]); int nIp3 = Convert.ToInt32(parts[2]); int nIp4 = Convert.ToInt32(parts[3]); int nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4; // ch:可指定端口号作为组播组端口 | en:multicast port MyCamera.MV_CC_TRANSMISSION_TYPE stTransmissionType = new MyCamera.MV_CC_TRANSMISSION_TYPE(); stTransmissionType.enTransmissionType = MyCamera.MV_GIGE_TRANSMISSION_TYPE.MV_GIGE_TRANSTYPE_MULTICAST; stTransmissionType.nDestIp = (uint)nIp; stTransmissionType.nDestPort = 8787; nRet = device.MV_GIGE_SetTransmissionType_NET(ref stTransmissionType); if (MyCamera.MV_OK != nRet) { Console.WriteLine("MV_GIGE_SetTransmissionType fail! nRet [%x]\n", nRet); break; } // ch:开启抓图 || en: start grab image nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } Thread thr = new Thread(WorkThread); thr.Start(); Console.WriteLine("Press enter to exit"); Console.ReadLine(); g_bExit = true; Thread.Sleep(1000); // ch:停止抓图 | en:Stop grabbing nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("Device Number : " + stUsb3DeviceInfo.chModelName); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en:Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:设置软触发模式 | en:Set Trigger Mode and Set Trigger Source nRet = device.MV_CC_SetEnumValueByString_NET("TriggerMode", "On"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Trigger Mode failed:{0:x8}", nRet); break; } nRet = device.MV_CC_SetEnumValueByString_NET("TriggerSource", "Software"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Trigger Source failed:{0:x8}", nRet); break; } UInt32 nImageNodeNum = 5; // ch:设置缓存节点个数 | en:Set number of image node nRet = device.MV_CC_SetImageNodeNum_NET(nImageNodeNum); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set number of image node fail:{0:x8}", nRet); break; } Console.WriteLine("\n**************************************************************************"); Console.WriteLine("* 0.MV_GrabStrategy_OneByOne; 1.MV_GrabStrategy_LatestImagesOnly; *"); Console.WriteLine("* 2.MV_GrabStrategy_LatestImages; 3.MV_GrabStrategy_UpcomingImage; *"); Console.WriteLine("**************************************************************************"); Console.Write("Please Input Grab Strategy:"); UInt32 nGrabStrategy = 0; try { nGrabStrategy = (UInt32)Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } // ch:U3V相机不支持MV_GrabStrategy_UpcomingImage | en:U3V device not support UpcomingImage if (nGrabStrategy == (UInt32)MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_UpcomingImage && MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { Console.Write("U3V device not support UpcomingImage\n"); break; } switch (nGrabStrategy) { case (UInt32)MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_OneByOne: { Console.Write("Grab using the MV_GrabStrategy_OneByOne default strategy\n"); nRet = device.MV_CC_SetGrabStrategy_NET(MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_OneByOne); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Grab Strategy fail:{0:x8}", nRet); break; } } break; case (UInt32)MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_LatestImagesOnly: { Console.Write("Grab using strategy MV_GrabStrategy_LatestImagesOnly\n"); nRet = device.MV_CC_SetGrabStrategy_NET(MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_LatestImagesOnly); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Grab Strategy fail:{0:x8}", nRet); break; } } break; case (UInt32)MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_LatestImages: { Console.Write("Grab using strategy MV_GrabStrategy_LatestImages\n"); nRet = device.MV_CC_SetGrabStrategy_NET(MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_LatestImages); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Grab Strategy fail:{0:x8}", nRet); break; } // ch:设置输出缓存个数 | en:Set Output Queue Size nRet = device.MV_CC_SetOutputQueueSize_NET(2); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Grab Strategy fail:{0:x8}", nRet); break; } } break; case (UInt32)MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_UpcomingImage: { Console.Write("Grab using strategy MV_GrabStrategy_UpcomingImage\n"); nRet = device.MV_CC_SetGrabStrategy_NET(MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_UpcomingImage); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set Grab Strategy fail:{0:x8}", nRet); break; } Thread hUpcomingThread = new Thread(UpcomingThread); hUpcomingThread.Start(device); } break; default: Console.Write("Input error!Use default strategy:MV_GrabStrategy_OneByOne\n"); break; } // ch:开启抓图 | en:start grab nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } // ch:发送软触发命令 | en:Send Trigger Software command for (UInt32 i = 0; i < nImageNodeNum; i++) { nRet = device.MV_CC_SetCommandValue_NET("TriggerSoftware"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Send Trigger Software command fail:{0:x8}", nRet); break; } Thread.Sleep(500);//如果帧率过小或TriggerDelay很大,可能会出现软触发命令没有全部起效而导致取不到数据的情况 } MyCamera.MV_FRAME_OUT stOutFrame = new MyCamera.MV_FRAME_OUT(); if (nGrabStrategy != (UInt32)MyCamera.MV_GRAB_STRATEGY.MV_GrabStrategy_UpcomingImage) { while (true) { nRet = device.MV_CC_GetImageBuffer_NET(ref stOutFrame, 0); if (MyCamera.MV_OK == nRet) { Console.WriteLine("Get Image Buffer:" + "Width[" + Convert.ToString(stOutFrame.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(stOutFrame.stFrameInfo.nHeight) + "] , FrameNum[" + Convert.ToString(stOutFrame.stFrameInfo.nFrameNum) + "]"); } else { Console.WriteLine("No data:{0:x8}", nRet); break; } nRet = device.MV_CC_FreeImageBuffer_NET(ref stOutFrame); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Free Image Buffer fail:{0:x8}", nRet); } } } else//仅用于upcoming { nRet = device.MV_CC_GetImageBuffer_NET(ref stOutFrame, 5000); if (MyCamera.MV_OK == nRet) { Console.WriteLine("Get Image Buffer:" + "Width[" + Convert.ToString(stOutFrame.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(stOutFrame.stFrameInfo.nHeight) + "] , FrameNum[" + Convert.ToString(stOutFrame.stFrameInfo.nFrameNum) + "]"); nRet = device.MV_CC_FreeImageBuffer_NET(ref stOutFrame); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Free Image Buffer fail:{0:x8}", nRet); } } else { Console.WriteLine("No data:{0:x8}", nRet); } } // ch:停止抓图 | en:Stop grab image nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); do { // ch:枚举设备 | en:Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en:Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } // ch:判断设备是否是设置的3D格式 | en:Judge Whether the device is set to 3D format MyCamera.MVCC_ENUMVALUE EnumValue = new MyCamera.MVCC_ENUMVALUE(); nRet = device.MV_CC_GetEnumValue_NET("PixelFormat", ref EnumValue); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get the Camera format fail:{0:x8}", nRet); break; } MyCamera.MvGvspPixelType ePixelFormat = (MyCamera.MvGvspPixelType)EnumValue.nCurValue; switch (ePixelFormat) { case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32: case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32f: case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32: case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32f: case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32: case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32f: { nRet = MyCamera.MV_OK; break; } default: { nRet = MyCamera.MV_E_SUPPORT; break; } } if (MyCamera.MV_OK != nRet) { Console.WriteLine("This is not a supported 3D format!"); break; } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", Convert.ToUInt32(nPacketSize)); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } // ch:获取触发模式的访问模式 | en:Get Access mode of trigger mode MyCamera.MV_XML_AccessMode pAccessMode = MyCamera.MV_XML_AccessMode.AM_NI; if (MyCamera.MV_OK != device.MV_XML_GetNodeAccessMode_NET("TriggerMode", ref pAccessMode)) { Console.WriteLine("Get Access mode of trigger mode fail! nRet [0x%x]\n", nRet); } else { // ch:设置触发模式为off || en:set trigger mode as off if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0)) { Console.WriteLine("Set TriggerMode failed!"); break; } } // ch:获取包大小 || en: Get Payload Size MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE(); nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet); break; } UInt32 nPayloadSize = stParam.nCurValue; // ch:开启抓图 | en:start grab nRet = device.MV_CC_StartGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Start grabbing failed:{0:x8}", nRet); break; } uint nImageNum = 100; byte[] bSaveImageBuf = null; try { // ch:申请足够大的缓存,用于保存获取到的图像 bSaveImageBuf = new byte[nPayloadSize * nImageNum]; } catch (Exception ex) { Console.WriteLine("Malloc Save buffer fail!\n"); break; } uint nSaveImageSize = nPayloadSize * nImageNum; // ch:已获取的总图片大小 uint nSaveDataLen = 0; MyCamera.MV_FRAME_OUT stOutFrame = new MyCamera.MV_FRAME_OUT(); for (uint i = 0; i < nImageNum; i++) { nRet = device.MV_CC_GetImageBuffer_NET(ref stOutFrame, 1000); if (nRet == MyCamera.MV_OK) { Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(stOutFrame.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(stOutFrame.stFrameInfo.nHeight) + "] , FrameNum[" + Convert.ToString(stOutFrame.stFrameInfo.nFrameNum) + "]"); if (nSaveImageSize > (nSaveDataLen + stOutFrame.stFrameInfo.nFrameLen)) { // ch:将图像拷贝到pSaveImageBuf | Copy one frame of image to the buffer named pSaveImageBuf Marshal.Copy(stOutFrame.pBufAddr, bSaveImageBuf, Convert.ToInt32(nSaveDataLen), Convert.ToInt32(stOutFrame.stFrameInfo.nFrameLen)); nSaveDataLen += stOutFrame.stFrameInfo.nFrameLen; } nRet = device.MV_CC_FreeImageBuffer_NET(ref stOutFrame); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Free Image Buffer fail:{0:x8}", nRet); } } else { Console.WriteLine("No data:{0:x8}", nRet); } } MyCamera.MV_SAVE_POINT_CLOUD_PARAM stSavePoCloudPar = new MyCamera.MV_SAVE_POINT_CLOUD_PARAM(); stSavePoCloudPar.nLinePntNum = stOutFrame.stFrameInfo.nWidth; stSavePoCloudPar.nLineNum = stOutFrame.stFrameInfo.nHeight * nImageNum; byte[] bDstImageBuf = new byte[stSavePoCloudPar.nLineNum * stSavePoCloudPar.nLinePntNum * (16 * 3 + 4) + 2048]; uint nDstImageSize = stSavePoCloudPar.nLineNum * stSavePoCloudPar.nLinePntNum * (16 * 3 + 4) + 2048; stSavePoCloudPar.enPointCloudFileType = MyCamera.MV_SAVE_POINT_CLOUD_FILE_TYPE.MV_PointCloudFile_PLY; stSavePoCloudPar.enSrcPixelType = stOutFrame.stFrameInfo.enPixelType; stSavePoCloudPar.nSrcDataLen = nSaveDataLen; GCHandle hSrcData = GCHandle.Alloc(bSaveImageBuf, GCHandleType.Pinned); stSavePoCloudPar.pSrcData = hSrcData.AddrOfPinnedObject(); GCHandle hDstData = GCHandle.Alloc(bDstImageBuf, GCHandleType.Pinned); stSavePoCloudPar.pDstBuf = hDstData.AddrOfPinnedObject(); stSavePoCloudPar.nDstBufSize = nDstImageSize; //Save point cloud data nRet = device.MV_CC_SavePointCloudData_NET(ref stSavePoCloudPar); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Save point cloud data fail:{0:x8}", nRet); break; } FileStream file = new FileStream("PointCloudData.ply", FileMode.Create, FileAccess.Write); file.Write(bDstImageBuf, 0, Convert.ToInt32(stSavePoCloudPar.nDstBufLen)); file.Close(); Console.WriteLine("Save point cloud data succeed"); hSrcData.Free(); hDstData.Free(); // ch:停止抓图 | en:Stop grab image nRet = device.MV_CC_StopGrabbing_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Stop grabbing failed{0:x8}", nRet); break; } // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
static void Main(string[] args) { MyCamera device = new MyCamera(); int nRet = MyCamera.MV_OK; do { // ch:枚举设备 | en:Enum deivce MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); } else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO)); Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName); Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber); Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber); } } Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // ch:创建设备 | en: Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // ch:打开设备 | en:Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } Console.WriteLine("Start export the camera properties to the file"); Console.WriteLine("Wait......"); // ch:将相机属性导出到文件中 // en:Export the camera properties to the file nRet = device.MV_CC_FeatureSave_NET("CameraFile"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("FeatureSave failed!"); break; } Console.WriteLine("Finish export the camera properties to the file\n"); Console.WriteLine("Start import the camera properties from the file"); Console.WriteLine("Wait......"); // ch:从文件中导入相机属性 // en:Import the camera properties from the file nRet = device.MV_CC_FeatureLoad_NET("CameraFile"); if (MyCamera.MV_OK != nRet) { Console.WriteLine("FeatureLoad failed!"); break; } Console.WriteLine("Finish import the camera properties from the file"); // ch:关闭设备 | en:Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } } while (false); if (MyCamera.MV_OK != nRet) { // ch:销毁设备 | en:Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); } } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }
/**************************************************************************** * @fn EnumDevices * @brief 枚举可连接设备 * @param nLayerType IN 传输层协议:1-GigE; 4-USB;可叠加 * @param stDeviceList OUT 设备列表 * @return 成功:0;错误:错误码 ****************************************************************************/ /// <summary> /// 枚举可连接设备 /// </summary> /// <param name="nLayerType"> 传输层协议:1-GigE; 4-USB;可叠加</param> /// <param name="stDeviceList"> 设备列表</param> /// <returns>成功:0;错误:错误码</returns> public static Dictionary <string, MyCamera.MV_CC_DEVICE_INFO> EnumDevices( ) { MyCamera.MV_CC_DEVICE_INFO_LIST stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();; List <string> Camerastrings = new List <string>(); Dictionary <string, MyCamera.MV_CC_DEVICE_INFO> Cameras = new Dictionary <string, MyCamera.MV_CC_DEVICE_INFO>(); int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDeviceList); if (nRet != MyCamera.MV_OK) { return(null);; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; for (Int32 i = 0; i < stDeviceList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); Camerastrings.Add(nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4); string ip = nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4; Cameras.Add(ip, stDevInfo); } } return(Cameras); }
public bool Init() { cameras.Clear(); int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Can not find device", nRet); return(false); } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { return(false); } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { AqHikVisionCamera camera = new AqHikVisionCamera(); stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_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); string ip = nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4; camera.Name = stGigEDeviceInfo.chUserDefinedName; camera.Ip = ip; camera.Id = stGigEDeviceInfo.chSerialNumber; cameras.Add(camera); } } if (cameras.Count == 0) { return(false); } return(true); } //end function
public int OpenCamera() { getonecamera = new MyCamera(); MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Can not find device", nRet); return(0); } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { return(0); } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // 通用设备信息 // ch:打印设备信息 en:Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { //赋值结构体的一种方式 stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); //judge camera is gige if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO)); // ch:显示IP | en:Display IP UInt32 nIp1 = (stGigEDeviceInfo.nCurrentIp & 0xFF000000) >> 24; UInt32 nIp2 = (stGigEDeviceInfo.nCurrentIp & 0x00FF0000) >> 16; UInt32 nIp3 = (stGigEDeviceInfo.nCurrentIp & 0x0000FF00) >> 8; UInt32 nIp4 = (stGigEDeviceInfo.nCurrentIp & 0x000000FF); string tempCameraIp = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString(); if (stGigEDeviceInfo.chUserDefinedName == this.Name || tempCameraIp == this.Ip) { nRet = getonecamera.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); return(0); } // ch:打开设备 | en:Open device nRet = getonecamera.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); return(0); } // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera) if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE) { int nPacketSize = getonecamera.MV_CC_GetOptimalPacketSize_NET(); if (nPacketSize > 0) { nRet = getonecamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize); if (nRet != MyCamera.MV_OK) { Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet); } } else { Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize); } } } } } return(1); }
static void Main(string[] args) { int nRet = MyCamera.MV_OK; MyCamera device = new MyCamera(); bool bDevConnected = false; //whether a device is conncected do { // Enum device MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST(); nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_CAMERALINK_DEVICE, ref stDevList); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Enum device failed:{0:x8}", nRet); break; } Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum)); if (0 == stDevList.nDeviceNum) { break; } MyCamera.MV_CC_DEVICE_INFO stDevInfo; // Print device info for (Int32 i = 0; i < stDevList.nDeviceNum; i++) { stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO)); if (MyCamera.MV_CAMERALINK_DEVICE == stDevInfo.nTLayerType) { MyCamera.MV_CamL_DEV_INFO stCamLDeviceInfo = (MyCamera.MV_CamL_DEV_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stCamLInfo, typeof(MyCamera.MV_CamL_DEV_INFO)); Console.WriteLine(i.ToString() + ": [CamL] Serial Number : " + stCamLDeviceInfo.chSerialNumber); Console.WriteLine("PortID : " + stCamLDeviceInfo.chPortID); Console.WriteLine("chManufacturerName : " + stCamLDeviceInfo.chManufacturerName); } else { Console.WriteLine("Unknown Error."); } } Console.WriteLine("Enum finish."); Int32 nDevIndex = 0; Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1); try { nDevIndex = Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Invalid Input!\n"); break; } if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0) { Console.Write("Input Error!\n"); break; } stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO)); // Create device nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Create device failed:{0:x8}", nRet); break; } // Open device nRet = device.MV_CC_OpenDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Open device failed:{0:x8}", nRet); break; } Console.WriteLine("Open finish."); bDevConnected = true; // Register Exception Callback pCallBackFunc = new MyCamera.cbExceptiondelegate(cbExceptiondelegate); nRet = device.MV_CC_RegisterExceptionCallBack_NET(pCallBackFunc, IntPtr.Zero); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Register expection callback failed:{0:x8}", nRet); break; } GC.KeepAlive(pCallBackFunc); /*******************characteristic interfaces for CameraLink device*********************/ // Get supported bauderates of the combined device and host interface uint nBaudrateAblity = 0; nRet = device.MV_CAML_GetSupportBauderates_NET(ref nBaudrateAblity); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get supported bauderate fail:{0:x8}", nRet); break; } Console.WriteLine("Current device supported bauderate:{0:x8}", nBaudrateAblity); // Set device bauderate nRet = device.MV_CAML_SetDeviceBauderate_NET((uint)MyCamera.MV_CAML_BAUDRATE_115200); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Set device bauderate fail:{0:x8}", nRet); break; } // Get the current device bauderate uint nCurrentBaudrate = 0; nRet = device.MV_CAML_GetDeviceBauderate_NET(ref nCurrentBaudrate); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Get device bauderate fail:{0:x8}", nRet); break; } Console.WriteLine("Current device bauderate:{0:x8}", nCurrentBaudrate); /****************************properties configuration**********************************/ // Get the value of various feature nodes nRet = GetParameters(ref device); if (MyCamera.MV_OK != nRet) { Console.WriteLine("GetParameters failed:{0:x8}", nRet); break; } // Set the value of various feature nodes nRet = SetParameters(ref device); if (MyCamera.MV_OK != nRet) { Console.WriteLine("SetParameters failed:{0:x8}", nRet); break; } // Close device nRet = device.MV_CC_CloseDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Close device failed{0:x8}", nRet); break; } bDevConnected = false; // Destroy device nRet = device.MV_CC_DestroyDevice_NET(); if (MyCamera.MV_OK != nRet) { Console.WriteLine("Destroy device failed:{0:x8}", nRet); break; } Console.WriteLine("\n Close finish."); } while (false); if (MyCamera.MV_OK != nRet) { // Ensure that the device is closed if (bDevConnected) { device.MV_CC_CloseDevice_NET(); bDevConnected = false; } // Destroy device device.MV_CC_DestroyDevice_NET(); } Console.WriteLine("Press enter to exit"); Console.ReadKey(); }