コード例 #1
0
 private void clientSoc_DataReceived(object sender, SocketPack socPak)
 {
     try
     {
         szData = clientSoc.DecodeBytesToString(socPak.dataBuffer);
         this.consoleTextBox.Text += szData + "\r\n";
         ExecList cmdRequest = ParseData(szData);
         if (!executing)
         {
             executing      = true;
             mainCmdRequest = cmdRequest;
             Thread actionThread = new Thread(new System.Threading.ThreadStart(Execute));
             actionThread.Start();
         }
         clientSoc.WaitForData();
     }
     catch (System.NullReferenceException sne)
     {
         consoleTextBox.Text += "Error @ OnDataReceived NULL ref";
     }
     catch (System.Net.Sockets.SocketException)
     {
         consoleTextBox.Text = "Disconnected";
         clientSoc.Close();
     }
     catch (Exception ex)
     {
         consoleTextBox.Text += "Error @ OnDataReceived" + ex.ToString();
     }
 }
コード例 #2
0
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            this.mrRoboto  = new ZalRobot();
            this.clientSoc = new UDPClientASocket(System.Text.Encoding.ASCII);

            InitIPPortGUI();

            this.clientSoc.DataReceived += new ZalSystem.UDPClientASocket.DataReceivedEventHandler(clientSoc_DataReceived);
            this.clientSoc.SocketClosed += new ZalSystem.UDPClientASocket.SocketClosedEventHandler(clientSoc_SocketClosed);

            this.KeyDown += new KeyEventHandler(Form1_KeyDown);

            commandCount   = 0;
            mainCmdRequest = new ExecList();
            executing      = false;
        }
コード例 #3
0
        public ExecList ParseData(string inMessage)
        {
            ExecList cmdRequest = new ExecList();

            if (inMessage == null)
            {
                return(cmdRequest);
            }

            //First, we clean the message from those characters that we found useless
            inMessage = clientSoc.ReadUntil(inMessage, '\n'.ToString());
            inMessage = inMessage.TrimEnd(new char[] { _charSPACE, _charNULL });

            //Second, we look for a command ID or the sender's IP
            if (inMessage.IndexOf("@") != _intERROR)
            {
                msgTokens = inMessage.Split('@');

                try
                {
                    cmdRequest.cmdID = int.Parse(msgTokens[1]);
                    inMessage        = msgTokens[0];
                }
                catch (System.FormatException)
                {
                    try
                    {
                        if (msgTokens[1].Equals(clientSoc.ip.ToString()))
                        {
                            inMessage = msgTokens[0];
                        }
                    }
                    catch (System.FormatException) {
                        cmdRequest = new ExecList();
                        return(cmdRequest);
                    }
                }
            }

            //Clean the command submessage and divide in commandTokens
            inMessage = inMessage.ToLower();
            string[] commandTokens;
            inMessage     = inMessage.TrimEnd(_charSPACE);
            commandTokens = inMessage.Split(_charSPACE);                //Our grammar divides commandTokens with space character

            int spaceCont = 0;

            for (int tokCont = 0; tokCont < commandTokens.Length; tokCont++)
            {
                if (commandTokens[tokCont] == "")
                {
                    spaceCont++;
                }
            }

            string[] finalTokens = new string[commandTokens.Length - spaceCont];
            int      finalCont   = 0;

            for (int tokCont = 0; tokCont < commandTokens.Length; tokCont++)
            {
                if (commandTokens[tokCont] != "")
                {
                    finalTokens[finalCont] = commandTokens[tokCont];
                    finalCont++;
                }
            }

            try
            {
                switch (finalTokens[0])
                {
                    //To Be Confirmed  commands (depends whether they are programmed on robot or not)
                    #region TBC Commands
                    //					case "hp":
                    //						break;
                    //					case "left":
                    //						mrRoboto.TurnLeft();
                    //						consoleTextBox.Text += "\r\nI'm here";
                    //						break;
                    //					case "right":
                    //						mrRoboto.TurnRight();
                    //						break;
                    //					case "forward":
                    //						mrRoboto.MoveForward();
                    //						break;
                    //					case "backward":
                    //						mrRoboto.MoveBackward();
                    //						break;
                    //					case "stop":
                    //						mrRoboto.Stop();
                    //						break;
                    //					case "raw":
                    //						if(commandTokens.Length > 1)
                    //						{
                    //							mrRoboto.sPort.WriteString(commandTokens[1]);
                    //							lastMessage = commandTokens[1];
                    //							messageSPort = commandTokens[1];
                    //						}
                    //						break;
                    //					case "setid":
                    //						if(commandTokens.Length>1)
                    //						{
                    //							mrRoboto.idNum = Int32.Parse(commandTokens[1]);
                    //						}
                    //						break;
                    //					case "setsp":
                    //						if(commandTokens.Length > 1)
                    //						{
                    //							mrRoboto.SetSpeed(Int32.Parse(commandTokens[1]));
                    //						}
                    //						break;
                    //					case "shid":
                    //						if(clientSoc.clientSocket.Connected)
                    //						{
                    //							clientSoc.SendMsg(mrRoboto.idNum.ToString());
                    //						}
                    //						break;
                    //					case "shs":
                    //						if(commandTokens.Length>1)
                    //						{
                    //							clientSoc.SendMsg(mrRoboto.ReqSensorVal(commandTokens[1], Int32.Parse(commandTokens[2])));
                    //						}
                    //						break;
                    //					case "bye":
                    //						throw new Exception("Call to end program");
                    #endregion
                case "mv":
                    if (finalTokens.Length > 2)
                    {
                        try
                        {
                            cmdRequest.mvReq   = true;
                            cmdRequest.mvDist  = float.Parse(finalTokens[1], System.Globalization.NumberStyles.Float);
                            cmdRequest.mvAngle = float.Parse(finalTokens[2], System.Globalization.NumberStyles.Float);
                        }
                        catch (System.Exception se)
                        {
                        }
                    }
                    break;

                case "raw":                             //Sends directly 1st parameter to serial port
                    if (finalTokens[1].Length == 10)
                    {
                        mrRoboto.sPort.WriteString(finalTokens[1]);
                    }
                    this.consoleTextBox.Text += finalTokens[1] + "\r\n";
                    break;

                case "shooton":
                {
                    mrRoboto.ShootOn();
                }
                break;

                case "shootoff":
                {
                    mrRoboto.ShootOff();
                }
                break;

                default: break;
                }
            }
            catch (Exception excp)
            {
                consoleTextBox.Text += _strENTER + "Error while Parsing."
                                       + _strENTER + excp.ToString();
                cmdRequest = new ExecList();
            }
            return(cmdRequest);
        }