Pete Brown’s Gadgeteer Robot from a Windows Form.. :-)

Talking about the Pete Brown’s Gadgeteer robot, several people ask me how to do a simple winform listener because they have the limit of express versions or simply don’t have developer licence to test app’s for the store.


Just in five minuts… everybody can jump driving a winform app and the Magic Robot using the Gadgeteer!



using System.Net;
using System.Net.Sockets;



namespace RobotServiceSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartListener();
 
            button1.MouseLeave += MouseLeave;
            button2.MouseLeave += MouseLeave;
            button3.MouseLeave += MouseLeave;
            button4.MouseLeave += MouseLeave;
            button5.MouseLeave += MouseLeave;



            button1.MouseEnter += Move_right;
            button2.MouseEnter += Move_left;
            button3.MouseEnter += Send_Stop;
            button4.MouseEnter += Move_backward;
            button5.MouseEnter += Move_forward;
        }



        private TcpListener _listener;
        private Thread _listenerThread;
        private void StartListener()
        {
            _listenerThread = new Thread(RunListener);
            _listenerThread.Start();
        } 


        TcpClient client;
        private void RunListener()
        {
            _listener = new TcpListener(new IPAddress(new byte[] {192,168,1,101}), 5150);
            _listener.Start();



            textBoxLog.Text += “listening at : “ + _listener.LocalEndpoint.ToString()+“\n\r”;
            while (true)
            {
                client = _listener.AcceptTcpClient();
                this.Invoke(
                    new Action(
                        () =>
                        {
                            textBoxLog.Text = “”;
                            textBoxLog.Text += string.Format(“New robot from {0} \n\r”, client.Client.RemoteEndPoint);
                        }
                    )); ;
                ThreadPool.QueueUserWorkItem(ProcessClient, client);
            }
        }



        private void ProcessClient(object state)
        {
            TcpClient client = state as TcpClient;
        }



        void MouseLeave(object sender, EventArgs e)
        {
            sendStream(Encoding.ASCII.GetBytes(“/panic”));
        }



        private void Move_right(object sender, EventArgs e)
        {
            sendStream(Encoding.ASCII.GetBytes(“/right”));
        } 


        private void Move_left(object sender, EventArgs e)
        {
            sendStream(Encoding.ASCII.GetBytes(“/left”));
        }



        private void Send_Stop(object sender, EventArgs e)
        {
            sendStream(Encoding.ASCII.GetBytes(“/stop”));
        }



        private void Move_backward(object sender, EventArgs e)
        {
            sendStream(Encoding.ASCII.GetBytes(“/backward”));
        }



        private void Move_forward(object sender, EventArgs e)
        {
            sendStream(Encoding.ASCII.GetBytes(“/forward”));
        }


         void sendStream(byte[] bytes)
        {
            NetworkStream stream = client.GetStream();
            stream.Write(bytes, 0, bytes.Length);
        }



    }
}




You can find the complete post in 10Rem at : http://10rem.net/blog/2012/09/10/controlling-a-netmf-gadgeteer-robot-from-a-windows-8-app


Enjoy!!
PepLluis,


PS: No doubt in ask if you need the same code for VB

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>