/* Queries the number of available devices and creates a list with device data. */ public static List <Device> EnumerateDevices() { /* Create a list for the device data. */ List <Device> list = new List <Device>(); /* Enumerate all camera devices. You must call * PylonEnumerateDevices() before creating a device. */ uint count = Pylon.EnumerateDevices(); /* Get device data from all devices. */ for (uint i = 0; i < count; ++i) { /* Create a new data packet. */ Device device = new Device(); /* Get the device info handle of the device. */ PYLON_DEVICE_INFO_HANDLE hDi = Pylon.GetDeviceInfoHandle(i); /* Get the name. */ device.Name = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoFriendlyNameKey); /* Set the index. */ device.Index = i; /* Add to the list. */ list.Add(device); } return(list); }
public static Dictionary <string, CameraProperty> Read(PYLON_DEVICE_HANDLE deviceHandle, string fullName) { Dictionary <string, CameraProperty> properties = new Dictionary <string, CameraProperty>(); // Enumerate devices again to make sure we have the correct device index and find the device class. DeviceEnumerator.Device device = null; List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices(); foreach (DeviceEnumerator.Device candidate in devices) { if (candidate.FullName != fullName) { continue; } device = candidate; break; } if (device == null) { return(properties); } string deviceClass = "BaslerGigE"; try { deviceClass = Pylon.DeviceInfoGetPropertyValueByName(device.DeviceInfoHandle, Pylon.cPylonDeviceInfoDeviceClassKey); } catch { log.ErrorFormat("Could not read Basler device class. Assuming BaslerGigE."); } properties.Add("width", ReadIntegerProperty(deviceHandle, "Width")); properties.Add("height", ReadIntegerProperty(deviceHandle, "Height")); properties.Add("enableFramerate", ReadBooleanProperty(deviceHandle, "AcquisitionFrameRateEnable")); if (deviceClass == "BaslerUsb") { properties.Add("framerate", ReadFloatProperty(deviceHandle, "AcquisitionFrameRate")); properties.Add("exposure", ReadFloatProperty(deviceHandle, "ExposureTime")); properties.Add("gain", ReadFloatProperty(deviceHandle, "Gain")); } else { properties.Add("framerate", ReadFloatProperty(deviceHandle, "AcquisitionFrameRateAbs")); properties.Add("exposure", ReadFloatProperty(deviceHandle, "ExposureTimeAbs")); properties.Add("gain", ReadIntegerProperty(deviceHandle, "GainRaw")); } return(properties); }
public static Dictionary <string, CameraProperty> Read(PYLON_DEVICE_HANDLE deviceHandle, string fullName) { Dictionary <string, CameraProperty> properties = new Dictionary <string, CameraProperty>(); // Enumerate devices again to make sure we have the correct device index and find the device class. DeviceEnumerator.Device device = null; List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices(); foreach (DeviceEnumerator.Device candidate in devices) { if (candidate.FullName != fullName) { continue; } device = candidate; break; } if (device == null) { return(properties); } string deviceClass = "BaslerGigE"; try { deviceClass = Pylon.DeviceInfoGetPropertyValueByName(device.DeviceInfoHandle, Pylon.cPylonDeviceInfoDeviceClassKey); } catch { log.ErrorFormat("Could not read Basler device class. Assuming BaslerGigE."); } properties.Add("width", ReadIntegerProperty(deviceHandle, "Width")); properties.Add("height", ReadIntegerProperty(deviceHandle, "Height")); // Camera properties in Kinovea combine the value and the "auto" flag. // We potentially need to read several Basler camera properties to create one Kinovea camera property. // Furthermore, some properties name or type depends on whether the camera is USB or GigE. ReadFramerate(deviceHandle, deviceClass, properties); ReadExposure(deviceHandle, deviceClass, properties); ReadGain(deviceHandle, deviceClass, properties); return(properties); }
/* Queries the number of available devices and creates a list with device data. */ public static List <Device> EnumerateDevices() { /* Create a list for the device data. */ List <Device> list = new List <Device>(); /* Enumerate all camera devices. You must call * PylonEnumerateDevices() before creating a device. */ uint count = Pylon.EnumerateDevices(); /* Get device data from all devices. */ for (uint i = 0; i < count; ++i) { /* Create a new data packet. */ Device device = new Device(); /* Get the device info handle of the device. */ PYLON_DEVICE_INFO_HANDLE hDi = Pylon.GetDeviceInfoHandle(i); /* Get the name. */ device.Name = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoFriendlyNameKey); /* Get the serial number */ device.FullName = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoFullNameKey); /* Set the index. */ device.Index = i; /* Create tooltip */ string tooltip = ""; uint propertyCount = Pylon.DeviceInfoGetNumProperties(hDi); if (propertyCount > 0) { for (uint j = 0; j < propertyCount; j++) { tooltip += Pylon.DeviceInfoGetPropertyName(hDi, j) + ": " + Pylon.DeviceInfoGetPropertyValueByIndex(hDi, j); if (j != propertyCount - 1) { tooltip += "\n"; } } } device.Tooltip = tooltip; /* Add to the list. */ list.Add(device); } return(list); }
/* The function to be called when the removal of an open device is detected. */ private static void removalCallbackFunction(PYLON_DEVICE_HANDLE hDevice) { /* Print out the name of the device. It is not possible to read the name * from the camera since it has been removed. Use the device's device * information instead. For accessing the device information, no reading from * the device is required. */ /* Retrieve the device information for the removed device. */ PYLON_DEVICE_INFO_HANDLE hDi = Pylon.DeviceGetDeviceInfoHandle(hDevice); string fullName = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoFullNameKey); string friendlyName = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoFriendlyNameKey); /* Print out the name. */ Console.WriteLine("\nCallback function for removal of device {0} ({1}).", friendlyName, fullName); /* Increment the counter to indicate that the callback has been fired. */ callbackCounter++; }
const uint GIGE_PROTOCOL_OVERHEAD = 36; /* Total number of bytes of protocol overhead. */ static void Main(string[] args) { PYLON_DEVICE_HANDLE[] hDev = new PYLON_DEVICE_HANDLE[NUM_DEVICES]; /* Handles for the pylon devices. */ for (int deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { hDev[deviceIndex] = new PYLON_DEVICE_HANDLE(); } try { uint numDevicesAvail; /* Number of the available devices. */ bool isAvail; /* Used for checking feature availability. */ bool isReady; /* Used as an output parameter. */ int i; /* Counter. */ int deviceIndex; /* Index of device used in the following variables. */ PYLON_WAITOBJECTS_HANDLE wos; /* Wait objects. */ int nGrabs; /* Counts the number of grab iterations. */ PYLON_WAITOBJECT_HANDLE woTimer; /* Timer wait object. */ /* These are camera specific variables: */ PYLON_STREAMGRABBER_HANDLE[] hGrabber = new PYLON_STREAMGRABBER_HANDLE[NUM_DEVICES]; /* Handle for the pylon stream grabber. */ PYLON_WAITOBJECT_HANDLE[] hWait = new PYLON_WAITOBJECT_HANDLE[NUM_DEVICES]; /* Handle used for waiting for a grab to be finished. */ uint[] payloadSize = new uint[NUM_DEVICES]; /* Size of an image frame in bytes. */ PylonGrabResult_t[] grabResult = new PylonGrabResult_t[NUM_DEVICES]; /* Stores the result of a grab operation. */ uint[] nStreams = new uint[NUM_DEVICES]; /* The number of streams provided by the device. */ Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >[] buffers = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > [NUM_DEVICES]; /* Holds handles and buffers used for grabbing. */ #if DEBUG /* This is a special debug setting needed only for GigE cameras. * See 'Building Applications with pylon' in the Programmer's Guide. */ Environment.SetEnvironmentVariable("PYLON_GIGE_HEARTBEAT", "300000" /*ms*/); #endif /* Before using any pylon methods, the pylon runtime must be initialized. */ Pylon.Initialize(); /* Enumerate all camera devices. You must call * PylonEnumerateDevices() before creating a device. */ numDevicesAvail = Pylon.EnumerateDevices(); if (numDevicesAvail < NUM_DEVICES) { Console.Error.WriteLine("Found {0} devices. At least {1} devices needed to run this sample.", numDevicesAvail, NUM_DEVICES); throw new Exception("Not enough devices found."); } /* Create wait objects. This must be done outside of the loop. */ wos = Pylon.WaitObjectsCreate(); /* In this sample, we want to grab for a given amount of time, then stop. * Create a timer that tiggers an AutoResetEvent, wrap the AutoResetEvent in a pylon C.NET wait object, and add it to * the wait object set. */ AutoResetEvent timoutEvent = new AutoResetEvent(false); /* The timeout event to wait for. */ TimerCallbackWrapper timerCallbackWrapper = new TimerCallbackWrapper(timoutEvent); /* Receives the timer callback and sets the timeout event. */ Timer timer = new Timer(timerCallbackWrapper.TimerCallback); /* The timeout timer. */ woTimer = Pylon.WaitObjectFromW32(timoutEvent.SafeWaitHandle, true); Pylon.WaitObjectsAdd(wos, woTimer); /* Open cameras and set parameters. */ for (deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { /* Get handles for the devices. */ hDev[deviceIndex] = Pylon.CreateDeviceByIndex((uint)deviceIndex); /* Before using the device, it must be opened. Open it for configuring * parameters and for grabbing images. */ Pylon.DeviceOpen(hDev[deviceIndex], Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream); /* Print out the name of the camera we are using. */ { bool isReadable = Pylon.DeviceFeatureIsReadable(hDev[deviceIndex], "DeviceModelName"); if (isReadable) { string name = Pylon.DeviceFeatureToString(hDev[deviceIndex], "DeviceModelName"); Console.WriteLine("Using camera '{0}'", name); } } /* Set the pixel format to Mono8, where gray values will be output as 8 bit values for each pixel. */ /* ... Check first to see if the device supports the Mono8 format. */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_PixelFormat_Mono8"); if (!isAvail) { /* Feature is not available. */ throw new Exception("Device doesn't support the Mono8 pixel format."); } /* ... Set the pixel format to Mono8. */ Pylon.DeviceFeatureFromString(hDev[deviceIndex], "PixelFormat", "Mono8"); /* Disable acquisition start trigger if available */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_TriggerSelector_AcquisitionStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSelector", "AcquisitionStart"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerMode", "Off"); } /* Disable frame burst start trigger if available */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_TriggerSelector_FrameBurstStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSelector", "FrameBurstStart"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerMode", "Off"); } /* Disable frame start trigger if available */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_TriggerSelector_FrameStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSelector", "FrameStart"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerMode", "Off"); } /* We will use the Continuous frame mode, i.e., the camera delivers * images continuously. */ Pylon.DeviceFeatureFromString(hDev[deviceIndex], "AcquisitionMode", "Continuous"); PYLON_DEVICE_INFO_HANDLE hDi = Pylon.GetDeviceInfoHandle((uint)deviceIndex); string deviceClass = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoDeviceClassKey); if (deviceClass == "BaslerGigE") { /* For GigE cameras, we recommend increasing the packet size for better * performance. When the network adapter supports jumbo frames, set the packet * size to a value > 1500, e.g., to 8192. In this sample, we only set the packet size * to 1500. * * We also set the Inter-Packet and the Frame Transmission delay * so the switch can line up packets better. */ Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "GevSCPSPacketSize", GIGE_PACKET_SIZE); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "GevSCPD", (GIGE_PACKET_SIZE + GIGE_PROTOCOL_OVERHEAD) * (NUM_DEVICES - 1)); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "GevSCFTD", (GIGE_PACKET_SIZE + GIGE_PROTOCOL_OVERHEAD) * deviceIndex); } else if (deviceClass == "Basler1394") { /* For FireWire we just set the PacketSize node to limit the bandwidth we're using. */ /* We first divide the available bandwidth (4915 for FW400, 9830 for FW800) * by the number of devices we are using. */ long newPacketSize = 4915 / NUM_DEVICES; long recommendedPacketSize = 0; /* Get the recommended packet size from the camera. */ recommendedPacketSize = Pylon.DeviceGetIntegerFeature(hDev[deviceIndex], "RecommendedPacketSize"); if (newPacketSize < recommendedPacketSize) { /* Get the increment value for the packet size. * We must make sure that the new value we're setting is divisible by the increment of that feature. */ long packetSizeInc = 0; packetSizeInc = Pylon.DeviceGetIntegerFeatureInc(hDev[deviceIndex], "PacketSize"); /* Adjust the new packet size so is divisible by its increment. */ newPacketSize -= newPacketSize % packetSizeInc; } else { /* The recommended packet size should always be valid. No need to check against the increment. */ newPacketSize = recommendedPacketSize; } /* Set the new packet size. */ Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "PacketSize", newPacketSize); Console.WriteLine("Using packetsize: {0}", newPacketSize); } } /* Allocate and register buffers for grab. */ for (deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { /* Determine the required size for the grab buffer. */ payloadSize[deviceIndex] = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev[deviceIndex], "PayloadSize")); /* Image grabbing is done using a stream grabber. * A device may be able to provide different streams. A separate stream grabber must * be used for each stream. In this sample, we create a stream grabber for the default * stream, i.e., the first stream ( index == 0 ). */ /* Get the number of streams supported by the device and the transport layer. */ nStreams[deviceIndex] = Pylon.DeviceGetNumStreamGrabberChannels(hDev[deviceIndex]); if (nStreams[deviceIndex] < 1) { throw new Exception("The transport layer doesn't support image streams."); } /* Create and open a stream grabber for the first channel. */ hGrabber[deviceIndex] = Pylon.DeviceGetStreamGrabber(hDev[deviceIndex], 0); Pylon.StreamGrabberOpen(hGrabber[deviceIndex]); /* Get a handle for the stream grabber's wait object. The wait object * allows waiting for buffers to be filled with grabbed data. */ hWait[deviceIndex] = Pylon.StreamGrabberGetWaitObject(hGrabber[deviceIndex]); /* Add the stream grabber's wait object to our wait objects. * This is needed to be able to wait until all cameras have * grabbed an image in our grab loop below. */ Pylon.WaitObjectsAdd(wos, hWait[deviceIndex]); /* We must tell the stream grabber the number and size of the buffers * we are using. */ /* .. We will not use more than NUM_BUFFERS for grabbing. */ Pylon.StreamGrabberSetMaxNumBuffer(hGrabber[deviceIndex], NUM_BUFFERS); /* .. We will not use buffers bigger than payloadSize bytes. */ Pylon.StreamGrabberSetMaxBufferSize(hGrabber[deviceIndex], payloadSize[deviceIndex]); /* Allocate the resources required for grabbing. After this, critical parameters * that impact the payload size must not be changed until FinishGrab() is called. */ Pylon.StreamGrabberPrepareGrab(hGrabber[deviceIndex]); /* Before using the buffers for grabbing, they must be registered at * the stream grabber. For each registered buffer, a buffer handle * is returned. After registering, these handles are used instead of the * buffer objects pointers. The buffer objects are held in a dictionary, * that provides access to the buffer using a handle as key. */ buffers[deviceIndex] = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >(); for (i = 0; i < NUM_BUFFERS; ++i) { PylonBuffer <Byte> buffer = new PylonBuffer <byte>(payloadSize[deviceIndex], true); PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber[deviceIndex], ref buffer); buffers[deviceIndex].Add(handle, buffer); } /* Feed the buffers into the stream grabber's input queue. For each buffer, the API * allows passing in an integer as additional context information. This integer * will be returned unchanged when the grab is finished. In our example, we use the index of the * buffer as context information. */ i = 0; foreach (KeyValuePair <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > pair in buffers[deviceIndex]) { Pylon.StreamGrabberQueueBuffer(hGrabber[deviceIndex], pair.Key, i++); } } /* The stream grabber is now prepared. As soon the camera starts acquiring images, * the image data will be grabbed into the provided buffers. */ for (deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { /* Let the camera acquire images. */ Pylon.DeviceExecuteCommandFeature(hDev[deviceIndex], "AcquisitionStart"); } /* Set the timer to 5 s and start it. */ timer.Change(5000, Timeout.Infinite); /* Counts the number of grabbed images. */ nGrabs = 0; /* Grab until the timer expires. */ for (;;) { int bufferIndex; /* Index of the buffer. */ Byte min, max; uint woIndex; /* Wait for the next buffer to be filled. Wait up to 1000 ms. */ isReady = Pylon.WaitObjectsWaitForAny(wos, 1000, out woIndex); if (!isReady) { /* Timeout occurred. */ throw new Exception("Grab timeout occurred."); } /* If the timer has expired, exit the grab loop. */ if (woIndex == 0) { Console.Error.WriteLine("Grabbing completed successfully."); break; /* Timer expired. */ } /* Account for the timer. */ --woIndex; /* Since the wait operation was successful, the result of at least one grab * operation is available. Retrieve it. */ isReady = Pylon.StreamGrabberRetrieveResult(hGrabber[woIndex], out grabResult[woIndex]); if (!isReady) { /* Oops. No grab result available? We should never have reached this point. * Since the wait operation above returned without a timeout, a grab result * should be available. */ throw new Exception("Failed to retrieve a grab result."); } /* Get the buffer index from the context information. */ bufferIndex = grabResult[woIndex].Context; /* Check to see if the image was grabbed successfully. */ if (grabResult[woIndex].Status == EPylonGrabStatus.Grabbed) { /* Success. Perform image processing. Since we passed more than one buffer * to the stream grabber, the remaining buffers are filled in the background while * we do the image processing. The processed buffer won't be touched by * the stream grabber until we pass it back to the stream grabber. */ PylonBuffer <Byte> buffer; /* Reference to the buffer attached to the grab result. */ /* Get the buffer from the dictionary. Since we also got the buffer index, * we could alternatively use an array, e.g. buffers[bufferIndex]. */ if (!buffers[woIndex].TryGetValue(grabResult[woIndex].hBuffer, out buffer)) { /* Oops. No buffer available? We should never have reached this point. Since all buffers are * in the dictionary. */ throw new Exception("Failed to find the buffer associated with the handle returned in grab result."); } /* Perform processing. */ getMinMax(buffer.Array, grabResult[woIndex].SizeX, grabResult[woIndex].SizeY, out min, out max); Console.WriteLine("Grabbed frame {0} from camera {1} into buffer {2}. Min. val={3}, Max. val={4}", nGrabs, woIndex, bufferIndex, min, max); /* Display image */ Pylon.ImageWindowDisplayImage <Byte>(woIndex, buffer, grabResult[woIndex]); } else if (grabResult[woIndex].Status == EPylonGrabStatus.Failed) { Console.Error.WriteLine("Frame {0} wasn't grabbed successfully. Error code = {1}", nGrabs, grabResult[woIndex].ErrorCode); } /* Once finished with the processing, requeue the buffer to be filled again. */ Pylon.StreamGrabberQueueBuffer(hGrabber[woIndex], grabResult[woIndex].hBuffer, bufferIndex); nGrabs++; } /* Clean up. */ /* Stop the image aquisition on the cameras. */ for (deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { /* ... Stop the camera. */ Pylon.DeviceExecuteCommandFeature(hDev[deviceIndex], "AcquisitionStop"); } // Remove all wait objects from WaitObjects. Pylon.WaitObjectsRemoveAll(wos); Pylon.WaitObjectDestroy(woTimer); Pylon.WaitObjectsDestroy(wos); for (deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { /* ... We must issue a cancel call to ensure that all pending buffers are put into the * stream grabber's output queue. */ Pylon.StreamGrabberCancelGrab(hGrabber[deviceIndex]); /* ... The buffers can now be retrieved from the stream grabber. */ do { isReady = Pylon.StreamGrabberRetrieveResult(hGrabber[deviceIndex], out grabResult[deviceIndex]); } while (isReady); /* ... When all buffers are retrieved from the stream grabber, they can be deregistered. * After deregistering the buffers, it is safe to free the memory. */ foreach (KeyValuePair <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > pair in buffers[deviceIndex]) { Pylon.StreamGrabberDeregisterBuffer(hGrabber[deviceIndex], pair.Key); pair.Value.Dispose(); } buffers[deviceIndex] = null; /* ... Release grabbing related resources. */ Pylon.StreamGrabberFinishGrab(hGrabber[deviceIndex]); /* After calling PylonStreamGrabberFinishGrab(), parameters that impact the payload size (e.g., * the AOI width and height parameters) are unlocked and can be modified again. */ /* ... Close the stream grabber. */ Pylon.StreamGrabberClose(hGrabber[deviceIndex]); /* ... Close and release the pylon device. The stream grabber becomes invalid * after closing the pylon device. Don't call stream grabber related methods after * closing or releasing the device. */ Pylon.DeviceClose(hDev[deviceIndex]); Pylon.DestroyDevice(hDev[deviceIndex]); } /* Dispose timer and event. */ timer.Dispose(); timoutEvent.Close(); Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); /* ... Shut down the pylon runtime system. Don't call any pylon function after * calling PylonTerminate(). */ Pylon.Terminate(); } catch (Exception e) { /* Retrieve the error message. */ string msg = GenApi.GetLastErrorMessage() + "\n" + GenApi.GetLastErrorDetail(); Console.Error.WriteLine("Exception caught:"); Console.Error.WriteLine(e.Message); if (msg != "\n") { Console.Error.WriteLine("Last error message:"); Console.Error.WriteLine(msg); } for (uint deviceIndex = 0; deviceIndex < NUM_DEVICES; ++deviceIndex) { try { if (hDev[deviceIndex].IsValid) { /* ... Close and release the pylon device. */ if (Pylon.DeviceIsOpen(hDev[deviceIndex])) { Pylon.DeviceClose(hDev[deviceIndex]); } Pylon.DestroyDevice(hDev[deviceIndex]); } } catch (Exception) { /*No further handling here.*/ } } Pylon.Terminate(); /* Releases all pylon resources. */ Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); Environment.Exit(1); } }
static void Main(string[] args) { for (uint i = 0; i < args.Length; i++) { switch (args[i]) { case par_cameraSerialNumber: arg_cameraSerialNumber = args[i + 1]; i++; break; case par_pathToFile: arg_pathToFile = args[i + 1]; i++; break; case par_imageFormat: arg_imageFormat = args[i + 1]; i++; break; case par_packageSize: arg_packageSize = args[i + 1]; i++; break; case par_interPackageDelay: arg_interPackageDelay = args[i + 1]; i++; break; case par_attemptsToGrab: arg_attemptsToGrab = args[i + 1]; i++; break; case par_exposureTime: arg_exposureTime = args[i + 1]; i++; break; default: break; } } //arg_exposureTime = "35000"; bool error = false; if (arg_pathToFile.Length == 0) { Console.WriteLine("Path to file is empty"); error = true; } if (!(arg_imageFormat.Equals("BMP") || !arg_imageFormat.Equals("PNG") || !arg_imageFormat.Equals("JPG") || !arg_imageFormat.Equals("RAW") || !arg_imageFormat.Equals("TIFF"))) { Console.WriteLine("File format should be [BMP|PNG|JPG|RAW|TIFF]"); error = true; } if (arg_cameraSerialNumber.Length == 0) { Console.WriteLine("Camera serial number is empty"); error = true; } int exposureTime = 0; try { exposureTime = Int32.Parse(arg_exposureTime); } catch (Exception e) { Console.WriteLine("Wrong exposure time value"); error = true; } int interPackageDelay = 0; try { interPackageDelay = Int32.Parse(arg_interPackageDelay); } catch (Exception e) { Console.WriteLine("Wrong interPackageDelay value"); error = true; } int attemptsToGrap = 0; try { attemptsToGrap = Int16.Parse(arg_attemptsToGrab); }catch (Exception e) { Console.WriteLine("Wrong attempts to grab value"); error = true; } //sodapef if (error) { Console.WriteLine("Parameters usage:"); Console.WriteLine("-s Camera serial number"); Console.WriteLine("-o Path to file"); Console.WriteLine("-d Inter package delay in ticks (default 1000)"); Console.WriteLine("-a Attempts tp grab image (default 1)"); Console.WriteLine("-p Package size (default 1500)"); Console.WriteLine("-e Exposure time (default 35000)"); Console.WriteLine("-f Image format [BMP|PNG|JPG|RAW|TIFF]"); return; } PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */ try { uint numDevices; /* Number of available devices. */ const int numGrabs = 1; /* Number of images to grab. */ PylonBuffer <Byte> imgBuf = null; /* Buffer used for grabbing. */ bool isAvail; Pylon.Initialize(); numDevices = Pylon.EnumerateDevices(); if (0 == numDevices) { Console.WriteLine("Error: No devices found"); return; } bool deviceFound = false; uint deviceNum = 0; for (uint di = 0; di < numDevices; di++) { PYLON_DEVICE_INFO_HANDLE hDi = Pylon.GetDeviceInfoHandle((uint)di); string serial = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoSerialNumberKey); deviceNum = di; if (serial.Equals(arg_cameraSerialNumber)) { deviceFound = true; break; } } if (!deviceFound) { Console.WriteLine("Error: No devices found by serial number"); return; } hDev = Pylon.CreateDeviceByIndex(deviceNum); Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream | Pylon.cPylonAccessModeExclusive); isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8"); if (!isAvail) { Console.WriteLine("Error: Device doesn't support the Mono8 pixel format"); return; } Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8"); isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart"); Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off"); } isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart"); Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off"); } isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart"); Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off"); } isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize"); if (isAvail) { int packageSize = 1500; try{ packageSize = Int32.Parse(arg_packageSize); } catch (Exception e) { } Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", packageSize); } isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPD"); if (isAvail) { Pylon.DeviceSetIntegerFeature(hDev, "GevSCPD", interPackageDelay); } Pylon.DeviceFeatureFromString(hDev, "ExposureAuto", "Off"); /* * isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ExposureTimeRaw"); * if (isAvail) * { * try * { * Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeRaw", exposureTime); * } * catch (Exception e) * { * Console.WriteLine("Some error 1"); * } * }*/ isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ExposureTimeAbs"); if (isAvail) { Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeAbs", (long)exposureTime); /* * try{ * Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeAbs", (long)exposureTime); * }catch (Exception e) { * //Console.WriteLine("Some error 2"); * } */ } Byte min, max; PylonGrabResult_t grabResult; for (int attempt = 0; attempt < attemptsToGrap; attempt++) { if (!Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 5000)) { /* Timeout occurred. */ //Console.WriteLine("Frame {0}: timeout.", i + 1); Console.WriteLine("Error: timeout"); } /* Check to see if the image was grabbed successfully. */ if (grabResult.Status == EPylonGrabStatus.Grabbed) { /* Success. Perform image processing. */ getMinMax(imgBuf.Array, grabResult.SizeX, grabResult.SizeY, out min, out max); //Console.WriteLine("Grabbed frame {0}. Min. gray value = {1}, Max. gray value = {2}", i + 1, min, max); Console.WriteLine("Frame grabbed success"); /* Display image */ //Pylon.ImagePersistenceSave<Byte>(EPylonImageFileFormat.ImageFileFormat_Png, "C:\\Users\\v.yakubov\\grabber\\test.png", imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); if (arg_imageFormat.Equals("PNG")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Png, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("JPG")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Jpeg, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("RAW")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Raw, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("TIFF")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Tiff, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("BMP")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Bmp, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Bmp, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } break; } else if (grabResult.Status == EPylonGrabStatus.Failed) { //Console.Error.WriteLine("Frame {0} wasn't grabbed successfully. Error code = {1}", i + 1, grabResult.ErrorCode); Console.WriteLine("Error: failed"); } } imgBuf.Dispose(); Pylon.DeviceClose(hDev); Pylon.DestroyDevice(hDev); imgBuf = null; Pylon.Terminate(); } catch (Exception e) { try { if (hDev.IsValid) { if (Pylon.DeviceIsOpen(hDev)) { Pylon.DeviceClose(hDev); } Pylon.DestroyDevice(hDev); } } catch (Exception) {} Pylon.Terminate(); /* Releases all pylon resources. */ Environment.Exit(1); } }
static int callbackCounter = 0; /* Will be incremented by the callback function. */ static void Main(string[] args) { PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */ try { uint numDevices; /* Number of available devices. */ PYLON_DEVICECALLBACK_HANDLE hCb; /* Required for deregistering the callback. */ int loopCount; /* Counter. */ #if DEBUG bool isGigECamera; /* 1 if the device is a GigE device. */ #endif DeviceCallbackHandler callbackHandler = new DeviceCallbackHandler(); /* Handles callbacks from a device. */ /* Before using any pylon methods, the pylon runtime must be initialized. */ Pylon.Initialize(); /* Enumerate all camera devices. You must call * PylonEnumerateDevices() before creating a device. */ numDevices = Pylon.EnumerateDevices(); if (0 == numDevices) { throw new Exception("No devices found."); } /* Get a handle for the first device found. */ hDev = Pylon.CreateDeviceByIndex(0); /* Before using the device, it must be opened. Open it for configuring * parameters, for grabbing images, and for grabbing events. */ Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream); /* Print out the name of the camera we are using. */ { bool isReadable = Pylon.DeviceFeatureIsReadable(hDev, "DeviceModelName"); if (isReadable) { string name = Pylon.DeviceFeatureToString(hDev, "DeviceModelName"); Console.WriteLine("Using camera {0}", name); } } /* Register the callback function. */ callbackHandler.CallbackEvent += new DeviceCallbackHandler.DeviceCallback(removalCallbackFunction); hCb = Pylon.DeviceRegisterRemovalCallback(hDev, callbackHandler); #if DEBUG /* For GigE cameras, the application periodically sends heartbeat signals to the camera to keep the * connection to the camera alive. If the camera doesn't receive heartbeat signals within the time * period specified by the heartbeat timeout counter, the camera resets the connection. * When the application is stopped by the debugger, the application cannot create the heartbeat signals. * For that reason, the pylon runtime extends the heartbeat timeout in debug mode to 5 minutes to allow * debugging. For GigE cameras, we will set the heartbeat timeout to a shorter period before testing the * callbacks. * The heartbeat mechanism is also used for detection of device removal. When the pylon runtime doesn't * receive an acknowledge for the heartbeat signal, it is assumed that the device has been removed. A * removal callback will be fired in that case. * By decreasing the heartbeat timeout in debug mode for GigE cameras, the surprise removal will be noticed sooner than set by the pylon runtime. */ { /* Find out if we are using a GigE camera. */ PYLON_DEVICE_INFO_HANDLE hDi = Pylon.DeviceGetDeviceInfoHandle(hDev); string deviceClass = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoDeviceClassKey); isGigECamera = deviceClass == "BaslerGigE"; /* Adjust the heartbeat timeout. */ if (isGigECamera) { setHeartbeatTimeout(hDev, 1000); /* 1000 ms */ } } #endif /* Ask the user to disconnect a device. */ loopCount = 20 * 4; Console.WriteLine("Please disconnect the device (timeout {0} s) ", loopCount / 4); /* Wait until the removal has been noticed and the callback function has been fired. */ do { /* Print a . every few seconds to tell the user we're waiting for the callback. */ if (--loopCount % 4 == 0) { Console.Write("."); } System.Threading.Thread.Sleep(250); }while (callbackCounter < 1 && loopCount >= 0); /* Check loopCount so we won't wait forever. */ if (callbackCounter < 1) { Console.WriteLine("\nTimeout expired. Device hasn't been removed."); } /* Clean up. */ /* ... Deregister the removal callback. */ Pylon.DeviceDeregisterRemovalCallback(hDev, hCb); /* ....Close and release the pylon device. */ Pylon.DeviceClose(hDev); Pylon.DestroyDevice(hDev); /* Shut down the pylon runtime system. Don't call any pylon method after * calling Pylon.Terminate(). */ Pylon.Terminate(); Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); } catch (Exception e) { /* Retrieve the error message. */ string msg = GenApi.GetLastErrorMessage() + "\n" + GenApi.GetLastErrorDetail(); Console.Error.WriteLine("Exception caught:"); Console.Error.WriteLine(e.Message); if (msg != "\n") { Console.Error.WriteLine("Last error message:"); Console.Error.WriteLine(msg); } try { if (hDev.IsValid) { /* ... Close and release the pylon device. */ if (Pylon.DeviceIsOpen(hDev)) { Pylon.DeviceClose(hDev); } Pylon.DestroyDevice(hDev); } } catch (Exception) { /*No further handling here.*/ } Pylon.Terminate(); /* Releases all pylon resources. */ Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); Environment.Exit(1); } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { /* Use a random number as the device key. */ uint DeviceKey = (uint)(new Random()).Next(int.MaxValue); /* In this sample all cameras belong to the same group. */ const uint GroupKey = 0x24; PYLON_DEVICE_HANDLE[] hDev = new PYLON_DEVICE_HANDLE[MAX_NUM_DEVICES]; /* Handles for the pylon devices. */ for (int deviceIndex = 0; deviceIndex < MAX_NUM_DEVICES; ++deviceIndex) { hDev[deviceIndex] = new PYLON_DEVICE_HANDLE(); } try { uint numDevicesEnumerated; /* Number of the devices connected to this PC. */ uint numDevicesToUse; /* Number of the devices to use in this sample. */ bool isAvail; /* Used for checking feature availability. */ bool isReady; /* Used as an output parameter. */ int i; /* Counter. */ uint deviceIndex; /* Index of device used in the following variables. */ PYLON_WAITOBJECTS_HANDLE wos; /* Wait objects. */ /* These are camera specific variables: */ PYLON_STREAMGRABBER_HANDLE[] hGrabber = new PYLON_STREAMGRABBER_HANDLE[MAX_NUM_DEVICES]; /* Handle for the pylon stream grabber. */ PYLON_WAITOBJECT_HANDLE[] hWait = new PYLON_WAITOBJECT_HANDLE[MAX_NUM_DEVICES]; /* Handle used for waiting for a grab to be finished. */ uint[] payloadSize = new uint[MAX_NUM_DEVICES]; /* Size of an image frame in bytes. */ uint[] nStreams = new uint[MAX_NUM_DEVICES]; /* The number of streams provided by the device. */ PYLON_STREAMBUFFER_HANDLE[] hBuffer = new PYLON_STREAMBUFFER_HANDLE[MAX_NUM_DEVICES]; PylonBuffer <Byte>[] buffer = new PylonBuffer <Byte> [MAX_NUM_DEVICES]; #if DEBUG /* This is a special debug setting needed only for GigE cameras. * See 'Building Applications with pylon' in the Programmer's Guide. */ Environment.SetEnvironmentVariable("PYLON_GIGE_HEARTBEAT", "300000" /*ms*/); #endif /* Before using any pylon methods, the pylon runtime must be initialized. */ Pylon.Initialize(); /* Enumerate all camera devices. You must call * PylonEnumerateDevices() before creating a device. */ numDevicesEnumerated = Pylon.EnumerateDevices(); if (numDevicesEnumerated == 0) { Pylon.Terminate(); Console.Error.WriteLine("No devices found!"); Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); return; } /* Create wait objects. This must be done outside of the loop. */ wos = Pylon.WaitObjectsCreate(); /* Open cameras and set parameter */ deviceIndex = 0; for (uint enumeratedDeviceIndex = 0; enumeratedDeviceIndex < numDevicesEnumerated; ++enumeratedDeviceIndex) { /* only open GigE devices */ PYLON_DEVICE_INFO_HANDLE hDI = Pylon.GetDeviceInfoHandle(enumeratedDeviceIndex); if (Pylon.DeviceInfoGetPropertyValueByName(hDI, Pylon.cPylonDeviceInfoDeviceClassKey) != "BaslerGigE") { continue; } /* Get handles for the devices. */ hDev[deviceIndex] = Pylon.CreateDeviceByIndex((uint)enumeratedDeviceIndex); /* Before using the device, it must be opened. Open it for configuring * parameters and for grabbing images. */ Pylon.DeviceOpen(hDev[deviceIndex], Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream); /* Print out the name of the camera we are using. */ Console.WriteLine("Using camera '{0}'", Pylon.DeviceInfoGetPropertyValueByName(hDI, Pylon.cPylonDeviceInfoModelNameKey)); isAvail = Pylon.DeviceFeatureIsReadable(hDev[deviceIndex], "ActionControl"); if (!isAvail) { throw new Exception("Device doesn't support the Action Command"); } /* Configure the first action */ Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "ActionSelector", 1); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "ActionDeviceKey", DeviceKey); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "ActionGroupKey", GroupKey); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "ActionGroupMask", AllGroupMask); /* Set the pixel format to Mono8, where gray values will be output as 8 bit values for each pixel. */ /* ... Check first to see if the device supports the Mono8 format. */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_PixelFormat_Mono8"); if (!isAvail) { /* Feature is not available. */ throw new Exception("Device doesn't support the Mono8 pixel format."); } /* ... Set the pixel format to Mono8. */ Pylon.DeviceFeatureFromString(hDev[deviceIndex], "PixelFormat", "Mono8"); /* Disable acquisition start trigger if available */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_TriggerSelector_AcquisitionStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSelector", "AcquisitionStart"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerMode", "Off"); } /* Disable line1 trigger if available */ isAvail = Pylon.DeviceFeatureIsAvailable(hDev[deviceIndex], "EnumEntry_TriggerSelector_Line1"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSelector", "Line1"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerMode", "Off"); } /* Enable frame start trigger with first action */ Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSelector", "FrameStart"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerMode", "On"); Pylon.DeviceFeatureFromString(hDev[deviceIndex], "TriggerSource", "Action1"); /* For GigE cameras, we recommend increasing the packet size for better * performance. When the network adapter supports jumbo frames, set the packet * size to a value > 1500, e.g., to 8192. In this sample, we only set the packet size * to 1500. * * We also set the Inter-Packet and the Frame Transmission delay * so the switch can line up packets better. */ Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "GevSCPSPacketSize", GIGE_PACKET_SIZE); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "GevSCPD", (GIGE_PACKET_SIZE + GIGE_PROTOCOL_OVERHEAD) * (MAX_NUM_DEVICES - 1)); Pylon.DeviceSetIntegerFeature(hDev[deviceIndex], "GevSCFTD", (GIGE_PACKET_SIZE + GIGE_PROTOCOL_OVERHEAD) * deviceIndex); /* one device opened */ ++deviceIndex; } /* remember how many devices we have actually created */ numDevicesToUse = deviceIndex; /* Remember the number of devices actually created */ numDevicesToUse = deviceIndex; if (numDevicesToUse == 0) { Console.Error.WriteLine("No suitable cameras found!"); Pylon.Terminate(); /* Releases all pylon resources. */ Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); Environment.Exit(0); } if (numDevicesToUse < 2) { Console.Error.WriteLine("WARNING: This sample works best with two or more GigE cameras supporting action commands."); } /* Allocate and register buffers for grab. */ for (deviceIndex = 0; deviceIndex < numDevicesToUse; ++deviceIndex) { /* Determine the required size for the grab buffer. */ payloadSize[deviceIndex] = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev[deviceIndex], "PayloadSize")); /* Image grabbing is done using a stream grabber. * A device may be able to provide different streams. A separate stream grabber must * be used for each stream. In this sample, we create a stream grabber for the default * stream, i.e., the first stream ( index == 0 ). */ /* Get the number of streams supported by the device and the transport layer. */ nStreams[deviceIndex] = Pylon.DeviceGetNumStreamGrabberChannels(hDev[deviceIndex]); if (nStreams[deviceIndex] < 1) { throw new Exception("The transport layer doesn't support image streams."); } /* Create and open a stream grabber for the first channel. */ hGrabber[deviceIndex] = Pylon.DeviceGetStreamGrabber(hDev[deviceIndex], 0); Pylon.StreamGrabberOpen(hGrabber[deviceIndex]); /* Get a handle for the stream grabber's wait object. The wait object * allows waiting for buffers to be filled with grabbed data. */ hWait[deviceIndex] = Pylon.StreamGrabberGetWaitObject(hGrabber[deviceIndex]); /* Add the stream grabber's wait object to our wait objects. * This is needed to be able to wait until all cameras have * grabbed an image in our grab loop below. */ Pylon.WaitObjectsAdd(wos, hWait[deviceIndex]); /* We must tell the stream grabber the number and size of the buffers * we are using. */ /* .. We will not use more than NUM_BUFFERS for grabbing. */ Pylon.StreamGrabberSetMaxNumBuffer(hGrabber[deviceIndex], NUM_BUFFERS); /* .. We will not use buffers bigger than payloadSize bytes. */ Pylon.StreamGrabberSetMaxBufferSize(hGrabber[deviceIndex], payloadSize[deviceIndex]); /* Allocate the resources required for grabbing. After this, critical parameters * that impact the payload size must not be changed until FinishGrab() is called. */ Pylon.StreamGrabberPrepareGrab(hGrabber[deviceIndex]); /* Before using the buffers for grabbing, they must be registered at * the stream grabber. For each registered buffer, a buffer handle * is returned. After registering, these handles are used instead of the * buffer objects pointers. The buffer objects are held in a dictionary, * that provides access to the buffer using a handle as key. */ buffer[deviceIndex] = new PylonBuffer <byte>(payloadSize[deviceIndex], true); hBuffer[deviceIndex] = Pylon.StreamGrabberRegisterBuffer(hGrabber[deviceIndex], ref buffer[deviceIndex]); /* Feed the buffers into the stream grabber's input queue. */ Pylon.StreamGrabberQueueBuffer(hGrabber[deviceIndex], hBuffer[deviceIndex], 0); } /* The stream grabber is now prepared. Start the image acquisition. * The camera won't send any image data, since it's configured to wait * for the action to trigger the acquisition */ for (deviceIndex = 0; deviceIndex < numDevicesToUse; ++deviceIndex) { Pylon.DeviceExecuteCommandFeature(hDev[deviceIndex], "AcquisitionStart"); } /* ====================================================================== * Issue an ActionCommand and retrieve the images. * ====================================================================== */ Console.WriteLine("*** Issuing action command ***"); /* Trigger the camera using an action command (w/o waiting for results). * If your setup support PTP, you could use a scheduled action command. * Pylon.GigEIssueScheduledActionCommand(subnet, DefaultDeviceKey, DefaultGroupKey, 1, triggertime, 0) */ string subnet = Pylon.DeviceInfoGetPropertyValueByName(Pylon.DeviceGetDeviceInfoHandle(hDev[0]), "SubnetAddress"); Pylon.GigEIssueActionCommand(DeviceKey, GroupKey, 1, subnet); /* Grab one image from each camera. */ for (i = 0; i < numDevicesToUse; ++i) { uint woIndex; /* this corresponds to the index in hDev and hGrabber */ /* Wait for the next buffer to be filled. Wait up to 5000 ms. */ isReady = Pylon.WaitObjectsWaitForAny(wos, 5000, out woIndex); if (!isReady) { /* Timeout occurred */ /* Grab Timeout occurred. */ throw new Exception("Grab timeout occurred."); } PylonGrabResult_t grabResult; /* Since the wait operation was successful, the result of at least one grab * operation is available. Retrieve it. */ isReady = Pylon.StreamGrabberRetrieveResult(hGrabber[woIndex], out grabResult); if (!isReady) { /* Oops. No grab result available? We should never have reached this point. * Since the wait operation above returned without a timeout, a grab result * should be available. */ throw new Exception("Failed to retrieve a grab result."); } /* Check to see if the image was grabbed successfully. */ if (grabResult.Status == EPylonGrabStatus.Grabbed) { /* Success. Perform image processing. Since we passed more than one buffer * to the stream grabber, the remaining buffers are filled in the background while * we do the image processing. The processed buffer won't be touched by * the stream grabber until we pass it back to the stream grabber. */ /* We only use one buffer per camera */ System.Diagnostics.Debug.Assert(grabResult.hBuffer == hBuffer[woIndex]); byte pixel = buffer[woIndex].Array[0]; /* Perform processing. */ Console.WriteLine("Grabbed a frame from camera {0}.", woIndex); /* Display image */ if (woIndex < 32) { Pylon.ImageWindowDisplayImage <Byte>(woIndex, buffer[woIndex], grabResult); } } else if (grabResult.Status == EPylonGrabStatus.Failed) { /* If a buffer has been incompletely grabbed the network bandwidth is possibly insufficient for transferring * multiple images simultaneously. See note above MAX_NUM_DEVICES. */ Console.Error.WriteLine("Frame from camera {0} wasn't grabbed successfully. Error code = {1}", woIndex, grabResult.ErrorCode); } } /* Stop the image acquisition on the cameras. */ for (deviceIndex = 0; deviceIndex < numDevicesToUse; ++deviceIndex) { /* Stop the camera. */ Pylon.DeviceExecuteCommandFeature(hDev[deviceIndex], "AcquisitionStop"); } // Remove all wait objects from WaitObjects. Pylon.WaitObjectsRemoveAll(wos); Pylon.WaitObjectsDestroy(wos); for (deviceIndex = 0; deviceIndex < numDevicesToUse; ++deviceIndex) { /* We must issue a cancel call to ensure that all pending buffers are put into the * stream grabber's output queue. */ Pylon.StreamGrabberCancelGrab(hGrabber[deviceIndex]); /* The buffers can now be retrieved from the stream grabber. */ do { PylonGrabResult_t grabResult; isReady = Pylon.StreamGrabberRetrieveResult(hGrabber[deviceIndex], out grabResult); } while (isReady); /* When all buffers are retrieved from the stream grabber, they can be de-registered. * After de-registering the buffers, it is safe to free the memory. */ Pylon.StreamGrabberDeregisterBuffer(hGrabber[deviceIndex], hBuffer[deviceIndex]); buffer[deviceIndex].Dispose(); buffer[deviceIndex] = null; /* Release grabbing related resources. */ Pylon.StreamGrabberFinishGrab(hGrabber[deviceIndex]); /* After calling PylonStreamGrabberFinishGrab(), parameters that impact the payload size (e.g., * the AOI width and height parameters) are unlocked and can be modified again. */ /* Close the stream grabber. */ Pylon.StreamGrabberClose(hGrabber[deviceIndex]); /* Close and release the pylon device. The stream grabber becomes invalid * after closing the pylon device. Don't call stream grabber related methods after * closing or releasing the device. */ Pylon.DeviceClose(hDev[deviceIndex]); Pylon.DestroyDevice(hDev[deviceIndex]); } Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); /* Shut down the pylon runtime system. Don't call any pylon function after * calling PylonTerminate(). */ Pylon.Terminate(); } catch (Exception e) { /* Retrieve the error message. */ string msg = GenApi.GetLastErrorMessage() + "\n" + GenApi.GetLastErrorDetail(); Console.Error.WriteLine("Exception caught:"); Console.Error.WriteLine(e.Message); if (msg != "\n") { Console.Error.WriteLine("Last error message:"); Console.Error.WriteLine(msg); } for (uint deviceIndex = 0; deviceIndex < MAX_NUM_DEVICES; ++deviceIndex) { try { if (hDev[deviceIndex].IsValid) { /* Close and release the pylon device. */ if (Pylon.DeviceIsOpen(hDev[deviceIndex])) { Pylon.DeviceClose(hDev[deviceIndex]); } Pylon.DestroyDevice(hDev[deviceIndex]); } } catch (Exception) { /* No further handling here.*/ } } Pylon.Terminate(); /* Releases all pylon resources. */ Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); Environment.Exit(1); } }
/// <summary> /// Check if device is ready /// </summary> /// <returns>true if device is ready</returns> /// <param name="isUpdateListDevice">true if update list of devices</param> private static bool IsReadyDevice(bool isUpdateListDevice) { bool isReady = false; try { // Update list of cameras if (isUpdateListDevice || list == null) { list = DeviceEnumerator.EnumerateDevices(); } if (list.Count == 0) { return(false); } PYLON_DEVICE_HANDLE device = null; BaslerParams baslerParams = new BaslerParams(); for (int i = 0; i < list.Count; i++) { // Check ip address try { device = Pylon.CreateDeviceByIndex((uint)i); string ipAddress = Pylon.DeviceInfoGetPropertyValueByName(Pylon.DeviceGetDeviceInfoHandle(device), Pylon.cPylonDeviceInfoIpAddressKey); if (string.Compare(baslerParams.IpAddress, ipAddress) == 0) { cameraIndex = i; return(true); } } catch (Exception ex) { isReady = false; } finally { try { // Clean up. Close and release the pylon device. if (Pylon.DeviceIsOpen(device)) { Pylon.DeviceClose(device); } Pylon.DestroyDevice(device); } catch (Exception ex) { isReady = false; } } } } catch (Exception ex) { isReady = false; } return(isReady); }