Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Data stream throught QSerialPort



  • Hello,
    i have a measuring device connected to PC via QSerialPort (bluetooth). It supports 2 communication modes:

    1. sending some settings in a manner of command-optional answer pair which I already implemented
    2. online measurment in which it streams continuous data until being stopped (by sending proper command)

    I am wondering which approach would be the best (mayby easiest to code) for getting streamed data if I want to plot it in real-time. I am considering QSerialPort::readData() combined with QSerialPort::availableBytes() to read only full data samples (2 to 4 bytes depending on settings). I also read something about QDataStream(QIODevice) but since I havent worked with QDataStrem yet I assume it might be more problematic. However class name promises wanted behaviour so I find it tempting :D
    Long story short - I would appreciate any kind of help/suggestions!


  • Lifetime Qt Champion

    Hi,

    QDataStream is indeed not what you are looking for. Using readyRead should do what you want. Does the data you receive follow a pattern ?



  • Yep it does, however this pattern depends on previously programmed pattern eg.: if i program N-channel measurment I'll be getting data like [L byte ch1] [H byte ch1] ... [L byte ch_N] [H byte ch_N] [END OF SAMPLE L] [END OF SAMPLE H]. So basicly after every portion of data I'll have an end-of-sample info (2 bytes) and the next byte will be already from another sample.
    @SGaist Do you have some particular solution in mind regarding your question? :)


  • Lifetime Qt Champion

    Yes, use a buffer where you append the data you receive from the serial port then check if it contains one or more complete packet. If so extract it/them from the buffer and do your processing the way you want it.


Log in to reply