コード例 #1
0
        /// <summary>
        /// 断开相机驱动的连接
        /// </summary>
        /// <param name="IE2VCamer_"></param>
        /// <returns></returns>
        public bool Set_Disconnect(IE2VCamer_ShuJu IE2VCamer_, OnLinkDisconnectedHandler disConnectCam_)
        {
            bool ok = false;

            try
            {
                //停止相机流
                if (Set_StopStreaming(IE2VCamer_))
                {
                    #region  无用代码
                    //if (mCameraBridgeManager != null)
                    //{
                    //    mCameraBridgeManager.Close();
                    //    mCameraBridgeManager.Dispose();
                    //    mCameraBridgeManager = null;
                    //}
                    //if (mPipeline != null)
                    //{
                    //    mPipeline.Dispose();
                    //    mPipeline = null;
                    //}
                    //if (mStream != null)
                    //{
                    //    mStream.Close();
                    //    mStream.Dispose();
                    //    mStream = null;
                    //}
                    //if (mDevice != null)
                    //{
                    //    if (mDevice.IsConnected)
                    //    {
                    //        // Disconnect events.
                    //        mDevice.OnLinkDisconnected -= new OnLinkDisconnectedHandler(OnLinkDisconnected);
                    //        foreach (PvGenParameter lP in mDevice.Parameters)
                    //        {
                    //            lP.OnParameterUpdate -= new OnParameterUpdateHandler(OnParameterChanged);
                    //        }
                    //        mDevice.Disconnect();
                    //        mDevice.Dispose();
                    //        mDevice = null;
                    //    }
                    //}
                    #endregion


                    if (IE2VCamer_.MPipeline != null)
                    {
                        IE2VCamer_.MPipeline.Dispose();
                        IE2VCamer_.MPipeline = null;
                    }
                    if (IE2VCamer_.MStream != null)
                    {
                        IE2VCamer_.MStream.Close();
                        IE2VCamer_.MStream.Dispose();
                        IE2VCamer_.MStream = null;
                    }

                    if (IE2VCamer_.MDevice != null)
                    {
                        if (IE2VCamer_.MDevice.IsConnected)
                        {
                            IE2VCamer_.MDevice.OnLinkDisconnected -= new OnLinkDisconnectedHandler(disConnectCam_);
                            IE2VCamer_.MDevice.Disconnect();
                            IE2VCamer_.MDevice.Dispose();
                            IE2VCamer_.MDevice = null;
                        }
                    }
                    ok = true;
                }
                else
                {
                    return(ok);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("断开相机驱动出错:" + ex.Message);
            }

            ok = true;
            return(ok);
        }
コード例 #2
0
        /// <summary>
        /// 连接相机
        /// </summary>
        /// <param name="Cam_">相机数据接口</param>
        /// <param name="i">连接第几个相机</param>
        /// <param name="disConnectCam_">相机丢失回调事件</param>
        /// <param name="CamerCallBack_">相机取图回调函数</param>
        /// <returns></returns>
        public bool SetConnectCamer(IE2VCamer_ShuJu Cam_, uint i, OnLinkDisconnectedHandler disConnectCam_, OnBufferDisplay CamerCallBack_)
        {
            bool ok = false;

            try
            {
                if (this._countCamer > 0 && this._countCamer > i && this.Set_Disconnect(Cam_))
                {
                    #region  无用代码
                    //try
                    //{
                    //    // Create and connect the device controller based on the selected device
                    //    mDevice = PvDevice.CreateAndConnect(aDI);

                    //    // Create and open stream
                    //    mStream = PvStream.CreateAndOpen(aDI);

                    //    // GigE Vision specific connection steps
                    //    if (aDI.Type == PvDeviceInfoType.GEV)
                    //    {
                    //        PvDeviceGEV lDeviceGEV = mDevice as PvDeviceGEV;
                    //        PvStreamGEV lStreamGEV = mStream as PvStreamGEV;

                    //        // Negotiate packet size
                    //        lDeviceGEV.NegotiatePacketSize();

                    //        // Set stream destination to our stream object
                    //        lDeviceGEV.SetStreamDestination(lStreamGEV.LocalIPAddress, lStreamGEV.LocalPort);
                    //    }

                    //    // Create pipeline - requires stream
                    //    mPipeline = new PvPipeline(mStream);
                    //}
                    //catch (PvException ex)
                    //{
                    //    // Failure at some point, display and abort
                    //    MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    Disconnect();

                    //    return;
                    //}

                    //// Register to all events of the parameters in the device's node map
                    //foreach (PvGenParameter lParameter in mDevice.Parameters)
                    //{
                    //    lParameter.OnParameterUpdate += new OnParameterUpdateHandler(OnParameterChanged);
                    //}

                    //// Connect link disconnection handler
                    //mDevice.OnLinkDisconnected += new OnLinkDisconnectedHandler(OnLinkDisconnected);

                    //// Update device attributes
                    //UpdateAttributes(aDI);

                    //// Fill acquisition mode combo box
                    //modeComboBox.Items.Clear();
                    //PvGenEnum lMode = mDevice.Parameters.GetEnum("AcquisitionMode");
                    //if (lMode != null)
                    //{
                    //    foreach (PvGenEnumEntry lEntry in lMode)
                    //    {
                    //        if (lEntry.IsAvailable)
                    //        {
                    //            int lIndex = modeComboBox.Items.Add(lEntry.ValueString);
                    //            if (lEntry.ValueInt == lMode.ValueInt)
                    //            {
                    //                modeComboBox.SelectedIndex = lIndex;
                    //            }
                    //        }
                    //    }
                    //}
                    #endregion

                    PvDeviceInfo aDI = this._pv.GetDeviceInfo(i);

                    Thread.Sleep(800);

                    PvDeviceInfoGEV lDeviceInfoGEV = aDI as PvDeviceInfoGEV;

                    if (lDeviceInfoGEV != null)//这一段代码不能小否则出错
                    {
                        string lMACAddress = lDeviceInfoGEV.MACAddress;
                        string lIPAddress  = lDeviceInfoGEV.IPAddress;
                    }

                    Cam_.MDevice = PvDevice.CreateAndConnect(aDI);
                    Cam_.MStream = PvStream.CreateAndOpen(aDI);

                    if (aDI.Type == PvDeviceInfoType.GEV)
                    {
                        PvDeviceGEV lDeviceGEV = Cam_.MDevice as PvDeviceGEV;
                        PvStreamGEV lStreamGEV = Cam_.MStream as PvStreamGEV;

                        // Negotiate packet size
                        lDeviceGEV.NegotiatePacketSize();

                        // Set stream destination to our stream object
                        lDeviceGEV.SetStreamDestination(lStreamGEV.LocalIPAddress, lStreamGEV.LocalPort);
                    }

                    // Create pipeline - requires stream
                    Cam_.MPipeline = new PvPipeline(Cam_.MStream);

                    if (disConnectCam_ != null)
                    {
                        // Connect link disconnection handler
                        Cam_.MDevice.OnLinkDisconnected += new OnLinkDisconnectedHandler(disConnectCam_);
                    }

                    if (CamerCallBack_ != null)
                    {
                        Cam_.MDisplayThread.OnBufferDisplay += new OnBufferDisplay(CamerCallBack_);
                    }

                    Set_StartStreaming(Cam_);

                    ok = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("相机绑定出错:" + ex.Message);
            }

            return(ok);
        }