AForge.Robotics.Surveyor.SRV1.Connect C# (CSharp) Method

Connect() public method

Connect to SRV-1 Blackfin robot/camera.

The method establishes connection to SRV-1 Blackfin robot/camera. If it succeeds then other methods can be used to manipulate the robot.

The method calls Disconnect before making any connection attempts to make sure previous connection is closed.

Failed connecting to SRV-1.
public Connect ( string ip, int port ) : void
ip string IP address of SRV-1 robot.
port int Port number to connect to.
return void
        public void Connect( string ip, int port )
        {
            Disconnect( );

            lock ( sync )
            {
                try
                {
                    // make sure communication queue is empty
                    communicationQueue.Clear( );

                    endPoint = new IPEndPoint( IPAddress.Parse( ip ), Convert.ToInt16( port ) );
                    // create TCP/IP socket and set timeouts
                    socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
                    socket.ReceiveTimeout = 5000;
                    socket.SendTimeout    = 1000;

                    // connect to SVS
                    socket.Connect( endPoint );

                    // create events
                    stopEvent = new ManualResetEvent( false );

                    requestIsAvailable = new AutoResetEvent( false );
                    replyIsAvailable   = new AutoResetEvent( false );

                    // create and start new thread
                    thread = new Thread( new ThreadStart( CommunicationThread ) );
                    thread.Start( );
                }
                catch ( SocketException )
                {
                    socket.Close( );
                    socket   = null;
                    endPoint = null;

                    throw new ConnectionFailedException( "Failed connecting to SRV-1." );
                }
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Connect to SVS board.
        /// </summary>
        ///
        /// <param name="ipAddress">IP address of SVS board.</param>
        ///
        /// <remarks><para>The method establishes connection to SVS board. If it succeeds then
        /// other methods can be used to manipulate the board.</para>
        ///
        /// <para><note>The method calls <see cref="Disconnect"/> before making any connection
        /// attempts to make sure previous connection is closed.</note></para>
        /// </remarks>
        ///
        /// <exception cref="ConnectionFailedException">Failed connecting to SVS.</exception>
        ///
        public void Connect(string ipAddress)
        {
            // close previous connection
            Disconnect();

            lock (sync1)
            {
                lock (sync2)
                {
                    try
                    {
                        communicator1 = new SRV1();
                        communicator2 = new SRV1();

                        communicator1.Connect(ipAddress, 10001);
                        communicator2.Connect(ipAddress, 10002);

                        hostAddress = ipAddress;
                    }
                    catch
                    {
                        Disconnect();

                        throw new ConnectionFailedException("Failed connecting to SVS.");
                    }
                }
            }
        }
All Usage Examples Of AForge.Robotics.Surveyor.SRV1::Connect