Location>code7788 >text

c# Oscilloscope Functions

Popularity:527 ℃/2024-12-12 19:55:27

c#uploader:Oscilloscope function

It's been a long time since I've updated, because recently I've mainly learned how to use c# to make an oscilloscope function, and here the oscilloscope is mainly used for microcontroller debugging. Below, I mainly share some of my experience in making an oscilloscope:

I here oscilloscope is made with winform, understand that there are a lot of open source curve controls, such as: chart, Oxyplot, scottplot, hslcontrols, etc., of course, there are also some paid curve controls, here will not be said one by one. At the same time, it is also possible to draw a curve chart by yourself.

The very first study of someone else's oscilloscope build is as follows:

image-20241211221100395

The principle of this oscilloscope:

1. First of all, the definition of the oscilloscope interface code or create a cache data class, in the definition of a static queue used to cache data, here four channels, it is four queues

 public static volatile Queue<int> quedata1 = new Queue<int>(dataCountMax); // channel 1 data
 public static volatile Queue<int> quedata2 = new Queue<int>(10001);
 public static volatile Queue<int> quedata3 = new Queue<int>(10001);
 public static volatile Queue<int> quedata4 = new Queue<int>(10001);

2. One data out and one data in at the place where the data is received

 FrmScope.();
 FrmScope.(comData.data1);
 FrmScope.();
 FrmScope.(comData.data2);
 FrmScope.();
 FrmScope.(comData.data3);
 FrmScope.();
 FrmScope.(comData.data4);

3. When opening the oscilloscope interface, fill the queue first

for (int i = 0; i < dataCountMax; i++) 
{
    if ( < dataCountMax)
    {
        (0);
        (0);
        (0);
        (0);
 
    }

}

4. The queue will be converted into an array assigned to their own painted controls for timed refresh, as long as the data to ensure that each of their own sampling time interval is the same, the waveform should be perfect!

if (ScopeRun)
{
    QueToArray(quedata1, arrScope1);
    QueToArray(quedata2, arrScope2); {
    QueToArray(quedata1, arrScope1); QueToArray(quedata2, arrScope2); QueToArray(quedata2, arrScope2)
    QueToArray(quedata4, arrScope4);
}

Invalidate();//refresh the display

Because I do a debugging software channel is particularly many, and there is no fixed channel, you need to do the function of adding curves at will, and also due to the aesthetic reasons did not use this approach. So, I went to study Scottplot and Oxyplot control, learning found that, for dynamic curves, Oxyplot control display better and more convenient operation, Scottplot in the static curve is more advantageous, the data can be up to millions of levels. After screening, I chose Oxyplot.

Rough interface of the oscilloscope (here I made my own analog data for easy display):

image-20241211225047579

The thought process is as follows:

1. First of all, we start with the same as above and definitely define a queue for data caching. However, here we first define a curve entity class to store the data, define a public class to cache the curve as well as index the name of the curve, and the queue uses a blocking queue (producer-consumer problem).

image-20241211225133617

image-20241211225145556

2. Simulate the production data and deposit it into the queue.

image-20241211225421020

3. In another thread inside the constant indexing of the added curve, you can encapsulate the method of adding and deleting curves, not shown here.

image-20241211225611805

That's it, the oscilloscope function is complete. I use blocking queue to display real-time waveforms, I do not know if there are other ways to realize, have good insights can be left in the comments section.

Of course, there are a few errors in the process of doing the above that have to be pitched:

1. Is it okay if I store the time without storing it? I think the actual process if the horizontal coordinate is the number of points, then you curve sampling time is the consumer's delay time, especially in the event of a failure to restore the curve again, there will be a time interval resulting in waveform problems.

2. transform to list before traversing the curve, don't go tolist inside foreach, when you add the curve will occasionally report an error.

image-20241211230306994

3. The consumer can be put to a shorter time, because you horizontal coordinate for the time, will not affect the actual waveform.

4. is a multi-threaded problem, do not want to save things, while the loop inside the first set of Invoke and then say, this will be time-consuming, as long as you need to delegate for the use of the place on the line, do not use indiscriminately.

image-20241211230512228