Beispiel #1
0
        private void oscReceiverWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            UDPListener udpListener     = null;
            bool        reinit          = true;
            OscMessage  messageReceived = null;

            int nameUpdateCounter = 0;

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            while (runThread)
            {
                try
                {
                    if (udpListener == null || reinit)
                    {
                        try
                        {
                            udpListener = new UDPListener(PORT);
                            reinit      = false;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Couldn't create udp listener");
                        }
                    }

                    /**
                     * Tuning Packet Descriptor
                     * Actual Value, Requested Value, IAccum, System Name
                     */
                    if (!initCompleted)
                    {
                        //Initialize the connection
                        sendIAccumReset();
                        initCompleted = true;
                    }

                    messageReceived = (OscMessage)udpListener.Receive();
                    if (messageReceived != null)
                    {
                        switch (messageReceived.Address)
                        {
                        case "/PIDData":
                            double timestamp    = (DateTime.Now - programLaunchTime).TotalSeconds;
                            double actualVal    = (double)messageReceived.Arguments[0];
                            double requestedVal = (double)messageReceived.Arguments[1];
                            dataChart.Invoke((MethodInvoker) delegate()
                            {
                                dataChart.Series[0].Points.AddXY(timestamp, actualVal);
                                dataChart.Series[1].Points.AddXY(timestamp, requestedVal);
                                for (int i = 0; i < dataChart.Series.Count; i++)
                                {
                                    while (dataChart.Series[i].Points.Count > CONSTRAINED_CHART_SIZE)
                                    {
                                        dataChart.Series[i].Points.RemoveAt(0);
                                    }
                                }
                                dataChart.ResetAutoValues();
                            });
                            if (stopWatch.ElapsedMilliseconds > 200)
                            {
                                //Update TextBox UI Every 200ms to avoid slowing down the app
                                double iAccum = (double)messageReceived.Arguments[2];
                                double err    = requestedVal - actualVal;
                                averageError.AddEntry(err);
                                txtActualVal.Invoke((MethodInvoker) delegate { txtActualVal.Text = actualVal.ToString("0.####"); });
                                txtDesiredVal.Invoke((MethodInvoker) delegate { txtDesiredVal.Text = requestedVal.ToString("0.####"); });
                                txtDeviation.Invoke((MethodInvoker) delegate { txtDeviation.Text = err.ToString("0.####"); });
                                txtAverageDev.Invoke((MethodInvoker) delegate { txtAverageDev.Text = averageError.Average.ToString("0.####"); });
                                txtIAccum.Invoke((MethodInvoker) delegate { txtIAccum.Text = iAccum.ToString("0.####"); });

                                if (nameUpdateCounter++ % 5 == 0)
                                {
                                    txtSysName.Invoke((MethodInvoker) delegate { txtSysName.Text = (string)messageReceived.Arguments[3]; });
                                }

                                stopWatch.Restart();
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    Thread.Sleep(1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    try
                    {
                        udpListener.Close();
                    }
                    catch (Exception)
                    {
                    }
                    reinit = true;
                }
            }

            udpListener.Close();
        }