TCP Connection Modification



Overview

With the Kinect basics understood, it is time to understand how the Arduino will be interfacing with the C# program. The post outlines how a successful connection between the C# program acting as a client and the arudino acting as a server is successfully made.

Background

The connection from the program to Arudino had problems so needed to be fixed. Firstly it was causing the whole program to load slower because if it had trouble connecting to the server, then it would stale the loading of the rest of the program. There was also no indication when the connection was lost.

Activity

The first task was to made the connection process happen in sync with the loading of the kinect to reduce loading times. To do this various attempts to reshape the connection process was undertaken however to no avail. The next attempt was to run the connection in its own thread. As seen below.
Thread thread = new Thread(send);
thread.Start(mes);
Once the connection was running in a thread the program ran much smoother, however this caused problems as even if the connection could not be made another thread would be started. To prevent this a boolean was set. This would help indicate weather a connection has been made and only if it has then process another connection. The code below shows when connecting to the host if it is successful then change the boolean to true. Also the 2nd section shows an if statement around the new thread code to prevent it running if no connection has been made.
 while (tcpclnt.Connected == false)
            {
                try
                {
                    ConState.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        ConState.Content = "Connecting";
                    }));
                    tcpclnt.Connect("192.168.0.177", 23);
                    ConState.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        ConState.Content = "Connected";
                    }));
                    connected = true;
                }
                catch (Exception)
                {
                    connected = false;
                    ConState.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        ConState.Content = "Connection Lost, reconnect";
                    }));
                }
            }
                        
if (connected == true)
                                        {
                                            Thread thread = new Thread(send);
                                            thread.Start(mes);
                                        }

The next problem is changing the content of the label in a thread, because multiple threads were trying to access the label it was causing the program to crash. The code below prevent any other thread from accessing the label and queues it until the current thread has finished.
ConState.Dispatcher.BeginInvoke(new Action(delegate()
{
    ConState.Content = "Connection Lost, reconnect";
}));

These modifications has improved the performance of the program and reliability of the program.

Comments

Popular Posts