Ejemplo n.º 1
0
        public override bool Connect()
        {
            bool result = false;

            try
            {
                //discover camera
                _camera = new DS_U3Wrapper.SniCameraInfo();
                Int32        cameraCount             = 0;
                IntPtr       unmanagedCameraInfoAddr = IntPtr.Zero;
                SniCamResult sResult = DS_U3Wrapper.SniCamDiscoverCameras(out unmanagedCameraInfoAddr, out cameraCount);

                if (sResult != SniCamResult.SNI_OK)
                {
                    throw new Exception("Could not connect to the DS-U3 successfully");
                }
                if (cameraCount == 0)
                {
                    throw new Exception("DS-U3 not available or no cameras connected.");
                }

                // Marshal the data at the returned pointer into a managed object
                _camera = (DS_U3Wrapper.SniCameraInfo)Marshal.PtrToStructure(unmanagedCameraInfoAddr, typeof(DS_U3Wrapper.SniCameraInfo));

                unmanagedCameraInfoAddr = IntPtr.Zero;

                //open camera
                IntPtr cameraHandle = IntPtr.Zero;
                sResult = DS_U3Wrapper.SniCamOpenCamera(ref _camera, out cameraHandle);

                if (sResult != SniCamResult.SNI_OK)
                {
                    throw new Exception("Could not open Camera Handle");
                }

                _camera.handle = cameraHandle;

                // Need to check status of camera. According to camera logs,
                // if the Nikon software finds the camera in a status other
                // than available, it requests the driver version, then closes
                // and opens the camera. Not sure why, but prevents the controller
                // from simply preventing communication with the camera.
                if (_camera.status != 0)
                {
                    StringBuilder version = new StringBuilder(34);
                    DS_U3Wrapper.SniCamGetDriverVersion(_camera.handle, version);
                    DS_U3Wrapper.SniCamCloseCamera(_camera.handle);
                    _camera.handle = IntPtr.Zero;
                    sResult        = DS_U3Wrapper.SniCamOpenCamera(ref _camera, out cameraHandle);

                    if (sResult != SniCamResult.SNI_OK)
                    {
                        throw new Exception("Could not open Camera Handle");
                    }

                    _camera.handle = cameraHandle;
                }


                _features = new Dictionary <Features, DS_U3Wrapper.SniFeature>();

                IntPtr p;

                sResult = DS_U3Wrapper.SniCamGetFeatures(_camera.handle, out p);
                if (sResult != SniCamResult.SNI_OK)
                {
                    throw new Exception("Could not get Camera features");
                }

                int numFeatures = Marshal.ReadInt32(p);
                //move to first feature element
                p = (IntPtr)(p.ToInt32() + Marshal.SizeOf(typeof(DS_U3Wrapper.SniFeatures)) -
                             Marshal.SizeOf(typeof(IntPtr)));

                // Setup a mapping from feature name to feature ID number, in order to more easily
                // access features by name.
                for (int i = 0; i < numFeatures; i++)
                {
                    DS_U3Wrapper.SniFeature feature = (DS_U3Wrapper.SniFeature)Marshal.PtrToStructure(p,
                                                                                                      typeof(DS_U3Wrapper.SniFeature));

                    foreach (Features featureName in Enum.GetValues(typeof(Features)))
                    {
                        if (featureName.ToString().Equals(feature.name))
                        {
                            _features.Add(featureName, feature);
                        }
                    }

                    //move to next feature element
                    p = (IntPtr)(p.ToInt32() + Marshal.SizeOf(typeof(DS_U3Wrapper.SniFeature)));
                }


                //initialise settings
                InitializeSettings();

                result = true;
            }
            catch (Exception /*ex*/)
            {
                if (_camera.handle != IntPtr.Zero)
                {
                    DS_U3Wrapper.SniCamCloseCamera(_camera.handle);
                    _camera.handle = IntPtr.Zero;
                }
                result = false;
            }


            return(result);
        }