コード例 #1
0
ファイル: UdpServer.cs プロジェクト: KKLTU/MPConBot-PC
        static void Main(string[] args)
        {
            
            LoCoMoCo MyBot = new LoCoMoCo("COM3"); // com port number
            var MainToken = new CancellationTokenSource(); //create token for the cancel

            UdpClient MainServerSocket = new UdpClient(15000); // declare a client
            byte[] MainDataReceived = new byte[1024]; // prepare container for received data
            string MainStringData = "";


            Capture capture = new Capture(); // declare object for camera
            Image<Bgr, Byte> frame; // declare image for capture
            int TotalMessageCount = 0;

            while (true) // this while for keeping the main server "listening"
            {
                try {

                    frame = capture.QueryFrame();
                    Console.WriteLine("Waiting for a UDP client..."); // display stuff
                    IPEndPoint MainClient = new IPEndPoint(IPAddress.Any,0); // prepare a client
                    MainDataReceived = MainServerSocket.Receive(ref MainClient); // receive packet
                    MainStringData = Encoding.ASCII.GetString(MainDataReceived, 0, MainDataReceived.Length); // get string from packet
                    Console.WriteLine("Response from " + MainClient.Address); // display stuff
                    Console.WriteLine("Message " + TotalMessageCount++ + ": " + MainStringData + "\n"); // display client's string

                    if (MainStringData.Equals("Picture"))
                    {
                        MainToken = new CancellationTokenSource(); // new cancellation token every iteration
                        Task.Run(() => SendPicture(MainServerSocket, MainClient, frame), MainToken.Token); //start method on another thread
                    }

                    if (MainStringData.Equals("StopV"))
                        MainToken.Cancel();

                    else if (MainStringData.Equals("Forward"))
                        MyBot.forward();

                    else if (MainStringData.Equals("Backward"))
                        MyBot.backward();

                    else if (MainStringData.Equals("Left"))
                        MyBot.turnleft();

                    else if (MainStringData.Equals("Right"))
                        MyBot.turnright();

                    else if (MainStringData.Equals("Stop"))
                        MyBot.stop();

                } catch (Exception e)
                { }
            }
        }
コード例 #2
0
 public Form1()
 {
     InitializeComponent();
     mc = new LoCoMoCo("COM7");
 }