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

UltrasonicPing() public method

Ping ultrasonic ranging modules.

The method sends 'p' SRV-1 command (see SRV-1 Control Protocol), which gets values from ultrasonic ranging modules attached to pins 27, 28, 29, 30 with trigger on pin 18. Supports Maxbotics EZ0 and EZ1 ultrasonic modules.

Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect. Failed parsing response from SRV-1.
public UltrasonicPing ( ) : float[]
return float[]
        public float[] UltrasonicPing( )
        {
            byte[] response = new byte[100];

            int    read = SendAndReceive( new byte[] { (byte) 'p' }, response );
            string str = System.Text.ASCIIEncoding.ASCII.GetString( response, 0, read );

            str = str.Replace( "##ping ", "" );
            str = str.Trim( );

            // split string into separate values
            string[] strs = str.Split( ' ' );

            try
            {
                float[] distance = new float[4];

                for ( int i = 0; i < 4; i++ )
                {
                    distance[i] = (float) int.Parse( strs[i] ) / 100f;
                }

                return distance;
            }
            catch
            {
                throw new ApplicationException( "Failed parsing response from SRV-1." );
            }
        }