最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Using WinForms.DataVisualization to plot time series data from serial port in Visual Studio - Stack Overflow

programmeradmin4浏览0评论
        private void UpdateCharts(int value)
    {
        try
        {
            if (startTime == DateTime.MinValue)
                startTime = DateTime.Now;

            double elapsedTime = (DateTime.Now - startTime).TotalSeconds;

            UpdateChartSafe(Input_chart, "Input Signal", elapsedTime, value);

            // Add the value to the buffer
            lock (inputBuffer)  // Ensure thread safety
            {
                if (inputBufferIndex < bufferSize)
                {
                    inputBuffer[inputBufferIndex] = value;
                    inputBufferIndex++;
                }
                else
                {
                    LogError("Buffer overflow detected. Resetting buffer.");
                    Array.Clear(inputBuffer, 0, inputBuffer.Length);
                    inputBufferIndex = 0;
                }
            }

            // Perform FFT when the buffer is half full
            if (inputBufferIndex >= bufferSize / 2)
            {
                Debug.WriteLine("Processing FFT with buffer size: " + inputBufferIndex);

                double[] bufferCopy = new double[bufferSize]; // Copy to avoid data corruption
                lock (inputBuffer)
                {
                    Array.Copy(inputBuffer, bufferCopy, bufferSize);
                }

                this.BeginInvoke(new Action(() => ProcessFFT(bufferCopy, Fft_input_chart, "Input FFT")));

                // Reset buffer index (but keep old data)
                inputBufferIndex = 0;
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"Error updating charts: {ex.Message}");
            LogError($"Error updating charts: {ex.Message}");

            // Reset buffer on error
            lock (inputBuffer)
            {
                Array.Clear(inputBuffer, 0, inputBuffer.Length);
                inputBufferIndex = 0;
            }
        }
    }

My serial device is sending 2 bytes every 100us and i want to plot it on my graph, I have been able to plot normally at 1ms, as System.Windows.Forms.Timers have max resolution of 1ms, i tried putting them in a buffer in different thread that will update points from the buffer but it doesn't work, my baud rate can support the data transfer, i am only trying to update/refresh my graph every 60ms. If anyone has any idea to go about doing it pls share.

edit: I have shared the function by which I update the graph, I use invoke to call the function, have also tried System.Threading and Task(). I am trying to make it work with backgroundworker.

I got my data for timer from this question:this , but when i try to plot for 55 milliseconds it dosen't work, was only able to get upto 10ms data serial plot. with 1ms using tasks();

Edit 2: I am adding a full example code here

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论