I am new to Qt and need to prepare a project to send hex commands from rs232. QString line contains 64bit binary data which i have to convert into hexadecimal and send it through rs232 .

 QString a=ui->comboBox->currentText();
    QString s1;
    s1="./calc "+a;
    QProcess p1;
    p1.start(s1);
    p1.waitForFinished(-1);
    QString line ;
     //read
           QFile file("TeleOutput.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;
     QTextStream in (&file);
  line = in.readAll();
    ui->plainTextEdit->setPlainText(line);

So, how to convert 64 bit binary data in QString line to hexadecimal value and transfer it through rs232?

有帮助吗?

解决方案

First of all - you should really use QtSerialPort

Second of all - QString is a class, which works with actual string. QByteArray works with raw data. When you write QString line = in.readAll(); it implicitly calls QString(const QByteArray &ba), which uses QString::fromAscii.

Last of all, if you want to process 64bit integers, you should do something like this:

quint64 d;
QDataStream stream(&file);
while (!stream.atEnd())
{
    stream >> d;
    process(d);
}

Update

Quote:

My problem is that in plainTextEdit "1111110101000101010101010101010101010101010101010101010......." 64 bit data is populated , i need to convert this data into hex and send it through rs232

Solution:

QString binData = plainTextEdit.toPlainText();
QByteArray result;
while (binData.size() >= 64)
{
    quint64 d;
    QString dataPiece = binData.left(64);
    binData.remove(0, 64);
    d = dataPiece.toULongLong(0, 2);
    result += QByteArray::number(d);
}
_com->write(result);
_com->flush();

Where _com is a pointer to QtSerialPort, with all parameters set and opened without errors.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top