Example #1
0
		//Thread worker.
		private void ConnectorThreadWorker ()
		{
			List<string> commandList = new List<string> () {"a", "b"};
			Thread.VolatileWrite (ref _threadStarted, 1);
			try {
                IPAddress ip = IPAddress.Parse(ipAddress);   // IPAddress ipAddress = IPAddress.Parse("192.168.1.105");
				IPEndPoint ipEndPoint = new IPEndPoint (ip, Convert.ToInt32 (port));

				mainActivity.UpdateConnectionState(1, "Connecting...");

				//Try to connect to the Arduino.
				socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				socket.Connect (ipEndPoint);

				//Check if connection is active -> if so, start receiver and sender
				if (socket.Connected) {
					mainActivity.UpdateConnectionState(2, "Connected");

					//Create Receiver instance.
					_receiver = new Receiver (mainActivity, socket);
					_receiver.StartReceiver ();

					//Create Sender instance.
					_sender = new Sender (socket);
					_sender.StartSender ();

					//Check if connection is active. 
					while ((Thread.VolatileRead (ref _threadStarted) == 1) && socket.Connected) {  // protocol: s: get status information
						socket.Send (Encoding.ASCII.GetBytes (commandList[mainActivity.listIndex]));
						Thread.Sleep (1000);
					}
				}
			} catch (Exception exception) {
				error = exception.Message;
			} finally {
				//Always close the socket.
				if (socket != null) {
					socket.Close ();
				}

				//Stop Sender and Receive when the connection closes.
				if (_sender != null) {
					_sender.StopSender ();
					if (_sender.GetThread () != null) {
						_sender.GetThread ().Join ();
					}
				}
				if (_receiver != null) {
					_receiver.StopReceiver ();

					if (_receiver.GetThread () != null) {
						_receiver.GetThread ().Join ();
					}			
				}
			}

			if (error != null) {
				mainActivity.UpdateConnectionState(3, "Error: " + error + ".");
			} else {
				mainActivity.UpdateConnectionState(4, "Disconnected.");              
			}
			_connectorThread = null;
		}
Example #2
0
        //Thread worker.
        private void ConnectorThreadWorker()
        {
            Thread.VolatileWrite(ref _threadStarted, 1);
            try
            {
                IPAddress  ip         = IPAddress.Parse(ipAddress); // IPAddress ipAddress = IPAddress.Parse("192.168.1.105");
                IPEndPoint ipEndPoint = new IPEndPoint(ip, Convert.ToInt32(port));

                mainActivity.UpdateConnectionState(1, "Connecting...");

                //Try to connect to the Arduino.
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(ipEndPoint);

                //Check if connection is active -> if so, start receiver and sender
                if (socket.Connected)
                {
                    mainActivity.UpdateConnectionState(2, "Connected");

                    //Create Receiver instance.
                    _receiver = new Receiver(mainActivity, socket);
                    _receiver.StartReceiver();

                    //Create Sender instance.
                    _sender = new Sender(socket);
                    _sender.StartSender();

                    //Check if connection is active.
                    while ((Thread.VolatileRead(ref _threadStarted) == 1) && socket.Connected)
                    {
                        //Ask for pin status (from Arduino) update every second.
                        socket.Send(Encoding.ASCII.GetBytes("s"));  // protocol: s: get status information
                        Thread.Sleep(1000);
                    }
                }
            }
            catch (Exception exception)
            {
                error = exception.Message;
            }
            finally
            {
                //Always close the socket.
                if (socket != null)
                {
                    socket.Close();
                }

                //Stop Sender and Receive when the connection closes.
                if (_sender != null)
                {
                    _sender.StopSender();
                    if (_sender.GetThread() != null)
                    {
                        _sender.GetThread().Join();
                    }
                }
                if (_receiver != null)
                {
                    _receiver.StopReceiver();

                    if (_receiver.GetThread() != null)
                    {
                        _receiver.GetThread().Join();
                    }
                }
            }

            if (error != null)
            {
                mainActivity.UpdateConnectionState(3, "Error: " + error + ".");
            }
            else
            {
                mainActivity.UpdateConnectionState(4, "Disconnected.");
            }
            _connectorThread = null;
        }