Esempio n. 1
0
        //
        private void UpdateTestData(Object Watch)
        {
            Double Elapsed = DB.GetTimeMs();
            Double Angle   = (Double)(2 * 3.1415 * 0.1 * Elapsed / 1000);

            Single TimeSec     = (Single)(Elapsed / 1000);
            Single SineValue   = (Single)Math.Sin(Angle);
            Single CosineVAlue = (Single)Math.Cos(Angle);

            int lck = DB.requestLock();

            if (lck >= 0)
            {
                //Write buffer Data
                if (Buffer[Buffer.Keys.First()].Count > 0)
                {
                    foreach (String key in Buffer.Keys)
                    {
                        DB.Write(key, Buffer[key]);
                        Buffer[key].Clear();
                    }
                }

                //Now write new data
                DB.Write("TEST_sin", TimeSec, SineValue);
                DB.Write("TEST_cos", TimeSec, CosineVAlue);
                DB.returnLock(lck);
            }
            else
            {
                //Write data to buffer couldnt get a lock
                Buffer["TEST_sin"].Add(new Tuple <Single, Single>(TimeSec, SineValue));
                Buffer["TEST_cos"].Add(new Tuple <Single, Single>(TimeSec, CosineVAlue));
            }
        }
Esempio n. 2
0
        public void ProcessMessage(List <byte> inData)
        {
            byte msgkey = inData[5];
            uint msgid  = inData[7];

            int start = 9;

            switch (msgkey)
            {
            case 0xB0:
                //Recieved a new Symbol List
                start = 9;
                while (inData.Count() > (start + 2 + 2))
                {
                    Int32 SignalKey = BitConverter.ToInt16(inData.Skip(start).Take(2).ToArray(), 0);

                    ASCIIEncoding encoding = new ASCIIEncoding();

                    //Get Name, read to null charcater
                    string SignalName  = "";
                    int    NameCharCnt = 0;
                    bool   done        = false;
                    while (!done)
                    {
                        if (inData[start + 2 + NameCharCnt] == (byte)'\0')
                        {
                            SignalName = encoding.GetString(inData.Skip(start + 2).Take(NameCharCnt).ToArray());
                            break;
                        }

                        if (NameCharCnt > 25)
                        {
                            done = true;
                            break;
                        }
                        NameCharCnt = NameCharCnt + 1;
                    }

                    byte SignalType = inData[start + 2 + NameCharCnt + 1];
                    start = start + 2 + NameCharCnt + 2;
                    if (ASI_Signals.Keys.Contains(SignalKey))
                    {
                        ASI_Signals[SignalKey].Name = SignalName;
                        ASI_Signals[SignalKey].Type = (asi_dtypes)SignalType;
                        Buffer[SignalKey].Clear();
                    }
                    else
                    {
                        ASI_Signal Signal = new ASI_Signal();
                        Signal.Name = SignalName;
                        Signal.Type = (asi_dtypes)SignalType;

                        ASI_Signals.Add(SignalKey, Signal);
                        Buffer[SignalKey] = new List <Tuple <Single, Single> >();
                    }
                }
                break;

            case 0xB1:
                //Recieved Symbol Data
                start = 9;
                while (inData.Count() > (start + 1))
                {
                    Int32 SignalKey = BitConverter.ToInt16(inData.Skip(start).Take(2).ToArray(), 0);
                    if (ASI_Signals.Keys.Contains(SignalKey))
                    {
                        asi_dtypes Type    = ASI_Signals[SignalKey].Type;
                        String     Name    = ASI_Signals[SignalKey].Name;
                        Double     TimeMS  = DB.GetTimeMs();
                        Single     TimeSec = (Single)(TimeMS / 1000);

                        switch (Type)
                        {
                        case (asi_dtypes.asi_bool): {
                            bool Value = BitConverter.ToBoolean(inData.Skip(start + 2).Take(1).ToArray(), 0);
                            int  lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 3;
                            break;
                        }

                        case (asi_dtypes.asi_byte): {
                            byte Value = inData[start + 2];
                            int  lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 3;
                            break;
                        }

                        case (asi_dtypes.asi_double): {
                            double Value = BitConverter.ToDouble(inData.Skip(start + 2).Take(8).ToArray(), 0);
                            int    lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 10;
                            break;
                        }

                        case (asi_dtypes.asi_float): {
                            float Value = BitConverter.ToSingle(inData.Skip(start + 2).Take(4).ToArray(), 0);
                            int   lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 6;
                            break;
                        }

                        case (asi_dtypes.asi_int): {
                            Int16 Value = BitConverter.ToInt16(inData.Skip(start + 2).Take(2).ToArray(), 0);
                            int   lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 4;
                            break;
                        }

                        case (asi_dtypes.asi_long): {
                            Int32 Value = BitConverter.ToInt32(inData.Skip(start + 2).Take(4).ToArray(), 0);
                            int   lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 6;
                            break;
                        }

                        case (asi_dtypes.asi_short): {
                            Int16 Value = BitConverter.ToInt16(inData.Skip(start + 2).Take(2).ToArray(), 0);
                            int   lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 4;
                            break;
                        }

                        case (asi_dtypes.asi_uint): {
                            UInt16 Value = BitConverter.ToUInt16(inData.Skip(start + 2).Take(2).ToArray(), 0);
                            int    lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 4;
                            break;
                        }

                        case (asi_dtypes.asi_ulong): {
                            UInt32 Value = BitConverter.ToUInt32(inData.Skip(start + 2).Take(4).ToArray(), 0);
                            int    lck   = DB.requestLock();
                            if (lck >= 0)
                            {
                                //Flush the buffer
                                DB.Write(Name, Buffer[SignalKey]);
                                Buffer[SignalKey].Clear();

                                //Write new data
                                DB.Write(Name, TimeSec, Convert.ToSingle(Value));
                                DB.returnLock(lck);
                            }
                            else
                            {
                                Buffer[SignalKey].Add(new Tuple <Single, Single>(TimeSec, Convert.ToSingle(Value)));
                            }
                            start = start + 6;
                            break;
                        }

                        default: {
                            start = start + 1;
                            break;
                        }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                break;
            }
        }
        /**Plotting Stuff************************************************************/

        private void Update(Object stateInfo)
        {
            if (this.IsDisposed)
            {
                return;
            }

            int Lock = DB.requestLock();

            if (Lock >= 0)
            {
                for (int rw = 0; rw < dgvplots.Rows.Count; rw++)
                {
                    DataGridViewCheckBoxCell visibleCell   = (DataGridViewCheckBoxCell)dgvplots.Rows[rw].Cells[0];
                    DataGridViewComboBoxCell paramNameCell = (DataGridViewComboBoxCell)dgvplots.Rows[rw].Cells[1];

                    if (!(paramNameCell.Value == null) && (DB.Signals().Contains(paramNameCell.Value.ToString())))
                    {
                        if ((bool)visibleCell.EditedFormattedValue)
                        {
                            //Find the series which goes with this rw.
                            Series Ser = null;
                            foreach (Series CSer in Chart.Series)
                            {
                                if (CSer.Name.Equals(rw.ToString()))
                                {
                                    Ser = CSer;
                                    break;
                                }
                            }

                            //If Series is still void, its not being plotted to add a new series to plot
                            if (Ser == null)
                            {
                                Ser             = new Series(rw.ToString());
                                Ser.ChartType   = SeriesChartType.FastLine;
                                Ser.ChartArea   = Chart.ChartAreas[0].Name;
                                Ser.BorderWidth = 2;
                                Ser.LegendText  = paramNameCell.Value.ToString();

                                if (!(this.Chart.IsDisposed))
                                {
                                    if (this.Chart.InvokeRequired)
                                    {
                                        addSeriesDelegate d = new addSeriesDelegate(AddChartSeries);
                                        this.Invoke(d, new object[] { Ser });
                                    }
                                    else
                                    {
                                        AddChartSeries(Ser);
                                    }
                                }
                            }
                            else
                            {
                                if (!(Ser.Name.Equals(rw.ToString())))
                                {
                                    Series Ser2 = new Series(rw.ToString());
                                    Ser2.ChartType   = SeriesChartType.FastLine;
                                    Ser2.ChartArea   = Chart.ChartAreas[0].Name;
                                    Ser2.BorderWidth = 2;
                                    Ser2.LegendText  = paramNameCell.Value.ToString();

                                    if (!(this.Chart.IsDisposed))
                                    {
                                        if (this.Chart.InvokeRequired)
                                        {
                                            removeSeriesDelegate d_rem = new removeSeriesDelegate(RemovedChartSeries);
                                            this.Invoke(d_rem, new object[] { Ser });

                                            addSeriesDelegate d_add = new addSeriesDelegate(AddChartSeries);
                                            this.Invoke(d_add, new object[] { Ser2 });
                                        }
                                        else
                                        {
                                            RemovedChartSeries(Ser);
                                            AddChartSeries(Ser2);
                                        }
                                    }
                                }
                            }


                            if (!(this.Chart.IsDisposed))
                            {
                                if (this.Chart.InvokeRequired)
                                {
                                    updateChartDelegate d = new updateChartDelegate(UpdateSeriesData);
                                    try
                                    {
                                        this.Invoke(d, new object[] { Ser, DB.Read(paramNameCell.Value.ToString()) });
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                                else
                                {
                                    String SignalName = paramNameCell.Value.ToString();
                                    UpdateSeriesData(Ser, DB.Read(SignalName));
                                }
                            }
                        }
                        else
                        {
                            //Find the series which goes with this rw.
                            Series Ser = null;
                            foreach (Series CSer in Chart.Series)
                            {
                                if (CSer.Name.Equals(rw.ToString()))
                                {
                                    Ser = CSer;
                                    break;
                                }
                            }

                            if (!(Ser == null))
                            {
                                if (!(this.Chart.IsDisposed))
                                {
                                    if (this.Chart.InvokeRequired)
                                    {
                                        removeSeriesDelegate d_rem = new removeSeriesDelegate(RemovedChartSeries);
                                        this.Invoke(d_rem, new object[] { Ser });
                                    }
                                    else
                                    {
                                        RemovedChartSeries(Ser);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            DB.returnLock(Lock);
        }