コード例 #1
0
ファイル: ImageProvider.cs プロジェクト: jfpk/kinoveaIDS
 /* Constructor with creation of basic objects. */
 public ImageProvider()
 {
     /* Create a thread for image grabbing. */
     m_grabThread = new Thread(Grab);
     /* Create objects used for buffer handling. */
     m_lockObject     = new Object();
     m_buffers        = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >();
     m_grabbedBuffers = new List <GrabResult>();
     /* Create handles. */
     m_hGrabber         = new PYLON_STREAMGRABBER_HANDLE();
     m_hDevice          = new PYLON_DEVICE_HANDLE();
     m_hRemovalCallback = new PYLON_DEVICECALLBACK_HANDLE();
     m_hConverter       = new PYLON_IMAGE_FORMAT_CONVERTER_HANDLE();
     /* Create callback handler and attach the method. */
     m_callbackHandler = new DeviceCallbackHandler();
     m_callbackHandler.CallbackEvent += new DeviceCallbackHandler.DeviceCallback(RemovalCallbackHandler);
 }
コード例 #2
0
        protected bool m_triggered = true; /* Indicates that we wish to trigger single shots */

        #endregion Fields

        #region Constructors

        /* Constructor with creation of basic objects. */
        public ImageProvider()
        {
            /* Create a thread for image grabbing. */
            m_grabThread = new Thread(Grab);
            /* Create objects used for buffer handling. */
            m_lockObject = new Object();
            m_buffers = new Dictionary<PYLON_STREAMBUFFER_HANDLE, PylonBuffer<Byte>>();
            m_grabbedBuffers = new List<GrabResult>();
            /* Create handles. */
            m_hGrabber = new PYLON_STREAMGRABBER_HANDLE();
            m_hDevice = new PYLON_DEVICE_HANDLE();
            m_hRemovalCallback = new PYLON_DEVICECALLBACK_HANDLE();
            m_hConverter = new PYLON_FORMAT_CONVERTER_HANDLE();
            /* Create callback handler and attach the method. */
            m_callbackHandler = new DeviceCallbackHandler();
            m_callbackHandler.CallbackEvent += new DeviceCallbackHandler.DeviceCallback(RemovalCallbackHandler);
        }
コード例 #3
0
        static int callbackCounter = 0;  /* Will be incremented by the callback function. */

        static void Main(string[] args)
        {
            PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */

            try
            {
                uint numDevices;                                                     /* Number of available devices.   */
                PYLON_DEVICECALLBACK_HANDLE hCb;                                     /* Required for deregistering the callback. */
                int loopCount;                                                       /* Counter. */
#if DEBUG
                bool isGigECamera;                                                   /* 1 if the device is a GigE device. */
#endif
                DeviceCallbackHandler callbackHandler = new DeviceCallbackHandler(); /* Handles callbacks from a device. */

                /* Before using any pylon methods, the pylon runtime must be initialized. */
                Pylon.Initialize();

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

                if (0 == numDevices)
                {
                    throw new Exception("No devices found.");
                }

                /* Get a handle for the first device found.  */
                hDev = Pylon.CreateDeviceByIndex(0);

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

                /* Print out the name of the camera we are using. */
                {
                    bool isReadable = Pylon.DeviceFeatureIsReadable(hDev, "DeviceModelName");
                    if (isReadable)
                    {
                        string name = Pylon.DeviceFeatureToString(hDev, "DeviceModelName");
                        Console.WriteLine("Using camera {0}", name);
                    }
                }

                /* Register the callback function. */
                callbackHandler.CallbackEvent += new DeviceCallbackHandler.DeviceCallback(removalCallbackFunction);
                hCb = Pylon.DeviceRegisterRemovalCallback(hDev, callbackHandler);

#if DEBUG
                /*  For GigE cameras, the application periodically sends heartbeat signals to the camera to keep the
                 *  connection to the camera alive. If the camera doesn't receive heartbeat signals within the time
                 *  period specified by the heartbeat timeout counter, the camera resets the connection.
                 *  When the application is stopped by the debugger, the application cannot create the heartbeat signals.
                 *  For that reason, the pylon runtime extends the heartbeat timeout in debug mode to 5 minutes to allow
                 *  debugging.  For GigE cameras, we will set the heartbeat timeout to a shorter period before testing the
                 * callbacks.
                 *  The heartbeat mechanism is also used for detection of device removal. When the pylon runtime doesn't
                 *  receive an acknowledge for the heartbeat signal, it is assumed that the device has been removed. A
                 *  removal callback will be fired in that case.
                 *  By decreasing the heartbeat timeout in debug mode for GigE cameras, the surprise removal will be noticed sooner than set by the pylon runtime. */
                {
                    /* Find out if we are using a GigE camera. */
                    PYLON_DEVICE_INFO_HANDLE hDi = Pylon.DeviceGetDeviceInfoHandle(hDev);
                    string deviceClass           = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoDeviceClassKey);

                    isGigECamera = deviceClass == "BaslerGigE";
                    /* Adjust the heartbeat timeout. */
                    if (isGigECamera)
                    {
                        setHeartbeatTimeout(hDev, 1000);  /* 1000 ms */
                    }
                }
#endif

                /* Ask the user to disconnect a device. */
                loopCount = 20 * 4;
                Console.WriteLine("Please disconnect the device (timeout {0} s) ", loopCount / 4);

                /* Wait until the removal has been noticed and the callback function has been fired. */
                do
                {
                    /* Print a . every few seconds to tell the user we're waiting for the callback. */
                    if (--loopCount % 4 == 0)
                    {
                        Console.Write(".");
                    }
                    System.Threading.Thread.Sleep(250);
                }while (callbackCounter < 1 && loopCount >= 0);  /*  Check loopCount so we won't wait forever. */


                if (callbackCounter < 1)
                {
                    Console.WriteLine("\nTimeout expired. Device hasn't been removed.");
                }


                /* Clean up. */

                /* ... Deregister the removal callback. */
                Pylon.DeviceDeregisterRemovalCallback(hDev, hCb);

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

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

                Console.Error.WriteLine("\nPress enter to exit.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                /* Retrieve the error message. */
                string msg = GenApi.GetLastErrorMessage() + "\n" + GenApi.GetLastErrorDetail();
                Console.Error.WriteLine("Exception caught:");
                Console.Error.WriteLine(e.Message);
                if (msg != "\n")
                {
                    Console.Error.WriteLine("Last error message:");
                    Console.Error.WriteLine(msg);
                }

                try
                {
                    if (hDev.IsValid)
                    {
                        /* ... Close and release the pylon device. */
                        if (Pylon.DeviceIsOpen(hDev))
                        {
                            Pylon.DeviceClose(hDev);
                        }
                        Pylon.DestroyDevice(hDev);
                    }
                }
                catch (Exception)
                {
                    /*No further handling here.*/
                }

                Pylon.Terminate();  /* Releases all pylon resources. */

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

                Environment.Exit(1);
            }
        }