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

SendAndReceive() public method

Enqueue communication request and wait for reply.

The method puts specified command into communication queue and waits until the command is sent to SRV-1 Blackfin robot and reply is received.

If SRV-1 responds with more data than response buffer can fit, then the response buffer will take all the data it can store, but the rest of response will be discarded. The only exception is image request - if response buffer is too small to fit image response, then IndexOutOfRangeException exception is thrown. It is user's responsibility to provide response buffer of the correct size. Check definition of the SRV-1 Control Protocol for information about supported commands and responses.

Not connected to SRV-1. Connection lost or communicaton failure. Response buffer is too small.
public SendAndReceive ( byte request, byte responseBuffer ) : int
request byte Array of bytes (command) to send to SRV-1 Blackfin robot/camera.
responseBuffer byte Buffer to read response into.
return int
        public int SendAndReceive( byte[] request, byte[] responseBuffer )
        {
            lock ( sync )
            {
                if ( socket == null )
                {
                    // handle error
                    throw new NotConnectedException( "Not connected to SRV-1." );
                }

                lock ( communicationQueue )
                {
                    communicationQueue.Enqueue( new CommunicationRequest( request, responseBuffer ) );
                }
                requestIsAvailable.Set( );

                // waiting for reply
                replyIsAvailable.WaitOne( );

                // no reply since we got disconnect request from user - background thread is exiting
                if ( lastRequestWithReply == null )
                    return 0;

                // get number of bytes read
                int bytesRead = lastRequestWithReply.BytesRead;

                // clean the last reply
                lastRequestWithReply = null;

                if ( bytesRead == -1 )
                {
                    // handle error
                    throw new ConnectionLostException( "Connection lost or communicaton failure." );
                }
                if ( bytesRead == -2 )
                {
                    // handle error
                    throw new IndexOutOfRangeException( "Response buffer is too small." );
                }
                return bytesRead;
            }
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Worker thread.
        /// </summary>
        ///
        private void WorkerThread( )
        {
            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch( );

            // buffer to read stream into
            byte[] buffer = new byte[bufferSize];

            while (!stopEvent.WaitOne(0, false))
            {
                try
                {
                    stopWatch.Reset( );
                    stopWatch.Start( );

                    int bytesRead = communicator.SendAndReceive(new byte[] { (byte)'I' }, buffer);

                    bytesReceived += bytesRead;

                    if (bytesRead > 10)
                    {
                        // check for image reply signature
                        if (
                            (buffer[0] == (byte)'#') &&
                            (buffer[1] == (byte)'#') &&
                            (buffer[2] == (byte)'I') &&
                            (buffer[3] == (byte)'M') &&
                            (buffer[4] == (byte)'J'))
                        {
                            // extract image size
                            int imageSize = System.BitConverter.ToInt32(buffer, 6);

                            if (!stopEvent.WaitOne(0, false))
                            {
                                try
                                {
                                    // decode image from memory stream
                                    Bitmap bitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 10, imageSize));
                                    framesReceived++;

                                    // let subscribers know if there are any
                                    if (NewFrame != null)
                                    {
                                        NewFrame(this, new NewFrameEventArgs(bitmap));
                                    }

                                    bitmap.Dispose( );
                                }
                                catch
                                {
                                }

                                // wait for a while ?
                                if (frameInterval > 0)
                                {
                                    // get download duration
                                    stopWatch.Stop( );

                                    // miliseconds to sleep
                                    int msec = frameInterval - (int)stopWatch.ElapsedMilliseconds;

                                    while ((msec > 0) && (stopEvent.WaitOne(0, false) == false))
                                    {
                                        // sleeping ...
                                        Thread.Sleep((msec < 100) ? msec : 100);
                                        msec -= 100;
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                    if (VideoSourceError != null)
                    {
                        VideoSourceError(this, new VideoSourceErrorEventArgs("Failed receiving video frame from SRV-1."));
                    }
                }
            }

            stopWatch.Stop( );

            if (PlayingFinished != null)
            {
                PlayingFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
        }