ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Add data to realtimemultithread chart
Posted by pmontazer on Dec-24-2021 03:13
Attachments:
Hi, I want to read the data from USB and add it to the chart
Device that connect to my PC send 10,240 bytes every 1.28 seconds
If I print the receivedData.size (), the serial receives the all 10240 bytes in 3 steps

recievedData.append(serial->readAll());
qDebug()<<recievedData.size();
the output:
4096
8192
10240

The signal I receive is a pulse signal, so for 10 seconds that I should have 20 peaks, but in the charts shows only 3 peaks and the signals become disordered like a  below picture
can chart handle this timing? If yes where is my problem?

In qdoublebufferedqueue.h I set int bufferSize = 10000000
and in realtimemultithread.h I set  static const int sampleSize = 100000;
The rest of codes is attached
thank you for helping
chart.jpg
code.rar
code.rar

8.30 Kb

  Re: Add data to realtimemultithread chart
Posted by Peter Kwan on Dec-24-2021 13:54
Hi pmontazer,

Yes, ChartDirector can definitely handle this timing.

Regarding how you read the data.


void RandomWalk::readData() {
  qtimestamp = QDateTime::currentDateTime();
  timeStamp = Chart::chartTime2(qtimestamp.toTime_t()) +
              qtimestamp.time().msec() / 1000.0;

  recievedData.append(serial->readAll());

  if (recievedData.size() == 10240) {

    sqlBuffering.append(recievedData);
    int bco = 0;
    for (int Sam = 0; Sam < 320; Sam++) {

     //Extract data from packets
      }
      handler(param, timeStamp, CH1[Sam], CH2[Sam], CH3[Sam], CH4[Sam]);
    }
    recievedData.clear();
  }
}


I noted that the brackets do not match in the above code. I assume you have removed some code to make easier to read, and the "}" under the "//Extract data from packets" comment should be removed too.

The issues I found are:

(a) The for loop adds 320 data points with exactly the same timeStamp. You mentioned that the data should span 1.28 second, so each data point should use a different timeStamp that is 4ms apart from each other.

(b) Your code uses QDateTime::currentDateTime(); to read the timeStamp. Note that reading the timeStamp this way has low resolution. On Windows, the resolution may be down to 16ms (that is, the timeStamp may have 16ms of random error), because the Windows timeslot is 16ms apart. On other OS, the tick may be different, and it also depends on what other programs you are running on the machine.

(c) Note that the timeStamp in your code is the time that you read from the serial port, not the timeStamp of the data. Because the serial port can be quite slow, the error can be significantly more than 4ms.


The best way to obtain the timeStamp is from the data source, that is, your serial port should include the timeStamp as well as the data value of each data point.

A lot of data source do not report the timeStamp, because they assume the "absolute time" (in terms of yyyy-mm-dd hh:nn:ss.ffff) is not important, and you already know the "elapsed time" accurately. In your case, you do know the elapsed time - it is 4ms apart for each point.

To obtain the accurate timeStamp at millisecond level, you can assume the first timeStamp is 0 (or any time convenient to you). For each data point, simply increment 0.004 to the timeStamp. You do not need to read the timeStamp from Qt, because it is not the timeStamp of your data (see (c) above), and it is not accurate at millisecond level (see (b) above).

Regards
Peter Kwan

  Re: Add data to realtimemultithread chart
Posted by pmontazer on Dec-25-2021 01:46
So you mean I should do this:
double timeStamp[320] = {0}; //define  in header
for (int Sam = 0; Sam < 320; Sam++) {
  //extract data
timeStamp[Sam] = timeStamp[Sam] + 0.004;
  //add to chart
}

Am I right?

  Re: Add data to realtimemultithread chart
Posted by Peter Kwan on Dec-25-2021 02:19
Hi,

I mean:

// In the header
double currentTimeStamp;

....

// In the constructor
currentTimeStamp= 0;

....

// In readData

for (.....)  {
    ....
    timeStamp[Sam] = currentTimeStamp;
    currentTimeStamp  += 0.004;
}

Regards
Peter Kwan

  Re: Add data to realtimemultithread chart
Posted by pmontazer on Dec-25-2021 04:34
Attachments:
Thanks, only the chart lines are too dense, can I fix it? They get good with double zoom
density2.jpg

  Re: Add data to realtimemultithread chart
Posted by Peter Kwan on Dec-25-2021 12:34
Hi pmontazer ,

The initialFullRange is set to 60 seconds in the sample code. (The code below is from "realtimemultithread.cpp".)

  // Use the initialFullRange (which is 60 seconds in this demo) if this is
  // sufficient.
  double duration = endDate - startDate;
  if (duration < initialFullRange)
    endDate = startDate + initialFullRange;

Please change the initialFullRange (declared and initialized in the header) to a value suitable for your case (eg. 10 seconds or 5 seconds)

Regards
Peter Kwan