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



  • 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)



  • 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
    


  • @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; 
    }
    


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



  • This post is deleted!


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.