Reading from SerialPort and convert it to QString?!



  • Hi,
    I want to read data from serial port which is connected to incidcator. The indicator sends 8 bits.

    1. Letter like "A" or "B"
      2-7 numbers
    2. Carriage return.

    I read the serial port without problem. like below code;

    serial = new QSerialPort(this);
        serial->setPortName("com6");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
    
        serial->open(QIODevice::ReadOnly);
    
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    

    And serialReceived function is:

    void MainWindow::serialReceived()
    {
        QByteArray ba;
        ba= serial->readAll();
        ui->label_2->setText(QString(ba));
       ui->plainTextEdit->insertPlainText(QString(ba));
    
    }
    

    It's very strange I can see the data in PlainText like "A 720" but I can not see this value in Label or lineEdit?!

    And I can not get the first letter of data like below command

    QString aValue = QString(ba).
    aValue.mid(0,1)
    

    Normally it must return with "A" letter but nothing?!
    What is the problem with QByteArray and QString?!...

    Regards,
    Mucip:)


  • Lifetime Qt Champion

    Hi,

    Are you sure there's no null character in the data you send ?



  • Hi,
    Thanks for fast return. :)

    May be?! I don't know. It can be "A 123456" or "A 123". Depends on Indicator. It can send 123 value or 123456?!
    The indicator sends 6 long int value.

    But I see it in Plain text. No problem?!... :(

    Regards,
    Mucip:)



  • @Mucip said in Reading from SerialPort and convert it to QString?!:

    But I see it in Plain text.

    How do you see it? in debug mode?



  • Hi,
    No. I already add the plaintextEditor on the form. and below code:

    ui->plainTextEdit->insertPlainText(QString(ba));

    I can see the results in the editor. No problem!... :(
    Like below;

    A 720
    A 720
    A 720

    Regards,
    Mucip:)



  • @Mucip if you enter a Text via Qt-Designer, can you see that, whan the program is running?
    Can you highlight the Label/LineEdit and copy paste the text into a text document?



  • Hi,
    I didn't understand? Please could you explain little bit more?...
    Regards,
    Mucip:)



  • Hi,
    I checked.
    When I highlight and copy from PlainTextEditor I can paste it on the TextEditor of windows.
    But when I highlight and copy it from LineEdit I can not paste it on the TextEditor of windows.

    I think between 2th and 7th bits contain NULL when the value less then 6 digits like "200".

    So what happend if the ByteArray has NULL and why PlainTextEditor shows the value (like A 720) well and Why LineEdit NOT?!...

    Regards,
    Mucip:)



  • @Mucip Well I just wanted to see, what is actually in that QByteArray

    You could use this function to "see" the content of your data:

    for(int i(0); i < ba.size();i++)
            qDebug() <<  i << QChar(ba[i]) << static_cast<int>(ba[i]);
    


  • @J.Hilk said in Reading from SerialPort and convert it to QString?!:

    for(int i(0); i < ba.size();i++)
    qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);

    Hi,
    I used it and result:
    0 'A' 65
    0 ' ' 32
    0 ' ' 32
    1 '1' 49
    0 '4' 52
    1 '5' 53
    2 '0' 48
    0 '\xd' 13

    And Idicator shows 1450... This is correct! :)
    But How can I get the complate output like "A1450"???

    Regards,
    Mucip:)



  • @J.Hilk said in Reading from SerialPort and convert it to QString?!:

    @Mucip Well I just wanted to see, what is actually in that QByteArray
    You could use this function to "see" the content of your data:
    for(int i(0); i < ba.size();i++)
    qDebug() << i << QChar(ba[i]) << static_cast<int>(ba[i]);

    Easier would be to just convert the byte array to hex:

    qDebug() << ba.toHex();



  • @Mucip said in Reading from SerialPort and convert it to QString?!:

    0 '\xd' 13

    I believe thats the culprit a carriage Return ist difficult to display in a QLabel and/or QLineEdit

    Also, seeing your index starting from 0 multiple times the text of the QLabel and QLineEdit are constantly overwritten. You'll need to construct a String out of your different received QByteArrays.
    I suggest something like this. If it always end with QChar(0x0D)

    //Member String
    QString str;
    
    void MainWindow::serialReceived()
    {
        QByteArray ba;
        bool finished(false);
        ba= serial->readAll();
        for(int i(0); i < ba.size();i++){
             if(ba[i] != 0x0D)
                 str.append(ba[i]);
             else finished = true;
       }
    
        if(finished){
           ui->label_2->setText(str);
           ui->plainTextEdit->insertPlainText(str);
           str.clear();
        }
    }
    


  • can you try:

    void MainWindow::serialReceived()
    {
    QTextStream stream(serial);
    const QString receivedStr = stream.readAll();
    
        ui->label_2->setText(receivedStr );
       ui->plainTextEdit->insertPlainText(receivedStr );
    
    }
    

    Of course there is nothing here assuring you all data was received when the slot gets called



  • Hi,
    No. Unfotunatelly same thing. I can see the data in plainTextEdit but not in lineEdit.

    Regards,
    Mucip:)



  • @Mucip said in Reading from SerialPort and convert it to QString?!:

    I can see the data in plainTextEdit but not in lineEdit

    You are not setting the data in the lineedit, you are setting the string in a label and a plainTextEdit



  • @J.Hilk said in Reading from SerialPort and convert it to QString?!:

    void MainWindow::serialReceived()
    {
    QByteArray ba;
    bool finished(false);
    ba= serial->readAll();
    for(int i(0); i < ba.size();i++){
    if(ba[i] != 0x0D)
    str.append(ba[i]);
    else finished = true;
    }

    Hi,
    I solved the problem above like solution...
    I checked the data one by one and add to QString.
    Thanks.


Log in to reply
 

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