Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to implement receive buffer

How to implement receive buffer

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zgembo
    wrote on 13 Aug 2019, 10:37 last edited by
    #1

    Hi all,

    I am working with serial port. Device that is connected to my pc is sending data in idle state. That data is equal to 0x63.
    When I press a start button on that device new frame is generated and I can receive it in my Qt app.
    Frame starts with 0x01 0x01 0xFF and it is transmitted every few milliseconds.

    What is the best way to handle this data. First I need to detect this start frame and from that position I need to read few more bytes and send to GUI.

    regards

    Z 1 Reply Last reply 13 Aug 2019, 14:18
    0
    • Z Zgembo
      13 Aug 2019, 10:37

      Hi all,

      I am working with serial port. Device that is connected to my pc is sending data in idle state. That data is equal to 0x63.
      When I press a start button on that device new frame is generated and I can receive it in my Qt app.
      Frame starts with 0x01 0x01 0xFF and it is transmitted every few milliseconds.

      What is the best way to handle this data. First I need to detect this start frame and from that position I need to read few more bytes and send to GUI.

      regards

      Z Offline
      Z Offline
      Zgembo
      wrote on 13 Aug 2019, 14:18 last edited by
      #2

      @zgembo Anyone has any suggestion?

      A 1 Reply Last reply 13 Aug 2019, 15:13
      0
      • Z Zgembo
        13 Aug 2019, 14:18

        @zgembo Anyone has any suggestion?

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 13 Aug 2019, 15:13 last edited by
        #3

        @zgembo

        And what is the problem? You already wrote:

        First I need to detect this start frame and from that position I need to read few more bytes and send to GUI.

        and that is exactly what you have to do. you can either peek() into QSerialPorts internal buffer, or readAll() and store the results in your own buffer.

        Just note, that the message comes in in parts and you may need multiple readyReadSignals until a complete message is arrived. And of course, you need to dismiss any incomplete message before the start sequence.

        Regards

        Qt has to stay free or it will die.

        Z 1 Reply Last reply 13 Aug 2019, 15:24
        2
        • A aha_1980
          13 Aug 2019, 15:13

          @zgembo

          And what is the problem? You already wrote:

          First I need to detect this start frame and from that position I need to read few more bytes and send to GUI.

          and that is exactly what you have to do. you can either peek() into QSerialPorts internal buffer, or readAll() and store the results in your own buffer.

          Just note, that the message comes in in parts and you may need multiple readyReadSignals until a complete message is arrived. And of course, you need to dismiss any incomplete message before the start sequence.

          Regards

          Z Offline
          Z Offline
          Zgembo
          wrote on 13 Aug 2019, 15:24 last edited by
          #4

          @aha_1980 Yes I understand the logic but I not quite sure how to implement it.

          Do you have any example that may be of any help?

          Regards

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            Zgembo
            wrote on 13 Aug 2019, 15:45 last edited by
            #5

            Data comes like this:
            0x63;0x63;0x63;0x01;0x01;0xFF;six byres of data; 0x00 final byte; then repeat from 0x01;0x01;0xFF

            So I need to detect start frame, record data and end frame.

            P A 2 Replies Last reply 13 Aug 2019, 15:53
            0
            • Z Zgembo
              13 Aug 2019, 15:45

              Data comes like this:
              0x63;0x63;0x63;0x01;0x01;0xFF;six byres of data; 0x00 final byte; then repeat from 0x01;0x01;0xFF

              So I need to detect start frame, record data and end frame.

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 13 Aug 2019, 15:53 last edited by
              #6

              @zgembo have you check the source code of any of the Qt serial port examples? like serial terminal for instance

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              Z 1 Reply Last reply 13 Aug 2019, 18:00
              1
              • P Pablo J. Rogina
                13 Aug 2019, 15:53

                @zgembo have you check the source code of any of the Qt serial port examples? like serial terminal for instance

                Z Offline
                Z Offline
                Zgembo
                wrote on 13 Aug 2019, 18:00 last edited by
                #7

                @pablo-j-rogina Yes I have, but I am having difficulties in detecting start frame 0x01;0x01;0xFF

                I have tried to use QByteArrayMatcher with QByteArray to detect start frame pattern but it is a little bit messy.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 13 Aug 2019, 18:19 last edited by
                  #8

                  @zgembo said in How to implement receive buffer:

                  I have tried to use QByteArrayMatcher with QByteArray

                  What's wrong with QByteArray::indexOf() ?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • Z Zgembo
                    13 Aug 2019, 15:45

                    Data comes like this:
                    0x63;0x63;0x63;0x01;0x01;0xFF;six byres of data; 0x00 final byte; then repeat from 0x01;0x01;0xFF

                    So I need to detect start frame, record data and end frame.

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 13 Aug 2019, 18:20 last edited by aha_1980
                    #9

                    Hi @zgembo,

                    Here is a (absolutely untested, not even compiled) code snippet that could show you the direction:

                    static QByteArray nextFrame(QByteArray &buffer)
                    {
                    	const QByteArray startOfFrame("\x01\0x01\xFF"); // SOF
                    	enum { FrameSize = 10 };
                    
                    	const int pos = buffer.indexIn(startOfFrame);
                    	if (pos < 0)
                     		return QByteArray();
                    
                    	// delete everything before SOF
                    	buffer.remove(0, pos);
                    	if (buffer.size() < FrameSize) // frame is not complete
                    		return;
                    
                    	// extract next frame from buffer
                    	const QByteArray newFrame = buffer.left(0, FrameSize);
                    	buffer.remove(0, FrameSize);
                    	return newFrame;
                    }
                    
                    // connect this slot to the QSerialPort::readRead signal
                    void serialReadyRead()
                    {
                    	static QByteArray buffer;
                    
                    	buffer.append(m_serial.readAll());
                    
                    	QByteArray frame = nextFrame(buffer);
                    	while (!frame.isEmpty) {
                    		qDebug() << frame.toHex();
                    		frame = nextFrame(buffer);
                    	}
                    }
                    

                    I hope you get the idea.

                    Regards

                    Qt has to stay free or it will die.

                    Z 1 Reply Last reply 14 Aug 2019, 11:23
                    5
                    • A aha_1980
                      13 Aug 2019, 18:20

                      Hi @zgembo,

                      Here is a (absolutely untested, not even compiled) code snippet that could show you the direction:

                      static QByteArray nextFrame(QByteArray &buffer)
                      {
                      	const QByteArray startOfFrame("\x01\0x01\xFF"); // SOF
                      	enum { FrameSize = 10 };
                      
                      	const int pos = buffer.indexIn(startOfFrame);
                      	if (pos < 0)
                       		return QByteArray();
                      
                      	// delete everything before SOF
                      	buffer.remove(0, pos);
                      	if (buffer.size() < FrameSize) // frame is not complete
                      		return;
                      
                      	// extract next frame from buffer
                      	const QByteArray newFrame = buffer.left(0, FrameSize);
                      	buffer.remove(0, FrameSize);
                      	return newFrame;
                      }
                      
                      // connect this slot to the QSerialPort::readRead signal
                      void serialReadyRead()
                      {
                      	static QByteArray buffer;
                      
                      	buffer.append(m_serial.readAll());
                      
                      	QByteArray frame = nextFrame(buffer);
                      	while (!frame.isEmpty) {
                      		qDebug() << frame.toHex();
                      		frame = nextFrame(buffer);
                      	}
                      }
                      

                      I hope you get the idea.

                      Regards

                      Z Offline
                      Z Offline
                      Zgembo
                      wrote on 14 Aug 2019, 11:23 last edited by
                      #10

                      @aha_1980 Thank you for your code snippet. I know that you did not even try to compile this code but I have some questions.
                      I do not understand nextFrame function completely. You read a frame and then you try to detect position of the SOF block with indexOf function.
                      I do not follow you after this part. If the buffer size is less than a frame size you return from the function, but what do you do if it is not?

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 14 Aug 2019, 16:26 last edited by
                        #11

                        When an empty QByteArray is returned, the loop in serialReadyRead() is stopped.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        4

                        1/11

                        13 Aug 2019, 10:37

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved