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!
-
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!
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)));
^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.