Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved How to implement receive buffer

    General and Desktop
    4
    11
    469
    Loading More Posts
    • 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.
    • Zgembo
      Zgembo last edited by

      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

      Zgembo 1 Reply Last reply Reply Quote 0
      • Zgembo
        Zgembo @Zgembo last edited by

        @zgembo Anyone has any suggestion?

        aha_1980 1 Reply Last reply Reply Quote 0
        • aha_1980
          aha_1980 Lifetime Qt Champion @Zgembo last edited by

          @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.

          Zgembo 1 Reply Last reply Reply Quote 2
          • Zgembo
            Zgembo @aha_1980 last edited by

            @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 Reply Quote 0
            • Zgembo
              Zgembo last edited by

              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. Rogina aha_1980 2 Replies Last reply Reply Quote 0
              • Pablo J. Rogina
                Pablo J. Rogina @Zgembo last edited by

                @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

                Zgembo 1 Reply Last reply Reply Quote 1
                • Zgembo
                  Zgembo @Pablo J. Rogina last edited by

                  @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 Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion last edited by

                    @zgembo said in How to implement receive buffer:

                    I have tried to use QByteArrayMatcher with QByteArray

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

                    Qt has to stay free or it will die.

                    1 Reply Last reply Reply Quote 1
                    • aha_1980
                      aha_1980 Lifetime Qt Champion @Zgembo last edited by 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

                      Qt has to stay free or it will die.

                      Zgembo 1 Reply Last reply Reply Quote 5
                      • Zgembo
                        Zgembo @aha_1980 last edited by

                        @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 Reply Quote 0
                        • Christian Ehrlicher
                          Christian Ehrlicher Lifetime Qt Champion last edited by

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

                          Qt has to stay free or it will die.

                          1 Reply Last reply Reply Quote 4
                          • First post
                            Last post