Beispiel #1
0
        /* Some features are floating point features. This function illustrates how to set and get floating
         * point parameters. */
        private static void demonstrateFloatFeature(PYLON_DEVICE_HANDLE hDev)
        {
            string featureName = "Gamma";  /* The name of the feature used. */
            bool   isAvailable;            /* Is the feature available? */
            bool   isWritable;             /* Is the feature writable? */
            double min, max, value;        /* Value range and current value. */

            isAvailable = Pylon.DeviceFeatureIsAvailable(hDev, featureName);

            if (isAvailable)
            {
                /* Query the value range and the current value. */
                min   = Pylon.DeviceGetFloatFeatureMin(hDev, featureName);
                max   = Pylon.DeviceGetFloatFeatureMax(hDev, featureName);
                value = Pylon.DeviceGetFloatFeature(hDev, featureName);

                Console.WriteLine("{0}: min = {1}, max = {2}, value = {3}", featureName, min, max, value);

                /* Set a new value. */
                isWritable = Pylon.DeviceFeatureIsWritable(hDev, featureName);
                if (isWritable)
                {
                    value = 0.5 * (min + max);
                    Console.WriteLine("Setting {0} to {1}.", featureName, value);
                    Pylon.DeviceSetFloatFeature(hDev, featureName, value);
                }
            }
            else
            {
                Console.WriteLine("The {0} feature isn't available.", featureName);
            }
        }
Beispiel #2
0
        /* Some features are boolean features that can be switched on and off.
         * This function illustrates how to access boolean features. */
        private static void demonstrateBooleanFeature(PYLON_DEVICE_HANDLE hDev)
        {
            string featureName = "GammaEnable"; /* The name of the feature. */
            bool   isWritable;                  /* Is the feature writable? */
            bool   value;                       /* The value of the feature. */

            /* Check to see if the feature is writable. */
            isWritable = Pylon.DeviceFeatureIsWritable(hDev, featureName);

            if (isWritable)
            {
                /* Retrieve the current state of the feature. */
                value = Pylon.DeviceGetBooleanFeature(hDev, featureName);

                Console.WriteLine("The {0} features is {1}.", featureName, value ? "on" : "off");

                /* Set a new value. */
                value = !value;  /* New value. */
                Console.WriteLine("Switching the {0} feature {1}.", featureName, value ? "on" : "off");
                Pylon.DeviceSetBooleanFeature(hDev, featureName, value);
            }
            else
            {
                Console.WriteLine("The {0} feature isn't writable.", featureName);
            }
        }
Beispiel #3
0
        private void setChunkModeFeatures()
        {
            /* Before enabling individual chunks, the chunk mode in general must be activated. */
            bool isAvail = Pylon.DeviceFeatureIsWritable(_pylonDevHandle, "ChunkModeActive");

            if (!isAvail)
            {
                throw new Exception("The device doesn't support the chunk mode.");
            }

            /* Activate the chunk mode. */
            Pylon.DeviceSetBooleanFeature(_pylonDevHandle, "ChunkModeActive", true);

            /* Enable some individual chunks... */

            /* ... The frame counter chunk feature. */
            /* Is the chunk feature available? */
            isAvail = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "EnumEntry_ChunkSelector_Framecounter");

            if (isAvail)
            {
                /* Select the frame counter chunk feature. */
                Pylon.DeviceFeatureFromString(_pylonDevHandle, "ChunkSelector", "Framecounter");

                /* Can the chunk feature be activated? */
                isAvail = Pylon.DeviceFeatureIsWritable(_pylonDevHandle, "ChunkEnable");

                if (isAvail)
                {
                    /* Activate the chunk feature. */
                    Pylon.DeviceSetBooleanFeature(_pylonDevHandle, "ChunkEnable", true);
                }
            }
            /* ... The CRC checksum chunk feature. */

            /*  Note: Enabling the CRC checksum chunk feature is not a prerequisite for using
             * chunks. Chunks can also be handled when the CRC checksum chunk feature is disabled. */
            isAvail = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "EnumEntry_ChunkSelector_PayloadCRC16");

            if (isAvail)
            {
                /* Select the CRC checksum chunk feature. */
                Pylon.DeviceFeatureFromString(_pylonDevHandle, "ChunkSelector", "PayloadCRC16");

                /* Can the chunk feature be activated? */
                isAvail = Pylon.DeviceFeatureIsWritable(_pylonDevHandle, "ChunkEnable");

                if (isAvail)
                {
                    /* Activate the chunk feature. */
                    Pylon.DeviceSetBooleanFeature(_pylonDevHandle, "ChunkEnable", true);
                }
            }
        }
Beispiel #4
0
        private bool setStreamModeFeature()
        {
            bool isAvail;

            /* 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(_pylonDevHandle, "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(_pylonDevHandle, "PixelFormat", "Mono8");

            /* Disable acquisition start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "EnumEntry_TriggerSelector_AcquisitionStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSelector", "AcquisitionStart");
                Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerMode", "Off");
            }

            /* Disable frame start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "EnumEntry_TriggerSelector_FrameStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSelector", "FrameStart");
                Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerMode", "Off");
            }

            /* We will use the Continuous frame mode, i.e., the camera delivers
             * images continuously. */
            Pylon.DeviceFeatureFromString(_pylonDevHandle, "AcquisitionMode", "Continuous");

            /* 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. */
            /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
            isAvail = Pylon.DeviceFeatureIsWritable(_pylonDevHandle, "GevSCPSPacketSize");
            if (isAvail)
            {
                /* ... The device supports the packet size feature. Set a value. */
                Pylon.DeviceSetIntegerFeature(_pylonDevHandle, "GevSCPSPacketSize", 8192);
            }
            return(isAvail);
        }
Beispiel #5
0
        /// <summary>
        /// 設定 Device Handle 功能
        /// </summary>
        /// <param name="hDev"></param>
        /// <param name="features"></param>
        /// <returns></returns>
        public bool SetPylonDeviceHandleFeatures(List <PylonFeature> features)
        {
            var  success = true;
            bool isAvail, isWritable;

            _latestMessage = "";
            foreach (var feature in features)
            {
                isAvail    = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, feature.Name);
                isWritable = Pylon.DeviceFeatureIsWritable(_pylonDevHandle, feature.Key);
                if (!isAvail)
                {
                    _latestMessage += "Device doesn't support the " + feature.Name + ";";
                    success         = false;
                }
                else if (!isWritable)
                {
                    _latestMessage += "Writable doesn't support the " + feature.Name + ";";
                    success         = false;
                }
                else
                {
                    Type valueType = feature.Value.GetType();
                    switch (valueType.Name)
                    {
                    case "Boolean":
                        Pylon.DeviceSetBooleanFeature(_pylonDevHandle, feature.Key, (bool)feature.Value);
                        break;

                    case "String":
                        Pylon.DeviceFeatureFromString(_pylonDevHandle, feature.Key, (string)feature.Value);
                        break;

                    case "Int32":
                        Pylon.DeviceSetIntegerFeature(_pylonDevHandle, feature.Key, (int)feature.Value);
                        break;

                    case "Double":
                        Pylon.DeviceSetFloatFeature(_pylonDevHandle, feature.Key, (double)feature.Value);
                        break;
                    }
                }
            }
            return(success);
        }
Beispiel #6
0
        /// <summary>
        /// Set camera config.
        /// </summary>
        /// <param name="device">camera device</param>
        /// <param name="featureName">name of config</param>
        /// <param name="value">value of config</param>
        private void SetConfig(PYLON_DEVICE_HANDLE device, string featureName, object value)
        {
            try
            {
                if (value == null)
                {
                    return;
                }

                // Check to see if a feature is implemented, writable.
                bool isAvailable;
                bool isWritable;

                // Check feature
                isAvailable = Pylon.DeviceFeatureIsImplemented(device, featureName);
                isWritable  = Pylon.DeviceFeatureIsWritable(device, featureName);

                // Set config feature
                if (isAvailable && isWritable)
                {
                    if (value.GetType() == typeof(int))
                    {
                        Pylon.DeviceSetIntegerFeature(device, featureName, (int)value);
                    }

                    if (value.GetType() == typeof(float))
                    {
                        Pylon.DeviceSetFloatFeature(device, featureName, (float)value);
                    }

                    if (value.GetType() == typeof(string))
                    {
                        Pylon.DeviceFeatureFromString(device, featureName, (string)value);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #7
0
        private void setGevSCPSPacketSize()
        {
            /* For GigE cameras, we recommend increasing the packet size for better
             *         performance. If 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. */
            /* ... Check first to see if the GigE camera packet size parameter is supported
             *      and if it is writable.
             *
             * 開啟 pylonViewr, 找到裝置的 Transport Layer 節點, 可以設定 Packet Size,
             * 在裝置的的 Stream Parameter 節點,觀察 Statistic 節點中的 Failed Buffer cout,
             * 若無 Fail, 則可調高 packet size
             *
             */
            bool isAvail = Pylon.DeviceFeatureIsWritable(_pylonDevHandle, "GevSCPSPacketSize");

            if (isAvail)
            {
                /* ... The device supports the packet size feature. Set a value. */
                Pylon.DeviceSetIntegerFeature(_pylonDevHandle, "GevSCPSPacketSize", 1500);
            }
        }
Beispiel #8
0
        /* This function demonstrates how to check the presence, readability, and writability
         *  of a feature. */
        private static void demonstrateAccessibilityCheck(PYLON_DEVICE_HANDLE hDev)
        {
            bool val;  /* Output of the check functions. */

            /* Check to see if a feature is implemented at all. */
            val = Pylon.DeviceFeatureIsImplemented(hDev, "Width");

            Console.WriteLine("The 'Width' feature {0} implemented.", val ? "is" : "isn't");
            val = Pylon.DeviceFeatureIsImplemented(hDev, "MyCustomFeature");
            Console.WriteLine("The 'MyCustomFeature' feature {0} implemented.", val ? "is" : "isn't");

            /* Although a feature is implemented by the device, it may not be available
             * with the device in its current state. Check to see if the feature is currently
             * available. The PylonDeviceFeatureIsAvailable sets val to 0 if either the feature
             * is not implemented or if the feature is not currently available. */

            val = Pylon.DeviceFeatureIsAvailable(hDev, "BinningVertical");
            Console.WriteLine("The 'BinningVertical' feature {0} available.", val ? "is" : "isn't");

            /* If a feature is available, it can be read-only, write-only, or both
             * readable and writable. Use the PylonDeviceFeatureIsReadable() and the
             * PylonDeviceFeatureIsWritable() functions(). It is safe to call these functions
             * for features that are currently not available or not implemented by the device.
             * A feature that is not available or not implemented is neither readable nor writable.
             * The readability and writability of a feature can change depending on the current
             * state of the device. For example, the Width parameter may not be writable when
             * the camera is acquiring images. */

            val = Pylon.DeviceFeatureIsReadable(hDev, "Width");
            Console.WriteLine("The 'Width' feature {0} readable.", val ? "is" : "isn't");

            val = Pylon.DeviceFeatureIsReadable(hDev, "MyCustomFeature");
            Console.WriteLine("The 'MyCustomFeature' feature {0} readable.", val ? "is" : "isn't");

            val = Pylon.DeviceFeatureIsWritable(hDev, "Width");
            Console.WriteLine("The 'Width' feature {0} writable.", val ? "is" : "isn't");
            Console.WriteLine("");
        }
Beispiel #9
0
        int i;                                                               /* Counter. */


        public void PylonC_Open()
        {
#if Local_Variables
            PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE();             /* Handle for the pylon device. */
#endif
            try
            {
#if Local_Variables
                uint numDevices;                                                     /* Number of available devices. */
                PYLON_STREAMGRABBER_HANDLE hGrabber;                                 /* Handle for the pylon stream grabber. */
                PYLON_WAITOBJECT_HANDLE    hWait;                                    /* Handle used for waiting for a grab to be finished. */
                uint payloadSize;                                                    /* Size of an image frame in bytes. */
                Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > buffers; /* Holds handles and buffers used for grabbing. */
                PylonGrabResult_t grabResult;                                        /* Stores the result of a grab operation. */
                int  nGrabs;                                                         /* Counts the number of buffers grabbed. */
                uint nStreams;                                                       /* The number of streams provides by the device.  */
                bool isAvail;                                                        /* Used for checking feature availability. */
                bool isReady;                                                        /* Used as an output parameter. */
                int  i;                                                              /* Counter. */
#endif

#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. */
                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 and for grabbing images. */
                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);
                    }
                }

                /* 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, "EnumEntry_PixelFormat_YUV422Packed");

                if (!isAvail)
                {
                    /* Feature is not available. */
                    throw new Exception("Device doesn't support the Mono8/YUV422Packed pixel format.");
                }
                /* ... Set the pixel format to Mono8. */
                Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "YUV422Packed");

                /* Disable acquisition start trigger if available */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* Disable frame burst start trigger if available */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* Disable frame start trigger if available */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* We will use the Continuous frame mode, i.e., the camera delivers
                 * images continuously. */
                Pylon.DeviceFeatureFromString(hDev, "AcquisitionMode", "Continuous");

                /* 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. */
                /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
                isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
                if (isAvail)
                {
                    /* ... The device supports the packet size feature. Set a value. */
                    Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
                }

                /* Determine the required size of the grab buffer. */
                payloadSize = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev, "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 = Pylon.DeviceGetNumStreamGrabberChannels(hDev);

                if (nStreams < 1)
                {
                    throw new Exception("The transport layer doesn't support image streams.");
                }

                /* Create and open a stream grabber for the first channel. */
                hGrabber = Pylon.DeviceGetStreamGrabber(hDev, 0);
                Pylon.StreamGrabberOpen(hGrabber);

                /* Get a handle for the stream grabber's wait object. The wait object
                 * allows waiting for buffers to be filled with grabbed data. */
                hWait = Pylon.StreamGrabberGetWaitObject(hGrabber);

                /* 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, NUM_BUFFERS);

                /* .. We will not use buffers bigger than payloadSize bytes. */
                Pylon.StreamGrabberSetMaxBufferSize(hGrabber, payloadSize);

                /*  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);

                /* 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 = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >();
                for (i = 0; i < NUM_BUFFERS; ++i)
                {
                    PylonBuffer <Byte>        buffer = new PylonBuffer <byte>(payloadSize, true);
                    PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber, ref buffer);
                    buffers.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)
                {
                    Pylon.StreamGrabberQueueBuffer(hGrabber, 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.  */

                /* Let the camera acquire images. */
                Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStart");

                /* Grab NUM_GRABS images */
                nGrabs = 0;                                         /* Counts the number of grabbed images. */
                //while (nGrabs < NUM_GRABS)
                while (nGrabs < NUM_GRABS)
                {
                    int  bufferIndex;                                   /* Index of the buffer. */
                    Byte min, max;
                    /* Wait for the next buffer to be filled. Wait up to 1000 ms. */
                    isReady = Pylon.WaitObjectWait(hWait, 1000);

                    if (!isReady)
                    {
                        /* Timeout occurred. */
                        throw new Exception("Grab timeout occurred.");
                    }

                    /* Since the wait operation was successful, the result of at least one grab
                     * operation is available. Retrieve it. */
                    isReady = Pylon.StreamGrabberRetrieveResult(hGrabber, 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");
                    }

                    nGrabs++;

                    /* Get the buffer index from the context information. */
                    bufferIndex = (int)grabResult.Context;

                    /* 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. */

                        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.TryGetValue(grabResult.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.SizeX, grabResult.SizeY, out min, out max);
                        Console.WriteLine("Grabbed frame {0} into buffer {1}. Min. gray value = {2}, Max. gray value = {3}",
                                          nGrabs, bufferIndex, min, max);

                        /* Display image */                        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                        WpfApp5.MainWindow.Pylon_Buffer = buffer;
                        WpfApp5.MainWindow.GrabResult   = grabResult;
                        Console.WriteLine("the contents of buffer: [{0, 10}] in OverlappedGrab Class.", buffer.Array[0]);
                        Console.WriteLine("the contents of buffer: [{0, 10}] in WpfApp3.MainWindow.Pylon_Buffer.", WpfApp5.MainWindow.Pylon_Buffer.Array[0]);

                        Pylon.ImageWindowDisplayImage <Byte>(0, buffer, grabResult);
                        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    }
                    else if (grabResult.Status == EPylonGrabStatus.Failed)
                    {
                        Console.Error.WriteLine("Frame {0} wasn't grabbed successfully.  Error code = {1}",
                                                nGrabs, grabResult.ErrorCode);
                    }

                    /* Once finished with the processing, requeue the buffer to be filled again. */
                    Pylon.StreamGrabberQueueBuffer(hGrabber, grabResult.hBuffer, bufferIndex);
                }

                /* Clean up. */

#if DONT_COMPILE_THESE_CODE
                /*  ... Stop the camera. */
                Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStop");

                /* ... We must issue a cancel call to ensure that all pending buffers are put into the
                 * stream grabber's output queue. */
                Pylon.StreamGrabberCancelGrab(hGrabber);

                /* ... The buffers can now be retrieved from the stream grabber. */
                do
                {
                    isReady = Pylon.StreamGrabberRetrieveResult(hGrabber, out grabResult);
                } 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)
                {
                    Pylon.StreamGrabberDeregisterBuffer(hGrabber, pair.Key);
                    pair.Value.Dispose();
                }
                buffers = null;

                /* ... Release grabbing related resources. */
                Pylon.StreamGrabberFinishGrab(hGrabber);

                /* 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);

                /* ... 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);
                Pylon.DestroyDevice(hDev);

                Console.Error.WriteLine("\nPress enter to exit.");
                Console.ReadLine();

                /* ... Shut down the pylon runtime system. Don't call any pylon method after
                 * calling Pylon.Terminate(). */
                Pylon.Terminate();
#endif
            }
            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);
            }
        }
Beispiel #10
0
        const uint NUM_EVENT_BUFFERS = 20;   /* Number of buffers used for grabbing. */

        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_STREAMGRABBER_HANDLE hStreamGrabber;                           /* Handle for the pylon stream grabber. */
                PYLON_EVENTGRABBER_HANDLE  hEventGrabber;                            /* Handle for the event grabber used for receiving events. */
                PYLON_EVENTADAPTER_HANDLE  hEventAdapter;                            /* Handle for the event adapter used for dispatching events. */
                PYLON_WAITOBJECT_HANDLE    hWaitStream;                              /* Handle used for waiting for a grab to be finished. */
                PYLON_WAITOBJECT_HANDLE    hWaitEvent;                               /* Handle used for waiting for an event message. */
                PYLON_WAITOBJECTS_HANDLE   hWaitObjects;                             /* Container allowing waiting for multiple wait objects. */
                NODEMAP_HANDLE             hNodeMap;                                 /* Handle for the node map containing the
                                                                                      *  camera parameters. */
                NODE_CALLBACK_HANDLE hCallback;                                      /* Used for deregistering a callback function. */
                NODE_HANDLE          hNode;                                          /* Handle for a camera parameter. */
                uint payloadSize;                                                    /* Size of an image frame in bytes. */
                Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > buffers; /* Holds handles and buffers used for grabbing. */
                PylonGrabResult_t   grabResult;                                      /* Stores the result of a grab operation. */
                NodeCallbackHandler callbackHandler = new NodeCallbackHandler();     /* Handles incoming callbacks. */
                int  nGrabs;                                                         /* Counts the number of buffers grabbed. */
                uint nStreams;                                                       /* The number of streams the device provides. */
                bool isAvail;                                                        /* Used for checking feature availability. */
                bool isReady;                                                        /* Used as an output parameter. */
                int  i;                                                              /* Counter. */
                PylonEventResult_t eventMsg = new PylonEventResult_t();              /* Event data container. */
                long sfncVersionMajor;                                               /* The major number of the Standard Feature Naming Convention (SFNC)
                                                                                      * version used by the camera device. */

#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. */
                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 | Pylon.cPylonAccessModeEvent);

                /* 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);
                    }
                }

                /* 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, "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, "PixelFormat", "Mono8");

                /* Disable acquisition start trigger if available. */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* Disable frame burst start trigger if available */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* Disable frame start trigger if available. */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* We will use the Continuous frame mode, i.e., the camera delivers
                 * images continuously. */
                Pylon.DeviceFeatureFromString(hDev, "AcquisitionMode", "Continuous");

                /* For GigE cameras, we recommend increasing the packet size for better
                 * performance. If 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. */
                /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
                isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");

                if (isAvail)
                {
                    /* ... The device supports the packet size feature. Set a value. */
                    Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
                }

                /* Determine the required size of the grab buffer. */
                payloadSize = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev, "PayloadSize"));

                /* Determine the major number of the SFNC version used by the camera device. */
                if (Pylon.DeviceFeatureIsAvailable(hDev, "DeviceSFNCVersionMajor"))
                {
                    sfncVersionMajor = Pylon.DeviceGetIntegerFeature(hDev, "DeviceSFNCVersionMajor");
                }
                else
                {
                    /* No SFNC version information is provided by the camera device. */
                    sfncVersionMajor = 0;
                }

                /* Enable camera events. */
                /* Select the end-of-exposure event.*/
                Pylon.DeviceFeatureFromString(hDev, "EventSelector", "ExposureEnd");
                /* Enable the event. Select the enumeration entry name depending on the SFNC version used by the camera device. */
                if (sfncVersionMajor >= 2)
                {
                    Pylon.DeviceFeatureFromString(hDev, "EventNotification", "On");
                }
                else
                {
                    Pylon.DeviceFeatureFromString(hDev, "EventNotification", "GenICamEvent");
                }

                /* 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 = Pylon.DeviceGetNumStreamGrabberChannels(hDev);
                if (nStreams < 1)
                {
                    throw new Exception("The transport layer doesn't support image streams");
                }

                /* Create and open a stream grabber for the first channel. */
                hStreamGrabber = Pylon.DeviceGetStreamGrabber(hDev, 0);
                Pylon.StreamGrabberOpen(hStreamGrabber);

                /* Get a handle for the stream grabber's wait object. The wait object
                 * allows waiting for buffers to be grabbed. */
                hWaitStream = Pylon.StreamGrabberGetWaitObject(hStreamGrabber);

                /* 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(hStreamGrabber, NUM_IMAGE_BUFFERS);

                /* .. We will not use buffers bigger than payloadSize bytes. */
                Pylon.StreamGrabberSetMaxBufferSize(hStreamGrabber, payloadSize);

                /*  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(hStreamGrabber);

                /* 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 object pointers. The buffer objects are held in a dictionary,
                 * that provides access to the buffer using a handle as key.
                 */
                buffers = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >();
                for (i = 0; i < NUM_IMAGE_BUFFERS; ++i)
                {
                    PylonBuffer <Byte>        buffer = new PylonBuffer <byte>(payloadSize, true);
                    PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hStreamGrabber, ref buffer);
                    buffers.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)
                {
                    Pylon.StreamGrabberQueueBuffer(hStreamGrabber, pair.Key, i++);
                }

                /* The stream grabber is now prepared. As soon the camera starts to acquire images,
                 * the image data will be grabbed into the provided buffers.  */


                /* Create and prepare an event grabber. */
                /* ... Get a handle for the event grabber. */
                hEventGrabber = Pylon.DeviceGetEventGrabber(hDev);

                if (!hEventGrabber.IsValid)
                {
                    /* The transport layer doesn't support event grabbers. */
                    throw new Exception("No event grabber supported.");
                }

                /* ... Tell the grabber how many buffers to use. */
                Pylon.EventGrabberSetNumBuffers(hEventGrabber, NUM_EVENT_BUFFERS);

                /* ... Open the event grabber. */
                Pylon.EventGrabberOpen(hEventGrabber);  /* The event grabber is now ready
                                                         * for receiving events. */

                /* Retrieve the wait object that is associated with the event grabber. The event
                 * will be signaled when an event message has been received. */
                hWaitEvent = Pylon.EventGrabberGetWaitObject(hEventGrabber);

                /* For extracting the event data from an event message, an event adapter is used. */
                hEventAdapter = Pylon.DeviceCreateEventAdapter(hDev);

                if (!hEventAdapter.IsValid)
                {
                    /* The transport layer doesn't support event grabbers. */
                    throw new Exception("No event adapter supported.");
                }

                /* Register the callback function for the ExposureEndEventFrameID parameter. */
                /*.Get the node map containing all parameters. */
                hNodeMap = Pylon.DeviceGetNodeMap(hDev);

                /* Get the ExposureEndEventFrameID parameter.
                 * Select the parameter name depending on the SFNC version used by the camera device.
                 */
                if (sfncVersionMajor >= 2)
                {
                    hNode = GenApi.NodeMapGetNode(hNodeMap, "EventExposureEndFrameID");
                }
                else
                {
                    hNode = GenApi.NodeMapGetNode(hNodeMap, "ExposureEndEventFrameID");
                }

                if (!hNode.IsValid)
                {
                    /* There is no ExposureEndEventFrameID parameter. */
                    throw new Exception("There is no ExposureEndEventFrameID or EventExposureEndFrameID parameter!");
                }
                /* ... Register the callback function. */
                callbackHandler.CallbackEvent += new NodeCallbackHandler.NodeCallback(endOfExposureCallback);
                hCallback = GenApi.NodeRegisterCallback(hNode, callbackHandler);

                /* Put the wait objects into a container. */
                /* ... Create the container. */
                hWaitObjects = Pylon.WaitObjectsCreate();

                /* ... Add the wait objects' handles. */
                Pylon.WaitObjectsAdd(hWaitObjects, hWaitEvent);
                Pylon.WaitObjectsAdd(hWaitObjects, hWaitStream);

                /* Let the camera acquire images. */
                Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStart");

                /* Grab NUM_GRABS images. */
                nGrabs = 0;                         /* Counts the number of images grabbed. */
                while (nGrabs < NUM_GRABS)
                {
                    int  bufferIndex;             /* Index of the buffer. */
                    uint waitObjectIndex;         /* Index of the wait object that is signalled.*/
                    Byte min, max;

                    /* Wait for either an image buffer grabbed or an event received. Wait up to 1000 ms. */
                    isReady = Pylon.WaitObjectsWaitForAny(hWaitObjects, 1000, out waitObjectIndex);

                    if (!isReady)
                    {
                        /* Timeout occurred. */
                        throw new Exception("Timeout. Neither grabbed an image nor received an event.");
                    }

                    if (0 == waitObjectIndex)
                    {
                        /* hWaitEvent has been signalled. At least one event message is available. Retrieve it. */
                        isReady = Pylon.EventGrabberRetrieveEvent(hEventGrabber, ref eventMsg);

                        if (!isReady)
                        {
                            /* Oops. No event message available? We should never have reached this point.
                             * Since the wait operation above returned without a timeout, an event message
                             * should be available. */
                            throw new Exception("Failed to retrieve an event.");
                        }
                        /* Check to see if the event was successfully received. */
                        if (0 == eventMsg.ErrorCode)
                        {
                            /* Successfully received an event message. */

                            /* Pass the event message to the event adapter. The event adapter will
                             * update the parameters related to events and will fire the callbacks
                             * registered to event related parameters. */
                            Pylon.EventAdapterDeliverMessage(hEventAdapter, eventMsg);
                        }
                        else
                        {
                            Console.Error.WriteLine("Error when receiving an event: {1}", eventMsg.ErrorCode);
                        }
                    }
                    else if (1 == waitObjectIndex)
                    {
                        /* hWaitStream  has been signalled. The result of at least one grab
                         * operation is available. Retrieve it. */
                        isReady = Pylon.StreamGrabberRetrieveResult(hStreamGrabber, 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.");
                        }

                        nGrabs++;

                        /* Get the buffer index from the context information. */
                        bufferIndex = (int)grabResult.Context;

                        /* 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 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.TryGetValue(grabResult.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.");
                            }

                            getMinMax(buffer.Array, out min, out max);
                            Console.WriteLine("Grabbed frame {0} into buffer {1}. Min. gray value = {2}, Max. gray value = {3}",
                                              nGrabs, bufferIndex, min, max);
                        }
                        else if (grabResult.Status == EPylonGrabStatus.Failed)
                        {
                            Console.Error.WriteLine("Frame {0} wasn't grabbed successfully.  Error code = {1}",
                                                    nGrabs, grabResult.ErrorCode);
                        }

                        /* Once finished with the processing, requeue the buffer to be filled again. */
                        Pylon.StreamGrabberQueueBuffer(hStreamGrabber, grabResult.hBuffer, bufferIndex);
                    }
                }

                /* Clean up. */

                /*  ... Stop the camera. */
                Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStop");

                /* ... Switch off the events. */
                Pylon.DeviceFeatureFromString(hDev, "EventSelector", "ExposureEnd");
                Pylon.DeviceFeatureFromString(hDev, "EventNotification", "Off");


                /* ... We must issue a cancel call to ensure that all pending buffers are put into the
                 * stream grabber's output queue. */
                Pylon.StreamGrabberCancelGrab(hStreamGrabber);

                /* ... The buffers can now be retrieved from the stream grabber. */
                do
                {
                    isReady = Pylon.StreamGrabberRetrieveResult(hStreamGrabber, out grabResult);
                } 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)
                {
                    Pylon.StreamGrabberDeregisterBuffer(hStreamGrabber, pair.Key);
                    pair.Value.Dispose();
                }
                buffers = null;


                /* ... Release grabbing related resources. */
                Pylon.StreamGrabberFinishGrab(hStreamGrabber);

                /* 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(hStreamGrabber);

                /* ... Deregister the callback. */
                GenApi.NodeDeregisterCallback(hNode, hCallback);

                /* ... Close the event grabber.*/
                Pylon.EventGrabberClose(hEventGrabber);

                /* ... Release the event adapter. */
                Pylon.DeviceDestroyEventAdapter(hDev, hEventAdapter);

                /* ... Release the wait object container. */
                Pylon.WaitObjectsDestroy(hWaitObjects);

                /* ... 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);
                Pylon.DestroyDevice(hDev);

                /* ... Shut down the pylon runtime system. Don't call any pylon method after
                 * calling PylonTerminate(). */
                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)
                    {
                        /*  Disable the software trigger. */
                        Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                        /* ... 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);
            }
        }
        //打开相机
        public override void OpenCameraSoftTrigger(string DCF_Name)
        {
            bool isAvail;

            Pylon.Initialize();

            /* Enumerate all camera devices. You must call PylonEnumerateDevices() before creating a device. */
            numDevices = Pylon.EnumerateDevices();

            if (0 == numDevices)
            {
                MessageBox.Show("没有找到相机");
                return;
            }
            else
            {
                /* 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 and for grabbing images. */
            Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);

            /* 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, "EnumEntry_PixelFormat_Mono8");

            if (!isAvail)
            {
                /* Feature is not available. */
                MessageBox.Show("设备不支持8位灰度图像");
            }

            /* ... Set the pixel format to Mono8. */
            Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");

            /* Disable acquisition start trigger if available. */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
            }


            /* Disable frame burst start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
            }

            /* Disable frame start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
            }

            /* For GigE cameras, we recommend increasing the packet size for better
             * performance. If 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. */
            /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
            isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");

            if (isAvail)
            {
                /* ... The device supports the packet size feature. Set a value. */
                Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
            }
        }
Beispiel #12
0
        /* There are camera features that behave like enumerations. These features can take a value from a fixed
         * set of possible values. One example is the pixel format feature. This function illustrates how to deal with
         * enumeration features.
         *
         */
        private static void demonstrateEnumFeature(PYLON_DEVICE_HANDLE hDev)
        {
            string value;                     /* The current value of the feature. */
            bool   isWritable,
                   supportsMono8,
                   supportsYUV422Packed,
                   supportsMono16;


            /* The allowed values for an enumeration feature are represented as strings. Use the
             * PylonDeviceFeatureFromString() and PylonDeviceFeatureToString() methods for setting and getting
             * the value of an enumeration feature. */

            /* Get the current value of the enumeration feature. */
            value = Pylon.DeviceFeatureToString(hDev, "PixelFormat");

            Console.WriteLine("PixelFormat: {0}", value);

            /*
             * For an enumeration feature, the pylon Viewer's "Feature Documentation" window lists the
             * names of the possible values. Some of the values might not be supported by the device.
             * To check if a certain "SomeValue" value for a "SomeFeature" feature can be set, call the
             * PylonDeviceFeatureIsAvailable() function with "EnumEntry_SomeFeature_SomeValue" as an argument.
             */
            /* Check to see if the Mono8 pixel format can be set. */
            supportsMono8 = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8");
            Console.WriteLine("Mono8 {0} a supported value for the PixelFormat feature.", supportsMono8 ? "is" : "isn't");

            /* Check to see if the YUV422Packed pixel format can be set. */
            supportsYUV422Packed = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_YUV422Packed");
            Console.WriteLine("YUV422Packed {0} a supported value for the PixelFormat feature.", supportsYUV422Packed ? "is" : "isn't");

            /* Check to see if the Mono16 pixel format can be set. */
            supportsMono16 = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono16");
            Console.WriteLine("Mono16 {0} a supported value for the PixelFormat feature.", supportsMono16 ? "is" : "isn't");

            /* Before writing a value, we recommend checking to see if the enumeration feature itself is
             * currently writable. */
            isWritable = Pylon.DeviceFeatureIsWritable(hDev, "PixelFormat");
            if (isWritable)
            {
                /* The PixelFormat feature is writable. Set it to one of the supported values. */
                if (supportsMono16)
                {
                    Console.WriteLine("Setting PixelFormat to Mono16.");
                    Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono16");
                }
                else if (supportsYUV422Packed)
                {
                    Console.WriteLine("Setting PixelFormat to YUV422Packed.");
                    Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "YUV422Packed");
                }
                else if (supportsMono8)
                {
                    Console.WriteLine("Setting PixelFormat to Mono8.");
                    Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");
                }

                /* Reset the PixelFormat feature to its previous value. */
                Pylon.DeviceFeatureFromString(hDev, "PixelFormat", value);
            }
        }
Beispiel #13
0
        const uint NUM_BUFFERS = 2;         /* Number of buffers used for grabbing. */

        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_STREAMGRABBER_HANDLE hGrabber;                                 /* Handle for the pylon stream grabber. */
                PYLON_CHUNKPARSER_HANDLE   hChunkParser;                             /* Handle for the parser extracting the chunk data. */
                PYLON_WAITOBJECT_HANDLE    hWait;                                    /* Handle used for waiting for a grab to be finished. */
                uint payloadSize;                                                    /* Size of an image frame in bytes. */
                Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > buffers; /* Holds handles and buffers used for grabbing. */
                PylonGrabResult_t grabResult;                                        /* Stores the result of a grab operation. */
                int    nGrabs;                                                       /* Counts the number of buffers grabbed. */
                uint   nStreams;                                                     /* The number of streams the device provides. */
                bool   isAvail;                                                      /* Used for checking feature availability */
                bool   isReady;                                                      /* Used as an output parameter */
                int    i;                                                            /* Counter. */
                string triggerSelectorValue = "FrameStart";                          /* Preselect the trigger for image acquisition */
                bool   isAvailFrameStart;                                            /* Used for checking feature availability */
                bool   isAvailAcquisitionStart;                                      /* Used for checking feature availability */

#if DEBUG
                /* This is a special debug setting needed only for GigE cameras.
                 * See 'Building Applications with pylon' in the programmers 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. */
                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 and for grabbing images. */
                Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);

                /* 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, "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, "PixelFormat", "Mono8");

                /* Check the available camera trigger mode(s) to select the appropriate one: acquisition start trigger mode (used by previous cameras;
                *  do not confuse with acquisition start command) or frame start trigger mode (equivalent to previous acquisition start trigger mode). */
                isAvailAcquisitionStart = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
                isAvailFrameStart       = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");

                /* Check to see if the camera implements the acquisition start trigger mode only. */
                if (isAvailAcquisitionStart && !isAvailFrameStart)
                {
                    /* Camera uses the acquisition start trigger as the only trigger mode. */
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "On");
                    triggerSelectorValue = "AcquisitionStart";
                }
                else
                {
                    /* Camera may have the acquisition start trigger mode and the frame start trigger mode implemented.
                     * In this case, the acquisition trigger mode must be switched off. */
                    if (isAvailAcquisitionStart)
                    {
                        Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                        Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                    }

                    /* Disable frame burst start trigger if available */
                    isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
                    if (isAvail)
                    {
                        Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                        Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                    }

                    /* To trigger each single frame by software or external hardware trigger: Enable the frame start trigger mode. */
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "On");
                }

                /* Note: the trigger selector must be set to the appropriate trigger mode
                 * before setting the trigger source or issuing software triggers.
                 * Frame start trigger mode for newer cameras, acquisition start trigger mode for previous cameras. */
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", triggerSelectorValue);

                /* Enable software triggering. */
                /* ... Select the software trigger as the trigger source. */
                Pylon.DeviceFeatureFromString(hDev, "TriggerSource", "Software");

                /* When using software triggering, the Continuous frame mode should be used. Once
                 * acquisition is started, the camera sends one image each time a software trigger is
                 * issued. */
                Pylon.DeviceFeatureFromString(hDev, "AcquisitionMode", "Continuous");

                /* For GigE cameras, we recommend increasing the packet size for better
                 * performance. If 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. */
                /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
                isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");

                if (isAvail)
                {
                    /* ... The device supports the packet size feature. Set a value. */
                    Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
                }

                /* Before enabling individual chunks, the chunk mode in general must be activated. */
                isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ChunkModeActive");

                if (!isAvail)
                {
                    throw new Exception("The device doesn't support the chunk mode.");
                }

                /* Activate the chunk mode. */
                Pylon.DeviceSetBooleanFeature(hDev, "ChunkModeActive", true);

                /* Enable some individual chunks... */

                /* ... The frame counter chunk feature. */
                /* Is the chunk feature available? */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_ChunkSelector_Framecounter");

                if (isAvail)
                {
                    /* Select the frame counter chunk feature. */
                    Pylon.DeviceFeatureFromString(hDev, "ChunkSelector", "Framecounter");

                    /* Can the chunk feature be activated? */
                    isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ChunkEnable");

                    if (isAvail)
                    {
                        /* Activate the chunk feature. */
                        Pylon.DeviceSetBooleanFeature(hDev, "ChunkEnable", true);
                    }
                }
                /* ... The CRC checksum chunk feature. */

                /*  Note: Enabling the CRC checksum chunk feature is not a prerequisite for using
                 * chunks. Chunks can also be handled when the CRC checksum chunk feature is disabled. */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_ChunkSelector_PayloadCRC16");

                if (isAvail)
                {
                    /* Select the CRC checksum chunk feature. */
                    Pylon.DeviceFeatureFromString(hDev, "ChunkSelector", "PayloadCRC16");

                    /* Can the chunk feature be activated? */
                    isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ChunkEnable");

                    if (isAvail)
                    {
                        /* Activate the chunk feature. */
                        Pylon.DeviceSetBooleanFeature(hDev, "ChunkEnable", true);
                    }
                }

                /* The data block containing the image chunk and the other chunks has a self-descriptive layout.
                 * A chunk parser is used to extract the appended chunk data from the grabbed image frame.
                 * Create a chunk parser. */
                hChunkParser = Pylon.DeviceCreateChunkParser(hDev);

                if (!hChunkParser.IsValid)
                {
                    /* The transport layer doesn't provide a chunk parser. */
                    throw new Exception("No chunk parser available.");
                }


                /* 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 = Pylon.DeviceGetNumStreamGrabberChannels(hDev);

                if (nStreams < 1)
                {
                    throw new Exception("The transport layer doesn't support image streams.");
                }

                /* Create and open a stream grabber for the first channel. */
                hGrabber = Pylon.DeviceGetStreamGrabber(hDev, 0);
                Pylon.StreamGrabberOpen(hGrabber);

                /* Get a handle for the stream grabber's wait object. The wait object
                 * allows waiting for buffers to be filled with grabbed data. */
                hWait = Pylon.StreamGrabberGetWaitObject(hGrabber);

                /* Determine the required size of the grab buffer. Since activating chunks will increase the
                 * payload size and thus the required buffer size, do this after enabling the chunks. */
                payloadSize = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev, "PayloadSize"));

                /* 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, NUM_BUFFERS);

                /* .. We will not use buffers bigger than payloadSize bytes. */
                Pylon.StreamGrabberSetMaxBufferSize(hGrabber, payloadSize);

                /*  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);

                /* 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 = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >();
                for (i = 0; i < NUM_BUFFERS; ++i)
                {
                    PylonBuffer <Byte>        buffer = new PylonBuffer <byte>(payloadSize, true);
                    PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber, ref buffer);
                    buffers.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)
                {
                    Pylon.StreamGrabberQueueBuffer(hGrabber, pair.Key, i++);
                }

                /* Issue an acquisition start command. Because the trigger mode is enabled, issuing the start command
                 * itself will not trigger any image acquisitions. Issuing the start command simply prepares the camera.
                 * Once the camera is prepared it will acquire one image for every trigger it receives. */
                Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStart");

                /* Trigger the first image. */
                Pylon.DeviceExecuteCommandFeature(hDev, "TriggerSoftware");

                /* Grab NUM_GRABS images. */
                nGrabs = 0;                           /* Counts the number of images grabbed. */
                while (nGrabs < NUM_GRABS)
                {
                    int  bufferIndex;     /* Index of the buffer. */
                    Byte min = 255, max = 0;
                    long chunkWidth  = 0; /* data retrieved from the chunk parser */
                    long chunkHeight = 0; /* data retrieved from the chunk parser */

                    /* Wait for the next buffer to be filled. Wait up to 1000 ms. */
                    isReady = Pylon.WaitObjectWait(hWait, 1000);

                    if (!isReady)
                    {
                        /* Timeout occurred. */
                        throw new Exception("Grab timeout occurred.\n");
                    }

                    /* Since the wait operation was successful, the result of at least one grab
                     * operation is available. Retrieve it. */
                    isReady = Pylon.StreamGrabberRetrieveResult(hGrabber, 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.\n");
                    }

                    nGrabs++;

                    /* Trigger the next image. Since we passed more than one buffer to the stream grabber,
                     * the triggered image will be grabbed while the image processing is performed.  */
                    Pylon.DeviceExecuteCommandFeature(hDev, "TriggerSoftware");

                    /* Get the buffer index from the context information. */
                    bufferIndex = (int)grabResult.Context;

                    /* Check to see if the image was grabbed successfully. */
                    if (grabResult.Status == EPylonGrabStatus.Grabbed)
                    {
                        /*  The grab is successful.  */

                        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.TryGetValue(grabResult.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.");
                        }

                        Console.WriteLine("Grabbed frame {0} into buffer {1}.", nGrabs, bufferIndex);

                        /* Check to see if we really got image data plus chunk data. */
                        if (grabResult.PayloadType != EPylonPayloadType.PayloadType_ChunkData)
                        {
                            Console.WriteLine("Received a buffer not containing chunk data?");
                        }
                        else
                        {
                            /* Process the chunk data. This is done by passing the grabbed image buffer
                             * to the chunk parser. When the chunk parser has processed the buffer, the chunk
                             * data can be accessed in the same manner as "normal" camera parameters.
                             * The only exception is the CRC checksum feature. There are dedicated functions for
                             * checking the CRC checksum. */

                            bool hasCRC;

                            /* Let the parser extract the data. */
                            Pylon.ChunkParserAttachBuffer(hChunkParser, buffer);


                            /* Check the CRC checksum. */
                            hasCRC = Pylon.ChunkParserHasCRC(hChunkParser);

                            if (hasCRC)
                            {
                                bool isOk = Pylon.ChunkParserCheckCRC(hChunkParser);

                                Console.WriteLine("Frame {0} contains a CRC checksum. The checksum {1} ok.", nGrabs, isOk ? "is" : "is not");
                            }

                            /* Retrieve the frame counter value. */
                            /* ... Check the availability. */
                            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "ChunkFramecounter");

                            Console.WriteLine("Frame {0} {1} a frame counter chunk.", nGrabs, isAvail ? "contains" : "doesn't contain");
                            if (isAvail)
                            {
                                /* ... Get the value. */
                                long counter;
                                counter = Pylon.DeviceGetIntegerFeature(hDev, "ChunkFramecounter");

                                Console.WriteLine("Frame counter of frame {0}: {1}.", nGrabs, counter);
                            }

                            /* Retrieve the chunk width value. */
                            /* ... Check the availability. */
                            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "ChunkWidth");

                            Console.WriteLine("Frame {0} {1} a frame width chunk.", nGrabs, isAvail ? "contains" : "doesn't contain");
                            if (isAvail)
                            {
                                /* ... Get the value. */
                                chunkWidth = Pylon.DeviceGetIntegerFeature(hDev, "ChunkWidth");

                                Console.WriteLine("Width of frame {0}: {1}.", nGrabs, chunkWidth);
                            }

                            /* Retrieve the chunk height value. */
                            /* ... Check the availability. */
                            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "ChunkHeight");

                            Console.WriteLine("Frame {0} {1} a frame height chunk.", nGrabs, isAvail ? "contains" : "doesn't contain");
                            if (isAvail)
                            {
                                /* ... Get the value. */
                                chunkHeight = Pylon.DeviceGetIntegerFeature(hDev, "ChunkHeight");

                                Console.WriteLine("Height of frame {0}: {1}.", nGrabs, chunkHeight);
                            }
                        }

                        /* Perform the image processing. */
                        getMinMax(buffer.Array, chunkWidth, chunkHeight, out min, out max);
                        Console.WriteLine("Min. gray value  = {0}, Max. gray value = {1}", min, max);

                        /* Before requeueing the buffer, you should detach it from the chunk parser. */
                        Pylon.ChunkParserDetachBuffer(hChunkParser);  /* Now the chunk data in the buffer is no longer accessible. */
                    }
                    else if (grabResult.Status == EPylonGrabStatus.Failed)
                    {
                        Console.Error.WriteLine("Frame {0} wasn't grabbed successfully.  Error code = {1}", nGrabs, grabResult.ErrorCode);
                    }

                    /* Once finished with the processing, requeue the buffer to be filled again. */
                    Pylon.StreamGrabberQueueBuffer(hGrabber, grabResult.hBuffer, bufferIndex);
                }

                /* Clean up. */

                /*  ... Stop the camera. */
                Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStop");

                /* ... We must issue a cancel call to ensure that all pending buffers are put into the
                 * stream grabber's output queue. */
                Pylon.StreamGrabberCancelGrab(hGrabber);

                /* ... The buffers can now be retrieved from the stream grabber. */
                do
                {
                    isReady = Pylon.StreamGrabberRetrieveResult(hGrabber, out grabResult);
                } 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)
                {
                    Pylon.StreamGrabberDeregisterBuffer(hGrabber, pair.Key);
                    pair.Value.Dispose();
                }
                buffers = null;

                /* ... Release grabbing related resources. */
                Pylon.StreamGrabberFinishGrab(hGrabber);


                /* 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);


                /* ... Release the chunk parser. */
                Pylon.DeviceDestroyChunkParser(hDev, hChunkParser);


                /*  Disable the software trigger and chunk mode. */
                if (hDev.IsValid)
                {
                    Pylon.DeviceSetBooleanFeature(hDev, "ChunkModeActive", false);
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* ... 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);
                Pylon.DestroyDevice(hDev);

                /* ... Shut down the pylon runtime system. Don't call any pylon method after
                 * calling PylonTerminate(). */
                Pylon.Terminate();

                Console.Error.WriteLine("\nPress enter to exit.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                /* Retrieve more details about the error.
                 * /* 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)
                    {
                        /*  Disable the software trigger. */
                        Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                        /* ... 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);
            }
        }
Beispiel #14
0
        private void initStreamMode()
        {
            _width  = (int)Pylon.DeviceGetIntegerFeature(hDev, "Width");
            _height = (int)Pylon.DeviceGetIntegerFeature(hDev, "Height");
            /* 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);
            }

            /* 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, "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. */
            bool isWritable = Pylon.DeviceFeatureIsWritable(hDev, "PixelFormat");

            if (isWritable)
            {
                Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");
            }

            /* Disable acquisition start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
            if (isAvail)
            {
                isWritable = Pylon.DeviceFeatureIsWritable(hDev, "TriggerSelector");
                if (isWritable)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                }

                isWritable = Pylon.DeviceFeatureIsWritable(hDev, "TriggerMode");
                if (isWritable)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }
            }

            /* Disable frame start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
            if (isAvail)
            {
                isWritable = Pylon.DeviceFeatureIsWritable(hDev, "TriggerSelector");
                if (isWritable)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                }

                isWritable = Pylon.DeviceFeatureIsWritable(hDev, "TriggerMode");
                if (isWritable)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }
            }

            /* We will use the Continuous frame mode, i.e., the camera delivers
             * images continuously. */
            isWritable = Pylon.DeviceFeatureIsWritable(hDev, "AcquisitionMode");
            if (isWritable)
            {
                Pylon.DeviceFeatureFromString(hDev, "AcquisitionMode", "Continuous");
            }

            /* 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. */
            /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
            isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
            if (isAvail)
            {
                /* ... The device supports the packet size feature. Set a value. */
                isWritable = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
                if (isWritable)
                {
                    Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", GevSCPSPacketSize);
                }
            }
            _isModelValid = isAvail;
            /* Determine the required size of the grab buffer. */
            payloadSize = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev, "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 = Pylon.DeviceGetNumStreamGrabberChannels(hDev);

            if (nStreams < 1)
            {
                throw new Exception("The transport layer doesn't support image streams.");
            }

            /* Create and open a stream grabber for the first channel. */
            hGrabber = Pylon.DeviceGetStreamGrabber(hDev, 0);
            Pylon.StreamGrabberOpen(hGrabber);

            /* Get a handle for the stream grabber's wait object. The wait object
             * allows waiting for buffers to be filled with grabbed data. */
            hWait = Pylon.StreamGrabberGetWaitObject(hGrabber);

            /* 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, NUM_BUFFERS);

            /* .. We will not use buffers bigger than payloadSize bytes. */
            Pylon.StreamGrabberSetMaxBufferSize(hGrabber, payloadSize);

            /*  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);

            /* 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 = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >();
            int i;

            for (i = 0; i < NUM_BUFFERS; ++i)
            {
                PylonBuffer <Byte>        buffer = new PylonBuffer <byte>(payloadSize, true);
                PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber, ref buffer);
                buffers.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)
            {
                Pylon.StreamGrabberQueueBuffer(hGrabber, pair.Key, i++);
            }
        }
Beispiel #15
0
        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. */
                const int          numGrabs = 10;   /* Number of images to grab. */
                PylonBuffer <Byte> imgBuf   = null; /* Buffer used for grabbing. */
                bool               isAvail;
                int i;
#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. */
                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 and for grabbing images. */
                Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);

                /* 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, "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, "PixelFormat", "Mono8");

                /* Disable acquisition start trigger if available. */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* Disable frame burst start trigger if available */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* Disable frame start trigger if available */
                isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
                if (isAvail)
                {
                    Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                    Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
                }

                /* For GigE cameras, we recommend increasing the packet size for better
                 * performance. If 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. */
                /* ... Check first to see if the GigE camera packet size parameter is supported
                 *  and if it is writable. */
                isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");

                if (isAvail)
                {
                    /* ... The device supports the packet size feature. Set a value. */
                    Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
                }

                /* Grab some images in a loop. */
                for (i = 0; i < numGrabs; ++i)
                {
                    Byte min, max;
                    PylonGrabResult_t grabResult;

                    /* Grab one single frame from stream channel 0. The
                     * camera is set to "single frame" acquisition mode.
                     * Wait up to 500 ms for the image to be grabbed.
                     * If imgBuf is null a buffer is automatically created with the right size.*/
                    if (!Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500))
                    {
                        /* Timeout occurred. */
                        Console.WriteLine("Frame {0}: timeout.", i + 1);
                    }

                    /* 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);

                        /* Display image */
                        Pylon.ImageWindowDisplayImage <Byte>(0, imgBuf, grabResult);
                    }
                    else if (grabResult.Status == EPylonGrabStatus.Failed)
                    {
                        Console.Error.WriteLine("Frame {0} wasn't grabbed successfully.  Error code = {1}", i + 1, grabResult.ErrorCode);
                    }
                }
                /* Release the buffer. */
                imgBuf.Dispose();

                /* Clean up. Close and release the pylon device. */
                Pylon.DeviceClose(hDev);
                Pylon.DestroyDevice(hDev);

                /* Free memory for grabbing. */
                imgBuf = null;

                Console.Error.WriteLine("\nPress enter to exit.");
                Console.ReadLine();

                /* Shut down the pylon runtime system. Don't call any pylon method after
                 * calling Pylon.Terminate(). */
                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);
                }

                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);
            }
        }
Beispiel #16
0
        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);
            }
        }
Beispiel #17
0
    public void cameraInit(string cameraType)
    {
        //Initiates camera to run in 12 bit mode with continuous acquisition.
        int  i           = 0;
        uint NUM_BUFFERS = 10;

        if (cameraType == "basler")
        {
            Pylon.Initialize();
            numDevices = Pylon.EnumerateDevices();
            if (ConfigurationManager.AppSettings.Get("ipAddress").Length > 0)
            {
                while (ip != ConfigurationManager.AppSettings.Get("ipAddress"))
                {
                    hDev = Pylon.CreateDeviceByIndex(j);
                    prop = Pylon.DeviceGetDeviceInfoHandle(hDev);
                    ip   = Pylon.DeviceInfoGetPropertyValueByIndex(prop, 8);
                    j++;
                    if (j > numDevices)
                    {
                        break;
                    }
                }
            }
            else
            {
                numDevices = Pylon.EnumerateDevices();
                hDev       = Pylon.CreateDeviceByIndex(0);
            }
            try
            {
                Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);
            }
            catch (System.Threading.ThreadAbortException) { }
            catch (Exception ex)
            {
                EmailError.emailAlert(ex);
                throw (ex);
            }

            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono12");
            Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono12");
            Pylon.DeviceFeatureFromString(hDev, "AcquisitionMode", "Continuous");
            Pylon.DeviceSetIntegerFeature(hDev, "Height", 1);
            Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeAbs", exp); //Exposure time is in microseconds and rounded to the closest 100 ns.
            isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");
            if (isAvail)
            {
                Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
            }
            payloadSize = checked ((uint)Pylon.DeviceGetIntegerFeature(hDev, "PayloadSize"));
            nStreams    = Pylon.DeviceGetNumStreamGrabberChannels(hDev);
            hGrabber    = Pylon.DeviceGetStreamGrabber(hDev, 0);
            Pylon.StreamGrabberOpen(hGrabber);
            hWait = Pylon.StreamGrabberGetWaitObject(hGrabber);
            Pylon.StreamGrabberSetMaxNumBuffer(hGrabber, NUM_BUFFERS);
            Pylon.StreamGrabberSetMaxBufferSize(hGrabber, payloadSize);
            Pylon.StreamGrabberPrepareGrab(hGrabber);
            buffers = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >();
            for (i = 0; i < NUM_BUFFERS; ++i)
            {
                PylonBuffer <Byte>        buffer = new PylonBuffer <byte>(payloadSize, true);
                PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber, ref buffer);
                buffers.Add(handle, buffer);
            }
            i = 0;
            foreach (KeyValuePair <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > pair in buffers)
            {
                Pylon.StreamGrabberQueueBuffer(hGrabber, pair.Key, i++);
            }
            Pylon.DeviceExecuteCommandFeature(hDev, "AcquisitionStart");
        }
    }
Beispiel #18
0
        /* Open using device.*/
        public void Open(PYLON_DEVICE_HANDLE device)
        {
            try
            {
                /* Use provided device. */
                m_hDevice = device;

                /* Before using the device, it must be opened. Open it for configuring
                 * parameters and for grabbing images. */
                Pylon.DeviceOpen(m_hDevice, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);

                /* Register the callback function. */
                m_hRemovalCallback = Pylon.DeviceRegisterRemovalCallback(m_hDevice, m_callbackHandler);

                /* 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. */
                /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
                if (Pylon.DeviceFeatureIsWritable(m_hDevice, "GevSCPSPacketSize"))
                {
                    /* ... The device supports the packet size feature. Set a value. */
                    Pylon.DeviceSetIntegerFeature(m_hDevice, "GevSCPSPacketSize", 1500);
                }

                /* The sample does not work in chunk mode. It must be disabled. */
                if (Pylon.DeviceFeatureIsWritable(m_hDevice, "ChunkModeActive"))
                {
                    /* Disable the chunk mode. */
                    Pylon.DeviceSetBooleanFeature(m_hDevice, "ChunkModeActive", false);
                }

                /* Disable acquisition start trigger if available. */
                if (Pylon.DeviceFeatureIsAvailable(m_hDevice, "EnumEntry_TriggerSelector_AcquisitionStart"))
                {
                    Pylon.DeviceFeatureFromString(m_hDevice, "TriggerSelector", "AcquisitionStart");
                    Pylon.DeviceFeatureFromString(m_hDevice, "TriggerMode", "Off");
                }

                /* Disable frame burst start trigger if available */
                if (Pylon.DeviceFeatureIsAvailable(m_hDevice, "EnumEntry_TriggerSelector_FrameBurstStart"))
                {
                    Pylon.DeviceFeatureFromString(m_hDevice, "TriggerSelector", "FrameBurstStart");
                    Pylon.DeviceFeatureFromString(m_hDevice, "TriggerMode", "Off");
                }

                /* Disable frame start trigger if available. */
                if (Pylon.DeviceFeatureIsAvailable(m_hDevice, "EnumEntry_TriggerSelector_FrameStart"))
                {
                    Pylon.DeviceFeatureFromString(m_hDevice, "TriggerSelector", "FrameStart");
                    Pylon.DeviceFeatureFromString(m_hDevice, "TriggerMode", "Off");
                }

                /* 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. */
                if (Pylon.DeviceGetNumStreamGrabberChannels(m_hDevice) < 1)
                {
                    throw new Exception("The transport layer doesn't support image streams.");
                }

                /* Create and open a stream grabber for the first channel. */
                m_hGrabber = Pylon.DeviceGetStreamGrabber(m_hDevice, 0);
                Pylon.StreamGrabberOpen(m_hGrabber);

                /* Get a handle for the stream grabber's wait object. The wait object
                 * allows waiting for m_buffers to be filled with grabbed data. */
                m_hWait = Pylon.StreamGrabberGetWaitObject(m_hGrabber);
            }
            catch
            {
                /* Get the last error message here, because it could be overwritten by cleaning up. */
                UpdateLastError();

                try
                {
                    Close(); /* Try to close any open handles. */
                }
                catch
                {
                    /* Another exception cannot be handled. */
                }
                throw;
            }

            /* Notify that the ImageProvider is open and ready for grabbing and configuration. */
            OnDeviceOpenedEvent();
        }
Beispiel #19
0
        private void initialize()
        {
            _initSuccess = true;
            /* 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)
            {
                _initSuccess = false;
                notifyError("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 and for grabbing images. */
            Pylon.DeviceOpen(_hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);

            /* 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, "EnumEntry_PixelFormat_Mono8");

            if (!isAvail)
            {
                /* Feature is not available. */
                _initSuccess = false;
                notifyError("Device doesn't support the Mono8 pixel format.");
            }
            else
            {
                /* ... Set the pixel format to Mono8. */
                Pylon.DeviceFeatureFromString(_hDev, "PixelFormat", "Mono8");
            }

            /* For GigE cameras, we recommend increasing the packet size for better
             *     performance. If 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. */
            /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
            isAvail = Pylon.DeviceFeatureIsWritable(_hDev, "GevSCPSPacketSize");

            if (isAvail)
            {
                /* ... The device supports the packet size feature. Set a value. */
                Pylon.DeviceSetIntegerFeature(_hDev, "GevSCPSPacketSize", 1500);
            }

            setStreamGrabber();

            //Free Run Settings
            //setFreeRunParams();
            setPEGParams(_PEGX, _PEGY);


            prepareDevice();
            setStatus(GrabInstruction.Initialize, GrabStage.Connected, GrabState.Idle);
            notifyStateChange(_bgworker, new GrabImageStatusChangedEventArgs()
            {
                Status = this.Status
            });
        }