Exemplo n.º 1
0
        // The main program loop, this is to control the validator, it polls at
        // a value set in this class (pollTimer).
        void MainLoop()
        {
            btnRun.Enabled = false;
            NV11.CommandStructure.ComPort    = Global.ComPort;
            NV11.CommandStructure.SSPAddress = Global.SSPAddress;
            NV11.CommandStructure.Timeout    = 1500;

            // connect to validator
            if (ConnectToValidator(reconnectionAttempts))
            {
                Running = true;
                textBox1.AppendText("\r\nPoll Loop\r\n*********************************\r\n");
                btnHalt.Enabled = true;
            }

            while (Running)
            {
                // if the poll fails, try to reconnect
                if (NV11.DoPoll(textBox1) == false)
                {
                    textBox1.AppendText("Poll failed, attempting to reconnect...\r\n");
                    while (true)
                    {
                        // attempt reconnect, pass over number of reconnection attempts
                        if (ConnectToValidator(reconnectionAttempts) == true)
                        {
                            break; // if connection successful, break out and carry on
                        }
                        // if not successful, stop the execution of the poll loop
                        btnRun.Enabled  = true;
                        btnHalt.Enabled = false;
                        NV11.SSPComms.CloseComPort(); // close com port before return
                        return;
                    }
                    textBox1.AppendText("Reconnected\r\n");
                }

                timer1.Enabled = true;
                // update form
                UpdateUI();
                // setup dynamic elements of win form once
                if (!FormSetup)
                {
                    SetupFormLayout();
                    FormSetup = true;
                }
                while (timer1.Enabled)
                {
                    Application.DoEvents();
                    Thread.Sleep(1); // Yield to free up CPU
                }
            }

            //close com port
            NV11.SSPComms.CloseComPort();

            btnRun.Enabled  = true;
            btnHalt.Enabled = false;
        }
Exemplo n.º 2
0
        // The main program loop, this is to control the validator, it polls at
        // a value set in this class (pollTimer).
        public void MainLoop()
        {
            btnRun.Enabled  = false;
            btnHalt.Enabled = true;
            Thread tNV11Rec = null, tHopRec = null;

            // Connect to the validators
            ConnectToNV11(textBox1);
            ConnectToHopper(textBox1);

            NV11.EnableValidator();
            Hopper.EnableValidator();

            // While application is still active
            while (!CHelpers.Shutdown)
            {
                // Setup form layout on first run
                if (!FormSetup)
                {
                    SetupFormLayout();
                    FormSetup = true;
                }

                // If the hopper is supposed to be running but the poll fails
                if (hopperRunning && !Hopper.DoPoll(textBox1))
                {
                    textBox1.AppendText("Lost connection to SMART Hopper\r\n");
                    // If the other unit isn't running, refresh the port by closing it
                    if (!NV11Running)
                    {
                        LibraryHandler.ClosePort();
                    }
                    hopperRunning = false;
                    tHopRec       = new Thread(() => ReconnectHopper());
                    tHopRec.Start();
                }

                // If the NV11 is supposed to be running but the poll fails
                if (NV11Running && !NV11.DoPoll(textBox1))
                {
                    textBox1.AppendText("Lost connection to NV11\r\n");
                    // If the other unit isn't running, refresh the port by closing it
                    if (!hopperRunning)
                    {
                        LibraryHandler.ClosePort();
                    }
                    NV11Running = false;
                    tNV11Rec    = new Thread(() => ReconnectNV11());
                    tNV11Rec.Start();
                }

                UpdateUI();
                timer1.Enabled = true;

                while (timer1.Enabled)
                {
                    Application.DoEvents();
                    Thread.Sleep(1); // Yield so windows can schedule other threads to run
                }
            }

            //close com port
            LibraryHandler.ClosePort();

            btnRun.Enabled  = true;
            btnHalt.Enabled = false;
        }