Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved copy unsigned char array into QByteArray ..... problems

    General and Desktop
    4
    16
    4883
    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.
    • O
      o6a6r9v1p last edited by

      I am sending 20 bytes of data using serial port.
      these bytes come from different blocks, some are int, some are bits; putting them together into 20 bytes structure. the values change with time, they are not constants.

      the structure defined as "unsigned char array"

      But QSerialPort() uses QByte Array as the input, it is const type.

      is there any way to assign unsigned char array to QByte Array
      OR is there any way to copy from array to QByte Array.
      code is shown below:

      unsigned char buf[20];
      
      QByteArray  writeData;
      .......
      .......
      for(i=0;i<64;i++){
         writeData =QByteArray::fromRawData(buf, sizeof(buf));    //ERROR here
      }
      .......
      const qint64 bytesWritten = m_serialPort->write(writeData);
      
      

      i tried some other ways, but of no use.

      Thanks,

      J.Hilk raven-worx 2 Replies Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @o6a6r9v1p last edited by J.Hilk

        hi @o6a6r9v1p
        QSerialport is derived from QioDevice, therefore write has at least 3 overwrites(those from the base class)

        • qint64 write(const char *data, qint64 maxSize)
        • qint64 write(const char *data)
        • qint64 write(const QByteArray &byteArray)

        So you can indeed pass it a char array, no need to convert it into a QByteArray

        That said, are you sure, about what you do ?

        because in here

        for(i=0;i<64;i++){
           writeData =QByteArray::fromRawData(buf, sizeof(buf));    //ERROR here
        }
        

        while the loop is running, buf will not change, It changes only if you do some hacky stuff!
        E.g threads without mutex, processEvent calls etc.

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

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

        O 1 Reply Last reply Reply Quote 3
        • raven-worx
          raven-worx Moderators @o6a6r9v1p last edited by

          @o6a6r9v1p
          it's not clear what the whole code of yours does.

          Also make sure that you read the docs about QByteArray::fromRawData():

          Constructs a QByteArray that uses the first size bytes of the data array. The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified

          so use the constructor instead.

          Otherwise simply cast your char pointer to the target type. The data behind it doesn't change.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 3
          • O
            o6a6r9v1p last edited by

            Thanks to all,
            in the test code, i am initializing the array with integers.
            then i tried to assign or copy into QByteArray.
            while copy/paste, missed the lines.

            int i;
            for(i=0;i<20;i++){
                wbuf[i]=i;
            }
             m_writeData =QByteArray::fromRawData(wbuf, sizeof(wbuf));
            
            

            how to change the above line of code.

            Thanks.

            1 Reply Last reply Reply Quote 0
            • O
              o6a6r9v1p @J.Hilk last edited by

              @J.Hilk hi,
              passed buf[] as you told, as a parameter to QSerialPort(), but it gives same error.
              The code is:

              for(i=0;i<20;i++){
                  wbuf[i]=i;
              }
              
              const qint64 bytesWritten = serPort->write(wbuf));
              

              the error is:

              invalid conversion from  "unsigned char*" to "const char*"
              

              can you point to examples or doc pages, if possible.

              Thanks.

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @o6a6r9v1p last edited by

                @o6a6r9v1p Please read what @raven-worx wrote (hint: casting)

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                O 1 Reply Last reply Reply Quote 3
                • O
                  o6a6r9v1p @jsulm last edited by

                  @jsulm
                  hi,
                  can we do it the way it is done C?
                  or is it different in C++?

                  I am not good in C++.

                  Thanks.

                  jsulm J.Hilk 2 Replies Last reply Reply Quote 0
                  • jsulm
                    jsulm Lifetime Qt Champion @o6a6r9v1p last edited by

                    @o6a6r9v1p

                    const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    O 1 Reply Last reply Reply Quote 1
                    • J.Hilk
                      J.Hilk Moderators @o6a6r9v1p last edited by

                      @o6a6r9v1p
                      take a look here

                      http://www.cplusplus.com/doc/tutorial/typecasting/

                      you can use c-style casting, but you're encuraged to use the cpp variants

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

                      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                      1 Reply Last reply Reply Quote 0
                      • O
                        o6a6r9v1p @jsulm last edited by

                        @jsulm
                        hi,
                        Thanks for the word. this line was used, think i am wrong some where.

                        const qint64 bytesWritten = serPort->write(const QByteArray ((unsigned char*) wbuf));
                        

                        where did i miss?

                        Thanks,

                        jsulm 1 Reply Last reply Reply Quote 0
                        • jsulm
                          jsulm Lifetime Qt Champion @o6a6r9v1p last edited by jsulm

                          @o6a6r9v1p Why do you create a byte array? @J-Hilk already said here that this is not needed.
                          What is wrong in your code: remove the const before QByteArray. You should learn more about C++.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          O 2 Replies Last reply Reply Quote 0
                          • O
                            o6a6r9v1p @jsulm last edited by

                            @jsulm
                            hi,
                            sending data bytes in certain order.
                            we use a structure of bytes.

                            jsulm 1 Reply Last reply Reply Quote 0
                            • jsulm
                              jsulm Lifetime Qt Champion @o6a6r9v1p last edited by

                              @o6a6r9v1p Again: there is NO need for QByteArray.
                              This line should work as well, did you try?

                              const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));
                              

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              O 1 Reply Last reply Reply Quote 0
                              • O
                                o6a6r9v1p @jsulm last edited by

                                @jsulm
                                removed const as told.
                                still i am getting:

                                invaild conversion from unsigned char* to const char* error.
                                

                                i know c programming, but not good in c++,

                                thanks,

                                jsulm 1 Reply Last reply Reply Quote 0
                                • jsulm
                                  jsulm Lifetime Qt Champion @o6a6r9v1p last edited by

                                  @o6a6r9v1p You should read more carefully - I already showed you what you need to do.

                                  1. Why do you want to use QByteArray? It is not needed. QByteArray does not care about any order - it is the order you give it.
                                  2. You need to cast unsigned char* to char*. See my previous posts to see how, I don't want to write it again and again.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply Reply Quote 2
                                  • O
                                    o6a6r9v1p @jsulm last edited by

                                    @jsulm
                                    hi,
                                    i under stood that line in wrongly. in the place of <char *>, i have used const QByteArray.
                                    Now i got the point after going trough the type casting page, adviced by you.

                                    Now it is working.
                                    Thanks to all of you.

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