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 must you use read(char *c, qint64)

how must you use read(char *c, qint64)

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.0k 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.
  • B Offline
    B Offline
    bask185
    wrote on last edited by
    #1

    I am trying to read in bytes from the serial buffer one at the time using read(char *c, qint64), but this cursed * pointer abomination is in my way. Nomather what I try I cannot get a byte and stuff it under c or *c

    How can I use read(char *c, qint64)

    1 Reply Last reply
    0
    • VRoninV VRonin

      @bask185 said in how must you use read(char *c, qint64):

      And why do I want to use this way instead of serial->read()??

      because that's how you do serialisation in Qt. It allows you to define operator>> and operator<< for your classes to have the same interface of serialisation for any generic type. see page 292 of http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf


      My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

      This won't happen with QDataStream you can remove the delay altogether.
      If you receive more that you read it will remain in the buffer for future read, if you read more that you receive commitTransaction will return false to notify you.

      You probably want something like:

      QDataStream serialStream(serial);
      qint8 singleByte;
      for(;;){ // keep reading for as long as there is data
      serialStream.startTransaction();
      serialStream >> singleByte;
      if(serialStream.commitTransaction())
      qDebug() << "received: " << singleByte;
      else
      break; //stop when you tried to read more than you received
      }
      

      of course you can do more than just reading a single byte every time

      B Offline
      B Offline
      bask185
      wrote on last edited by
      #6

      @VRonin said in how must you use read(char *c, qint64):

      @bask185 said in how must you use read(char *c, qint64):

      My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

      This won't happen with QDataStream you can remove the delay altogether.

      Actually I cannot remove the delay. The machines with which we work can send at 115200BPS but they cannot continously send bytes. Effectively they are sending about ~4800bps or ~480 bytes per second and 1 /480 = ~2.08ms hence I put a 2ms delay in the arduino.

      This is a hardware limitation of the machine's PCBs, atleast this is what I have been told.

      The solution with the for loop has solved the problem though I now have a correct output:

      "g"  >> 
      <<   "6"
      <<   "a2"
      <<   "1"
      <<   "67"  // All 4 bytes are read and processed :D
      

      So thank you.

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        How about you don't and you use datasteram?

        QDataStream serialStream(serial);
        char singleByte;
        serialStream.startTransaction();
        serialStream >> singleByte;
        if(serialStream.commitTransaction())
        //read correctly
        else
        // something went wrong
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        B 1 Reply Last reply
        0
        • VRoninV VRonin

          How about you don't and you use datasteram?

          QDataStream serialStream(serial);
          char singleByte;
          serialStream.startTransaction();
          serialStream >> singleByte;
          if(serialStream.commitTransaction())
          //read correctly
          else
          // something went wrong
          
          B Offline
          B Offline
          bask185
          wrote on last edited by
          #3

          @VRonin said in how must you use read(char *c, qint64):

          serialStream >> singleByte;

          so this line stuffs a byte from the buffer in the variable singleByte?

          And why do I want to use this way instead of serial->read()??

          anyways, thank you I will try it out. My current method might occasionally miss a byte. My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

          while (serial->bytesAvailable() > 0) {
                  QByteArray d = serial->readAll(); 
                  int b = d[0]; // this only works because of a 2ms delay between byte transfers
                  qDebug() << "<<  " << b; 
          }
          
          VRoninV 1 Reply Last reply
          0
          • B bask185

            @VRonin said in how must you use read(char *c, qint64):

            serialStream >> singleByte;

            so this line stuffs a byte from the buffer in the variable singleByte?

            And why do I want to use this way instead of serial->read()??

            anyways, thank you I will try it out. My current method might occasionally miss a byte. My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

            while (serial->bytesAvailable() > 0) {
                    QByteArray d = serial->readAll(); 
                    int b = d[0]; // this only works because of a 2ms delay between byte transfers
                    qDebug() << "<<  " << b; 
            }
            
            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #4

            @bask185 said in how must you use read(char *c, qint64):

            And why do I want to use this way instead of serial->read()??

            because that's how you do serialisation in Qt. It allows you to define operator>> and operator<< for your classes to have the same interface of serialisation for any generic type. see page 292 of http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf


            My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

            This won't happen with QDataStream you can remove the delay altogether.
            If you receive more that you read it will remain in the buffer for future read, if you read more that you receive commitTransaction will return false to notify you.

            You probably want something like:

            QDataStream serialStream(serial);
            qint8 singleByte;
            for(;;){ // keep reading for as long as there is data
            serialStream.startTransaction();
            serialStream >> singleByte;
            if(serialStream.commitTransaction())
            qDebug() << "received: " << singleByte;
            else
            break; //stop when you tried to read more than you received
            }
            

            of course you can do more than just reading a single byte every time

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            B 1 Reply Last reply
            0
            • B Offline
              B Offline
              bask185
              wrote on last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • VRoninV VRonin

                @bask185 said in how must you use read(char *c, qint64):

                And why do I want to use this way instead of serial->read()??

                because that's how you do serialisation in Qt. It allows you to define operator>> and operator<< for your classes to have the same interface of serialisation for any generic type. see page 292 of http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf


                My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

                This won't happen with QDataStream you can remove the delay altogether.
                If you receive more that you read it will remain in the buffer for future read, if you read more that you receive commitTransaction will return false to notify you.

                You probably want something like:

                QDataStream serialStream(serial);
                qint8 singleByte;
                for(;;){ // keep reading for as long as there is data
                serialStream.startTransaction();
                serialStream >> singleByte;
                if(serialStream.commitTransaction())
                qDebug() << "received: " << singleByte;
                else
                break; //stop when you tried to read more than you received
                }
                

                of course you can do more than just reading a single byte every time

                B Offline
                B Offline
                bask185
                wrote on last edited by
                #6

                @VRonin said in how must you use read(char *c, qint64):

                @bask185 said in how must you use read(char *c, qint64):

                My sending device has a 2ms delay between 2 following bytes, occasionally 2 bytes got stuffed in the Qbytearray and the 2nd will then be lost/not used.

                This won't happen with QDataStream you can remove the delay altogether.

                Actually I cannot remove the delay. The machines with which we work can send at 115200BPS but they cannot continously send bytes. Effectively they are sending about ~4800bps or ~480 bytes per second and 1 /480 = ~2.08ms hence I put a 2ms delay in the arduino.

                This is a hardware limitation of the machine's PCBs, atleast this is what I have been told.

                The solution with the for loop has solved the problem though I now have a correct output:

                "g"  >> 
                <<   "6"
                <<   "a2"
                <<   "1"
                <<   "67"  // All 4 bytes are read and processed :D
                

                So thank 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