Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[solved] error in reading values from uart



  • I got a hardware which send data about 300 single data per second. I developed GUI application on Qt , use Qextserialport. problem is when i read data from serial port it display in AScii value. i tried to conver to Hex, but the value are wrong. I need Value in Binary , for further process....
    @
    this->rs232_ = new QextSerialPort();
    this->rs232_->setPortName("/dev/ttyS0");
    this->rs232_->setBaudRate(BAUD4800);
    this->rs232_->setFlowControl(FLOW_OFF);
    this->rs232_->setParity(PAR_ODD);
    this->rs232_->setDataBits(DATA_8);
    this->rs232_->setStopBits(STOP_1);
    this->rs232_->open(QIODevice::ReadWrite);

    if (!this->rs232_->isOpen()) {
        qDebug()<<"....Port ttyS0 CanNot Open...."<<endl;
          this->rs232_->flush();
          this->rs232_->close();
               }
    connect(rs232_, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
    

    void MainWindow::onDataAvailable()
    {
    QByteArray data = rs232_->readAll();
    qDebug()<<data<<data.toHex();
    QByteArray process = data.toHex();
    }@

    output I Got:
    @
    "�""�"" "06"
    ""�" "dc"
    "�" "ec"
    "�" "ff"
    "�" "80"
    "�"" "06"
    ""�" "dc"
    "t���"�" "f9"
    "�" "80"
    "�"" "06"
    ""�" "dc"
    "l" "6c"
    "�" "f3"
    "�" "80"
    "�"" "06"
    ""�" "dc"
    "$" "24"
    "�" "f1"
    @



  • Hi shajeen.
    Welcome to DevNet.
    Do you want display incoming data as binary, like:
    06 - > "00000110"
    or you mean some thing else?



  • yes i need in binary --- "00000110"

    Thank you qxoz for replay



  • Ok if you want string display of binary data use
    @QString::number(value, 2);@
    look at "doc":https://qt-project.org/doc/qt-4.8/qstring.html#number



  • i Tried to display ,but values become zero

    @QString data = rs232_->read(rs232_->bytesAvailable());

    QString str = data;
    bool ok;
    int binary = str.toInt(&ok, 2);
    int dec = str.toInt(&ok, 10);
    qDebug()<<binary<<dec;@

    Output:
    0 3
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 9
    0 0
    0 0
    0 0
    0 0
    0 0



  • i don't know what are you trying to do, but if i understand you, here the solution:
    @void MainWindow::onDataAvailable()
    {
    QByteArray data = rs232_->readAll();
    foreach (char tch, data) {
    qDebug()<<QString::number((quint8)tch, 10)<< QString::number((quint8)tch, 2);
    }
    }@



  • Thx qxoz it convert's......



  • You 're welcome.
    By the way if you want display all 8 bits
    @QString tempZero;
    QString tStr;
    foreach (char tch, tArray) {
    tempZero = "00000000";
    tStr = QString::number((quint8)tch, 2);
    tempZero.replace(8-tStr.length(), tStr.length(), tStr);
    qDebug()<< tempZero;
    }@



  • Hi qxoz

    I need your help..currently working on a embedded board(friendlyarm mini2440), with Xbee pro module.. using qext i am able to transmit the correct data to hyperterminal..

    But from when i send from hyperterminal the data received at the board end is different..for example if press "9" then on receiving side its is showing as "y" ..same for all..may i know the solution for it..

    my aim is to read entire data from xbee(zigbee module) and show on lcd..please help me..


Log in to reply