QSerialPort Qchar + QByteArray



  • Hello every one! I develop some application to write/read data from COM-port. I get data from lineEdit and send it to port like:

    SerialPort->WriteToPort(ui->lineEdit_2->text().toLocal8Bit());
    

    all work fine, but i need to add some char to start and and line like:

    SerialPort->WriteToPort(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254))
    

    but i get and error... Please help, how i can convert Qchar to QByteArray. Thanks!


  • Moderators

    @chiffaCff

    Hi and welcome to devnet

    This has nothing to do with QSerialPort. WriteToPort is not a routine of QSerialPort. It boils down to a pure QByteArray issue.

    If I am not mistaken this may work:

    SerialPort->WriteToPort(QByteArray(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254)));
    

    My guess is that you do not have a routine fitting the outcome. You should see in the error protocol of your compile run.



  • get the error:
    mainwindow.cpp:35: error: ambiguous overload for 'operator+' (operand types are 'QChar' and 'QByteArray')
    SerialPort->WriteToPort(QByteArray(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254)));
    ^


  • Moderators

    @chiffaCff

    What is the prototyp for WriteToPort look like?

    QString tmp (QChar(253)+ui->lineEdit_2->text()+QChar(254));
    SerialPort->WriteToPort(tmp.toLocal8Bit());
    

    Not sure why you are using toLocal8bit. With standard string the statement above shall work, so it should be with QString, I guess.



  •   QString s="string text";
      QByteArray b= s.toUtf8()+QByteArray("tekst")+QByteArray::fromHex("1310");
      SerialPort->WriteToPort(b);
    


  • Thanks for everyone . Create like:

    QByteArray data;
       data.append(char(253));
       data.append(ui->lineEdit_3->text().toLocal8Bit());
       data.append(char(254));
    

Log in to reply
 

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