0
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft;
using System.Net;

namespace Plugino3D
{
 public class TCPIPClient
  {
    public int port = 50010;
    public string logMsg = "";
    public string ipAdress;
    public string description;
    public TcpClient clientSocket;
    public TcpListener mTCPlistener;

    public List<double> zcoordinate = null;
    public double zaxis = 0.0;
    public List<double[]> FinalCoordinates = null;

    public double xCoordinateMin = 0.0;
    public double yCoordinateMin = 0.0;
    public List<double> xcoordinate = null;
    public List<double> ycoordinate = null;

    public double xcoordinateZero = 0.0;
    public double ycoordinateZero = 0.0;
    public double zCoordinateMinn = 0.0;

    public bool connected = true;

    public TCPIPClient()
    {
        clientSocket = null;
        xcoordinate = new List<double>();
        ycoordinate = new List<double>();
        zcoordinate = new List<double>();
        FinalCoordinates = new List<double[]>();

    }




    public void Connect()
    {
        try
        {
            if (clientSocket != null)
            {

                clientSocket.Close();
                clientSocket = null;
                connected = true;
                readData(clientSocket);

            }
            else if (clientSocket == null)
            {
                clientSocket = new TcpClient();
                connected = true;

                logMsg = "Connecting to " + ipAdress + "having port number " + port;

                clientSocket.Connect(ipAdress, port);
                logMsg = "Connected to " + ipAdress + "having port number" + port;

                readData(clientSocket);
            }
        }
        catch (Exception excp)
        {
            this.logMsg = "Cant Connect 1";
            this.logMsg = excp.ToString();

        }
    }

    public void CloseAndDisconnect()
    {
        if (clientSocket != null)
        {
            if (clientSocket.Connected)
            {

                clientSocket.Close();
            }
        }
    }

    public void readData(TcpClient mClient)
    {

        try
        {
            StreamReader clientStreamReader = new StreamReader(mClient.GetStream());
            logMsg = "1";

            char[] buff = new char[1024];
            logMsg = "2";
            int readByCount = 0;


            while (connected)
            {
                readByCount = clientStreamReader.Read(buff, 0, buff.Length);
                logMsg = "3";


                if (readByCount <= 0)
                {

                    mClient.Close();
                    break;
                }
                if (readByCount > 30)
                {

                    var output = (new string(buff).TrimEnd('\u0000'));
                    logMsg = "4";

                    var output1 = output.Split(new[] { ";;" }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(';')).ToArray();
                    logMsg = "5";

                    JToken jsonParsed = JToken.FromObject(output1);
                    logMsg = "6";

                    jsonParsed.Last.Remove();
                    logMsg = "7";
                    jsonParsed.First.Remove();
                    logMsg = "8";


                    foreach (var arrayItem in jsonParsed)
                    {
                        var innerArray = arrayItem.ToObject<double[]>();

                        FinalCoordinates.Add(innerArray);

                        zaxis = innerArray.ElementAt<double>(4);

                        zcoordinate.Add(zaxis);

                        foreach (var item in zcoordinate)
                        {
                            zCoordinateMinn = zcoordinate.Min();

                        }

                    }
                    logMsg = "9";

                    foreach (var item in FinalCoordinates)
                    {

                        if (item.ElementAt(4) == zCoordinateMinn)
                        {
                            xCoordinateMin = (item[0] + item[2]) / 2;
                            yCoordinateMin = (item[1] + item[3]) / 2;

                            xcoordinate.Add(xCoordinateMin);
                            ycoordinate.Add(yCoordinateMin);
                        }

                    }
                    logMsg = "10";
                    xcoordinateZero = xcoordinate.ElementAt(0);
                    ycoordinateZero = ycoordinate.ElementAt(0);
                    zCoordinateMinn = 1000 * zCoordinateMinn;

                    Convert();

                    logMsg = "11";

                    xcoordinate.Clear();
                    ycoordinate.Clear();
                    zcoordinate.Clear();


                    connected = false;
                    //StopServer();                      

                }
            }   
            Array.Clear(buff, 0, buff.Length);

        }
        catch (Exception ex)
        {
            this.logMsg = "O3D: Could not connect to camera:";
        }
    }

    public void Convert()
    {
        xcoordinateZero = (530 / 131) * xcoordinateZero;
        xcoordinateZero = 155 + xcoordinateZero;
        Console.WriteLine(xcoordinateZero);

        ycoordinateZero = (715 / 175) * ycoordinateZero;
        ycoordinateZero = -320 + ycoordinateZero;
        Console.WriteLine(ycoordinateZero);

        zCoordinateMinn = zCoordinateMinn - 460;
        zCoordinateMinn = 300 - zCoordinateMinn;
        Console.WriteLine(zCoordinateMinn);
    }
}

} This is the TCPIP connection code of the plugin I made to send camera data to the camera. I am getting this error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

I am getting this error on the robot software.

How to solve this? Any advice or ideas?

  • 3
    Which line is throwing the exception? Can you create a [mre]? – gunr2171 Mar 23 '20 at 14:52
  • My first guess (due to lack of information) would be that this "item.ElementAt(4)" in you `foreach` loop is throwing the error.. You might what to cheke the `Length` or 'Count` before you try to access it. – Barns Mar 23 '20 at 15:00
  • You are not setting ipAdress. – jdweng Mar 23 '20 at 16:09
  • Actually, code is working fine when I run it by making a solution on c# but it is giving an error when I use it in my plugin as one of the classes in my robot plugin. – Vedant Gonnade Mar 24 '20 at 07:58

0 Answers0