Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Serial communication
Qt 6.12 LTS is out! See what's new in the release blog

Serial communication

Scheduled Pinned Locked Moved Solved General and Desktop
39 Posts 6 Posters 14.5k Views 1 Watching
  • 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.
  • D Damian7546

    @jsulm Better?

    if(!messageComplete(m_request_part)) {
    return;
    }
    
    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #16

    @Damian7546 More readable/clear :)

    Christian EhrlicherC 1 Reply Last reply
    0
    • JonBJ JonB

      @Damian7546 More readable/clear :)

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #17

      From my pov the parsing is incorrect as there may be more than one message in the buffer so the second (or even the first part of the second) is thrown away.

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

      D 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        From my pov the parsing is incorrect as there may be more than one message in the buffer so the second (or even the first part of the second) is thrown away.

        D Offline
        D Offline
        Damian7546
        wrote on last edited by
        #18

        @Christian-Ehrlicher so how to fix it?

        JonBJ 1 Reply Last reply
        0
        • D Damian7546

          @Christian-Ehrlicher so how to fix it?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #19

          @Damian7546
          Remove only what is used for a message from the buffer, leaving any further unused bytes there might be for appending to create next "message".

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @Damian7546 Something like this. But better don't use c-style casts:

            m_responseStatus = static_cast<Response::Status>(m_request.mid(2,1).toInt(nullptr, 16));
            
            D Offline
            D Offline
            Damian7546
            wrote on last edited by Damian7546
            #20

            @jsulm
            This conversion doesn't work.

            class Response
            {
            public:
                enum class Status : quint8{
                     DISABLE = 0x1A,
                };
                Response();
            };
            
            

            In m_responseStatus variable I have 0x1A value, but below statement is not equal:

            m_responseStatus = static_cast<Response::Status>(m_request.mid(2,1).toInt(nullptr, 16));
                qDebug() << "m_responseStatus: " << m_request.mid(2,1);
            
                if(Response::Status::DISABLE == m_responseStatus)
                    qDebug() << "ok convert";
                else
                    qDebug() << "bad convert";
            
            

            Problem is in toInt conversion:

            qDebug() << "m_responseStatus: " << m_request.mid(2,1);
            qDebug() << "m_responseStatus: " << m_request.mid(2,1).toInt(nullptr, 16);
            

            result:
            m_responseStatus: "\x1A"
            m_responseStatus: 0

            JonBJ 1 Reply Last reply
            0
            • D Damian7546

              @jsulm
              This conversion doesn't work.

              class Response
              {
              public:
                  enum class Status : quint8{
                       DISABLE = 0x1A,
                  };
                  Response();
              };
              
              

              In m_responseStatus variable I have 0x1A value, but below statement is not equal:

              m_responseStatus = static_cast<Response::Status>(m_request.mid(2,1).toInt(nullptr, 16));
                  qDebug() << "m_responseStatus: " << m_request.mid(2,1);
              
                  if(Response::Status::DISABLE == m_responseStatus)
                      qDebug() << "ok convert";
                  else
                      qDebug() << "bad convert";
              
              

              Problem is in toInt conversion:

              qDebug() << "m_responseStatus: " << m_request.mid(2,1);
              qDebug() << "m_responseStatus: " << m_request.mid(2,1).toInt(nullptr, 16);
              

              result:
              m_responseStatus: "\x1A"
              m_responseStatus: 0

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #21

              @Damian7546 said in Serial communication:

              In m_responseStatus variable I have 0x1A value, but below statement is not equal:

              Sorry, I don't believe that. Start with:

              qDebug() << int(m_responseStatus)  << int(Response::Status::DISABLE) << 0x1A;
              

              [I assume int() is good enough here to test. You can go static_cast<int>(...) if necessary.]

              D 1 Reply Last reply
              0
              • JonBJ JonB

                @Damian7546 said in Serial communication:

                In m_responseStatus variable I have 0x1A value, but below statement is not equal:

                Sorry, I don't believe that. Start with:

                qDebug() << int(m_responseStatus)  << int(Response::Status::DISABLE) << 0x1A;
                

                [I assume int() is good enough here to test. You can go static_cast<int>(...) if necessary.]

                D Offline
                D Offline
                Damian7546
                wrote on last edited by
                #22

                @JonB said in Serial communication:

                Sorry, I don't believe that. Start with:
                qDebug() << int(m_responseStatus) << int(Response::Status::DISABLE) << 0x1A;

                Result:
                0 26 26

                JonBJ 1 Reply Last reply
                0
                • D Damian7546

                  @JonB said in Serial communication:

                  Sorry, I don't believe that. Start with:
                  qDebug() << int(m_responseStatus) << int(Response::Status::DISABLE) << 0x1A;

                  Result:
                  0 26 26

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #23

                  @Damian7546
                  So you have your answer from the first 2 outputs. 0 != 26, the two values being compared are indeed not equal/the same.

                  Behaviour is quite correct given your m_request.mid(2,1).toInt(nullptr, 16);, which (QByteArray::toInt()) is quite incorrect to use on your byte array containing a byte with value \x16 (26). toInt() is not for use in your case, as you do not have a string representation of an integer to convert.

                  D 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Damian7546
                    So you have your answer from the first 2 outputs. 0 != 26, the two values being compared are indeed not equal/the same.

                    Behaviour is quite correct given your m_request.mid(2,1).toInt(nullptr, 16);, which (QByteArray::toInt()) is quite incorrect to use on your byte array containing a byte with value \x16 (26). toInt() is not for use in your case, as you do not have a string representation of an integer to convert.

                    D Offline
                    D Offline
                    Damian7546
                    wrote on last edited by
                    #24

                    @JonB So ho to compare Response::Status::DISABLE to byte in QByteArray m_request[2] ?

                    JonBJ 1 Reply Last reply
                    0
                    • D Damian7546

                      @JonB So ho to compare Response::Status::DISABLE to byte in QByteArray m_request[2] ?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #25

                      @Damian7546
                      Exactly as you have just written it!

                      qDebug() << Response::Status::DISABLE << m_request[2];
                      

                      [You may have to do casting.] Sightly better is to use m_request.at(2) in place of m_request[2] here, but that is a detail and not related to your issue. You want to inspect the actual bytes in the QByteArray, not call QByteArray::toInt().

                      J.HilkJ 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Damian7546
                        Exactly as you have just written it!

                        qDebug() << Response::Status::DISABLE << m_request[2];
                        

                        [You may have to do casting.] Sightly better is to use m_request.at(2) in place of m_request[2] here, but that is a detail and not related to your issue. You want to inspect the actual bytes in the QByteArray, not call QByteArray::toInt().

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #26

                        @JonB casting is highly suggested, as Response::Status is an uint_8t and at() returns a char, aka int8_t


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        JonBJ 1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @JonB casting is highly suggested, as Response::Status is an uint_8t and at() returns a char, aka int8_t

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by JonB
                          #27

                          @J-Hilk
                          Yes, but what I meant is I don't have anything available to test what compiler says as I write answers. So that detail is left to OP :) Since the OP asks about comparing (with ==) one should be able to compare a unit_8t against a char/int8_t, I think, using C++ automatic expression promotion without bothering to cast/convert, I think? But maybe not/warning for enum value against integer?

                          The important thing is no QByteArray::toInt() here!

                          D J.HilkJ 2 Replies Last reply
                          0
                          • JonBJ JonB

                            @J-Hilk
                            Yes, but what I meant is I don't have anything available to test what compiler says as I write answers. So that detail is left to OP :) Since the OP asks about comparing (with ==) one should be able to compare a unit_8t against a char/int8_t, I think, using C++ automatic expression promotion without bothering to cast/convert, I think? But maybe not/warning for enum value against integer?

                            The important thing is no QByteArray::toInt() here!

                            D Offline
                            D Offline
                            Damian7546
                            wrote on last edited by Damian7546
                            #28

                            @JonB It doesn't work:
                            qDebug() << static_cast<int>(Response::Status::DISABLE) << m_request.at(2);

                            Result:
                            ASSERT: "uint(i) < uint(size())" in file C:/Qt/5.15.2/mingw81_32/include/QtCore/qbytearray.h, line 500

                            And this way:
                            qDebug() << static_cast<int>(Response::Status::DISABLE) << m_request[2];

                            Result:
                            Bez tytułu.jpg

                            Where :
                            m_request response: "\xFC\x05\x1A\xF4\xE8"

                            JonBJ 1 Reply Last reply
                            0
                            • D Damian7546

                              @JonB It doesn't work:
                              qDebug() << static_cast<int>(Response::Status::DISABLE) << m_request.at(2);

                              Result:
                              ASSERT: "uint(i) < uint(size())" in file C:/Qt/5.15.2/mingw81_32/include/QtCore/qbytearray.h, line 500

                              And this way:
                              qDebug() << static_cast<int>(Response::Status::DISABLE) << m_request[2];

                              Result:
                              Bez tytułu.jpg

                              Where :
                              m_request response: "\xFC\x05\x1A\xF4\xE8"

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by JonB
                              #29

                              @Damian7546
                              How difficult to try:

                              qDebug() << static_cast<int>(Response::Status::DISABLE) << static_cast<int>(m_request[2]);
                              // or
                              qDebug() << static_cast<int>(Response::Status::DISABLE) << static_cast<int>(m_request.at(2));
                              

                              ? I leave you to resolve the C++ to your satisfaction.

                              D 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @J-Hilk
                                Yes, but what I meant is I don't have anything available to test what compiler says as I write answers. So that detail is left to OP :) Since the OP asks about comparing (with ==) one should be able to compare a unit_8t against a char/int8_t, I think, using C++ automatic expression promotion without bothering to cast/convert, I think? But maybe not/warning for enum value against integer?

                                The important thing is no QByteArray::toInt() here!

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #30

                                @JonB said in Serial communication:

                                one should be able to compare a unit_8t against a char/int8_t, I think, using C++ automatic expression promotion without bothering to cast/convert, I think?

                                nono, comparing different types via == or != will never promote any side "automatically"

                                you can get something like that fi you wrap it in a function call or something but.

                                But comparing uint8_t and int_8 will only work correctly if both values are between 0 and 128.


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                JonBJ 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @Damian7546
                                  How difficult to try:

                                  qDebug() << static_cast<int>(Response::Status::DISABLE) << static_cast<int>(m_request[2]);
                                  // or
                                  qDebug() << static_cast<int>(Response::Status::DISABLE) << static_cast<int>(m_request.at(2));
                                  

                                  ? I leave you to resolve the C++ to your satisfaction.

                                  D Offline
                                  D Offline
                                  Damian7546
                                  wrote on last edited by
                                  #31

                                  @JonB Sill of topic.
                                  I have variable: Response::Status m_responseStatus.
                                  How properly assign byte form QByteArray to m_responseStatus?

                                  J.HilkJ 1 Reply Last reply
                                  0
                                  • D Damian7546

                                    @JonB Sill of topic.
                                    I have variable: Response::Status m_responseStatus.
                                    How properly assign byte form QByteArray to m_responseStatus?

                                    J.HilkJ Offline
                                    J.HilkJ Offline
                                    J.Hilk
                                    Moderators
                                    wrote on last edited by
                                    #32

                                    @Damian7546 you were originally pretty spot on:

                                    static_cast<Response::Status>(m_request.at(0));
                                    

                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    JonBJ D 2 Replies Last reply
                                    0
                                    • J.HilkJ J.Hilk

                                      @JonB said in Serial communication:

                                      one should be able to compare a unit_8t against a char/int8_t, I think, using C++ automatic expression promotion without bothering to cast/convert, I think?

                                      nono, comparing different types via == or != will never promote any side "automatically"

                                      you can get something like that fi you wrap it in a function call or something but.

                                      But comparing uint8_t and int_8 will only work correctly if both values are between 0 and 128.

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by JonB
                                      #33

                                      @J-Hilk said in Serial communication:

                                      nono, comparing different types via == or != will never promote any side "automatically"

                                      Umm, C or C++

                                          char c('A');
                                          if (c == 65)
                                          {}
                                      
                                      example.cpp
                                      Compiler returned: 0
                                      

                                      From Godbolt. That's "automatically promoting" char c to int 65, per C/C++ expression rules. Has done since K&R... ?
                                      https://en.cppreference.com/w/cpp/language/implicit_conversion, e.g. Integral conversions topic.

                                      1 Reply Last reply
                                      0
                                      • J.HilkJ J.Hilk

                                        @Damian7546 you were originally pretty spot on:

                                        static_cast<Response::Status>(m_request.at(0));
                                        
                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by
                                        #34

                                        @J-Hilk said in Serial communication:

                                        static_cast<Response::Status>(m_request.at(0));

                                        I trust healthy disagreement in polite spirit is allowed. Personally, for style, I would not cast an arbitrary integer value to an enumeration, where it might fall outside the range of values. I would prefer

                                        if (static_cast<int>(Response::Status::DISABLE) == static_cast<int>(m_request.at(2))
                                        

                                        which is what I wrote earlier, so doing the casting to int.

                                        J.HilkJ 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @J-Hilk said in Serial communication:

                                          static_cast<Response::Status>(m_request.at(0));

                                          I trust healthy disagreement in polite spirit is allowed. Personally, for style, I would not cast an arbitrary integer value to an enumeration, where it might fall outside the range of values. I would prefer

                                          if (static_cast<int>(Response::Status::DISABLE) == static_cast<int>(m_request.at(2))
                                          

                                          which is what I wrote earlier, so doing the casting to int.

                                          J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by
                                          #35

                                          @JonB said in Serial communication:

                                          which is what I wrote earlier, so doing the casting to int.

                                          yes I agree, but not what the op asked.

                                          From Godbolt. That's "automatically promoting" char c to int 65, per C/C++ expression rules. Has done since K&R... ?

                                          fair! Not what I had in mind when I wrote my answer and not the pitfall I fell into numerous times :D


                                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                          Q: What's that?
                                          A: It's blue light.
                                          Q: What does it do?
                                          A: It turns blue.

                                          1 Reply Last reply
                                          0

                                          • Login

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