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. copy unsigned char array into QByteArray ..... problems
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 6.7k 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.
  • J.HilkJ 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.

    O Offline
    O Offline
    o6a6r9v1p
    wrote on last edited by
    #5

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

    jsulmJ 1 Reply Last reply
    0
    • O o6a6r9v1p

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

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

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

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

      O 1 Reply Last reply
      3
      • jsulmJ jsulm

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

        O Offline
        O Offline
        o6a6r9v1p
        wrote on last edited by
        #7

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

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • O o6a6r9v1p

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

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @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
          1
          • O o6a6r9v1p

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

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

            @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


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

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @o6a6r9v1p

              const qint64 bytesWritten = serPort->write(reinterpret_cast<char*>(wbuf)));
              
              O Offline
              O Offline
              o6a6r9v1p
              wrote on last edited by
              #10

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

              jsulmJ 1 Reply Last reply
              0
              • O o6a6r9v1p

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

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #11

                @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
                0
                • jsulmJ 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++.

                  O Offline
                  O Offline
                  o6a6r9v1p
                  wrote on last edited by
                  #12

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

                  jsulmJ 1 Reply Last reply
                  0
                  • O o6a6r9v1p

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

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @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
                    0
                    • jsulmJ 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++.

                      O Offline
                      O Offline
                      o6a6r9v1p
                      wrote on last edited by
                      #14

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

                      jsulmJ 1 Reply Last reply
                      0
                      • O o6a6r9v1p

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

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        @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
                        2
                        • jsulmJ jsulm

                          @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)));
                          
                          O Offline
                          O Offline
                          o6a6r9v1p
                          wrote on last edited by
                          #16

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

                          • Login

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