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
QtWS25 Last Chance

How to implement receive buffer

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.1k 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.
  • ZgemboZ Offline
    ZgemboZ Offline
    Zgembo
    wrote on 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

    ZgemboZ 1 Reply Last reply
    0
    • ZgemboZ Zgembo

      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

      ZgemboZ Offline
      ZgemboZ Offline
      Zgembo
      wrote on last edited by
      #2

      @zgembo Anyone has any suggestion?

      aha_1980A 1 Reply Last reply
      0
      • ZgemboZ Zgembo

        @zgembo Anyone has any suggestion?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 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.

        ZgemboZ 1 Reply Last reply
        2
        • aha_1980A aha_1980

          @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

          ZgemboZ Offline
          ZgemboZ Offline
          Zgembo
          wrote on 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
          • ZgemboZ Offline
            ZgemboZ Offline
            Zgembo
            wrote on 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.

            Pablo J. RoginaP aha_1980A 2 Replies Last reply
            0
            • ZgemboZ Zgembo

              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.

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on 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

              ZgemboZ 1 Reply Last reply
              1
              • Pablo J. RoginaP Pablo J. Rogina

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

                ZgemboZ Offline
                ZgemboZ Offline
                Zgembo
                wrote on 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
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  • ZgemboZ Zgembo

                    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.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 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.

                    ZgemboZ 1 Reply Last reply
                    5
                    • aha_1980A aha_1980

                      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

                      ZgemboZ Offline
                      ZgemboZ Offline
                      Zgembo
                      wrote on 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
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 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

                        • Login

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