【问题标题】:Plotting data given from a serial port in ILNumerics在 ILNumerics 中绘制从串行端口给出的数据
【发布时间】:2013-12-12 16:32:09
【问题描述】:

我目前正在不遗余力地制作一个 Win32 (.net) 程序,该程序绘制来自串行端口的数据。我已经能够从串行端口获取数据,并设置 ilnumerics plot cube 等。但我不能做的是将 (x,y,z) 转换为一个 ILAray 以绘制点。

    public void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    {
        if (serialPort1.IsOpen == true)
        {
            Varriables.numberOfSerialBytes = serialPort1.BytesToRead;

            if (Varriables.numberOfSerialBytes >= Varriables.numberOfSerialBytesPerLine) //change 13 to number of bytes, changes for number of stars (stars x3 x BytesPerInt)
            {
                string serialText = serialPort1.ReadExisting();
                if (serialText != "") RecievedText(serialText);
                else { }
            }
        }
    }

    public void RecievedText(string text)
    {
        if ( richTextBox1.InvokeRequired)
        {
            SetTextCallBack x = new SetTextCallBack(RecievedText);
            this.Invoke(x, new object[] { text });
        }
        else
        {
            richTextBox1.Text = text;
            try { CalculatePoints(text); }
            catch { }
        }
    }

    public void CalculatePoints(string positionsString)
    {
        string[] starpositionStringList = positionsString.Split("   ".ToCharArray());
        List<float> starPositionFloatListX = new List<float>();
        List<float> starPositionFloatListY = new List<float>();
        List<float> starPositionFloatListZ = new List<float>();
        System.Console.WriteLine(starpositionStringList.Count());

        for (int i = 0; i < starpositionStringList.Count() / 3; i += 3)
        {
            int iy = i + 1;
            int iz = i + 2;

            float x = float.Parse(starpositionStringList[i]);
            float y = float.Parse(starpositionStringList[iy]);
            float z = float.Parse(starpositionStringList[iz]);

            starPositionFloatListX.Add(x);
            starPositionFloatListY.Add(y);
            starPositionFloatListZ.Add(z);
        }

        float[] xArray = starPositionFloatListX.ToArray<float>();
        float[] yArray = starPositionFloatListY.ToArray<float>();
        float[] zArray = starPositionFloatListZ.ToArray<float>();

        PlotPoints(xArray, yArray, zArray);
    }

    public void PlotPoints(float[] x, float[] y, float[] z)
    {
        //could send data through PlotPoints(x,y,z);
        //or make a long array using a for loop and send that
        //either way i still need to make an array to plot and use ilPlanel2.Scene = Scene to continue upadating the scene

        ILArray<float> starPositionsX = x;
        ILArray<float> starPositionsY = y;
        ILArray<float> starPositionsZ = z;
        ILArray<float> starPositions = ???????????????; //need to convert (x,y,z) to ILArray<float>

        ilPanel2.Scene.First<ILPlotCube>().Animations.AsParallel();

        if(Varriables.pointsPlotted == false)
        {
            //ilPanel2.Scene.First<ILPlotCube>(){ new ILPoints { Positions = starPositions, Colors  } };
            //Cube.Add(new ILPoints { Positions = starPositions });
            ilPanel2.Scene.First<ILPlotCube>().Add<ILPoints>(new ILPoints{ Positions = starPositions });

            Varriables.pointsPlotted = true;
        }

        else
        {
            ilPanel2.Scene.First<ILPoints>().Positions.Update(starPositions);
        }
        ilPanel2.Refresh();
    }

我可以使用 IL.Math 函数来执行此操作吗?

【问题讨论】:

    标签: c# serial-port ilnumerics


    【解决方案1】:

    创建所需大小的 ILArray 并直接使用传入的 float[] System.Arrays 来填充值:

    public void PlotPoints(float[] x, float[] y, float[] z) {
    
        using (ILScope.Enter()) {
    
            ILArray<float> starPositions = ILMath.zeros<float>(3, x.Length);
            starPositions["0;:"] = x;
            starPositions["1;:"] = y;
            starPositions["2;:"] = z;
    
            ilPanel1.Scene.First<ILPlotCube>().Add<ILPoints>(new ILPoints { Positions = starPositions });
            // (... or update accordingly, if the shape exists already)
    
            // at the end of all modifications, call Configure()
    
            ilPanel1.Scene.Configure(); 
            ilPanel1.Refresh();
        }
    }
    

    之后不要忘记在修改后的形状(或根路径上的任何父节点)上调用 Configure()!此外,我通常将所有与 ILArray 相关的代码包装在 using (ILScope.Enter()) { .. 块中。它有助于显着提高频繁更新的性能。

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2013-05-26
      • 2014-05-16
      • 2013-11-06
      相关资源
      最近更新 更多