Esempio n. 1
0
 public void SearchDevice(ref DeviceEnumerator.Device[] devices)
 {
     try
     {
         deviceTable.Clear();
         List <DeviceEnumerator.Device> deviceList = new List <DeviceEnumerator.Device>();
         deviceList = DeviceEnumerator.EnumerateDevices();
         deviceList.Sort((x, y) => { return(x.Name.CompareTo(y.Name)); });
         foreach (DeviceEnumerator.Device item in deviceList)
         {
             deviceTable.Rows.Add(
                 deviceTable.Rows.Count + 1,
                 item.Index,
                 item.FullName,
                 item.Name,
                 item.Tooltip);
         }
         devices = deviceList.ToArray();
     }
     catch (Exception ex)
     {
         StackFrame[] stackFrames = new StackTrace(true).GetFrames();
         clsLogFile.LogTryCatch(stackFrames, ex.Message, true, true);
     }
 }
Esempio n. 2
0
        private void UpdateDeviceList()
        {
            try
            {
                /* 向設備列舉器詢問設備列表。 */
                List <DeviceEnumerator.Device> list = DeviceEnumerator.EnumerateDevices();

                /* 將每個新設備添加到列表中。 */
                foreach (DeviceEnumerator.Device device in list)
                {
                    ListViewItem item = new ListViewItem(device.Name);
                    if (device.Tooltip.Length > 0)
                    {
                        item.ToolTipText = device.Tooltip;
                    }
                    item.Tag = device;

                    //******//
                    try
                    {
                        // 使用設備數據中的索引打開圖像提供者。
                        m_imageProvider.Open(device.Index);
                    }
                    catch (Exception e)
                    {
                        ShowException(e, m_imageProvider.GetLastErrorMessage());
                    }
                    //*******//
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }
Esempio n. 3
0
        public static Dictionary <string, CameraProperty> Read(PYLON_DEVICE_HANDLE deviceHandle, string fullName)
        {
            Dictionary <string, CameraProperty> properties = new Dictionary <string, CameraProperty>();

            // Enumerate devices again to make sure we have the correct device index and find the device class.
            DeviceEnumerator.Device        device  = null;
            List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices();

            foreach (DeviceEnumerator.Device candidate in devices)
            {
                if (candidate.FullName != fullName)
                {
                    continue;
                }

                device = candidate;
                break;
            }

            if (device == null)
            {
                return(properties);
            }

            string deviceClass = "BaslerGigE";

            try
            {
                deviceClass = Pylon.DeviceInfoGetPropertyValueByName(device.DeviceInfoHandle, Pylon.cPylonDeviceInfoDeviceClassKey);
            }
            catch
            {
                log.ErrorFormat("Could not read Basler device class. Assuming BaslerGigE.");
            }

            properties.Add("width", ReadIntegerProperty(deviceHandle, "Width"));
            properties.Add("height", ReadIntegerProperty(deviceHandle, "Height"));
            properties.Add("enableFramerate", ReadBooleanProperty(deviceHandle, "AcquisitionFrameRateEnable"));

            if (deviceClass == "BaslerUsb")
            {
                properties.Add("framerate", ReadFloatProperty(deviceHandle, "AcquisitionFrameRate"));
                properties.Add("exposure", ReadFloatProperty(deviceHandle, "ExposureTime"));
                properties.Add("gain", ReadFloatProperty(deviceHandle, "Gain"));
            }
            else
            {
                properties.Add("framerate", ReadFloatProperty(deviceHandle, "AcquisitionFrameRateAbs"));
                properties.Add("exposure", ReadFloatProperty(deviceHandle, "ExposureTimeAbs"));
                properties.Add("gain", ReadIntegerProperty(deviceHandle, "GainRaw"));
            }

            return(properties);
        }
Esempio n. 4
0
        /// <summary>
        /// Refreshes the Device-list.
        /// </summary>
        public void RefreshDevices()
        {
            logger.Info("Refreshing devices list...");


            try {
                rawDevices = DeviceEnumerator.EnumerateDevices();
            }
            catch (Exception ex) {
                logger.Fatal("ERROR Refreshing devices list: " + ex.ToString() + ".");
            }
            finally {
                logger.Info("Successfully refreshed the device list.");
            }
        }
Esempio n. 5
0
        public static Dictionary <string, CameraProperty> Read(PYLON_DEVICE_HANDLE deviceHandle, string fullName)
        {
            Dictionary <string, CameraProperty> properties = new Dictionary <string, CameraProperty>();

            // Enumerate devices again to make sure we have the correct device index and find the device class.
            DeviceEnumerator.Device        device  = null;
            List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices();

            foreach (DeviceEnumerator.Device candidate in devices)
            {
                if (candidate.FullName != fullName)
                {
                    continue;
                }

                device = candidate;
                break;
            }

            if (device == null)
            {
                return(properties);
            }

            string deviceClass = "BaslerGigE";

            try
            {
                deviceClass = Pylon.DeviceInfoGetPropertyValueByName(device.DeviceInfoHandle, Pylon.cPylonDeviceInfoDeviceClassKey);
            }
            catch
            {
                log.ErrorFormat("Could not read Basler device class. Assuming BaslerGigE.");
            }

            properties.Add("width", ReadIntegerProperty(deviceHandle, "Width"));
            properties.Add("height", ReadIntegerProperty(deviceHandle, "Height"));

            // Camera properties in Kinovea combine the value and the "auto" flag.
            // We potentially need to read several Basler camera properties to create one Kinovea camera property.
            // Furthermore, some properties name or type depends on whether the camera is USB or GigE.
            ReadFramerate(deviceHandle, deviceClass, properties);
            ReadExposure(deviceHandle, deviceClass, properties);
            ReadGain(deviceHandle, deviceClass, properties);

            return(properties);
        }
        public override void Connect()
        {
            //Stop,and Close
            m_imageProvider.Stop();
            m_imageProvider.Close();
            setStatus(GrabInstruction.Connect, GrabStage.Closed, GrabState.Idle);
            Status.IsConnection = false;


            List <DeviceEnumerator.Device> list = DeviceEnumerator.EnumerateDevices();
            var device = list.FirstOrDefault();

            if (device != null)
            {
                m_imageProvider.Open(device.Index);
                setStatus(GrabInstruction.Connect, GrabStage.Connected, GrabState.Idle);
                Status.IsConnection = true;
            }
        }
Esempio n. 7
0
        public static Dictionary <string, CameraProperty> Read(PYLON_DEVICE_HANDLE deviceHandle, string fullName)
        {
            Dictionary <string, CameraProperty> properties = new Dictionary <string, CameraProperty>();

            // Enumerate devices again to make sure we have the correct device index and find the device class.
            DeviceEnumerator.Device        device  = null;
            List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices();

            foreach (DeviceEnumerator.Device candidate in devices)
            {
                if (candidate.FullName != fullName)
                {
                    continue;
                }

                device = candidate;
                break;
            }

            if (device == null)
            {
                return(properties);
            }

            try
            {
                properties.Add("width", ReadIntegerProperty(deviceHandle, "Width", "WidthMax"));
                properties.Add("height", ReadIntegerProperty(deviceHandle, "Height", "HeightMax"));

                // Camera properties in Kinovea combine the value and the "auto" flag.
                // We potentially need to read several Basler camera properties to create one Kinovea camera property.
                ReadFramerate(deviceHandle, properties);
                ReadExposure(deviceHandle, properties);
                ReadGain(deviceHandle, properties);
            }
            catch (Exception e)
            {
                log.ErrorFormat("Error while reading Basler camera properties. {0}.", e.Message);
            }


            return(properties);
        }
Esempio n. 8
0
 /* Updates the list of available devices in the upper left area. */
 private void UpdateDeviceList()
 {
     try
     {
         /* Clear the device list. */
         deviceListView.Items.Clear();
         /* Ask the device enumerator for a list of devices. */
         List <DeviceEnumerator.Device> list = DeviceEnumerator.EnumerateDevices();
         /* Add each device to the list. */
         foreach (DeviceEnumerator.Device device in list)
         {
             ListViewItem item = new ListViewItem(device.Name);
             item.Tag = device; /* Attach the device data. */
             deviceListView.Items.Add(item);
         }
     }
     catch (Exception e)
     {
         ShowException(e, m_imageProvider.GetLastErrorMessage());
     }
 }
Esempio n. 9
0
        public override List <CameraSummary> DiscoverCameras(IEnumerable <CameraBlurb> blurbs)
        {
            List <CameraSummary> summaries = new List <CameraSummary>();

            // We don't do the discover step every time to avoid UI freeze since Enumerate takes some time.
            if (discoveryStep > 0)
            {
                discoveryStep = (discoveryStep + 1) % discoverySkip;
                foreach (CameraSummary summary in cache.Values)
                {
                    summaries.Add(summary);
                }

                return(summaries);
            }

            discoveryStep = 1;
            List <CameraSummary> found = new List <CameraSummary>();

            List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices();

            foreach (DeviceEnumerator.Device device in devices)
            {
                string identifier = device.FullName;

                bool cached = cache.ContainsKey(identifier);
                if (cached)
                {
                    deviceIndices[identifier] = device.Index;
                    summaries.Add(cache[identifier]);
                    found.Add(cache[identifier]);
                    continue;
                }

                string             alias            = device.Name;
                Bitmap             icon             = null;
                SpecificInfo       specific         = new SpecificInfo();
                Rectangle          displayRectangle = Rectangle.Empty;
                CaptureAspectRatio aspectRatio      = CaptureAspectRatio.Auto;
                deviceIndices[identifier] = device.Index;

                if (blurbs != null)
                {
                    foreach (CameraBlurb blurb in blurbs)
                    {
                        if (blurb.CameraType != this.CameraType || blurb.Identifier != identifier)
                        {
                            continue;
                        }

                        alias            = blurb.Alias;
                        icon             = blurb.Icon ?? defaultIcon;
                        displayRectangle = blurb.DisplayRectangle;
                        if (!string.IsNullOrEmpty(blurb.AspectRatio))
                        {
                            aspectRatio = (CaptureAspectRatio)Enum.Parse(typeof(CaptureAspectRatio), blurb.AspectRatio);
                        }

                        // Restore saved parameters.
                        specific = SpecificInfoDeserialize(blurb.Specific);
                        break;
                    }
                }

                icon = icon ?? defaultIcon;

                CameraSummary summary = new CameraSummary(alias, device.Name, identifier, icon, displayRectangle, aspectRatio, specific, this);

                summaries.Add(summary);
                found.Add(summary);
                cache.Add(identifier, summary);
            }

            List <CameraSummary> lost = new List <CameraSummary>();

            foreach (CameraSummary summary in cache.Values)
            {
                if (!found.Contains(summary))
                {
                    lost.Add(summary);
                }
            }

            foreach (CameraSummary summary in lost)
            {
                cache.Remove(summary.Identifier);
            }

            return(summaries);
        }
Esempio n. 10
0
        /* Updates the list of available devices in the upper left area. */
        private void UpdateDeviceList()
        {
            try
            {
                /* Ask the device enumerator for a list of devices. */
                List <DeviceEnumerator.Device> list = DeviceEnumerator.EnumerateDevices();

                ListView.ListViewItemCollection items = deviceListView.Items;

                /* Add each new device to the list. */
                foreach (DeviceEnumerator.Device device in list)
                {
                    bool newitem = true;
                    /* For each enumerated device check whether it is in the list view. */
                    foreach (ListViewItem item in items)
                    {
                        /* Retrieve the device data from the list view item. */
                        DeviceEnumerator.Device tag = item.Tag as DeviceEnumerator.Device;

                        if (tag.FullName == device.FullName)
                        {
                            /* Update the device index. The index is used for opening the camera. It may change when enumerating devices. */
                            tag.Index = device.Index;
                            /* No new item needs to be added to the list view */
                            newitem = false;
                            break;
                        }
                    }

                    /* If the device is not in the list view yet the add it to the list view. */
                    if (newitem)
                    {
                        ListViewItem item = new ListViewItem(device.Name);
                        if (device.Tooltip.Length > 0)
                        {
                            item.ToolTipText = device.Tooltip;
                        }
                        item.Tag = device;

                        /* Attach the device data. */
                        deviceListView.Items.Add(item);
                    }
                }

                /* Delete old devices which are removed. */
                foreach (ListViewItem item in items)
                {
                    bool exists = false;

                    /* For each device in the list view check whether it has not been found by device enumeration. */
                    foreach (DeviceEnumerator.Device device in list)
                    {
                        if (((DeviceEnumerator.Device)item.Tag).FullName == device.FullName)
                        {
                            exists = true;
                            break;
                        }
                    }
                    /* If the device has not been found by enumeration then remove from the list view. */
                    if (!exists)
                    {
                        deviceListView.Items.Remove(item);
                    }
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }
Esempio n. 11
0
 /* Updates the list of available devices in the upper left area. */
 private void UpdateDeviceList()
 {
     /* Ask the device enumerator for a list of devices. */
     Device_list.Clear();
     Device_list = new List <DeviceEnumerator.Device>(DeviceEnumerator.EnumerateDevices());
 }
Esempio n. 12
0
        /// <summary>
        /// Check if device is ready
        /// </summary>
        /// <returns>true if device is ready</returns>
        /// <param name="isUpdateListDevice">true if update list of devices</param>
        private static bool IsReadyDevice(bool isUpdateListDevice)
        {
            bool isReady = false;

            try
            {
                // Update list of cameras
                if (isUpdateListDevice || list == null)
                {
                    list = DeviceEnumerator.EnumerateDevices();
                }

                if (list.Count == 0)
                {
                    return(false);
                }

                PYLON_DEVICE_HANDLE device       = null;
                BaslerParams        baslerParams = new BaslerParams();

                for (int i = 0; i < list.Count; i++)
                {
                    // Check ip address
                    try
                    {
                        device = Pylon.CreateDeviceByIndex((uint)i);
                        string ipAddress = Pylon.DeviceInfoGetPropertyValueByName(Pylon.DeviceGetDeviceInfoHandle(device), Pylon.cPylonDeviceInfoIpAddressKey);
                        if (string.Compare(baslerParams.IpAddress, ipAddress) == 0)
                        {
                            cameraIndex = i;
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        isReady = false;
                    }
                    finally
                    {
                        try
                        {
                            // Clean up. Close and release the pylon device.
                            if (Pylon.DeviceIsOpen(device))
                            {
                                Pylon.DeviceClose(device);
                            }

                            Pylon.DestroyDevice(device);
                        }
                        catch (Exception ex)
                        {
                            isReady = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                isReady = false;
            }

            return(isReady);
        }