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
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);
            }
        }