1 using Cognex.VisionPro.FGGigE; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using Cognex.VisionPro; 8 namespace ClassLibrary1 9 { 10 public class Class1 11 { 12 13 #region 获得相机信息方法 14 15 /// <summary> 16 /// 公有静态方法,查找单个相机。例如“Basler” 17 /// </summary> 18 public static ICogFrameGrabber FindFrameGrabber(string CameraType) 19 { 20 CogFrameGrabberGigEs frameGrabbers = new CogFrameGrabberGigEs(); 21 foreach (ICogFrameGrabber fg in frameGrabbers) 22 { 23 if (fg.Name.Contains(CameraType)) 24 { 25 return (fg); 26 } 27 } 28 return null; 29 } 30 /// <summary> 31 /// 公有静态方法,查找相机列表。 32 /// </summary> 33 public static void FindFrameGrabber(List<ICogFrameGrabber> List) 34 { 35 CogFrameGrabberGigEs frameGrabbers = new CogFrameGrabberGigEs(); 36 foreach (ICogFrameGrabber fg in frameGrabbers) 37 { 38 if (fg.Name.Contains("Basler")) 39 { 40 List.Add(fg); 41 } 42 } 43 } 44 /// <summary> 45 /// 公有静态方法,创建相机初始化工具获取信息。 46 /// </summary> 47 public static ICogAcqFifo GetCogAcqFifo(int index) 48 { 49 List<ICogFrameGrabber> list = new List<ICogFrameGrabber>(); 50 FindFrameGrabber(list); 51 ICogFrameGrabber frameGrabbers = list[index]; 52 ICogAcqFifo mCogAcqFifo = null; ; 53 54 if (frameGrabbers == null) 55 { 56 mCogAcqFifo = null; 57 return mCogAcqFifo; 58 } 59 if (frameGrabbers.Name.Contains("gm")) 60 { 61 mCogAcqFifo = frameGrabbers.CreateAcqFifo("Generic GigEVision (Mono)", CogAcqFifoPixelFormatConstants.Format8Grey, 0, false); 62 } 63 else if (frameGrabbers.Name.Contains("gc")) 64 { 65 mCogAcqFifo = frameGrabbers.CreateAcqFifo("Generic GigEVision (Bayer Color)", CogAcqFifoPixelFormatConstants.Format32RGB, 0, false); 66 } 67 return mCogAcqFifo; 68 } 69 /// <summary> 70 /// 公有静态方法,查找相机数量。 71 /// </summary> 72 public static int GetAllCCDCount() 73 { 74 CogFrameGrabberGigEs frameGrabbers = new CogFrameGrabberGigEs(); 75 76 int count = frameGrabbers.Count; 77 78 return count; 79 } 80 /// <summary> 81 /// 公有静态方法,获得CCD曝光exposure。 82 /// </summary> 83 public static double GetCurCCDExposure(ICogAcqFifo acqFifo) 84 { 85 ICogAcqExposure exposureParams = acqFifo.OwnedExposureParams; 86 double exposure; 87 if (exposureParams == null) 88 { 89 exposure = 0; 90 } 91 else 92 { 93 exposure = exposureParams.Exposure; 94 } 95 return exposure; 96 } 97 /// <summary> 98 /// 公有静态方法,获得CCD亮度light。 99 /// </summary> 100 public static double GetCurCCDLight(ICogAcqFifo acqFifo) 101 { 102 ICogAcqLight lightParams = acqFifo.OwnedLightParams; 103 double light; 104 if (lightParams == null) 105 { 106 light = 0; 107 } 108 else 109 { 110 light = lightParams.LightPower; 111 } 112 return light; 113 } 114 115 /// <summary> 116 /// 公有静态方法,获得CCD对比度Contrast。 117 /// </summary> 118 public static double GetCurCCDContrast(ICogAcqFifo acqFifo) 119 { 120 ICogAcqContrast ContrastParams = acqFifo.OwnedContrastParams; 121 double Contrast; 122 if (ContrastParams == null) 123 { 124 Contrast = 0; 125 } 126 else 127 { 128 Contrast = ContrastParams.Contrast; 129 } 130 return Contrast; 131 } 132 /// <summary> 133 /// 公有静态方法,获得CCD序列号SN 134 /// </summary> 135 public static string GetCurCCDSN(ICogAcqFifo acqFifo) 136 { 137 string SerialNumber; 138 if (acqFifo == null) 139 { 140 SerialNumber = ""; 141 } 142 else 143 { 144 SerialNumber = acqFifo.FrameGrabber.SerialNumber; 145 } 146 return SerialNumber; 147 } 148 /// <summary> 149 /// 公有静态方法,获得CCD名称Name 150 /// </summary> 151 public static string GetCurCCDName(ICogAcqFifo acqFifo) 152 { 153 string CCDName; 154 if (acqFifo == null) 155 { 156 CCDName = ""; 157 } 158 else 159 { 160 CCDName = acqFifo.FrameGrabber.Name; 161 } 162 return CCDName; 163 } 164 /// <summary> 165 /// 公有静态方法,获得CCD名称IP 166 /// </summary> 167 public static string GetCurCCDIP(ICogAcqFifo acqFifo) 168 { 169 string IP; 170 if (acqFifo == null) 171 { 172 IP = "0.0.0.0"; 173 } 174 else 175 { 176 IP = acqFifo.FrameGrabber.OwnedGigEAccess.CurrentIPAddress; 177 } 178 return IP; 179 } 180 /// <summary> 181 /// 公有静态方法,获得CCD名称HostIP 182 /// </summary> 183 public static string GetCurCCDHostIP(ICogAcqFifo acqFifo) 184 { 185 string HostIP; 186 if (acqFifo == null) 187 { 188 HostIP = "0.0.0.0"; 189 } 190 else 191 { 192 HostIP = acqFifo.FrameGrabber.OwnedGigEAccess.HostIPAddress; 193 } 194 return HostIP; 195 } 196 /// <summary> 197 /// 公有静态方法,获得CCD信号反跳转时间参数。 198 /// </summary> 199 public static double GetCurCCDLineDebouncerTime(ICogGigEAccess gigEAccess) 200 { 201 double LineDebouncerTimeAbs = 0; 202 try 203 { 204 LineDebouncerTimeAbs = gigEAccess.GetDoubleFeature("LineDebouncerTimeAbs"); 205 return LineDebouncerTimeAbs; 206 } 207 catch { } 208 return LineDebouncerTimeAbs; 209 } 210 /// <summary> 211 /// 公有静态方法,获得CCD帧率参数。 212 /// </summary> 213 public static double GetCurCCDAcquisitionLineRate(ICogGigEAccess gigEAccess) 214 { 215 double AcquisitionLineRateAbs = 0; 216 try 217 { 218 AcquisitionLineRateAbs = gigEAccess.GetDoubleFeature("AcquisitionLineRateAbs"); 219 return AcquisitionLineRateAbs; 220 } 221 catch { } 222 return AcquisitionLineRateAbs; 223 } 224 #endregion 获得相机信息方法 225 226 #region 设置相机参数方法 227 /// <summary> 228 /// 公有静态方法,设置CCD曝光exposure 229 /// </summary> 230 public static void ConfigureExposure(ICogAcqFifo acqFifo, double exposure) 231 { 232 ICogAcqExposure exposureParams = acqFifo.OwnedExposureParams; 233 if (exposureParams != null) 234 { 235 exposureParams.Exposure = exposure; 236 acqFifo.Prepare(); 237 } 238 } 239 240 /// <summary> 241 /// 公有静态方法,设置CCD亮度light。 242 /// </summary> 243 public static void ConfigureLight(ICogAcqFifo acqFifo, double light) 244 { 245 ICogAcqLight lightParams = acqFifo.OwnedLightParams; 246 247 if (lightParams != null) 248 { 249 if (light > 1 || light < 0) 250 { 251 // System.Windows.Forms.MessageBox.Show("参数需要在0-1区间!", "提示"); 252 } 253 else 254 { 255 lightParams.LightPower = light; 256 acqFifo.Prepare(); 257 } 258 } 259 } 260 261 /// <summary> 262 /// 公有静态方法,设置CCD对比度Contrast。 263 /// </summary> 264 public static void ConfigureContrast(ICogAcqFifo acqFifo, double Contrast) 265 { 266 ICogAcqContrast ContrastParams = acqFifo.OwnedContrastParams; 267 268 if (ContrastParams != null) 269 { 270 if (Contrast > 1 || Contrast < 0) 271 { 272 // System.Windows.Forms.MessageBox.Show("参数需要在0-1区间!", "提示"); 273 } 274 else 275 { 276 ContrastParams.Contrast = Contrast; 277 acqFifo.Prepare(); 278 } 279 } 280 } 281 282 /// <summary> 283 /// 公有静态方法,设置CCD外触发参数。 284 /// </summary> 285 public static void ConfigureTrigger(ICogGigEAccess gigEAccess, double lineDebouncerTime, double AcquisitionLineRateAbs) 286 { 287 //gigEAccess.SetFeature("TriggerSelector", "LineStart"); 288 //gigEAccess.SetFeature("TriggerMode", "Off"); 289 gigEAccess.SetFeature("TriggerSelector", "FrameStart");//帧 290 gigEAccess.SetFeature("TriggerMode", "On"); 291 gigEAccess.SetFeature("TriggerSource", "Line3"); 292 // gigEAccess.SetFeature("TriggerActivation", "RisingEdge"); 293 // 或者可以触发激活到fallingedge。 294 gigEAccess.SetFeature("TriggerActivation", "FallingEdge"); 295 //gigEAccess.SetFeature("LineSelector", "Line3"); 296 gigEAccess.SetFeature("LineTermination", "false"); 297 gigEAccess.SetDoubleFeature("LineDebouncerTimeAbs", lineDebouncerTime); 298 gigEAccess.SetDoubleFeature("AcquisitionLineRateAbs", AcquisitionLineRateAbs); 299 } 300 public static void SetlineDebouncerTime(ICogGigEAccess gigEAccess, double time) 301 { 302 gigEAccess.SetFeature("TriggerSelector", "FrameStart");//帧 303 gigEAccess.SetFeature("TriggerSource", "Line1"); 304 gigEAccess.SetFeature("TriggerActivation", "FallingEdge"); 305 //gigEAccess.SetFeature("TriggerActivation", "RisingEdge"); 306 gigEAccess.SetFeature("LineSelector", "Line1"); 307 //gigEAccess.SetFeature("LineTermination", "false"); 308 gigEAccess.SetDoubleFeature("LineDebouncerTimeAbs", time); 309 } 310 /// <summary> 311 /// 公有静态方法,设置CCD旋转编码器触发。 312 /// </summary> 313 public static void ConfigureEncoder(ICogGigEAccess gigEAccess) 314 { 315 gigEAccess.SetFeature("ShaftEncoderModuleLineSelector", "PhaseA"); 316 gigEAccess.SetFeature("ShaftEncoderModuleLineSource", "Line2"); 317 gigEAccess.SetFeature("ShaftEncoderModuleLineSelector", "PhaseB"); 318 gigEAccess.SetFeature("ShaftEncoderModuleLineSource", "Line3"); 319 // Enable line termination for the RS-422 encoder signals 320 gigEAccess.SetFeature("LineSelector", "Line2"); 321 gigEAccess.SetFeature("LineTermination", "true"); 322 gigEAccess.SetFeature("LineSelector", "Line3"); 323 gigEAccess.SetFeature("LineTermination", "true"); 324 // Set the shaft encoder module counter mode 325 gigEAccess.SetFeature("ShaftEncoderModuleCounterMode", "IgnoreDirection"); 326 gigEAccess.SetFeature("TriggerSelector", "LineStart"); 327 gigEAccess.SetFeature("TriggerMode", "On"); 328 gigEAccess.SetFeature("TriggerSource", "ShaftEncoderModuleOut"); 329 gigEAccess.SetFeature("TriggerActivation", "FallingEdge"); 330 //gigEAccess.SetFeature("TriggerActivation", "RisingEdge"); 331 } 332 333 public static void ConfigureAcquisitionLineRateAbs(ICogGigEAccess gigEAccess, double _AcquisitionLineRateAbs) 334 { 335 gigEAccess.SetDoubleFeature("AcquisitionLineRateAbs", _AcquisitionLineRateAbs); 336 } 337 public static void ConfigurelineDebouncerTime(ICogGigEAccess gigEAccess, double _lineDebouncerTime) 338 { 339 gigEAccess.SetDoubleFeature("LineDebouncerTimeAbs", _lineDebouncerTime); 340 } 341 342 343 /// <summary> 344 /// 公有静态方法,设置位宽。 345 /// </summary> 346 public static void SetBandwidth(ICogGigEAccess gigEAccess, 347 double percentageOfBandwidth) 348 { 349 Double maxRate = 100 * 1024 * 1024; 350 uint packetSize = gigEAccess.GetIntegerFeature("GevSCPSPacketSize"); 351 Double packetTime = packetSize / maxRate; 352 Double desiredTime = packetTime / percentageOfBandwidth; 353 Double delaySec = desiredTime - packetTime; 354 ulong timeStampFreq = gigEAccess.TimeStampFrequency; 355 uint delay = (uint)(delaySec * timeStampFreq); 356 gigEAccess.SetIntegerFeature("GevSCPD", delay); 357 } 358 #endregion 设置相机参数方法 359 /// <summary> 360 /// 公有静态方法,保存用户设置参数。 361 /// </summary> 362 public static void SaveUserSet(ICogGigEAccess gigEAccess) 363 { 364 gigEAccess.SetFeature("UserSetSelector", "UserSet1"); 365 gigEAccess.ExecuteCommand("UserSetSave"); 366 gigEAccess.SetFeature("UserSetDefaultSelector", "UserSet1"); 367 } 368 } 369 }