Beispiel #1
0
        void tryToConnect()
        {
            String resp;

            String[] respl;
            Char[]   delimn = new Char[] { '\n', '\r' };
            Char[]   deliml = new Char[] { ' ' };
            while (true)
            {
                if (isConnected == false && isBusy == false)
                {
                    serialPort             = new SerialPort(portName);
                    serialPort.ReadTimeout = 1000;
                    resp = "";
                    try
                    {
                        serialPort.Open();
                        serialPort.WriteLine("");
                        resp = serialPort.ReadTo(">");
                        serialPort.WriteLine("ID");
                        serialPort.WriteLine(" ");
                        Thread.Sleep(1000);
                        resp  = serialPort.ReadTo(">");
                        resp  = resp.Replace('-', '_');
                        respl = resp.Split(delimn, StringSplitOptions.RemoveEmptyEntries);
                        respl = respl[1].Split(deliml, StringSplitOptions.RemoveEmptyEntries);
                        if (textRobotName.InvokeRequired)
                        {
                            textRobotName.Invoke(new Action(delegate()
                            {
                                textRobotName.Text = respl[2] + respl[9];
                            }));
                        }
                        if (labStatus.InvokeRequired)
                        {
                            labStatus.Invoke(new Action(delegate()
                            {
                                labStatus.Text      = "Connected";
                                labStatus.BackColor = Color.Chartreuse;
                            }));
                        }
                        soundPlayer = new SoundPlayer(Properties.Resources.maxcon);//(Sounds[1]);
                        soundPlayer.Play();
                        consoleWrite("Connected to " + portName, Color.Green);
                        isConnected = true;
                        if (blockPanel.InvokeRequired)
                        {
                            blockPanel.Invoke(new Action(delegate()
                            {
                                blockPanel.Enabled = true;
                            }));
                        }
                    }
                    catch (Exception e)
                    {
                        serialPort.Close();
                    }
                }
                Thread.Sleep(1000);
            }
        }