serial communication



  • i want to send data in serial i write code like
    QString cmd=ui->lineEdit->text();
    QByteArray value=cmd.toUtf8();
    qDebug()<<"vlaue arrary"<<value;
    serial->write(value);

    receiver side:
    QByteArray str;
    str.append(serial->readAll());
    qDebug()<<"second string"<<str;

    In receiver side the value is splited like "WELCOME" splited to W ELCOME
    why the string is splited


  • Qt Champions 2016

    @Rameshguru

    The string is split as data comes in groups.
    This is normal, and its your job to put it back together again while reading.



  • i want to click button one time only


  • Qt Champions 2016

    @Rameshguru

    It has nothing to with clicking a button

    When Data signal comes
    you do
    QByteArray str; // this should live in class
    str.append(serial->readAll()); // append to buffer for each read

    when str.size() is big enough that you know all data have been read, you can then look at the data.

    There is NO WAY to make sure that it only takes 1 serial->readAll()
    to get all data.



  • Most common way would be defining a very simple protocol used to deliver your data via serial port.
    In your case, you may just add a <newline> to the data you want to transmit. On the receiver side, you can then wait for that <newline> (using the mechanism already mentioned) to identify the end of the transmission and that your received data is now complete.

    However this only works if your data itself does not contain a <newline>. In that case you will need to use another "marker" or define a more complex protocol.


Log in to reply
 

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